Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,28 +7,28 @@ from PIL import Image
|
|
7 |
def get_grayscale(image):
|
8 |
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
9 |
|
10 |
-
def thresholding(src):
|
11 |
-
return cv2.threshold(src,
|
12 |
|
13 |
-
def ocr_with_easy(
|
14 |
reader = easyocr.Reader(['en'])
|
15 |
-
bounds = reader.readtext(
|
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
|
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!")
|
@@ -105,17 +105,18 @@ Optical Character Recognition (OCR) is a technology used to convert different ty
|
|
105 |
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.
|
106 |
3. **OCR using EasyOCR:** We use the EasyOCR library to recognize and extract text from the preprocessed image.
|
107 |
**Interactive Tutorial:**
|
108 |
-
Please upload an image and select the correct order of steps to perform OCR.
|
109 |
"""
|
110 |
|
111 |
image = gr.Image()
|
112 |
steps = gr.CheckboxGroup(choices=tutorial_steps, label="Select and order the steps for OCR")
|
|
|
113 |
output = gr.Textbox(label="OCR Output")
|
114 |
explanation = gr.Markdown(explanation_text)
|
115 |
|
116 |
ocr_app = gr.Interface(
|
117 |
fn=generate_ocr,
|
118 |
-
inputs=[image, steps],
|
119 |
outputs=output,
|
120 |
title="Optical Character Recognition",
|
121 |
description=explanation_text,
|
@@ -129,3 +130,4 @@ quiz_app = gr.TabbedInterface(
|
|
129 |
)
|
130 |
|
131 |
quiz_app.launch()
|
|
|
|
7 |
def get_grayscale(image):
|
8 |
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
9 |
|
10 |
+
def thresholding(src, threshold_value):
|
11 |
+
return cv2.threshold(src, threshold_value, 255, cv2.THRESH_BINARY)[1]
|
12 |
|
13 |
+
def ocr_with_easy(img_path):
|
14 |
reader = easyocr.Reader(['en'])
|
15 |
+
bounds = reader.readtext(img_path, paragraph="False", detail=0)
|
16 |
bounds = ''.join(bounds)
|
17 |
return bounds
|
18 |
|
19 |
+
def process_image(img, steps, threshold_value):
|
20 |
for step in steps:
|
21 |
if step == "Grayscale Conversion":
|
22 |
img = get_grayscale(img)
|
23 |
elif step == "Thresholding":
|
24 |
+
img = thresholding(img, threshold_value)
|
25 |
cv2.imwrite('processed_image.png', img)
|
26 |
return 'processed_image.png'
|
27 |
|
28 |
+
def generate_ocr(img, steps, threshold_value):
|
29 |
text_output = ''
|
30 |
+
if img is not None and img.any():
|
31 |
+
processed_image_path = process_image(img, steps, threshold_value)
|
32 |
text_output = ocr_with_easy(processed_image_path)
|
33 |
else:
|
34 |
raise gr.Error("Please upload an image and select the processing steps!")
|
|
|
105 |
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.
|
106 |
3. **OCR using EasyOCR:** We use the EasyOCR library to recognize and extract text from the preprocessed image.
|
107 |
**Interactive Tutorial:**
|
108 |
+
Please upload an image and select the correct order of steps to perform OCR. You can also adjust the threshold value using the slider.
|
109 |
"""
|
110 |
|
111 |
image = gr.Image()
|
112 |
steps = gr.CheckboxGroup(choices=tutorial_steps, label="Select and order the steps for OCR")
|
113 |
+
threshold = gr.Slider(0, 255, value=127, step=1, label="Threshold Value")
|
114 |
output = gr.Textbox(label="OCR Output")
|
115 |
explanation = gr.Markdown(explanation_text)
|
116 |
|
117 |
ocr_app = gr.Interface(
|
118 |
fn=generate_ocr,
|
119 |
+
inputs=[image, steps, threshold],
|
120 |
outputs=output,
|
121 |
title="Optical Character Recognition",
|
122 |
description=explanation_text,
|
|
|
130 |
)
|
131 |
|
132 |
quiz_app.launch()
|
133 |
+
|