File size: 2,462 Bytes
c83c9c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
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()