Spaces:
Sleeping
Sleeping
File size: 4,984 Bytes
d47a89d ed9a3a1 d47a89d ed9a3a1 d47a89d 93dd2dc faf3b9f 93dd2dc ed9a3a1 93dd2dc ed9a3a1 d47a89d 93dd2dc 76a09f4 93dd2dc ed9a3a1 93dd2dc ed9a3a1 93dd2dc ed9a3a1 d47a89d 76a09f4 ed9a3a1 76a09f4 bea12a5 ed9a3a1 76a09f4 bea12a5 ed9a3a1 bea12a5 ed9a3a1 bea12a5 ed9a3a1 bea12a5 ed9a3a1 bea12a5 ff5f533 ed9a3a1 76a09f4 93dd2dc ff5f533 596ff69 ed9a3a1 93dd2dc ed9a3a1 ff5f533 d47a89d ed9a3a1 93dd2dc 6b27525 ed9a3a1 ff5f533 c02f435 d47a89d bea12a5 ed9a3a1 bea12a5 93dd2dc 596ff69 |
1 2 3 4 5 6 7 8 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 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 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 |
import gradio as gr
import cv2
import easyocr
from PIL import Image
# Functions for OCR steps
def get_grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
def thresholding(src, threshold_value):
return cv2.threshold(src, threshold_value, 255, cv2.THRESH_BINARY)[1]
def ocr_with_easy(img_path):
reader = easyocr.Reader(['en'])
bounds = reader.readtext(img_path, paragraph="False", detail=0)
bounds = ''.join(bounds)
return bounds
def process_image(img, steps, threshold_value):
for step in steps:
if step == "Grayscale Conversion":
img = get_grayscale(img)
elif step == "Thresholding":
img = thresholding(img, threshold_value)
cv2.imwrite('processed_image.png', img)
return 'processed_image.png'
def generate_ocr(img, steps, threshold_value):
text_output = ''
if img is not None and img.any():
processed_image_path = process_image(img, steps, threshold_value)
text_output = ocr_with_easy(processed_image_path)
else:
raise gr.Error("Please upload an image and select the processing steps!")
return text_output
# Interactive tutorial steps
tutorial_steps = [
"Grayscale Conversion",
"Thresholding"
]
# Interactive questions
questions = [
{
"question": "What is the first step in OCR?",
"options": ["Binarization", "Grayscale Conversion", "Edge Detection"],
"answer": "Grayscale Conversion"
},
{
"question": "What is the purpose of thresholding in OCR?",
"options": ["To detect edges", "To convert image to grayscale", "To binarize the image"],
"answer": "To binarize the image"
},
{
"question": "Which library is used for OCR in this app?",
"options": ["Tesseract", "EasyOCR", "OpenCV"],
"answer": "EasyOCR"
},
{
"question": "What format is the image saved in after preprocessing?",
"options": ["JPG", "PNG", "TIFF"],
"answer": "PNG"
},
{
"question": "What does OCR stand for?",
"options": ["Optical Character Recognition", "Optical Character Reading", "Optical Code Recognition"],
"answer": "Optical Character Recognition"
}
]
def quiz_interface():
def check_answer(question_idx, selected):
if questions[question_idx]["answer"] == selected:
return "Correct!"
else:
return "Incorrect. The correct answer is: " + questions[question_idx]["answer"]
interfaces = []
for idx, question in enumerate(questions):
radio = gr.Radio(choices=question["options"], label=question["question"])
button = gr.Button("Submit")
output = gr.Textbox(label="Result")
def create_submit_fn(idx):
def submit(selected):
return check_answer(idx, selected)
return submit
interfaces.append(gr.Interface(
create_submit_fn(idx),
radio,
output,
live=True
))
return interfaces
# Explanation text
explanation_text = """
**Welcome to the OCR Tutorial!**
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.
**Steps in the OCR Process:**
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.
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.
3. **OCR using EasyOCR:** We use the EasyOCR library to recognize and extract text from the preprocessed image.
**Interactive Tutorial:**
Please upload an image and select the correct order of steps to perform OCR. You can also adjust the threshold value using the slider.
"""
example_image_path = "path_to_your_example_image.png" # Provide the path to your example image
image = gr.Image(value=example_image_path, tool="editor", label="Upload or edit image for OCR")
steps = gr.CheckboxGroup(choices=tutorial_steps, label="Select and order the steps for OCR")
threshold = gr.Slider(0, 255, value=127, step=1, label="Threshold Value")
output = gr.Textbox(label="OCR Output")
explanation = gr.Markdown(explanation_text)
ocr_app = gr.Interface(
fn=generate_ocr,
inputs=[image, steps, threshold],
outputs=output,
title="Optical Character Recognition",
description=explanation_text,
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}"
)
quiz_app = gr.TabbedInterface(
[ocr_app] + quiz_interface(),
["OCR Tool"] + [f"Question {i+1}" for i in range(len(questions))],
title="OCR Tutorial and Quiz"
)
quiz_app.launch()
|