Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,43 +2,45 @@ import gradio as gr
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
'Egyptian_Mau', 'Maine_Coon', 'Persian', 'Ragdoll', 'Russian_Blue',
|
| 10 |
-
'Siamese', 'Sphynx'
|
| 11 |
-
]
|
| 12 |
|
| 13 |
-
# Load the model
|
| 14 |
model = tf.keras.models.load_model("cat_breed_model.keras")
|
| 15 |
|
| 16 |
def predict(image: Image.Image):
|
| 17 |
try:
|
| 18 |
if image is None:
|
| 19 |
-
return {
|
|
|
|
|
|
|
| 20 |
img = image.resize((224, 224)).convert("RGB")
|
| 21 |
x = np.array(img) / 255.0
|
| 22 |
x = np.expand_dims(x, axis=0)
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
| 25 |
return result
|
|
|
|
| 26 |
except Exception as e:
|
| 27 |
print(f"Prediction error: {e}")
|
| 28 |
-
return {
|
| 29 |
|
|
|
|
| 30 |
iface = gr.Interface(
|
| 31 |
fn=predict,
|
| 32 |
inputs=gr.Image(type="pil"),
|
| 33 |
-
outputs=
|
| 34 |
-
title="Cat Breed Classifier",
|
| 35 |
-
description="Upload a cat image to classify its breed.",
|
| 36 |
article="""
|
| 37 |
-
⚠️ **Note:** This model will always predict one of the 12 cat breeds, even for non-cat images. For best results, upload a clear image of a cat.
|
| 38 |
|
| 39 |
-
**Note:**
|
| 40 |
-
This project can detect only the following 12 cat breeds:
|
| 41 |
|
|
|
|
| 42 |
- Abyssinian
|
| 43 |
- Bengal
|
| 44 |
- Birman
|
|
@@ -51,10 +53,8 @@ This project can detect only the following 12 cat breeds:
|
|
| 51 |
- Russian Blue
|
| 52 |
- Siamese
|
| 53 |
- Sphynx
|
| 54 |
-
|
| 55 |
-
*Please upload a clear image of a cat belonging to one of these breeds for best results.*
|
| 56 |
"""
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
-
iface.launch()
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
+
import json
|
| 6 |
|
| 7 |
+
# 🔹 Load class names from JSON file
|
| 8 |
+
with open("cat_class_names.json", "r") as f:
|
| 9 |
+
SUPPORTED_CLASSES = json.load(f)
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# Load the trained model
|
| 12 |
model = tf.keras.models.load_model("cat_breed_model.keras")
|
| 13 |
|
| 14 |
def predict(image: Image.Image):
|
| 15 |
try:
|
| 16 |
if image is None:
|
| 17 |
+
return {cls: 0.0 for cls in SUPPORTED_CLASSES}
|
| 18 |
+
|
| 19 |
+
# Preprocess the image
|
| 20 |
img = image.resize((224, 224)).convert("RGB")
|
| 21 |
x = np.array(img) / 255.0
|
| 22 |
x = np.expand_dims(x, axis=0)
|
| 23 |
+
|
| 24 |
+
# Predict
|
| 25 |
+
preds = model.predict(x)[0] # shape: (13,)
|
| 26 |
+
result = {cls: float(conf) for cls, conf in zip(SUPPORTED_CLASSES, preds)}
|
| 27 |
return result
|
| 28 |
+
|
| 29 |
except Exception as e:
|
| 30 |
print(f"Prediction error: {e}")
|
| 31 |
+
return {cls: 0.0 for cls in SUPPORTED_CLASSES}
|
| 32 |
|
| 33 |
+
# Define Gradio interface
|
| 34 |
iface = gr.Interface(
|
| 35 |
fn=predict,
|
| 36 |
inputs=gr.Image(type="pil"),
|
| 37 |
+
outputs=gr.Label(num_top_classes=1, label="Predicted Class"),
|
| 38 |
+
title="Cat Breed Classifier (with not_cat)",
|
| 39 |
+
description="Upload a cat image to classify its breed. If it's not a cat, the model will say so!",
|
| 40 |
article="""
|
|
|
|
| 41 |
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
**Supported Cat Breeds:**
|
| 44 |
- Abyssinian
|
| 45 |
- Bengal
|
| 46 |
- Birman
|
|
|
|
| 53 |
- Russian Blue
|
| 54 |
- Siamese
|
| 55 |
- Sphynx
|
|
|
|
|
|
|
| 56 |
"""
|
| 57 |
)
|
| 58 |
|
| 59 |
if __name__ == "__main__":
|
| 60 |
+
iface.launch()
|