| import pytesseract | |
| from PIL import Image | |
| import os | |
| class OCRReader: | |
| def __init__(self): | |
| # Tesseract نیازی به مدل سنگین ندارد، فقط باینری سیستم عامل را میخواند | |
| print("✅ موتور Tesseract OCR آماده است (سبک و سریع).") | |
| def extract_text(self, image_path: str) -> str: | |
| if not os.path.exists(image_path): | |
| raise FileNotFoundError(f"Image not found: {image_path}") | |
| # باز کردن عکس | |
| image = Image.open(image_path) | |
| # استخراج متن با استفاده از Tesseract | |
| # lang='eng' یعنی انگلیسی و اعداد | |
| try: | |
| text = pytesseract.image_to_string(image, lang='eng') | |
| return text | |
| except Exception as e: | |
| raise Exception(f"خطا در اجرای Tesseract: {str(e)} (ممکن است tesseract نصب نباشد)") |