File size: 1,558 Bytes
503c7ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
from PIL import Image
import numpy as np

# Load your custom regression model
model_path = "kia_mnist_keras_model.weights.h5"  
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=[28, 28]),
    tf.keras.layers.Rescaling(1./255.),
    tf.keras.layers.Dense(300, activation="relu"),
    tf.keras.layers.Dense(100, activation="relu"),
    tf.keras.layers.Dense(10, activation="softmax")
])
model.load_weights(model_path)

labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

# Define regression function
def predict_regression(image):
    # Preprocess image
    image = Image.fromarray(image.astype('uint8'))  # Convert numpy array to PIL image
    image = image.resize((28, 28)).convert('L') #resize the image to 28x28 and converts it to gray scale
    image = np.array(image)
    print(image.shape)
    # Predict
    prediction = model.predict(image[None, ...])  # Assuming single regression value
    confidences = {labels[i]: np.round(float(prediction[0][i]), 2) for i in range(len(labels))}
    return confidences

# Create Gradio interface
input_image = gr.Image()
output_text = gr.Textbox(label="Predicted Value")
interface = gr.Interface(fn=predict_regression, 
                         inputs=input_image, 
                         outputs=gr.Label(),
                         examples=["images/0.jpeg", "images/1.jpeg", "images/2.jpeg", "images/5.jpeg"],   
                         description="A custom regression model for image regression using .h5 file.")
interface.launch()