File size: 890 Bytes
98b1303
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import tensorflow as tf
import cv2

model = tf.keras.models.load_model(r"./vgg16_model.keras")

class_names = [
    "airplane",
    "automobile",
    "bird",
    "cat",
    "deer",
    "dog",
    "frog",
    "horse",
    "ship",
    "truck",
]


def predict(image):
    # Resize image to (32, 32)
    image = cv2.resize(image, (32, 32))
    print("Resized image shape:", image.shape)  # Print the shape of the resized image
    # Convert image to float32 and normalize
    image = image.astype("float32") / 255.0
    # Add batch dimension
    image = tf.expand_dims(image, 0)
    # Predict using the model
    prediction = model.predict(image)
    class_index = tf.argmax(prediction, axis=1)[0].numpy()
    class_label = class_names[class_index]  # Get the class label
    return class_label


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