Spaces:
Sleeping
Sleeping
File size: 4,989 Bytes
d47a89d faf3b9f d47a89d faf3b9f d47a89d faf3b9f d47a89d 76a09f4 faf3b9f d47a89d 76a09f4 faf3b9f 76a09f4 bea12a5 faf3b9f 76a09f4 bea12a5 faf3b9f bea12a5 faf3b9f bea12a5 faf3b9f bea12a5 faf3b9f bea12a5 ff5f533 faf3b9f 76a09f4 faf3b9f ff5f533 faf3b9f ff5f533 d47a89d faf3b9f 76a09f4 6b27525 faf3b9f ff5f533 c02f435 d47a89d bea12a5 faf3b9f bea12a5 |
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
import gradio as gr
import cv2
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
# Functions for MNIST processing steps
def load_mnist():
(x_train, y_train), (x_test, y_test) = mnist.load_data()
return x_test, y_test
def get_grayscale(image):
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
def thresholding(src):
return cv2.threshold(src, 127, 255, cv2.THRESH_BINARY)[1]
def gaussian_blur(image):
return cv2.GaussianBlur(image, (5, 5), 0)
def edge_detection(image):
return cv2.Canny(image, 100, 200)
def process_mnist_image(img, steps):
original_img = img.copy()
step_images = {'Original': original_img}
for step in steps:
if step == "Grayscale Conversion":
img = get_grayscale(img)
elif step == "Thresholding":
img = thresholding(img)
elif step == "Gaussian Blur":
img = gaussian_blur(img)
elif step == "Edge Detection":
img = edge_detection(img)
step_images[step] = img
return step_images
def visualize_steps(img, steps):
step_images = process_mnist_image(img, steps)
fig, axes = plt.subplots(1, len(step_images), figsize=(15, 5))
for ax, (step, img) in zip(axes, step_images.items()):
ax.imshow(img, cmap='gray')
ax.set_title(step)
ax.axis('off')
plt.tight_layout()
plt.savefig('mnist_processing_steps.png')
return 'mnist_processing_steps.png'
# Interactive tutorial steps
tutorial_steps = [
"Grayscale Conversion",
"Thresholding",
"Gaussian Blur",
"Edge Detection"
]
# Interactive questions
questions = [
{
"question": "What is the first step in preprocessing MNIST images?",
"options": ["Gaussian Blur", "Grayscale Conversion", "Edge Detection"],
"answer": "Grayscale Conversion"
},
{
"question": "What does thresholding do in image preprocessing?",
"options": ["Detects edges", "Blurs the image", "Binarizes the image"],
"answer": "Binarizes the image"
},
{
"question": "What library is used to load the MNIST dataset?",
"options": ["OpenCV", "TensorFlow", "Pytorch"],
"answer": "TensorFlow"
},
{
"question": "What color space conversion is used in Grayscale Conversion?",
"options": ["BGR to RGB", "RGB to GRAY", "BGR to GRAY"],
"answer": "BGR to GRAY"
},
{
"question": "What is the purpose of Gaussian Blur?",
"options": ["Detect edges", "Reduce noise", "Convert to grayscale"],
"answer": "Reduce noise"
}
]
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 MNIST Processing Tutorial!**
This tutorial will guide you through the basic steps of preprocessing images from the MNIST dataset.
**Steps in the MNIST Image Processing:**
1. **Grayscale Conversion:** Converts the image to grayscale to simplify the data.
2. **Thresholding:** Converts the grayscale image into a binary image to distinguish the digits more clearly.
3. **Gaussian Blur:** Applies a blur to reduce noise and detail in the image.
4. **Edge Detection:** Detects the edges of the digits to enhance the features for further processing or recognition tasks.
**Interactive Tutorial:**
Please upload an MNIST image and select the preprocessing steps to visualize their effects.
"""
(x_test, y_test) = load_mnist()
image = gr.Image(shape=(28, 28), image_mode='L')
steps = gr.CheckboxGroup(choices=tutorial_steps, label="Select and order the steps for MNIST processing")
output = gr.Image(type='file', label="Processing Steps Visualization")
explanation = gr.Markdown(explanation_text)
mnist_app = gr.Interface(
fn=visualize_steps,
inputs=[image, steps],
outputs=output,
title="MNIST Image Processing",
description=explanation_text,
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}"
)
quiz_app = gr.TabbedInterface(
[mnist_app] + quiz_interface(),
["MNIST Tool"] + [f"Question {i+1}" for i in range(len(questions))],
title="MNIST Tutorial and Quiz"
)
quiz_app.launch()
|