# -*- coding: utf-8 -*-
import requests
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
def ocr(language, base64_data):
if language == "en":
# English
language_code = "models/config_en.txt"
elif language == "zh":
# 简体中文
language_code = "models/config_chinese.txt"
elif language == "tw":
# 繁體中文
language_code = "models/config_chinese_cht(v2).txt"
elif language == "kr":
# 한국어
language_code = "models/config_korean.txt"
elif language == "ru":
# Русский
language_code = "models/config_cyrillic.txt"
elif language == "ja":
# 日本語
language_code = "models/config_japan.txt"
else:
print("没有传入语言代码")
return False
payload = {
"base64": base64_data,
"options": {
# 通用参数
"tbpu.parser": "multi_para",
"data.format": "json",
# 引擎参数
"ocr.angle": False, # 是否进行图像旋转校正
"ocr.language": language_code,
}
}
# 论坛不能发url,因此,自己加前缀... IP:1224/api/ocr
result = requests.post('IP:1224/api/ocr', json=payload).json()
if result['code'] == 100:
return result
else:
return None
def ocr_lua_data_dict(data, box):
return {
"text": data['text'],
"x": (box[0][0] + box[1][0]) / 2,
"y": (box[0][1] + box[2][1]) / 2,
"box": data['box'],
}
def umi_ocr(keyword, language, base64_data, contains: bool = True):
result = ocr(language, base64_data)
if result is None:
return None
# 左上角 右上角 左下角 右下角
# [111, 826], [330, 826], [330, 854], [111, 854]
for data in result['data']:
if contains:
if keyword in data["text"]:
print(data)
return ocr_lua_data_dict(data, data['box'])
else:
if data["text"] == keyword:
print(data)
return ocr_lua_data_dict(data, data['box'])
print("ocr没识别到包含文本")
return None
class OcrItem(BaseModel):
keyword: str
language: str
Base64: str
contains: bool
@app.post("/ocr_text", summary="umi ocr识别指定文本-返回坐标")
async def ocr_img(data: OcrItem):
# "英语":"en",
# "日语":"ja",
# "简体中文":"zh",
# "繁体中文":"tw",
# "俄语":"ru",
# "韩语":"kr",
# keyword: 要匹配的关键词 language:语言模型 contains:包含关键词还是等于,如果为假就是等于,全部匹配
# payload = {
# "keyword": "Search",
# "language": "en",
# "contains": "true",
# "Base64": img_data
# }
try:
body = data.model_dump()
result = umi_ocr(body['keyword'], body['language'], data.Base64, body['contains'])
if result is None or result == []:
return {"status": "error", "result": None}
else:
return {"status": "ok", "result": result}
except Exception as e:
return {"status": "error", "result": None, "error": e}
if __name__ == "__main__":
# 这python文件名字一定是 server.py 不然无法启动
uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=False)