This commit is contained in:
Twilight 2024-10-02 17:17:30 +08:00
parent 7688e822d6
commit fb03a380db
5 changed files with 1445 additions and 3 deletions

2
.gitignore vendored
View File

@ -321,3 +321,5 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder. # option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/ #.idea/
# Model files
models

View File

@ -2,9 +2,10 @@
<module type="PYTHON_MODULE" version="4"> <module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager"> <component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.idea/httpRequests" />
<excludeFolder url="file://$MODULE_DIR$/.venv" /> <excludeFolder url="file://$MODULE_DIR$/.venv" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="jdk" jdkName="Python 3.12 (ocr)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -5,10 +5,11 @@ from typing import Optional
import asyncio import asyncio
import aiohttp # 用于异步 HTTP 请求 import aiohttp # 用于异步 HTTP 请求
MODEL_DIR = "./models"
app = FastAPI() app = FastAPI()
# 初始化常用的语言组合 OCR 读取器 # 初始化常用的语言组合 OCR 读取器
reader_sim = easyocr.Reader(['ch_sim', 'en']) reader_sim = easyocr.Reader(lang_list=['ch_sim', 'en'], model_storage_directory=MODEL_DIR)
# 存储 OCR 读取器的缓存字典 # 存储 OCR 读取器的缓存字典
readers = {} readers = {}
@ -38,7 +39,8 @@ async def get_reader(lang_1: Optional[str], lang_2: Optional[str]):
if lang_combination not in readers: if lang_combination not in readers:
try: try:
# 尝试初始化 OCR 读取器 # 尝试初始化 OCR 读取器
readers[lang_combination] = easyocr.Reader(list(lang_combination)) readers[lang_combination] = easyocr.Reader(lang_list=list(lang_combination),
model_storage_directory=MODEL_DIR)
except ValueError as ve: except ValueError as ve:
return None, str(ve) # 返回错误信息 return None, str(ve) # 返回错误信息
except Exception as e: except Exception as e:

1419
poetry.lock generated Normal file

File diff suppressed because it is too large Load Diff

18
pyproject.toml Normal file
View File

@ -0,0 +1,18 @@
[project]
name = "ocr"
[tool.poetry]
name = "ocr"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.12"
fastapi = "^0.115.0"
easyocr = "^1.7.2"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"