Spaces:
Sleeping
Sleeping
easyocr
Browse files- Dockerfile +1 -4
- main.py +26 -12
- requirements.txt +0 -0
Dockerfile
CHANGED
@@ -1,13 +1,10 @@
|
|
1 |
FROM python:3.12.1
|
2 |
|
3 |
ENV HF_HOME /tmp
|
4 |
-
ENV TESSDATA_PREFIX /usr/local/share/tessdata
|
5 |
|
6 |
WORKDIR /code
|
7 |
|
8 |
-
RUN apt-get update && apt-get install -y libgl1-mesa-glx libboost-thread1.74.0
|
9 |
-
&& echo "Created Tesseract data directory at ${TESSDATA_PREFIX}" \
|
10 |
-
&& wget --quiet -O "${TESSDATA_PREFIX}/eng.traineddata" "https://github.com/tesseract-ocr/tessdata_best/raw/main/eng.traineddata"
|
11 |
|
12 |
|
13 |
|
|
|
1 |
FROM python:3.12.1
|
2 |
|
3 |
ENV HF_HOME /tmp
|
|
|
4 |
|
5 |
WORKDIR /code
|
6 |
|
7 |
+
RUN apt-get update && apt-get install -y libgl1-mesa-glx libboost-thread1.74.0 && rm -rf /var/lib/apt/lists/*
|
|
|
|
|
8 |
|
9 |
|
10 |
|
main.py
CHANGED
@@ -6,8 +6,8 @@ from datetime import datetime
|
|
6 |
from typing import List
|
7 |
|
8 |
import cv2
|
|
|
9 |
import numpy as np
|
10 |
-
import pytesseract
|
11 |
from fastapi import FastAPI, HTTPException, Request, UploadFile
|
12 |
from fastapi.middleware.cors import CORSMiddleware
|
13 |
from fastapi.responses import JSONResponse
|
@@ -51,9 +51,22 @@ app.add_middleware(
|
|
51 |
|
52 |
# AIモデルとテンプレートを起動時に読み込む
|
53 |
# trocr_pipeline = None # load_model()のロジックをここに
|
|
|
54 |
suit_templates = None
|
55 |
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
# @app.on_event("startup")
|
58 |
# def load_dependencies():
|
59 |
# global trocr_pipeline, suit_templates
|
@@ -81,14 +94,15 @@ async def analyze_image(image_paths: list[UploadFile]):
|
|
81 |
# image_paths = request["image_paths"]
|
82 |
print(image_paths)
|
83 |
progress = lambda x, desc: print(x, desc)
|
|
|
84 |
# global trocr_pipeline
|
85 |
# # モデルが読み込まれているか確認
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
|
93 |
all_results = []
|
94 |
num_total_files = len(image_paths)
|
@@ -187,14 +201,14 @@ async def analyze_image(image_paths: list[UploadFile]):
|
|
187 |
try:
|
188 |
ocr_results = []
|
189 |
for candidate in all_candidates_global:
|
190 |
-
img = Image.fromarray(
|
191 |
-
|
192 |
-
)
|
193 |
custom_config = (
|
194 |
r"--psm 10 -c tessedit_char_whitelist=0123456789AKQJ"
|
195 |
)
|
196 |
|
197 |
-
|
198 |
ocr_results.append(text)
|
199 |
# candidates_pil_images = [
|
200 |
# Image.fromarray(cv2.cvtColor(c["img"], cv2.COLOR_BGR2RGB))
|
@@ -214,7 +228,7 @@ async def analyze_image(image_paths: list[UploadFile]):
|
|
214 |
# reds = []
|
215 |
for i, result in enumerate(ocr_results):
|
216 |
# text = result[0]["generated_text"].upper().strip()
|
217 |
-
text = result
|
218 |
print(text, is_text_valid(text))
|
219 |
|
220 |
text = is_text_valid(text)
|
|
|
6 |
from typing import List
|
7 |
|
8 |
import cv2
|
9 |
+
import easyocr
|
10 |
import numpy as np
|
|
|
11 |
from fastapi import FastAPI, HTTPException, Request, UploadFile
|
12 |
from fastapi.middleware.cors import CORSMiddleware
|
13 |
from fastapi.responses import JSONResponse
|
|
|
51 |
|
52 |
# AIモデルとテンプレートを起動時に読み込む
|
53 |
# trocr_pipeline = None # load_model()のロジックをここに
|
54 |
+
reader = None
|
55 |
suit_templates = None
|
56 |
|
57 |
|
58 |
+
@app.on_event("startup")
|
59 |
+
def load_ocr_model():
|
60 |
+
"""
|
61 |
+
アプリケーション起動時に一度だけEasyOCRのモデルを読み込み、
|
62 |
+
グローバル変数readerに格納する。
|
63 |
+
"""
|
64 |
+
global reader
|
65 |
+
# 使用する言語をリストで指定(英語の場合は'en')
|
66 |
+
reader = easyocr.Reader(["en"])
|
67 |
+
print("EasyOCR model loaded successfully.")
|
68 |
+
|
69 |
+
|
70 |
# @app.on_event("startup")
|
71 |
# def load_dependencies():
|
72 |
# global trocr_pipeline, suit_templates
|
|
|
94 |
# image_paths = request["image_paths"]
|
95 |
print(image_paths)
|
96 |
progress = lambda x, desc: print(x, desc)
|
97 |
+
global reader
|
98 |
# global trocr_pipeline
|
99 |
# # モデルが読み込まれているか確認
|
100 |
+
if reader is None:
|
101 |
+
print(
|
102 |
+
"AIモデルがまだ読み込まれていません。しばらく待ってから再度お試しください。"
|
103 |
+
)
|
104 |
+
# 空の更新を返すことで、UIの状態を変えずに処理を終了
|
105 |
+
return
|
106 |
|
107 |
all_results = []
|
108 |
num_total_files = len(image_paths)
|
|
|
201 |
try:
|
202 |
ocr_results = []
|
203 |
for candidate in all_candidates_global:
|
204 |
+
# img = Image.fromarray(
|
205 |
+
# cv2.cvtColor(candidate["img"], cv2.COLOR_BGR2RGB)
|
206 |
+
# )
|
207 |
custom_config = (
|
208 |
r"--psm 10 -c tessedit_char_whitelist=0123456789AKQJ"
|
209 |
)
|
210 |
|
211 |
+
result = reader.readtext(candidate["img"])
|
212 |
ocr_results.append(text)
|
213 |
# candidates_pil_images = [
|
214 |
# Image.fromarray(cv2.cvtColor(c["img"], cv2.COLOR_BGR2RGB))
|
|
|
228 |
# reds = []
|
229 |
for i, result in enumerate(ocr_results):
|
230 |
# text = result[0]["generated_text"].upper().strip()
|
231 |
+
_, text, _ = result[0]
|
232 |
print(text, is_text_valid(text))
|
233 |
|
234 |
text = is_text_valid(text)
|
requirements.txt
CHANGED
Binary files a/requirements.txt and b/requirements.txt differ
|
|