Spaces:
Sleeping
Sleeping
| 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): | |
| return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1] | |
| def ocr_with_easy(img): | |
| gray_scale_image = get_grayscale(img) | |
| thresholded_image = thresholding(gray_scale_image) | |
| cv2.imwrite('image.png', thresholded_image) | |
| reader = easyocr.Reader(['en']) | |
| bounds = reader.readtext('image.png', paragraph="False", detail=0) | |
| bounds = ''.join(bounds) | |
| return bounds | |
| def generate_ocr(img): | |
| text_output = '' | |
| if (img).any(): | |
| if img is not None: | |
| text_output = ocr_with_easy(img) | |
| else: | |
| raise gr.Error("Please upload an image!!!!") | |
| return text_output | |
| # 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 | |
| image = gr.Image() | |
| output = gr.Textbox(label="Output") | |
| ocr_app = gr.Interface( | |
| generate_ocr, | |
| image, | |
| output, | |
| title="Optical Character Recognition", | |
| 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() | |