Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
|
7 |
|
8 |
-
# Initialize
|
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 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
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 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
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"""
|