ocr_engine_server.py 2.75 KB
# -*- coding: utf-8 -*-
# @Author        : Lyu Kui
# @Email         : 9428.al@gmail.com
# @Create Date   : 2022-06-05 20:49:51
# @Last Modified : 2022-08-19 17:24:55
# @Description   : 

import os

os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

from sanic import Sanic
from sanic.response import json

from turnsole.ocr_engine import angle_detector
from turnsole.ocr_engine import text_detector
from turnsole.ocr_engine import text_recognizer
from turnsole.ocr_engine import object_detector
from turnsole.ocr_engine import signature_detector

from turnsole import bytes_to_bgr

app = Sanic("OCR_ENGINE")
app.config.REQUEST_MAX_SIZE = 1000000000  # 请求的大小(字节)/ 1GB
app.config.REQUEST_BUFFER_QUEUE_SIZE = 1000  # 请求流缓冲区队列大小
app.config.REQUEST_TIMEOUT = 600  # 请求到达需要多长时间(秒)
app.config.RESPONSE_TIMEOUT = 600  # 处理响应需要多长时间(秒)


@app.post('/gen_ocr')
async def ocr_engine(request):
    # request.files.get() 具有 type/body/name 三个属性
    file = request.files.get('file').body
    # 将 bytes 转成 bgr 图片
    image = bytes_to_bgr(file)
    # 文字检测
    boxes = text_detector.predict(image)
    # 文字识别
    res, _ = text_recognizer.predict_batch(image[..., ::-1], boxes)
    resp = {}
    resp["ocr_results"] = res
    return json(resp)


@app.post('/gen_ocr_with_rotation', )
async def ocr_engine_with_rotation(request):
    # request.files.get() 具有 type/body/name 三个属性
    file = request.files.get('file').body
    # 将 bytes 转成 bgr 图片
    image = bytes_to_bgr(file)
    # 方向检测
    image, direction = angle_detector.ADC(image.copy(), fine_degree=False)
    # 文字检测
    boxes = text_detector.predict(image)
    # 文字识别
    res, _ = text_recognizer.predict_batch(image[..., ::-1], boxes)

    resp = {}
    resp["ocr_results"] = res
    resp["direction"] = direction
    return json(resp)


@app.post("/object_detect")
async def object_detect(request):
    # request.files.get() 具有 type/body/name 三个属性
    file = request.files.get('file').body
    # 将 bytes 转成 bgr 图片
    image = bytes_to_bgr(file)
    # 通用文件检测
    object_list = object_detector.process(image)
    return json(object_list)


@app.post("/signature_detect")
async def signature_detect(request):
    # request.files.get() 具有 type/body/name 三个属性
    file = request.files.get('file').body
    # 将 bytes 转成 bgr 图片
    image = bytes_to_bgr(file)
    # 签字盖章二维码条形码检测
    signature_list = signature_detector.process(image)
    return json(signature_list)


if __name__ == "__main__":
    # app.run(host="0.0.0.0", port=9001)
    app.run(host="192.168.10.11", port=9002, workers=10)
    # uvicorn server:app --port 9001 --workers 10