|
|
|
import gradio as gr |
|
import tensorflow as tf |
|
import cv2 |
|
import numpy as np |
|
|
|
|
|
title = "Welcome to your first sketch recognition app!" |
|
|
|
|
|
head = ( |
|
"<center>" |
|
"<img src='mnist-classes.png' width=400><br>" |
|
"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 = tf.keras.models.load_model("./sketch_recognition_numbers_model.h5") |
|
|
|
|
|
def predict(img): |
|
if img is not None: |
|
|
|
img = np.array(img) |
|
|
|
|
|
if len(img.shape) == 3: |
|
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) |
|
|
|
|
|
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)} |
|
return None |
|
|
|
|
|
label = gr.Label(num_top_classes=3) |
|
|
|
|
|
interface = gr.Interface( |
|
fn=predict, |
|
inputs=gr.Sketchpad(height=280, width=280), |
|
outputs=label, |
|
title=title, |
|
description=head, |
|
article=ref |
|
) |
|
interface.launch() |