1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- from fastapi import FastAPI, Request
- from fastapi.middleware.cors import CORSMiddleware
- from pydantic import BaseModel
- from paddleocr import PaddleOCR
- from sx_utils.sximage import *
- from sx_utils.sxtime import sxtimeit
- from sx_utils.sxweb 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=["*"],
- )
- # templates = Jinja2Templates(directory='templates')
- use_gpu = False
- if os.getenv('USE_CUDA') == 'gpu':
- use_gpu = True
- print(f'use gpu: {use_gpu}')
- # 初始化ocr模型和后处理模型
- ocr = PaddleOCR(use_angle_cls=True,
- rec_model_dir="./ch_ppocr_server_v2.0_rec_infer/",
- det_model_dir="./ch_ppocr_server_v2.0_det_infer/",
- cls_model_dir="./huko_cls_infer/",
- rec_algorithm='CRNN',
- ocr_version='PP-OCRv2',
- rec_char_dict_path="./ppocr_keys_v1.txt", lang="ch",
- use_gpu=use_gpu,
- warmup=True)
- # ocr = PaddleOCR(use_angle_cls=True,
- # use_gpu=use_gpu)
- m = IdCardOcr(ocr)
- @app.get("/ping")
- def ping():
- return "pong!"
- # @app.get("/")
- # def home(request: Request):
- # ''' Returns html jinja2 template render for home page form
- # '''
- #
- # return templates.TemplateResponse('home.html', {
- # "request": request,
- # })
- class ParamInfo(BaseModel):
- image: str
- image_type: str
- # /ocr_system/bankcard 银行卡
- # /ocr_system/regbook 户口本
- # /ocr_system/schoolcert 学信网
- @app.post("/ocr_system/regbook")
- @web_try()
- @sxtimeit
- def detect(request: Request, param: ParamInfo):
- image = base64_to_np(param.image)
- 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)
|