""" OCR فارسی با یک خط بارگذاری تصویر + نمایش نتایج در گریدیو """ import gradio as gr import cv2, io, os import numpy as np from PIL import Image, ImageDraw, ImageFont import tesserocr # حتماً در تصویر داکر نصب شده باشد from tesserocr import PyTessBaseAPI # ------------------------------- # تحریرک Tesseract ساده‌خط # ------------------------------- DEFAULT_TESS_PATH = '/usr/share/tesseract-ocr/4.00/tessdata/' # در بیشتر تصاویر داکر HuggingFace RESIZE_LONG_SIDE = 1024 # مقیاس برای کاهش هزینه پردازش def resize_longest(img: Image.Image, max_long: int = RESIZE_LONG_SIDE): w, h = img.size ratio = max_long / max(w, h) if ratio >= 1: return img new_w = int(w * ratio) new_h = int(h * ratio) return img.resize((new_w, new_h), Image.LANCZOS) def persian_ocr_one(file): if isinstance(file, tuple): img = Image.open(file[0]) else: img = Image.open(file) img = img.convert("RGB") img = resize_longest(img) pix = np.array(img) # تبدیل به طیف‌های تأثیرگذار برای متون فارسی gray = cv2.cvtColor(pix, cv2.COLOR_RGB2GRAY) den = cv2.fastNlMeansDenoising(gray, None, 10, 7, 21) image_bin = Image.fromarray(den) # راه‌اندازی Tesseract فقط یک‌بار در هر فراخوانی (به دلیل thread-safe بودن) # دقت کنید زبان را روی 'fas' تنظیم کرده باشید tess_args = ( 6, # PSM__SINGLE_BLOCK {"lang": "fas"} # فارسی ) api = PyTessBaseAPI(path=DEFAULT_TESS_PATH, lang='fas', psm=tesserocr.PSM.SINGLE_BLOCK) api.SetImage(image_bin) text = api.GetUTF8Text().strip() api.End() return text # ------------------------------- # رابط کاربری مینیمال # ------------------------------- demo = gr.Interface( fn=persian_ocr_one, inputs=gr.Image(type="filepath", label="تصویر"), outputs=gr.Textbox(label="متن استخراج‌شده (فارسی)"), title="OCR فارسی", description="تصویری که شامل متن فارسی است را بارگذاری کنید تا متن داخل آن استخراج شود." ) if __name__ == "__main__": demo.queue(max_size=20).launch()