File size: 770 Bytes
5e6f7f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tensorflow as tf
import gradio as gr
import numpy as npd
from PIL import Image


loaded_model = tf.keras.models.load_model('digit_recognition_model.h5')

def recognize_digit(image):
    # Preprocess the input image
    image = Image.fromarray(image).convert('L')  # Convert to grayscale
    image = image.resize((28, 28))  # Resize to 28x28
    image = np.array(image)
    image = image / 255.0  # Normalize the pixel values

    # Make a prediction using the loaded model
    image = np.expand_dims(image, axis=0)  # Add a batch dimension
    prediction = loaded_model.predict(image)
    predicted_digit = np.argmax(prediction)

    return str(predicted_digit)

iface = gr.Interface(fn=recognize_digit, inputs="image", outputs="text")
iface.launch(share = True)