Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import tensorflow as tf | |
| import keras_ocr | |
| import requests | |
| import cv2 | |
| import os | |
| import csv | |
| import numpy as np | |
| import pandas as pd | |
| import huggingface_hub | |
| from huggingface_hub import Repository | |
| from datetime import datetime | |
| import scipy.ndimage.interpolation as inter | |
| import easyocr | |
| import datasets | |
| from datasets import load_dataset, Image | |
| from PIL import Image | |
| from paddleocr import PaddleOCR | |
| import pytesseract | |
| from pdf2image import convert_from_path | |
| from PIL import Image | |
| import os | |
| import tempfile | |
| from openai import OpenAI | |
| # Function to perform OCR | |
| def ocr(input_file, lang='fas'): # 'fas': Persian language (Farsi) | |
| extracted_text = "" | |
| # if isinstance(input_file, Image.Image): # If the input is an image | |
| text = pytesseract.image_to_string(input_file, lang=lang) | |
| extracted_text = text | |
| return extracted_text | |
| """ | |
| Paddle OCR | |
| """ | |
| def ocr_with_paddle(img): | |
| finaltext = '' | |
| ocr = PaddleOCR(lang='fa', use_angle_cls=True) | |
| # img_path = 'exp.jpeg' | |
| result = ocr.ocr(img) | |
| for i in range(len(result[0])): | |
| text = result[0][i][1][0] | |
| finaltext += ' '+ text | |
| return finaltext | |
| """ | |
| Keras OCR | |
| """ | |
| def ocr_with_keras(img): | |
| output_text = '' | |
| pipeline=keras_ocr.pipeline.Pipeline() | |
| images=[keras_ocr.tools.read(img)] | |
| predictions=pipeline.recognize(images) | |
| first=predictions[0] | |
| for text,box in first: | |
| output_text += ' '+ text | |
| return output_text | |
| """ | |
| easy OCR | |
| """ | |
| # gray scale image | |
| def get_grayscale(image): | |
| return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
| # Thresholding or Binarization | |
| def thresholding(src): | |
| return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1] | |
| def ocr_with_easy(img): | |
| gray_scale_image=get_grayscale(img) | |
| thresholding(gray_scale_image) | |
| cv2.imwrite('image.png',gray_scale_image) | |
| reader = easyocr.Reader(['fa','en']) | |
| bounds = reader.readtext('image.png',paragraph="False",detail = 0) | |
| bounds = ''.join(bounds) | |
| return bounds | |
| """ | |
| Generate OCR | |
| """ | |
| def process(input_type, gr_img, lang): | |
| if isinstance(gr_img, np.ndarray): | |
| # Create a temporary file to save the image | |
| with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file: | |
| temp_path = temp_file.name | |
| # Save the NumPy array as an image file | |
| Image.fromarray(gr_img).save(temp_path) | |
| image = Image.open(temp_path) | |
| # image = file | |
| extracted_text = ocr(image, lang) | |
| return extracted_text | |
| def generate_ocr(img): | |
| text_output = '' | |
| add_csv = [] | |
| image_id = 1 | |
| text_output = process("img", img, "fas") | |
| if not text_output: | |
| return text_output | |
| else: | |
| client = OpenAI( | |
| base_url="https://openrouter.ai/api/v1", | |
| api_key= os.environ.get("OPENR_KEY"), # Get from https://openrouter.ai | |
| ) | |
| response = client.chat.completions.create( | |
| model="deepseek/deepseek-r1:free", | |
| messages=[{"role": "user", "content": f"""متن زیر پر از ایرادات تایپی و نگارشی است اصلاح کن و فقط متن اصلاح شده رو بهم بده | |
| اطلاعات اظافه رو نیار و فقط متن اصلاح شده رو برگردون | |
| {text_output} | |
| """}] | |
| ) | |
| return response.choices[0].message.content | |
| """ | |
| Create user interface for OCR demo | |
| """ | |
| # image = gr.Image(shape=(300, 300)) | |
| # image = gr.File(label="Upload PDF/Image") | |
| image = gr.Image() | |
| output = gr.Textbox(label="Output") | |
| demo = gr.Interface( | |
| generate_ocr, | |
| [image], | |
| output, | |
| title="Persian Optical Character Recognition", | |
| css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}", | |
| article = """<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at | |
| <a href="mailto:zaribar2928@gmail.com" target="_blank">zaribar2928@gmail.com</a> | |
| <p style='text-align: center;'>Developed by: <a href="https://github.com/mohammad2928" target="_blank">Mohammad Mahmoudi</a></p>""" | |
| ) | |
| # demo.launch(enable_queue = False) | |
| demo.launch(share=True) |