Mnist-Digits / app.py
cisemh's picture
Update app.py
9a87dce verified
raw
history blame
1.55 kB
import cv2
import gradio as gr
import tensorflow as tf
import numpy as np
title = "Welcome to 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>"
)
ref = "Find the whole code [here](https://github.com/ovh/ai-training-examples/tree/main/apps/gradio/sketch-recognition)."
img_size = 28
labels = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
# Model yükleniyor
model = tf.keras.models.load_model("number_recognition_model_colab.keras")
def predict(img):
try:
# Girdi görselini NumPy array'e çevir
if not isinstance(img, np.ndarray):
img = np.array(img)
# Görüntüyü gri tonlamaya çevir ve yeniden boyutlandır
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if img.ndim == 3 else img
img = cv2.resize(img, (img_size, img_size))
img = img.astype('float32') / 255.0
img = img.reshape(1, img_size, img_size, 1)
preds = model.predict(img)[0]
return {label: float(pred) for label, pred in zip(labels, preds)}
except Exception as e:
return {"Error": str(e)}
label = gr.Label(num_top_classes=3)
# Yeni Gradio bileşenleriyle uyumlu hale getirildi
interface = gr.Interface(
fn=predict,
inputs=gr.Sketchpad(label="Draw a number"), # Sketchpad kullanımı
outputs=label,
title=title,
description=head,
article=ref
)
interface.launch(debug=True)