Spaces:
Sleeping
Sleeping
File size: 2,084 Bytes
9453eac 5c3f634 8f46e75 4abc449 9453eac 4abc449 9453eac 8f46e75 4e751ce 8f46e75 4e751ce 8f46e75 9453eac 4abc449 dd4c7df 4abc449 4e751ce 768d260 4e751ce 768d260 4e751ce 8f46e75 3c028c0 8f46e75 2bf547d 8f46e75 9453eac 4e751ce 3c028c0 279ab91 4e751ce 8f46e75 2bf547d 4e751ce 3c028c0 9453eac 4abc449 |
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 |
import gradio as gr
import easyocr
from transformers import pipeline
from PIL import Image
import numpy as np
# Initialize EasyOCR for Persian
reader = easyocr.Reader(['fa'])
# Initialize text processing pipeline
try:
# استفاده از مدل محلی یا مدلهای عمومی
text_processor = pipeline("text-generation", model="gpt2") # مدل جایگزین
except Exception as e:
text_processor = None
def run_ocr(image):
"""استخراج متن از تصویر با EasyOCR"""
try:
if isinstance(image, Image.Image):
image = np.array(image)
results = reader.readtext(image)
return " ".join([result[1] for result in results]) if results else "متنی یافت نشد!"
except Exception as e:
return f"خطا در OCR: {str(e)}"
def process_text(text):
"""پردازش ساده متن"""
if text == "متنی یافت نشد!":
return text
# اگر پردازشگر متن وجود داشت از آن استفاده کن
if text_processor:
try:
return text_processor(text, max_length=50)[0]['generated_text']
except:
return text # اگر خطا رخ داد متن اصلی را برگردان
return text # اگر پردازشگر متن وجود نداشت
with gr.Blocks(title="سیستم OCR فارسی") as app:
gr.Markdown("## استخراج متن فارسی از تصاویر")
with gr.Row():
with gr.Column():
img_input = gr.Image(label="تصویر ورودی", type="pil")
btn = gr.Button("پردازش تصویر", variant="primary")
with gr.Column():
ocr_output = gr.Textbox(label="متن استخراج شده")
processed_output = gr.Textbox(label="متن پردازش شده", visible=False) # غیرفعال شده
btn.click(
fn=lambda x: (run_ocr(x), process_text(run_ocr(x))),
inputs=img_input,
outputs=[ocr_output, processed_output]
)
if __name__ == "__main__":
app.launch() |