Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import keras_ocr
|
4 |
+
import requests
|
5 |
+
import cv2
|
6 |
+
import os
|
7 |
+
import csv
|
8 |
+
import numpy as np
|
9 |
+
import pandas as pd
|
10 |
+
import huggingface_hub
|
11 |
+
from huggingface_hub import Repository
|
12 |
+
from datetime import datetime
|
13 |
+
import scipy.ndimage.interpolation as inter
|
14 |
+
import easyocr
|
15 |
+
import datasets
|
16 |
+
from datasets import load_dataset, Image
|
17 |
+
from PIL import Image
|
18 |
+
from paddleocr import PaddleOCR
|
19 |
+
import pytesseract
|
20 |
+
from pdf2image import convert_from_path
|
21 |
+
from PIL import Image
|
22 |
+
import os
|
23 |
+
import tempfile
|
24 |
+
from openai import OpenAI
|
25 |
+
|
26 |
+
# Function to perform OCR
|
27 |
+
def ocr(input_file, lang='fas'): # 'fas': Persian language (Farsi)
|
28 |
+
extracted_text = ""
|
29 |
+
# if isinstance(input_file, Image.Image): # If the input is an image
|
30 |
+
text = pytesseract.image_to_string(input_file, lang=lang)
|
31 |
+
extracted_text = text
|
32 |
+
|
33 |
+
return extracted_text
|
34 |
+
|
35 |
+
"""
|
36 |
+
Paddle OCR
|
37 |
+
"""
|
38 |
+
def ocr_with_paddle(img):
|
39 |
+
finaltext = ''
|
40 |
+
ocr = PaddleOCR(lang='fa', use_angle_cls=True)
|
41 |
+
# img_path = 'exp.jpeg'
|
42 |
+
result = ocr.ocr(img)
|
43 |
+
|
44 |
+
for i in range(len(result[0])):
|
45 |
+
text = result[0][i][1][0]
|
46 |
+
finaltext += ' '+ text
|
47 |
+
return finaltext
|
48 |
+
|
49 |
+
"""
|
50 |
+
Keras OCR
|
51 |
+
"""
|
52 |
+
def ocr_with_keras(img):
|
53 |
+
output_text = ''
|
54 |
+
pipeline=keras_ocr.pipeline.Pipeline()
|
55 |
+
images=[keras_ocr.tools.read(img)]
|
56 |
+
predictions=pipeline.recognize(images)
|
57 |
+
first=predictions[0]
|
58 |
+
for text,box in first:
|
59 |
+
output_text += ' '+ text
|
60 |
+
return output_text
|
61 |
+
|
62 |
+
"""
|
63 |
+
easy OCR
|
64 |
+
"""
|
65 |
+
# gray scale image
|
66 |
+
def get_grayscale(image):
|
67 |
+
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
68 |
+
|
69 |
+
# Thresholding or Binarization
|
70 |
+
def thresholding(src):
|
71 |
+
return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
|
72 |
+
def ocr_with_easy(img):
|
73 |
+
gray_scale_image=get_grayscale(img)
|
74 |
+
thresholding(gray_scale_image)
|
75 |
+
cv2.imwrite('image.png',gray_scale_image)
|
76 |
+
reader = easyocr.Reader(['fa','en'])
|
77 |
+
bounds = reader.readtext('image.png',paragraph="False",detail = 0)
|
78 |
+
bounds = ''.join(bounds)
|
79 |
+
return bounds
|
80 |
+
|
81 |
+
"""
|
82 |
+
Generate OCR
|
83 |
+
"""
|
84 |
+
|
85 |
+
def process(input_type, gr_img, lang):
|
86 |
+
|
87 |
+
if isinstance(gr_img, np.ndarray):
|
88 |
+
# Create a temporary file to save the image
|
89 |
+
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
|
90 |
+
temp_path = temp_file.name
|
91 |
+
# Save the NumPy array as an image file
|
92 |
+
Image.fromarray(gr_img).save(temp_path)
|
93 |
+
|
94 |
+
image = Image.open(temp_path)
|
95 |
+
# image = file
|
96 |
+
extracted_text = ocr(image, lang)
|
97 |
+
return extracted_text
|
98 |
+
|
99 |
+
|
100 |
+
def generate_ocr(Method, img):
|
101 |
+
|
102 |
+
text_output = ''
|
103 |
+
|
104 |
+
add_csv = []
|
105 |
+
image_id = 1
|
106 |
+
if Method == 'Version3':
|
107 |
+
text_output = process("img", img, "fas")
|
108 |
+
|
109 |
+
if not text_output:
|
110 |
+
return text_output
|
111 |
+
else:
|
112 |
+
client = OpenAI(
|
113 |
+
base_url="https://openrouter.ai/api/v1",
|
114 |
+
api_key="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", # Get from https://openrouter.ai
|
115 |
+
)
|
116 |
+
|
117 |
+
response = client.chat.completions.create(
|
118 |
+
model="deepseek/deepseek-r1:free",
|
119 |
+
messages=[{"role": "user", "content": f"""متن زیر پر از ایرادات تایپی و نگارشی است اصلاح کن و فقط متن اصلاح شده رو بهم بده
|
120 |
+
اطلاعات اظافه رو نیار و فقط متن اصلاح شده رو برگردون
|
121 |
+
{text_output}
|
122 |
+
"""}]
|
123 |
+
)
|
124 |
+
|
125 |
+
return response.choices[0].message.content
|
126 |
+
|
127 |
+
|
128 |
+
"""
|
129 |
+
Create user interface for OCR demo
|
130 |
+
"""
|
131 |
+
|
132 |
+
# image = gr.Image(shape=(300, 300))
|
133 |
+
# image = gr.File(label="Upload PDF/Image")
|
134 |
+
image = gr.Image()
|
135 |
+
method = "Version3"
|
136 |
+
output = gr.Textbox(label="Output")
|
137 |
+
|
138 |
+
demo = gr.Interface(
|
139 |
+
generate_ocr,
|
140 |
+
[method, image],
|
141 |
+
output,
|
142 |
+
title="Persian Optical Character Recognition",
|
143 |
+
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
|
144 |
+
article = """<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
|
145 |
+
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
|
146 |
+
<p style='text-align: center;'>Developed by: <a href="https://github.com/mohammad2928" target="_blank">Mohammad Mahmoudi</a></p>"""
|
147 |
+
|
148 |
+
|
149 |
+
)
|
150 |
+
# demo.launch(enable_queue = False)
|
151 |
+
demo.launch(share=True)
|