Spaces:
Sleeping
Sleeping
import cv2 | |
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
from PIL import 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 | |
labels = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] | |
def predict(img): | |
try: | |
# Enhanced image validation and conversion | |
if img is None: | |
raise ValueError("No image provided") | |
# Convert to numpy array if it's a PIL Image | |
if isinstance(img, Image.Image): | |
img = np.array(img) | |
# Handle base64 image strings | |
elif isinstance(img, str): | |
# Check if it's a base64 data URL | |
if img.startswith('data:image'): | |
# Split and decode base64 part | |
img = img.split(',')[1] | |
# Decode base64 to image | |
try: | |
img = Image.open(io.BytesIO(base64.b64decode(img))) | |
img = np.array(img) | |
except Exception as e: | |
print(f"Base64 decoding error: {e}") | |
raise ValueError("Invalid base64 image") | |
# Validate numpy array | |
if not isinstance(img, np.ndarray): | |
raise ValueError("Input could not be converted to a valid image") | |
# Print initial image details for debugging | |
print(f"Initial image type: {type(img)}, shape: {img.shape}") | |
# Handle color channels | |
if img.ndim == 3: | |
if img.shape[-1] == 3: # Color image | |
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
elif img.shape[-1] == 4: # RGBA image | |
img = cv2.cvtColor(img, cv2.COLOR_RGBA2GRAY) | |
# Ensure single channel | |
if img.ndim == 2: | |
img = np.expand_dims(img, axis=-1) | |
# Resize and normalize | |
img = cv2.resize(img, (img_size, img_size)) | |
img = img.astype('float32') / 255.0 | |
img = img.reshape(1, img_size, img_size, 1) | |
# Print processed image details | |
print(f"Processed image shape: {img.shape}") | |
# Get predictions from the model | |
preds = model.predict(img)[0] | |
# Print predictions for debugging | |
print("Predictions:", preds) | |
# Return predictions as a dictionary | |
return {label: float(pred) for label, pred in zip(labels, preds)} | |
except Exception as e: | |
# Comprehensive error logging | |
print(f"Full 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=gr.Sketchpad(type="pil"), | |
outputs=label, | |
title="Sketch Recognition App", | |
description="Draw a number (0-9) and see the model's top predictions." | |
) | |
interface.launch(debug=True, share=True) |