File size: 900 Bytes
fc9e577
 
 
 
1e635e1
fc9e577
1e635e1
fc9e577
 
1e635e1
 
fc9e577
fbb8092
 
 
fc9e577
 
89a2c48
fbb8092
 
fc9e577
1e635e1
 
fbb8092
 
 
 
89a2c48
1e635e1
 
fbb8092
 
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
import gradio as gr
import tensorflow as tf
import numpy as np
from PIL import Image
import json

# Load model and class names JSON
model = tf.keras.models.load_model("animal_classifier.keras")

with open("class_names.json", "r") as f:
    class_names = json.load(f)

def predict_image(image):
    img = image.resize((224, 224))
    img_array = np.array(img) / 255.0
    img_array = np.expand_dims(img_array, axis=0)

    preds = model.predict(img_array)
    confidence = np.max(preds)
    predicted_index = np.argmax(preds)

    threshold = 0.5  # minimum confidence to accept prediction

    if confidence < threshold:
        return "Image not recognized as any animal in the dataset"
    else:
        return class_names[predicted_index]

demo = gr.Interface(fn=predict_image, inputs=gr.Image(type="pil"), outputs="text",
                    title="MobileNetV2 Animal Classifier")

demo.launch()