Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,71 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
from tensorflow.keras.datasets import mnist
|
| 6 |
-
|
| 7 |
-
# Functions for MNIST processing steps
|
| 8 |
-
def load_mnist():
|
| 9 |
-
(x_train, y_train), (x_test, y_test) = mnist.load_data()
|
| 10 |
-
return x_test, y_test
|
| 11 |
|
|
|
|
| 12 |
def get_grayscale(image):
|
| 13 |
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 14 |
|
| 15 |
def thresholding(src):
|
| 16 |
-
return cv2.threshold(src, 127, 255, cv2.
|
| 17 |
-
|
| 18 |
-
def gaussian_blur(image):
|
| 19 |
-
return cv2.GaussianBlur(image, (5, 5), 0)
|
| 20 |
-
|
| 21 |
-
def edge_detection(image):
|
| 22 |
-
return cv2.Canny(image, 100, 200)
|
| 23 |
|
| 24 |
-
def
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
| 27 |
|
|
|
|
| 28 |
for step in steps:
|
| 29 |
if step == "Grayscale Conversion":
|
| 30 |
img = get_grayscale(img)
|
| 31 |
elif step == "Thresholding":
|
| 32 |
img = thresholding(img)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
fig, axes = plt.subplots(1, len(step_images), figsize=(15, 5))
|
| 46 |
-
for ax, (step, img) in zip(axes, step_images.items()):
|
| 47 |
-
ax.imshow(img, cmap='gray')
|
| 48 |
-
ax.set_title(step)
|
| 49 |
-
ax.axis('off')
|
| 50 |
-
|
| 51 |
-
plt.tight_layout()
|
| 52 |
-
plt.savefig('mnist_processing_steps.png')
|
| 53 |
-
return 'mnist_processing_steps.png'
|
| 54 |
|
| 55 |
# Interactive tutorial steps
|
| 56 |
tutorial_steps = [
|
| 57 |
"Grayscale Conversion",
|
| 58 |
-
"Thresholding"
|
| 59 |
-
"Gaussian Blur",
|
| 60 |
-
"Edge Detection"
|
| 61 |
]
|
| 62 |
|
| 63 |
# Interactive questions
|
| 64 |
questions = [
|
| 65 |
{
|
| 66 |
-
"question": "What is the first step in
|
| 67 |
-
"options": ["
|
| 68 |
"answer": "Grayscale Conversion"
|
| 69 |
},
|
| 70 |
{
|
| 71 |
-
"question": "What
|
| 72 |
-
"options": ["
|
| 73 |
-
"answer": "
|
| 74 |
},
|
| 75 |
{
|
| 76 |
-
"question": "
|
| 77 |
-
"options": ["
|
| 78 |
-
"answer": "
|
| 79 |
},
|
| 80 |
{
|
| 81 |
-
"question": "What
|
| 82 |
-
"options": ["
|
| 83 |
-
"answer": "
|
| 84 |
},
|
| 85 |
{
|
| 86 |
-
"question": "What
|
| 87 |
-
"options": ["
|
| 88 |
-
"answer": "
|
| 89 |
}
|
| 90 |
]
|
| 91 |
|
|
@@ -118,36 +98,34 @@ def quiz_interface():
|
|
| 118 |
|
| 119 |
# Explanation text
|
| 120 |
explanation_text = """
|
| 121 |
-
**Welcome to the
|
| 122 |
-
|
| 123 |
-
**Steps in the
|
| 124 |
-
1. **Grayscale Conversion:**
|
| 125 |
-
2. **Thresholding:**
|
| 126 |
-
3. **
|
| 127 |
-
4. **Edge Detection:** Detects the edges of the digits to enhance the features for further processing or recognition tasks.
|
| 128 |
**Interactive Tutorial:**
|
| 129 |
-
Please upload an
|
| 130 |
"""
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
output = gr.Image(type='file', label="Processing Steps Visualization")
|
| 136 |
explanation = gr.Markdown(explanation_text)
|
| 137 |
|
| 138 |
-
|
| 139 |
-
fn=
|
| 140 |
inputs=[image, steps],
|
| 141 |
outputs=output,
|
| 142 |
-
title="
|
| 143 |
description=explanation_text,
|
| 144 |
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}"
|
| 145 |
)
|
| 146 |
|
| 147 |
quiz_app = gr.TabbedInterface(
|
| 148 |
-
[
|
| 149 |
-
["
|
| 150 |
-
title="
|
| 151 |
)
|
| 152 |
|
| 153 |
quiz_app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
+
import easyocr
|
| 4 |
+
from PIL import Image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Functions for OCR steps
|
| 7 |
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 |
+
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?",
|
| 52 |
+
"options": ["To detect edges", "To convert image to grayscale", "To binarize the image"],
|
| 53 |
+
"answer": "To binarize the image"
|
| 54 |
},
|
| 55 |
{
|
| 56 |
+
"question": "Which library is used for OCR in this app?",
|
| 57 |
+
"options": ["Tesseract", "EasyOCR", "OpenCV"],
|
| 58 |
+
"answer": "EasyOCR"
|
| 59 |
},
|
| 60 |
{
|
| 61 |
+
"question": "What format is the image saved in after preprocessing?",
|
| 62 |
+
"options": ["JPG", "PNG", "TIFF"],
|
| 63 |
+
"answer": "PNG"
|
| 64 |
},
|
| 65 |
{
|
| 66 |
+
"question": "What does OCR stand for?",
|
| 67 |
+
"options": ["Optical Character Recognition", "Optical Character Reading", "Optical Code Recognition"],
|
| 68 |
+
"answer": "Optical Character Recognition"
|
| 69 |
}
|
| 70 |
]
|
| 71 |
|
|
|
|
| 98 |
|
| 99 |
# Explanation text
|
| 100 |
explanation_text = """
|
| 101 |
+
**Welcome to the OCR Tutorial!**
|
| 102 |
+
Optical Character Recognition (OCR) is a technology used to convert different types of documents, such as scanned paper documents, PDF files, or images captured by a digital camera, into editable and searchable data.
|
| 103 |
+
**Steps in the OCR Process:**
|
| 104 |
+
1. **Grayscale Conversion:** The first step in OCR is converting the image to grayscale. This simplifies the image and reduces the amount of data the OCR algorithm needs to process.
|
| 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,
|
| 122 |
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}"
|
| 123 |
)
|
| 124 |
|
| 125 |
quiz_app = gr.TabbedInterface(
|
| 126 |
+
[ocr_app] + quiz_interface(),
|
| 127 |
+
["OCR Tool"] + [f"Question {i+1}" for i in range(len(questions))],
|
| 128 |
+
title="OCR Tutorial and Quiz"
|
| 129 |
)
|
| 130 |
|
| 131 |
quiz_app.launch()
|