Spaces:
Sleeping
Sleeping
import cv2 | |
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
import PIL.Image | |
title = "Welcome on your first sketch recognition app!" | |
head = ( | |
"<center>" | |
"The robot was trained to classify numbers (from 0 to 9). To test it, write your number in the space provided." | |
"</center>" | |
) | |
# Model yükleniyor | |
model = tf.keras.models.load_model("number_recognition_model_colab.keras") | |
img_size = 28 | |
# Etiketler (0'dan 9'a kadar sayılar) | |
labels = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] | |
# Tahmin fonksiyonu | |
def predict(img): | |
try: | |
# If the input is a dictionary (Gradio sketchpad), extract the image | |
if isinstance(img, dict): | |
img = img.get('image', None) # Get the image from the dictionary | |
if img is None: | |
raise ValueError("No image data found") | |
# Convert the input image to a NumPy array if needed | |
if not isinstance(img, np.ndarray): | |
img = np.array(img) | |
# Print shape and type of the input image | |
print(f"Initial image type: {type(img)}, shape: {img.shape}") | |
# Ensure the image is in grayscale and has a single channel | |
if img.ndim == 3 and img.shape[-1] == 3: | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
elif img.ndim == 2: | |
img = np.expand_dims(img, axis=-1) | |
# Print the shape of the grayscale image | |
print(f"Grayscale image shape: {img.shape}") | |
# Resize the image | |
img = cv2.resize(img, (img_size, img_size)) | |
# Normalize the image | |
img = img.astype('float32') / 255.0 | |
img = img.reshape(1, img_size, img_size, 1) | |
# Print the shape after resizing and normalizing | |
print(f"Processed image shape: {img.shape}") | |
# Get the predictions from the model | |
preds = model.predict(img)[0] | |
# Print the predictions | |
print("Predictions:", preds) | |
# Return the predictions for each label | |
return {label: float(pred) for label, pred in zip(labels, preds)} | |
except Exception as e: | |
# Print the exception to the console | |
print(f"Error during prediction: {e}") | |
return {"Error": str(e)} | |
# Set up the Gradio interface with the input as a sketchpad and output as labels | |
label = gr.Label(num_top_classes=3) | |
# Gradio arayüzü | |
interface = gr.Interface( | |
fn=predict, | |
inputs="sketchpad", | |
outputs="textbox", | |
title="Sketch Recognition App", | |
description="Draw a number (0-9) and see the model's top predictions." | |
) | |
interface.launch(debug=True) | |