1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- from fastapi import FastAPI, Request
- from fastapi.middleware.cors import CORSMiddleware
- from pydantic import BaseModel
- from paddleocr import PaddleOCR
- import cv2
- from core.direction import AngleDetector
- from utils.image import *
- from utils.time import timeit
- from utils.web import web_try
- from core.ocr import IdCardOcr
- import os
- app = FastAPI()
- origins = ["*"]
- app.add_middleware(
- CORSMiddleware,
- allow_origins=origins,
- allow_credentials=True,
- allow_methods=["*"],
- allow_headers=["*"],
- )
- use_gpu = os.getenv('USE_CUDA') == 'gpu'
- print(f'use gpu: {use_gpu}')
- # 初始化ocr模型和后处理模型
- ocr = PaddleOCR(use_angle_cls=True,
- rec_model_dir="./models/rec/",
- det_model_dir="./models/det/",
- cls_model_dir="./models/cls/",
- rec_algorithm='CRNN',
- ocr_version='PP-OCRv2',
- lang="ch",
- use_gpu=use_gpu,
- det_db_unclip_ratio=1.7,
- warmup=True)
- # 初始化 角度检测器 对象
- ad = AngleDetector(ocr)
- m = IdCardOcr(ocr, ad)
- @app.get("/ping")
- def ping():
- return "pong!"
- class ParamInfo(BaseModel):
- image: str
- image_type: str
- @app.post("/ocr_system/regbook")
- @web_try()
- @timeit
- def detect(request: Request, param: ParamInfo):
- image = base64_to_np(param.image)
- if image.size > 36000000:
- image = cv2.resize(image, (int(image.shape[0]*0.8), int(image.shape[1]*0.8)), interpolation=cv2.INTER_CUBIC)
- return m.predict(image, param.image_type)
- if __name__ == '__main__':
- import uvicorn
- import argparse
- parser = argparse.ArgumentParser()
- parser.add_argument('--host', default='0.0.0.0')
- parser.add_argument('--port', default=8080)
- opt = parser.parse_args()
- app_str = 'server:app' # make the app string equal to whatever the name of this file is
- uvicorn.run(app_str, host=opt.host, port=int(opt.port), reload=True)
|