File size: 972 Bytes
4cf48c8
 
 
 
 
 
cf652b0
4cf48c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import tensorflow as tf
import numpy as np
import gradio as gr
import cv2 # opencv-python


model = tf.keras.models.load_model("TP_MNIST_CNN_model.h5")


def preprocess_image(img):
    img = tf.image.rgb_to_grayscale(img)  # Convert to grayscale
    img = tf.image.resize(img, (28, 28))   # Resize to model input size
    img = img / 255.0                      # Normalize pixel values
    img = np.expand_dims(img, axis=0)      # Add batch dimension
    return img

# Function to make predictions
def predict_image(img):
    processed_img = preprocess_image(img)
    prediction = model.predict(processed_img)
    return {str(i): float(prediction[0][i]) for i in range(10)}

gr.Interface(
    fn=predict_image, 
    inputs=gr.Image(),  
    outputs=gr.Label(num_top_classes=3), 
    examples=["minst6.png", "mnist1.png", "mnist2_5.png", "mnist69.png"],  # Example images for interface
    title='Handwritten Digits Classification stage DREAMS Team'  
).launch(share=True)