Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,56 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
from PIL import Image
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
# Load
|
| 9 |
-
model = load_model(
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=predict,
|
| 28 |
inputs=gr.Image(type="pil"),
|
| 29 |
-
outputs=gr.
|
| 30 |
title="Dog Breed Classifier",
|
| 31 |
-
description="Upload
|
| 32 |
)
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
+
import gradio as gr
|
| 3 |
from PIL import Image
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
|
| 6 |
+
# Load your model here
|
| 7 |
+
model = tf.keras.models.load_model('your_model.h5')
|
| 8 |
+
|
| 9 |
+
# List of classes your model predicts
|
| 10 |
+
class_names = ['beagle', 'bulldog', 'golden_retriever', 'toy', 'human'] # add all your classes here
|
| 11 |
+
|
| 12 |
+
# Classes to flag as unknown
|
| 13 |
+
unknown_classes = {'toy', 'human'}
|
| 14 |
+
|
| 15 |
+
# Image size expected by your model
|
| 16 |
+
IMAGE_SIZE = (224, 224)
|
| 17 |
+
|
| 18 |
+
def preprocess_image(img):
|
| 19 |
+
img = img.convert("RGB").resize(IMAGE_SIZE)
|
| 20 |
+
img = np.array(img) / 255.0
|
| 21 |
+
img = np.expand_dims(img, axis=0)
|
| 22 |
+
return img
|
| 23 |
+
|
| 24 |
+
def predict(img):
|
| 25 |
+
img_processed = preprocess_image(img)
|
| 26 |
+
preds = model.predict(img_processed)[0] # assuming batch size 1
|
| 27 |
+
top5_indices = preds.argsort()[-5:][::-1]
|
| 28 |
+
top5_preds = [(class_names[i], preds[i]) for i in top5_indices]
|
| 29 |
+
|
| 30 |
+
# Check unknown classes in top 5
|
| 31 |
+
if any(cls in unknown_classes for cls, conf in top5_preds):
|
| 32 |
+
return "Unknown class detected, unable to classify reliably."
|
| 33 |
+
|
| 34 |
+
# Final prediction with threshold (e.g. 90%)
|
| 35 |
+
final_idx = np.argmax(preds)
|
| 36 |
+
confidence = preds[final_idx]
|
| 37 |
+
if confidence < 0.9:
|
| 38 |
+
return "Confidence too low to predict reliably."
|
| 39 |
+
|
| 40 |
+
final_breed = class_names[final_idx]
|
| 41 |
+
result_str = f"Final Prediction: {final_breed} (Confidence: {confidence:.2f})\n\nTop 5 predictions:\n"
|
| 42 |
+
for cls, conf in top5_preds:
|
| 43 |
+
result_str += f"{cls}: {conf:.2f}\n"
|
| 44 |
+
|
| 45 |
+
return result_str
|
| 46 |
+
|
| 47 |
iface = gr.Interface(
|
| 48 |
fn=predict,
|
| 49 |
inputs=gr.Image(type="pil"),
|
| 50 |
+
outputs=gr.Textbox(),
|
| 51 |
title="Dog Breed Classifier",
|
| 52 |
+
description="Upload an image and see top 5 predictions and final breed with threshold and unknown detection."
|
| 53 |
)
|
| 54 |
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
iface.launch()
|