Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,34 @@
|
|
| 1 |
-
import numpy as np
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# Load
|
| 7 |
-
model =
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 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.
|
| 51 |
title="Dog Breed Classifier",
|
| 52 |
-
description="Upload
|
| 53 |
)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
iface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.models import load_model
|
| 5 |
from PIL import Image
|
| 6 |
+
from sklearn.preprocessing import LabelEncoder
|
| 7 |
+
|
| 8 |
+
# Load the trained model
|
| 9 |
+
model = load_model("best_dog_breed_model.keras") # ✅ match the actual file name
|
| 10 |
+
|
| 11 |
+
# Load and process labels
|
| 12 |
+
df = pd.read_csv("labels.csv")
|
| 13 |
+
le = LabelEncoder()
|
| 14 |
+
le.fit(df['breed'])
|
| 15 |
+
breed_list = list(le.classes_)
|
| 16 |
+
|
| 17 |
+
# Define prediction function
|
| 18 |
+
def predict(image):
|
| 19 |
+
image = image.resize((224, 224))
|
| 20 |
+
img_array = np.expand_dims(np.array(image) / 255.0, axis=0)
|
| 21 |
+
preds = model.predict(img_array)[0]
|
| 22 |
+
top5_idx = preds.argsort()[-5:][::-1]
|
| 23 |
+
return {breed_list[i]: float(preds[i]) for i in top5_idx}
|
| 24 |
+
|
| 25 |
+
# Create Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=predict,
|
| 28 |
inputs=gr.Image(type="pil"),
|
| 29 |
+
outputs=gr.Label(num_top_classes=5),
|
| 30 |
title="Dog Breed Classifier",
|
| 31 |
+
description="Upload a dog image to see the top 5 predicted breeds."
|
| 32 |
)
|
| 33 |
|
| 34 |
+
iface.launch()
|
|
|