Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ from doctr.models import ocr_predictor
|
|
8 |
|
9 |
# Initialize models
|
10 |
models = {
|
11 |
-
"EasyOCR": easyocr.Reader(['
|
12 |
"TrOCR": {
|
13 |
"processor": TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed"),
|
14 |
"model": VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-printed")
|
@@ -17,24 +17,24 @@ models = {
|
|
17 |
}
|
18 |
|
19 |
def run_easyocr(image):
|
20 |
-
"""
|
21 |
try:
|
22 |
result = models["EasyOCR"].readtext(np.array(image), detail=0)
|
23 |
return ' '.join(result) if result else ''
|
24 |
except Exception as e:
|
25 |
-
return f"
|
26 |
|
27 |
def run_trocr(image):
|
28 |
-
"""
|
29 |
try:
|
30 |
pixel_values = models["TrOCR"]["processor"](image, return_tensors="pt").pixel_values
|
31 |
generated_ids = models["TrOCR"]["model"].generate(pixel_values)
|
32 |
return models["TrOCR"]["processor"].batch_decode(generated_ids, skip_special_tokens=True)[0]
|
33 |
except Exception as e:
|
34 |
-
return f"
|
35 |
|
36 |
def run_doctr(image):
|
37 |
-
"""
|
38 |
try:
|
39 |
if isinstance(image, Image.Image):
|
40 |
image = np.array(image)
|
@@ -42,10 +42,10 @@ def run_doctr(image):
|
|
42 |
return ' '.join([word[0] for page in result.pages for block in page.blocks
|
43 |
for line in block.lines for word in line.words])
|
44 |
except Exception as e:
|
45 |
-
return f"
|
46 |
|
47 |
def compare_models(image):
|
48 |
-
"""
|
49 |
if isinstance(image, np.ndarray):
|
50 |
image = Image.fromarray(image)
|
51 |
image = image.convert("RGB")
|
@@ -53,7 +53,7 @@ def compare_models(image):
|
|
53 |
results = {}
|
54 |
times = {}
|
55 |
|
56 |
-
#
|
57 |
for name, func in [("EasyOCR", run_easyocr),
|
58 |
("TrOCR", run_trocr),
|
59 |
("DocTR", run_doctr)]:
|
@@ -61,14 +61,14 @@ def compare_models(image):
|
|
61 |
results[name] = func(image)
|
62 |
times[name] = time.time() - start
|
63 |
|
64 |
-
#
|
65 |
table_rows = []
|
66 |
for name in results:
|
67 |
table_rows.append(f"""
|
68 |
<tr>
|
69 |
<td style="padding: 8px; border: 1px solid #ddd; text-align: center; font-weight: bold;">{name}</td>
|
70 |
-
<td style="padding: 8px; border: 1px solid #ddd;">{results[name]}</td>
|
71 |
-
<td style="padding: 8px; border: 1px solid #ddd; text-align: center;">{times[name]:.3f}
|
72 |
</tr>
|
73 |
""")
|
74 |
|
@@ -76,9 +76,9 @@ def compare_models(image):
|
|
76 |
<div style="overflow-x: auto;">
|
77 |
<table style="width:100%; border-collapse: collapse; margin: 15px 0; font-family: Arial, sans-serif;">
|
78 |
<tr style="background-color: #4CAF50; color: white;">
|
79 |
-
<th style="padding: 12px; border: 1px solid #ddd; text-align: center;"
|
80 |
-
<th style="padding: 12px; border: 1px solid #ddd; text-align: center;"
|
81 |
-
<th style="padding: 12px; border: 1px solid #ddd; text-align: center;"
|
82 |
</tr>
|
83 |
{''.join(table_rows)}
|
84 |
</table>
|
@@ -87,27 +87,27 @@ def compare_models(image):
|
|
87 |
|
88 |
return comparison, results['EasyOCR'], results['TrOCR'], results['DocTR']
|
89 |
|
90 |
-
#
|
91 |
-
with gr.Blocks(title="
|
92 |
gr.Markdown("""
|
93 |
-
# 🚀
|
94 |
-
|
95 |
""")
|
96 |
|
97 |
with gr.Row():
|
98 |
with gr.Column():
|
99 |
-
img_input = gr.Image(label="
|
100 |
gr.Examples(
|
101 |
-
examples=["
|
102 |
inputs=img_input,
|
103 |
-
label="
|
104 |
)
|
105 |
-
submit_btn = gr.Button("
|
106 |
|
107 |
with gr.Column():
|
108 |
-
comparison = gr.HTML(label="
|
109 |
-
with gr.Accordion("
|
110 |
-
gr.Markdown("###
|
111 |
easy_output = gr.Textbox(label="EasyOCR")
|
112 |
trocr_output = gr.Textbox(label="TrOCR")
|
113 |
doctr_output = gr.Textbox(label="DocTR")
|
|
|
8 |
|
9 |
# Initialize models
|
10 |
models = {
|
11 |
+
"EasyOCR": easyocr.Reader(['fa']), # تنظیم زبان فارسی
|
12 |
"TrOCR": {
|
13 |
"processor": TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed"),
|
14 |
"model": VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-printed")
|
|
|
17 |
}
|
18 |
|
19 |
def run_easyocr(image):
|
20 |
+
"""تابع پردازش تصویر با EasyOCR برای فارسی"""
|
21 |
try:
|
22 |
result = models["EasyOCR"].readtext(np.array(image), detail=0)
|
23 |
return ' '.join(result) if result else ''
|
24 |
except Exception as e:
|
25 |
+
return f"خطا: {str(e)}"
|
26 |
|
27 |
def run_trocr(image):
|
28 |
+
"""تابع پردازش تصویر با TrOCR برای فارسی"""
|
29 |
try:
|
30 |
pixel_values = models["TrOCR"]["processor"](image, return_tensors="pt").pixel_values
|
31 |
generated_ids = models["TrOCR"]["model"].generate(pixel_values)
|
32 |
return models["TrOCR"]["processor"].batch_decode(generated_ids, skip_special_tokens=True)[0]
|
33 |
except Exception as e:
|
34 |
+
return f"خطا: {str(e)}"
|
35 |
|
36 |
def run_doctr(image):
|
37 |
+
"""تابع پردازش تصویر با DocTR برای فارسی"""
|
38 |
try:
|
39 |
if isinstance(image, Image.Image):
|
40 |
image = np.array(image)
|
|
|
42 |
return ' '.join([word[0] for page in result.pages for block in page.blocks
|
43 |
for line in block.lines for word in line.words])
|
44 |
except Exception as e:
|
45 |
+
return f"خطا: {str(e)}"
|
46 |
|
47 |
def compare_models(image):
|
48 |
+
"""تابع اصلی مقایسه مدلها"""
|
49 |
if isinstance(image, np.ndarray):
|
50 |
image = Image.fromarray(image)
|
51 |
image = image.convert("RGB")
|
|
|
53 |
results = {}
|
54 |
times = {}
|
55 |
|
56 |
+
# اجرای تمام مدلهای OCR
|
57 |
for name, func in [("EasyOCR", run_easyocr),
|
58 |
("TrOCR", run_trocr),
|
59 |
("DocTR", run_doctr)]:
|
|
|
61 |
results[name] = func(image)
|
62 |
times[name] = time.time() - start
|
63 |
|
64 |
+
# ایجاد جدول مقایسه
|
65 |
table_rows = []
|
66 |
for name in results:
|
67 |
table_rows.append(f"""
|
68 |
<tr>
|
69 |
<td style="padding: 8px; border: 1px solid #ddd; text-align: center; font-weight: bold;">{name}</td>
|
70 |
+
<td style="padding: 8px; border: 1px solid #ddd; text-align: right; direction: rtl;">{results[name]}</td>
|
71 |
+
<td style="padding: 8px; border: 1px solid #ddd; text-align: center;">{times[name]:.3f} ثانیه</td>
|
72 |
</tr>
|
73 |
""")
|
74 |
|
|
|
76 |
<div style="overflow-x: auto;">
|
77 |
<table style="width:100%; border-collapse: collapse; margin: 15px 0; font-family: Arial, sans-serif;">
|
78 |
<tr style="background-color: #4CAF50; color: white;">
|
79 |
+
<th style="padding: 12px; border: 1px solid #ddd; text-align: center;">مدل</th>
|
80 |
+
<th style="padding: 12px; border: 1px solid #ddd; text-align: center;">متن استخراج شده</th>
|
81 |
+
<th style="padding: 12px; border: 1px solid #ddd; text-align: center;">زمان پردازش</th>
|
82 |
</tr>
|
83 |
{''.join(table_rows)}
|
84 |
</table>
|
|
|
87 |
|
88 |
return comparison, results['EasyOCR'], results['TrOCR'], results['DocTR']
|
89 |
|
90 |
+
# رابط کاربری Gradio
|
91 |
+
with gr.Blocks(title="مقایسه مدلهای OCR فارسی", theme=gr.themes.Soft()) as demo:
|
92 |
gr.Markdown("""
|
93 |
+
# 🚀 مقایسه مدلهای تشخیص متن فارسی
|
94 |
+
مقایسه عملکرد مدلهای مختلف OCR برای استخراج متن از تصاویر فارسی
|
95 |
""")
|
96 |
|
97 |
with gr.Row():
|
98 |
with gr.Column():
|
99 |
+
img_input = gr.Image(label="تصویر ورودی", type="pil")
|
100 |
gr.Examples(
|
101 |
+
examples=["sample_fa1.jpg", "sample_fa2.png"],
|
102 |
inputs=img_input,
|
103 |
+
label="تصاویر نمونه"
|
104 |
)
|
105 |
+
submit_btn = gr.Button("مقایسه مدلها", variant="primary")
|
106 |
|
107 |
with gr.Column():
|
108 |
+
comparison = gr.HTML(label="نتایج مقایسه")
|
109 |
+
with gr.Accordion("نتایج تفکیکی", open=False):
|
110 |
+
gr.Markdown("### خروجی هر مدل")
|
111 |
easy_output = gr.Textbox(label="EasyOCR")
|
112 |
trocr_output = gr.Textbox(label="TrOCR")
|
113 |
doctr_output = gr.Textbox(label="DocTR")
|