OmidSakaki commited on
Commit
dd4c7df
·
verified ·
1 Parent(s): abcd869

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -2,33 +2,41 @@ import gradio as gr
2
  import time
3
  import numpy as np
4
  from PIL import Image
5
- from paddleocr import PaddleOCR
 
 
 
 
 
 
6
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
7
 
8
- # Initialize models
9
- paddle_ocr = PaddleOCR(lang='fa', use_textline_orientation=True)
10
  trocr_processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed")
11
  trocr_model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-printed")
12
 
13
  def run_paddleocr(image):
14
  """Run PaddleOCR on image"""
15
- # Convert to numpy array if needed
16
  if isinstance(image, Image.Image):
17
  image = np.array(image)
18
 
19
- result = paddle_ocr.ocr(image, cls=True)
20
- text = ' '.join([line[1][0] for line in result[0]]) if result else ''
21
- return text
 
 
22
 
23
  def run_trocr(image):
24
  """Run TrOCR on image"""
25
- # Convert to PIL Image if needed
26
  if isinstance(image, np.ndarray):
27
  image = Image.fromarray(image)
28
 
29
- pixel_values = trocr_processor(image, return_tensors="pt").pixel_values
30
- generated_ids = trocr_model.generate(pixel_values)
31
- return trocr_processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
 
 
 
32
 
33
  def compare_models(image):
34
  """Compare PaddleOCR and TrOCR models"""
 
2
  import time
3
  import numpy as np
4
  from PIL import Image
5
+
6
+ try:
7
+ from paddleocr import PaddleOCR
8
+ paddle_ocr = PaddleOCR(lang='fa', use_textline_orientation=True)
9
+ except ImportError:
10
+ raise ImportError("لطفا ابتدا paddlepaddle و paddleocr را نصب کنید: pip install paddlepaddle paddleocr")
11
+
12
  from transformers import TrOCRProcessor, VisionEncoderDecoderModel
13
 
14
+ # Initialize TrOCR
 
15
  trocr_processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed")
16
  trocr_model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-printed")
17
 
18
  def run_paddleocr(image):
19
  """Run PaddleOCR on image"""
 
20
  if isinstance(image, Image.Image):
21
  image = np.array(image)
22
 
23
+ try:
24
+ result = paddle_ocr.ocr(image, cls=True)
25
+ return ' '.join([line[1][0] for line in result[0]]) if result else ''
26
+ except Exception as e:
27
+ return f"خطا در PaddleOCR: {str(e)}"
28
 
29
  def run_trocr(image):
30
  """Run TrOCR on image"""
 
31
  if isinstance(image, np.ndarray):
32
  image = Image.fromarray(image)
33
 
34
+ try:
35
+ pixel_values = trocr_processor(image, return_tensors="pt").pixel_values
36
+ generated_ids = trocr_model.generate(pixel_values)
37
+ return trocr_processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
38
+ except Exception as e:
39
+ return f"خطا در TrOCR: {str(e)}"
40
 
41
  def compare_models(image):
42
  """Compare PaddleOCR and TrOCR models"""