Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,32 +8,44 @@ def get_grayscale(image):
|
|
8 |
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
9 |
|
10 |
def thresholding(src):
|
11 |
-
return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
|
12 |
|
13 |
def ocr_with_easy(img):
|
14 |
-
gray_scale_image = get_grayscale(img)
|
15 |
-
thresholded_image = thresholding(gray_scale_image)
|
16 |
-
cv2.imwrite('image.png', thresholded_image)
|
17 |
reader = easyocr.Reader(['en'])
|
18 |
-
bounds = reader.readtext(
|
19 |
bounds = ''.join(bounds)
|
20 |
return bounds
|
21 |
|
22 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
text_output = ''
|
24 |
-
if (img).any():
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
return text_output
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
# Interactive questions
|
32 |
questions = [
|
33 |
{
|
34 |
"question": "What is the first step in OCR?",
|
35 |
-
"options": ["Binarization", "Grayscale
|
36 |
-
"answer": "Grayscale
|
37 |
},
|
38 |
{
|
39 |
"question": "What is the purpose of thresholding in OCR?",
|
@@ -95,16 +107,18 @@ Optical Character Recognition (OCR) is a technology used to convert different ty
|
|
95 |
2. **Thresholding:** This step converts the grayscale image into a binary image, where the text is in black, and the background is in white. This makes it easier for the OCR algorithm to distinguish text from the background.
|
96 |
3. **OCR using EasyOCR:** We use the EasyOCR library to recognize and extract text from the preprocessed image.
|
97 |
|
98 |
-
|
|
|
99 |
"""
|
100 |
|
101 |
image = gr.Image()
|
102 |
-
|
|
|
103 |
explanation = gr.Markdown(explanation_text)
|
104 |
|
105 |
ocr_app = gr.Interface(
|
106 |
fn=generate_ocr,
|
107 |
-
inputs=image,
|
108 |
outputs=output,
|
109 |
title="Optical Character Recognition",
|
110 |
description=explanation_text,
|
@@ -119,3 +133,4 @@ quiz_app = gr.TabbedInterface(
|
|
119 |
|
120 |
quiz_app.launch()
|
121 |
|
|
|
|
8 |
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
9 |
|
10 |
def thresholding(src):
|
11 |
+
return cv2.threshold(src, 127, 255, cv2.THRESH_TOZERO)[1]
|
12 |
|
13 |
def ocr_with_easy(img):
|
|
|
|
|
|
|
14 |
reader = easyocr.Reader(['en'])
|
15 |
+
bounds = reader.readtext(img, paragraph="False", detail=0)
|
16 |
bounds = ''.join(bounds)
|
17 |
return bounds
|
18 |
|
19 |
+
def process_image(img, steps):
|
20 |
+
for step in steps:
|
21 |
+
if step == "Grayscale Conversion":
|
22 |
+
img = get_grayscale(img)
|
23 |
+
elif step == "Thresholding":
|
24 |
+
img = thresholding(img)
|
25 |
+
cv2.imwrite('processed_image.png', img)
|
26 |
+
return 'processed_image.png'
|
27 |
+
|
28 |
+
def generate_ocr(img, steps):
|
29 |
text_output = ''
|
30 |
+
if img is not None and (img).any():
|
31 |
+
processed_image_path = process_image(img, steps)
|
32 |
+
text_output = ocr_with_easy(processed_image_path)
|
33 |
+
else:
|
34 |
+
raise gr.Error("Please upload an image and select the processing steps!")
|
35 |
return text_output
|
36 |
|
37 |
+
# Interactive tutorial steps
|
38 |
+
tutorial_steps = [
|
39 |
+
"Grayscale Conversion",
|
40 |
+
"Thresholding"
|
41 |
+
]
|
42 |
+
|
43 |
# Interactive questions
|
44 |
questions = [
|
45 |
{
|
46 |
"question": "What is the first step in OCR?",
|
47 |
+
"options": ["Binarization", "Grayscale Conversion", "Edge Detection"],
|
48 |
+
"answer": "Grayscale Conversion"
|
49 |
},
|
50 |
{
|
51 |
"question": "What is the purpose of thresholding in OCR?",
|
|
|
107 |
2. **Thresholding:** This step converts the grayscale image into a binary image, where the text is in black, and the background is in white. This makes it easier for the OCR algorithm to distinguish text from the background.
|
108 |
3. **OCR using EasyOCR:** We use the EasyOCR library to recognize and extract text from the preprocessed image.
|
109 |
|
110 |
+
**Interactive Tutorial:**
|
111 |
+
Please upload an image and select the correct order of steps to perform OCR.
|
112 |
"""
|
113 |
|
114 |
image = gr.Image()
|
115 |
+
steps = gr.CheckboxGroup(choices=tutorial_steps, label="Select and order the steps for OCR")
|
116 |
+
output = gr.Textbox(label="OCR Output")
|
117 |
explanation = gr.Markdown(explanation_text)
|
118 |
|
119 |
ocr_app = gr.Interface(
|
120 |
fn=generate_ocr,
|
121 |
+
inputs=[image, steps],
|
122 |
outputs=output,
|
123 |
title="Optical Character Recognition",
|
124 |
description=explanation_text,
|
|
|
133 |
|
134 |
quiz_app.launch()
|
135 |
|
136 |
+
|