File size: 1,353 Bytes
8d28437
 
 
e7ea0c7
8d28437
3a813cf
8d28437
 
3a813cf
8d28437
3a813cf
 
e7ea0c7
5ab5708
3a813cf
5ab5708
 
 
 
e7ea0c7
3a813cf
 
 
 
 
 
 
8d28437
3a813cf
 
8d28437
 
3a813cf
8d28437
3a813cf
 
 
8d28437
 
e7ea0c7
 
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
30
31
32
33
34
35
36
37
38
39
40
41
42
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image

# Load the model (ensure you have the correct model path)
model = tf.keras.models.load_model("denis_mnist_cnn_model.h5")

# Define a function to preprocess input and make predictions
def predict(image):
    # Convert image to a numpy array
    image = np.array(image)
    
    # Resize the image to the expected shape (28, 28, 3) for RGB images
    image = tf.image.resize(image, (28, 28))  # Resize to 28x28 pixels
    
    # Check if the image is grayscale (single channel), and convert to RGB if necessary
    if image.shape[-1] == 1:  # If it's grayscale (single channel)
        image = np.repeat(image, 3, axis=-1)  # Convert grayscale to RGB by repeating the channel
    
    # Normalize the image
    image = image / 255.0
    
    # Add batch dimension
    image = np.expand_dims(image, axis=0)  # Add batch dimension to match the model's expected input shape (1, 28, 28, 3)
    
    # Perform prediction
    prediction = model.predict(image)
    
    # Return prediction as JSON
    return {"prediction": prediction.tolist()}

# Create a Gradio interface
interface = gr.Interface(
    fn=predict, 
    inputs="image",  # Image input for testing
    outputs="json"  # JSON output for prediction results
)

# Launch the interface
interface.launch(share=True)