import gradio as gr import time import numpy as np from PIL import Image from paddleocr import PaddleOCR from transformers import TrOCRProcessor, VisionEncoderDecoderModel import easyocr # Initialize models paddle_ocr = PaddleOCR(lang='en') # PaddleOCR برای انگلیسی easy_ocr = easyocr.Reader(['en']) # EasyOCR برای انگلیسی trocr_processor = TrOCRProcessor.from_pretrained("microsoft/trocr-base-printed") trocr_model = VisionEncoderDecoderModel.from_pretrained("microsoft/trocr-base-printed") def run_paddleocr(image): """Run PaddleOCR on image""" if isinstance(image, Image.Image): image = np.array(image) try: result = paddle_ocr.ocr(image) return ' '.join([line[1][0] for line in result[0]]) if result else '' except Exception as e: return f"PaddleOCR Error: {str(e)}" def run_easyocr(image): """Run EasyOCR on image""" if isinstance(image, Image.Image): image = np.array(image) try: result = easy_ocr.readtext(image, detail=0) return ' '.join(result) if result else '' except Exception as e: return f"EasyOCR Error: {str(e)}" def run_trocr(image): """Run TrOCR on image""" if isinstance(image, np.ndarray): image = Image.fromarray(image) try: pixel_values = trocr_processor(image, return_tensors="pt").pixel_values generated_ids = trocr_model.generate(pixel_values) return trocr_processor.batch_decode(generated_ids, skip_special_tokens=True)[0] except Exception as e: return f"TrOCR Error: {str(e)}" def compare_models(image): """Compare all three OCR models""" if isinstance(image, np.ndarray): image = Image.fromarray(image) image = image.convert("RGB") results = {} times = {} # Run PaddleOCR start = time.time() results['PaddleOCR'] = run_paddleocr(image) times['PaddleOCR'] = time.time() - start # Run EasyOCR start = time.time() results['EasyOCR'] = run_easyocr(image) times['EasyOCR'] = time.time() - start # Run TrOCR start = time.time() results['TrOCR'] = run_trocr(image) times['TrOCR'] = time.time() - start # Create comparison table comparison = f"""
Model | Extracted Text | Processing Time (s) |
---|---|---|
PaddleOCR | {results['PaddleOCR']} | {times['PaddleOCR']:.3f} |
EasyOCR | {results['EasyOCR']} | {times['EasyOCR']:.3f} |
TrOCR | {results['TrOCR']} | {times['TrOCR']:.3f} |