OmidSakaki commited on
Commit
1f0a2e7
·
verified ·
1 Parent(s): 35b0235

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -8,7 +8,7 @@ from doctr.models import ocr_predictor
8
 
9
  # Initialize models
10
  models = {
11
- "EasyOCR": easyocr.Reader(['en']),
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
- """Run EasyOCR on image"""
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"Error: {str(e)}"
26
 
27
  def run_trocr(image):
28
- """Run TrOCR on image"""
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"Error: {str(e)}"
35
 
36
  def run_doctr(image):
37
- """Run DocTR on image"""
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"Error: {str(e)}"
46
 
47
  def compare_models(image):
48
- """Compare all OCR models"""
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
- # Run all OCR models
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
- # Create comparison table
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}s</td>
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;">Model</th>
80
- <th style="padding: 12px; border: 1px solid #ddd; text-align: center;">Extracted Text</th>
81
- <th style="padding: 12px; border: 1px solid #ddd; text-align: center;">Processing Time</th>
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
- # Create Gradio interface
91
- with gr.Blocks(title="English OCR Comparison", theme=gr.themes.Soft()) as demo:
92
  gr.Markdown("""
93
- # 🚀 English OCR Model Comparison
94
- Compare the performance of top OCR models for English text extraction
95
  """)
96
 
97
  with gr.Row():
98
  with gr.Column():
99
- img_input = gr.Image(label="Upload Image", type="pil")
100
  gr.Examples(
101
- examples=["sample1.jpg", "sample2.png"],
102
  inputs=img_input,
103
- label="Try these sample images"
104
  )
105
- submit_btn = gr.Button("Compare Models", variant="primary")
106
 
107
  with gr.Column():
108
- comparison = gr.HTML(label="Comparison Results")
109
- with gr.Accordion("Detailed Results", open=False):
110
- gr.Markdown("### Individual Model Outputs")
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")