Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,176 +1,177 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import cv2
|
3 |
-
import numpy as np
|
4 |
-
import pytesseract
|
5 |
-
import re
|
6 |
-
import google.generativeai as genai
|
7 |
-
from rapidfuzz.distance import Levenshtein
|
8 |
-
import os
|
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 |
-
text = re.sub(r'\
|
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 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
Denoised Image Accuracy: {
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
- **
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
|
|
176 |
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import pytesseract
|
5 |
+
import re
|
6 |
+
import google.generativeai as genai
|
7 |
+
from rapidfuzz.distance import Levenshtein
|
8 |
+
import os
|
9 |
+
|
10 |
+
os.system('apt-get update && apt-get install -y tesseract-ocr')
|
11 |
+
# Configure Generative AI
|
12 |
+
OPENAI_API_KEY = os.getenv("API_KEY")
|
13 |
+
genai.configure(api_key=OPENAI_API_KEY)
|
14 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
15 |
+
|
16 |
+
# Image processing functions
|
17 |
+
def threshold_image(img, threshold_value=None):
|
18 |
+
if threshold_value is None: # Adaptive thresholding
|
19 |
+
thresholded_image = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
|
20 |
+
cv2.THRESH_BINARY, 11, 2)
|
21 |
+
else: # Manual thresholding
|
22 |
+
_, thresholded_image = cv2.threshold(img, threshold_value, 255, cv2.THRESH_BINARY)
|
23 |
+
return thresholded_image
|
24 |
+
|
25 |
+
def bm3d_denoising(img, sigma_psd=55):
|
26 |
+
return cv2.fastNlMeansDenoising(img, None, sigma_psd)
|
27 |
+
|
28 |
+
def remove_noise(img, kernel_size=3):
|
29 |
+
kernel = np.ones((kernel_size, kernel_size), np.float32) / (kernel_size**2)
|
30 |
+
denoised = cv2.filter2D(img, -1, kernel)
|
31 |
+
return cv2.medianBlur(denoised, 3)
|
32 |
+
|
33 |
+
def sharpen_image(img):
|
34 |
+
kernel = np.array([[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]])
|
35 |
+
return cv2.filter2D(img, -1, kernel)
|
36 |
+
|
37 |
+
def remove_extra_spaces_and_lines(text):
|
38 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
39 |
+
text = re.sub(r'\n\s*\n', '\n\n', text)
|
40 |
+
return text
|
41 |
+
|
42 |
+
def calculate_accuracy(text1, text2):
|
43 |
+
# matcher = difflib.SequenceMatcher(None, generated_text, transcribed_text)
|
44 |
+
# return matcher.ratio()
|
45 |
+
distance = Levenshtein.distance(text1, text2)
|
46 |
+
max_length = max(len(text1), len(text2))
|
47 |
+
accuracy = (1 - (distance / max_length))
|
48 |
+
return accuracy
|
49 |
+
|
50 |
+
# Gradio app
|
51 |
+
def process_image(image, threshold_value=None, correct_transcription=None):
|
52 |
+
img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
53 |
+
|
54 |
+
# Process the image
|
55 |
+
thresholded = threshold_image(img, threshold_value)
|
56 |
+
bm3d_denoised_image = bm3d_denoising(thresholded)
|
57 |
+
denoised = remove_noise(thresholded)
|
58 |
+
sharpened_image = sharpen_image(bm3d_denoised_image)
|
59 |
+
|
60 |
+
# OCR
|
61 |
+
original_text = pytesseract.image_to_string(img)
|
62 |
+
thresholded_text = pytesseract.image_to_string(thresholded)
|
63 |
+
bm3d_denoised_text = pytesseract.image_to_string(bm3d_denoised_image)
|
64 |
+
denoised_text = pytesseract.image_to_string(denoised)
|
65 |
+
sharpened_text = pytesseract.image_to_string(sharpened_image)
|
66 |
+
|
67 |
+
# Clean up text
|
68 |
+
original_text = remove_extra_spaces_and_lines(original_text)
|
69 |
+
thresholded_text = remove_extra_spaces_and_lines(thresholded_text)
|
70 |
+
bm3d_denoised_text = remove_extra_spaces_and_lines(bm3d_denoised_text)
|
71 |
+
denoised_text = remove_extra_spaces_and_lines(denoised_text)
|
72 |
+
sharpened_text = remove_extra_spaces_and_lines(sharpened_text)
|
73 |
+
|
74 |
+
# Generative AI model response
|
75 |
+
user_prompt = user_prompt = f"""
|
76 |
+
below are the output texts of OCR on multiple image processing techniques of a faded letter written in English, can you predict the original text, provide only the original text.
|
77 |
+
Pre-Processing Image Text:
|
78 |
+
{original_text}
|
79 |
+
Sharpened Image Text:
|
80 |
+
{sharpened_text}
|
81 |
+
Thresholded Image Text:
|
82 |
+
{thresholded_text}
|
83 |
+
BM3D Denoised Image Text:
|
84 |
+
{bm3d_denoised_text}
|
85 |
+
Denoised Image Text:
|
86 |
+
{denoised_text}
|
87 |
+
"""
|
88 |
+
response = model.generate_content(user_prompt)
|
89 |
+
model_text = response.text
|
90 |
+
|
91 |
+
if not correct_transcription:
|
92 |
+
correct_transcription = model_text
|
93 |
+
# Accuracy metrics
|
94 |
+
if correct_transcription:
|
95 |
+
original_accuracy = calculate_accuracy(original_text, correct_transcription)
|
96 |
+
thresholded_accuracy = calculate_accuracy(thresholded_text, correct_transcription)
|
97 |
+
bm3d_denoised_accuracy = calculate_accuracy(bm3d_denoised_text, correct_transcription)
|
98 |
+
denoised_accuracy = calculate_accuracy(denoised_text, correct_transcription)
|
99 |
+
sharpened_accuracy = calculate_accuracy(sharpened_text, correct_transcription)
|
100 |
+
model_accuracy = calculate_accuracy(model_text, correct_transcription)
|
101 |
+
accuracy_metrics = f"""
|
102 |
+
Original Image Accuracy: {original_accuracy:.2%}
|
103 |
+
Thresholded Image Accuracy: {thresholded_accuracy:.2%}
|
104 |
+
BM3D Denoised Image Accuracy: {bm3d_denoised_accuracy:.2%}
|
105 |
+
Denoised Image Accuracy: {denoised_accuracy:.2%}
|
106 |
+
Sharpened Image Accuracy: {sharpened_accuracy:.2%}
|
107 |
+
Model Response Accuracy: {model_accuracy:.2%}
|
108 |
+
"""
|
109 |
+
else:
|
110 |
+
accuracy_metrics = "No correct transcription provided."
|
111 |
+
|
112 |
+
# Return results
|
113 |
+
return (
|
114 |
+
image, thresholded, bm3d_denoised_image, denoised, sharpened_image,
|
115 |
+
original_text, thresholded_text, bm3d_denoised_text, denoised_text, sharpened_text,
|
116 |
+
model_text, accuracy_metrics
|
117 |
+
)
|
118 |
+
|
119 |
+
# Interface
|
120 |
+
with gr.Blocks() as demo:
|
121 |
+
with gr.Row():
|
122 |
+
gr.Markdown("## Image Preprocessing and OCR App")
|
123 |
+
with gr.Row():
|
124 |
+
gr.Markdown("""
|
125 |
+
### Legend
|
126 |
+
- **Model Response**: Text generated by the Generative AI model.
|
127 |
+
- **Accuracy Metrics**: Comparison of OCR results with the provided correct transcription if provided, otherwise with the model response.
|
128 |
+
""")
|
129 |
+
with gr.Row():
|
130 |
+
with gr.Column():
|
131 |
+
image_input = gr.Image(label="Upload Image", type="numpy")
|
132 |
+
threshold_slider = gr.Slider(label="Threshold Value", minimum=0, maximum=255, step=1, value=242)
|
133 |
+
adaptive_checkbox = gr.Checkbox(label="Use Adaptive Thresholding", value=False)
|
134 |
+
transcription_input = gr.Textbox(label="Correct Transcription (Optional)")
|
135 |
+
process_button = gr.Button("Process Image")
|
136 |
+
|
137 |
+
with gr.Column():
|
138 |
+
tabs = gr.Tabs()
|
139 |
+
with tabs:
|
140 |
+
with gr.TabItem("Original"):
|
141 |
+
original_image_display = gr.Image(label="Original Image")
|
142 |
+
original_text_display = gr.Textbox(label="Original Image Text")
|
143 |
+
with gr.TabItem("Thresholded"):
|
144 |
+
thresholded_image_display = gr.Image(label="Thresholded Image")
|
145 |
+
thresholded_text_display = gr.Textbox(label="Thresholded Image Text", lines=1)
|
146 |
+
with gr.TabItem("BM3D Denoised"):
|
147 |
+
bm3d_denoised_image_display = gr.Image(label="BM3D Denoised Image")
|
148 |
+
bm3d_denoised_text_display = gr.Textbox(label="BM3D Denoised Image Text")
|
149 |
+
with gr.TabItem("Denoised"):
|
150 |
+
denoised_image_display = gr.Image(label="Denoised Image")
|
151 |
+
denoised_text_display = gr.Textbox(label="Denoised Image Text")
|
152 |
+
with gr.TabItem("Sharpened"):
|
153 |
+
sharpened_image_display = gr.Image(label="Sharpened Image")
|
154 |
+
sharpened_text_display = gr.Textbox(label="Sharpened Image Text")
|
155 |
+
accuracy_output = gr.Textbox(label="Accuracy Metrics")
|
156 |
+
model_text_display = gr.Textbox(label="Model Response Text")
|
157 |
+
|
158 |
+
# Link button to processing function
|
159 |
+
def update_process(image, threshold_value, use_adaptive, correct_transcription):
|
160 |
+
threshold_value = None if use_adaptive else threshold_value
|
161 |
+
return process_image(image, threshold_value, correct_transcription)
|
162 |
+
|
163 |
+
process_button.click(
|
164 |
+
update_process,
|
165 |
+
inputs=[image_input, threshold_slider, adaptive_checkbox, transcription_input],
|
166 |
+
outputs=[
|
167 |
+
original_image_display, thresholded_image_display,
|
168 |
+
bm3d_denoised_image_display, denoised_image_display,
|
169 |
+
sharpened_image_display, original_text_display,
|
170 |
+
thresholded_text_display, bm3d_denoised_text_display,
|
171 |
+
denoised_text_display, sharpened_text_display,
|
172 |
+
model_text_display, accuracy_output
|
173 |
+
],
|
174 |
+
)
|
175 |
+
|
176 |
+
# Launch app
|
177 |
demo.launch()
|