-
-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rapidocr_api): support mutli rapidocr
- Loading branch information
Showing
2 changed files
with
17 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,48 @@ | ||
# -*- encoding: utf-8 -*- | ||
# @Author: SWHL | ||
# @Contact: [email protected] | ||
|
||
import argparse | ||
import base64 | ||
import importlib.util | ||
import io | ||
import os | ||
import sys | ||
from pathlib import Path | ||
from typing import Dict | ||
import importlib.util | ||
|
||
import numpy as np | ||
import uvicorn | ||
from fastapi import FastAPI, Form, UploadFile | ||
from PIL import Image | ||
|
||
if importlib.util.find_spec("rapidocr_runtime"): | ||
if importlib.util.find_spec("rapidocr_onnxruntime"): | ||
from rapidocr_onnxruntime import RapidOCR | ||
elif importlib.util.find_spec("rapidocr_paddle"): | ||
from rapidocr_paddle import RapidOCR | ||
elif importlib.util.find_spec("rapidocr_openvino"): | ||
from rapidocr_openvino import RapidOCR | ||
else: | ||
raise ImportError( | ||
"Pleas install one of [rapidocr-runtime,rapidocr-paddle,rapidocr-openvino]" | ||
"Please install one of [rapidocr_onnxruntime,rapidocr-paddle,rapidocr-openvino]" | ||
) | ||
|
||
sys.path.append(str(Path(__file__).resolve().parent.parent)) | ||
|
||
|
||
class OCRAPIUtils: | ||
def __init__(self) -> None: | ||
# 从环境变量中读取参数 | ||
det_model_path = os.getenv("det_model_path", None) | ||
cls_model_path = os.getenv("cls_model_path", None) | ||
rec_model_path = os.getenv("rec_model_path", None) | ||
|
||
self.ocr = RapidOCR( | ||
det_model_path=det_model_path, | ||
cls_model_path=cls_model_path, | ||
rec_model_path=rec_model_path, | ||
) | ||
if det_model_path is None or cls_model_path is None or rec_model_path is None: | ||
self.ocr = RapidOCR() | ||
else: | ||
self.ocr = RapidOCR( | ||
det_model_path=det_model_path, | ||
cls_model_path=cls_model_path, | ||
rec_model_path=rec_model_path, | ||
) | ||
|
||
def __call__( | ||
self, img: Image.Image, use_det=None, use_cls=None, use_rec=None, **kwargs | ||
|
@@ -54,7 +55,6 @@ def __call__( | |
if not ocr_res: | ||
return {} | ||
|
||
# 转换为字典格式: 兼容所有参数情况 | ||
out_dict = {} | ||
for i, dats in enumerate(ocr_res): | ||
values = {} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters