Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,13 @@ import gradio as gr
|
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
-
# Load
|
7 |
model = tf.keras.models.load_model("animal_classifier.keras")
|
8 |
|
9 |
-
|
10 |
-
class_names =
|
11 |
|
12 |
def predict_image(image):
|
13 |
img = image.resize((224, 224))
|
@@ -18,12 +19,14 @@ def predict_image(image):
|
|
18 |
confidence = np.max(preds)
|
19 |
predicted_index = np.argmax(preds)
|
20 |
|
21 |
-
threshold = 0.5
|
|
|
22 |
if confidence < threshold:
|
23 |
return "Image not recognized as any animal in the dataset"
|
24 |
else:
|
25 |
return class_names[predicted_index]
|
26 |
|
27 |
-
demo = gr.Interface(fn=predict_image, inputs=gr.Image(type="pil"), outputs="text",
|
|
|
28 |
|
29 |
demo.launch()
|
|
|
2 |
import tensorflow as tf
|
3 |
import numpy as np
|
4 |
from PIL import Image
|
5 |
+
import json
|
6 |
|
7 |
+
# Load model and class names JSON
|
8 |
model = tf.keras.models.load_model("animal_classifier.keras")
|
9 |
|
10 |
+
with open("class_names.json", "r") as f:
|
11 |
+
class_names = json.load(f)
|
12 |
|
13 |
def predict_image(image):
|
14 |
img = image.resize((224, 224))
|
|
|
19 |
confidence = np.max(preds)
|
20 |
predicted_index = np.argmax(preds)
|
21 |
|
22 |
+
threshold = 0.5 # minimum confidence to accept prediction
|
23 |
+
|
24 |
if confidence < threshold:
|
25 |
return "Image not recognized as any animal in the dataset"
|
26 |
else:
|
27 |
return class_names[predicted_index]
|
28 |
|
29 |
+
demo = gr.Interface(fn=predict_image, inputs=gr.Image(type="pil"), outputs="text",
|
30 |
+
title="MobileNetV2 Animal Classifier")
|
31 |
|
32 |
demo.launch()
|