|
import os |
|
import numpy as np |
|
import cv2 |
|
import gradio as gr |
|
import tensorflow as tf |
|
from PIL import Image |
|
|
|
|
|
title = "Welcome on your first sketch recognition app!" |
|
|
|
|
|
head = ( |
|
"<center>" |
|
"<img src='./mnist-classes.png' width=400>" |
|
"<p>The robot was trained to classify numbers (0 to 9). " |
|
"To test it, write your number in the space provided!</p>" |
|
"</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_path = "./sketch_recognition_numbers_model.h5" |
|
try: |
|
model = tf.keras.models.load_model(model_path) |
|
except Exception as e: |
|
raise FileNotFoundError(f"Model file '{model_path}' not found or failed to load. {str(e)}") |
|
|
|
def predict(img): |
|
|
|
if img is None: |
|
return {"error": "No image provided."} |
|
|
|
|
|
if not isinstance(img, Image.Image): |
|
img = Image.fromarray(np.uint8(img)) |
|
|
|
|
|
img = img.convert("L") |
|
|
|
|
|
img = np.array(img, dtype=np.uint8) |
|
|
|
|
|
img = cv2.resize(img, (img_size, img_size)) |
|
|
|
|
|
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)} |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Sketchpad(type="pil"), |
|
outputs=gr.Label(num_top_classes=3), |
|
title=title, |
|
description=head, |
|
article=ref |
|
) |
|
|
|
interface.launch() |