123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- from fastapi import FastAPI, Request
- from fastapi.middleware.cors import CORSMiddleware
- from pydantic import BaseModel
- from paddleocr import PaddleOCR
- from core.direction import AngleDetector
- from sx_utils.sximage import *
- from sx_utils.sxtime import sxtimeit
- from sx_utils.sxweb import web_try
- from core.ocr import BusinessLicenseOcr
- import os
- app = FastAPI()
- origins = ["*"]
- # CORS 跨源资源共享
- 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 = PaddleOCR(
- use_gpu=use_gpu,
- det_db_unclip_ratio=1.8,
- det_db_thresh=0.1,
- det_db_box_thresh=0.3,)
- # 初始化 角度检测器 对象
- ad = AngleDetector(ocr)
- # 初始化 ocr识别 对象
- m = BusinessLicenseOcr(ocr, ad)
- # Get 健康检查
- @app.get("/ping")
- def ping():
- return "pong!"
- # 解析传入的 json对象
- class BusinessLicenseInfo(BaseModel):
- image: str
- # Post 接口
- # 计算耗时
- # 异常处理
- @app.post("/ocr_system/business_license")
- @sxtimeit
- @web_try()
- # 传入=> base64码 -> np
- # 返回=> 检测到到结果 -> (conf, angle, parser, image_type)
- def blfe(request: Request, blfe: BusinessLicenseInfo):
- image = base64_to_np(blfe.image)
- return m.predict(image)
- 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)
|