项目初始化
This commit is contained in:
0
api/__init__.py
Normal file
0
api/__init__.py
Normal file
61
api/easy_orc_api.py
Normal file
61
api/easy_orc_api.py
Normal file
@@ -0,0 +1,61 @@
|
||||
import os
|
||||
import cv2
|
||||
import easyocr
|
||||
import numpy as np
|
||||
from core.response import SuccessResponse
|
||||
from fastapi import APIRouter, UploadFile, Form
|
||||
from schemas.orc_result import ResultInfo, ResultMain
|
||||
|
||||
|
||||
app = APIRouter()
|
||||
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
model_dir = os.path.join(BASE_DIR, 'model')
|
||||
|
||||
reader = easyocr.Reader(
|
||||
['ch_sim', 'en'],
|
||||
model_storage_directory=model_dir,
|
||||
download_enabled=False,
|
||||
gpu=True)
|
||||
|
||||
|
||||
def preprocess_img(image):
|
||||
# 转为灰度图
|
||||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
# 二值化
|
||||
_, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
|
||||
# 去噪声
|
||||
denoised = cv2.medianBlur(binary, 3)
|
||||
return denoised
|
||||
|
||||
|
||||
@app.post("/upload")
|
||||
async def orc(files: list[UploadFile] = Form(...)):
|
||||
result = []
|
||||
for file in files:
|
||||
main = ResultMain()
|
||||
main.file_name = file.filename
|
||||
# 读取上传的文件内容
|
||||
image_data = await file.read()
|
||||
|
||||
# 2. 将字节数据转换为 numpy 数组
|
||||
np_array = np.frombuffer(image_data, np.uint8)
|
||||
|
||||
# 3. 使用 cv2.imdecode 解码为 OpenCV 图像
|
||||
image = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
|
||||
new_image = preprocess_img(image)
|
||||
datas = reader.readtext(np.array(new_image))
|
||||
infos = []
|
||||
for data in datas:
|
||||
info = ResultInfo()
|
||||
bounding_boxs = data[0]
|
||||
left_up = bounding_boxs[0]
|
||||
right_down = bounding_boxs[2]
|
||||
info.bounding_box_left_up = [int(left_up[0]), int(left_up[1])]
|
||||
info.bounding_box_right_down = [int(right_down[0]), int(right_down[1])]
|
||||
info.text = data[1]
|
||||
info.confidence = round(data[2], 4)
|
||||
infos.append(info)
|
||||
main.infos = infos
|
||||
result.append(main.model_dump())
|
||||
return SuccessResponse(data=result)
|
55
api/paddle_ocr_api.py
Normal file
55
api/paddle_ocr_api.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from core.response import SuccessResponse
|
||||
from fastapi import APIRouter, UploadFile, Form
|
||||
from schemas.orc_result import ResultInfo, ResultMain
|
||||
|
||||
from paddleocr import PaddleOCR
|
||||
import numpy as np
|
||||
import cv2
|
||||
|
||||
|
||||
app = APIRouter()
|
||||
|
||||
|
||||
paddle_ocr = PaddleOCR(lang='ch')
|
||||
|
||||
|
||||
def enhance_image(img):
|
||||
"""增强亮部和暗部,提升数字清晰度"""
|
||||
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
|
||||
l, a, b = cv2.split(lab)
|
||||
clahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8,8))
|
||||
cl = clahe.apply(l)
|
||||
enhanced = cv2.merge((cl, a, b))
|
||||
return cv2.cvtColor(enhanced, cv2.COLOR_LAB2BGR)
|
||||
|
||||
|
||||
@app.post("/upload")
|
||||
async def orc(files: list[UploadFile] = Form(...)):
|
||||
result = []
|
||||
for file in files:
|
||||
main = ResultMain()
|
||||
main.file_name = file.filename
|
||||
# 读取上传的文件内容
|
||||
image_data = await file.read()
|
||||
# 从字节数据读取图像
|
||||
np_array = np.frombuffer(image_data, np.uint8)
|
||||
img = cv2.imdecode(np_array, cv2.IMREAD_COLOR)
|
||||
# 图像增强
|
||||
img = enhance_image(img)
|
||||
# 4. 调用 OCR 模型进行识别
|
||||
datas = paddle_ocr.ocr(img, cls=False)
|
||||
infos = []
|
||||
if datas:
|
||||
for data in datas[0]:
|
||||
info = ResultInfo()
|
||||
bounding_boxs = data[0]
|
||||
left_up = bounding_boxs[0]
|
||||
right_down = bounding_boxs[2]
|
||||
info.bounding_box_left_up = [int(left_up[0]), int(left_up[1])]
|
||||
info.bounding_box_right_down = [int(right_down[0]), int(right_down[1])]
|
||||
info.text = data[1][0]
|
||||
info.confidence = round(data[1][1], 4)
|
||||
infos.append(info)
|
||||
main.infos = infos
|
||||
result.append(main.model_dump())
|
||||
return SuccessResponse(data=result)
|
Reference in New Issue
Block a user