# -*- coding: utf-8 -*- # ------------------------------------------------------------------------------ # File Name: paddleocr_service.py # Original Author: Clark Lin # Email: clark_lin@outlook.com # # Change History # Version Date By Description # 0.01 2024-04-24 Clark Lin Initial version # # Main features summary: # - Implementation of PaddleOCR Invocation # # Copyright Information: # Copyright © 2024 Oasis # Licensed TBD # ------------------------------------------------------------------------------ from paddleocr import PaddleOCR from io import BytesIO from PIL import Image import numpy as np import base64 import fastapi_security_util from pydantic import BaseModel from jose import JWTError, jwt from fastapi import HTTPException, status # Import your custom logging manager import os import logging from common.script.logging_manager import LoggingManager # Initialize logging manager curr_module = os.path.basename(__file__) # lm = LoggingManager() lm = LoggingManager.get_instance() # ------------------------------------------------ # Model Definition # ------------------------------------------------ class RawImage(BaseModel): image_b64_string: str # ------------------------------------------------ # Sub Function - Verify Access Token # ------------------------------------------------ def verify_token(token: str): secret_key, client_db = fastapi_security_util.get_credentials(fastapi_security_util.credential_file) try: payload = jwt.decode(token, secret_key, algorithms=[fastapi_security_util.algorithm]) username: str = payload.get("sub") if username is None: return False return True except JWTError: lm.log(logging.ERROR, curr_module, 'JWTError: ', str(JWTError)) return False # ------------------------------------------------ # Sub Function - Read Image # ------------------------------------------------ def read_image(token: str, image: RawImage): if not verify_token(token = token): raise HTTPException( status_code = status.HTTP_401_UNAUTHORIZED, detail = "Authentication Failed", headers={"WWW-Authenticate": "Bearer"}, ) l_text = [] try: ocr = PaddleOCR(use_angle_cls=True, lang="ch") # Convert the base64 string to a byte array image_data = base64.b64decode(image.image_b64_string) # 将解码后的二进制数据转换为 PIL Image 对象 image_buffer = BytesIO(image_data) pil_image = Image.open(image_buffer) # 将 PIL Image 对象转换为 numpy 数组 image_array = np.array(pil_image) # 初始化 PaddleOCR 并加载图像数组 result = ocr.ocr(image_array, det=True, rec=True) for idx in range(len(result)): res = result[idx] for line in res: l_text.append(line[1][0]) lm.log(logging.INFO, curr_module, 'read_image completed with normal') except Exception as e: lm.log(logging.ERROR, curr_module, 'Exception: ', str(e)) return l_text