zbparse/flask_app/general/local_ocr.py
2024-12-03 11:50:15 +08:00

31 lines
955 B
Python

import os.path
from flask_app.PaddleOCR.python_api.PPOCR_api import GetOcrApi
from flask_app.PaddleOCR.python_api.tbpu import GetParser
class LocalOCR:
def __init__(self):
# 初始化识别器对象,传入 PaddleOCR_json.exe 的路径
# 获取当前脚本所在目录
script_dir = os.path.dirname(os.path.abspath(__file__))
# 构建绝对路径
ocr_path = 'flask_app/PaddleOCR/PaddleOCR-json.exe'
ocr = GetOcrApi(ocr_path)
# parser = GetParser("single_para")
self.ocr = ocr
def run(self, pic_path):
# 识别图片,传入图片路径
getObj = self.ocr.run(pic_path)
if getObj["code"] == 100:
text = ""
data = getObj["data"]
for boxes in data:
# 每个块之间进行分行
text += boxes["text"]
text += "\n"
return text
else:
return ""