实现百度ocr功能
This commit is contained in:
parent
d20d46aafb
commit
8e280a593d
@ -1,3 +1,4 @@
|
||||
#--------------------------数据库配置-----------------------------------
|
||||
DB_HOST="10.0.0.17"
|
||||
DB_PORT= 3306
|
||||
DB_USER="root"
|
||||
@ -7,4 +8,11 @@ DB_CHARSET= "utf8mb4"
|
||||
DB_POOL_SIZE= 6
|
||||
DB_MAX_OVERFLOW= 10
|
||||
DB_POOL_TIMEOUT= 30
|
||||
DB_POOL_RECYCLE= 1800
|
||||
DB_POOL_RECYCLE= 1800
|
||||
#--------------------------数据库配置end-----------------------------------
|
||||
|
||||
|
||||
#---------------------------ocr配置----------------------------------------
|
||||
TEXT_DETECTION_MODEL_DIR= '/home/admin-root/haotian/康达瑞贝斯机器狗/PaddleOCR-3.1.0/output/PP-OCRv5_server_det_infer_20250814'
|
||||
TEXT_RECONGNITION_MODEL_DIR= '/home/admin-root/haotian/康达瑞贝斯机器狗/PaddleOCR-3.1.0/output/PP-OCRv5_server_rec_infer_20250815'
|
||||
#---------------------------ocr配置end----------------------------------------
|
||||
@ -5,9 +5,12 @@ from app.core.database import get_db
|
||||
from app.services.imageServices import ImageService
|
||||
from app.schemas.image import ImageBase
|
||||
from app.util.responseHttp import ResponseUtil
|
||||
from app.util.baiduOCR import BaiduOCR
|
||||
# from app.crud.event import event
|
||||
# from app.schemas.event import EventList, EventDetail, EventUpdate, EventQuery, TestEvent
|
||||
|
||||
baiduOCR = BaiduOCR()
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/hello")
|
||||
@ -25,3 +28,20 @@ async def test_select(
|
||||
"""
|
||||
result = await ImageService.get_image_list(db,image_query)
|
||||
return ResponseUtil.success(msg="success", data=result)
|
||||
|
||||
|
||||
@router.get("/test_ocr")
|
||||
async def test_ocr(
|
||||
# image_path: str = None
|
||||
):
|
||||
"""
|
||||
测试OCR
|
||||
"""
|
||||
|
||||
image_path = '/home/admin-root/haotian/康达瑞贝斯机器狗/data_image/001读表图片/2c7cc83019e7388a7041101da92c9829_frame_000000.jpg'
|
||||
|
||||
|
||||
result = baiduOCR.ocr(image_path)
|
||||
# print(result)
|
||||
return ResponseUtil.success(msg="success", data=result)
|
||||
# return ResponseUtil.success(msg="success")
|
||||
|
||||
@ -20,6 +20,9 @@ class DataBaseSettings(BaseSettings):
|
||||
# class Config:
|
||||
# env_file = ".env"
|
||||
|
||||
class OCRSettings(BaseException):
|
||||
TEXT_DETECTION_MODEL_DIR: str = '/home/admin-root/haotian/康达瑞贝斯机器狗/PaddleOCR-3.1.0/output/PP-OCRv5_server_det_infer_20250814'
|
||||
TEXT_RECONGNITION_MODEL_DIR: str = '/home/admin-root/haotian/康达瑞贝斯机器狗/PaddleOCR-3.1.0/output/PP-OCRv5_server_rec_infer_20250815'
|
||||
|
||||
class GetSettings:
|
||||
|
||||
@ -29,6 +32,9 @@ class GetSettings:
|
||||
def get_database_settings(self):
|
||||
return DataBaseSettings()
|
||||
|
||||
def get_ocr_settings(self):
|
||||
return OCRSettings()
|
||||
|
||||
|
||||
|
||||
def parse_cli_args(self):
|
||||
@ -63,4 +69,6 @@ get_settings = GetSettings()
|
||||
|
||||
database_settings = get_settings.get_database_settings()
|
||||
|
||||
ocr_settings = get_settings.get_ocr_settings()
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
|
||||
|
||||
from sqlalchemy.orm import sessionmaker, declarative_base
|
||||
from app.core.config import database_settings
|
||||
from app.config.config import database_settings
|
||||
|
||||
engine = create_async_engine(
|
||||
f"mysql+aiomysql://{database_settings.DB_USER}:{database_settings.DB_PASSWORD}@{database_settings.DB_HOST}:{database_settings.DB_PORT}/{database_settings.DB_NAME}",
|
||||
|
||||
32
机器狗后台服务/app/util/baiduOCR.py
Normal file
32
机器狗后台服务/app/util/baiduOCR.py
Normal file
@ -0,0 +1,32 @@
|
||||
from paddleocr import PaddleOCR
|
||||
from app.config.config import OCRSettings
|
||||
|
||||
|
||||
class BaiduOCR:
|
||||
|
||||
def __init__(self):
|
||||
self.model = PaddleOCR(
|
||||
# 文本检测模型地址
|
||||
# text_detection_model_dir = "/home/admin-root/haotian/康达瑞贝斯机器狗/ocr_model/PP-OCRv5_server_det",
|
||||
text_detection_model_dir=OCRSettings.TEXT_DETECTION_MODEL_DIR,
|
||||
# 文本识别模型地址
|
||||
# text_recognition_model_dir = "/home/admin-root/haotian/康达瑞贝斯机器狗/ocr_model/PP-OCRv5_server_rec",
|
||||
text_recognition_model_dir=OCRSettings.TEXT_RECONGNITION_MODEL_DIR,
|
||||
use_doc_orientation_classify=False,
|
||||
use_doc_unwarping=False,
|
||||
use_textline_orientation=False)
|
||||
def ocr(self, image_path:str):
|
||||
result = self.model.predict(image_path)
|
||||
result = self.parse_result(result)
|
||||
return result
|
||||
|
||||
def parse_result(self, result):
|
||||
text_list = []
|
||||
for item in result:
|
||||
text_list.append([item['rec_texts'], item['rec_scores']])
|
||||
return text_list
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
ocr = BaiduOCR()
|
||||
print(ocr.ocr(""))
|
||||
Loading…
Reference in New Issue
Block a user