Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ 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):
|
|
@@ -17,22 +18,25 @@ def ocr_with_easy(img):
|
|
| 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 = [
|
|
@@ -114,12 +118,13 @@ Please upload an image and select the correct order of steps to perform OCR.
|
|
| 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,
|
| 125 |
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}"
|
|
@@ -133,4 +138,3 @@ quiz_app = gr.TabbedInterface(
|
|
| 133 |
|
| 134 |
quiz_app.launch()
|
| 135 |
|
| 136 |
-
|
|
|
|
| 2 |
import cv2
|
| 3 |
import easyocr
|
| 4 |
from PIL import Image
|
| 5 |
+
import numpy as np
|
| 6 |
|
| 7 |
# Functions for OCR steps
|
| 8 |
def get_grayscale(image):
|
|
|
|
| 18 |
return bounds
|
| 19 |
|
| 20 |
def process_image(img, steps):
|
| 21 |
+
gray_image = None
|
| 22 |
for step in steps:
|
| 23 |
if step == "Grayscale Conversion":
|
| 24 |
img = get_grayscale(img)
|
| 25 |
+
gray_image = img
|
| 26 |
elif step == "Thresholding":
|
| 27 |
img = thresholding(img)
|
| 28 |
cv2.imwrite('processed_image.png', img)
|
| 29 |
+
return 'processed_image.png', gray_image
|
| 30 |
|
| 31 |
def generate_ocr(img, steps):
|
| 32 |
text_output = ''
|
| 33 |
+
gray_image = None
|
| 34 |
if img is not None and (img).any():
|
| 35 |
+
processed_image_path, gray_image = process_image(img, steps)
|
| 36 |
text_output = ocr_with_easy(processed_image_path)
|
| 37 |
else:
|
| 38 |
raise gr.Error("Please upload an image and select the processing steps!")
|
| 39 |
+
return text_output, gray_image
|
| 40 |
|
| 41 |
# Interactive tutorial steps
|
| 42 |
tutorial_steps = [
|
|
|
|
| 118 |
image = gr.Image()
|
| 119 |
steps = gr.CheckboxGroup(choices=tutorial_steps, label="Select and order the steps for OCR")
|
| 120 |
output = gr.Textbox(label="OCR Output")
|
| 121 |
+
gray_image_output = gr.Image(label="Grayscale Image", type="numpy")
|
| 122 |
explanation = gr.Markdown(explanation_text)
|
| 123 |
|
| 124 |
ocr_app = gr.Interface(
|
| 125 |
fn=generate_ocr,
|
| 126 |
inputs=[image, steps],
|
| 127 |
+
outputs=[output, gray_image_output],
|
| 128 |
title="Optical Character Recognition",
|
| 129 |
description=explanation_text,
|
| 130 |
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}"
|
|
|
|
| 138 |
|
| 139 |
quiz_app.launch()
|
| 140 |
|
|
|