shkumar156 commited on
Commit
2a8e58b
·
verified ·
1 Parent(s): f35da0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -11
app.py CHANGED
@@ -4,42 +4,50 @@ 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",
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
@@ -53,6 +61,10 @@ iface = gr.Interface(
53
  - Russian Blue
54
  - Siamese
55
  - Sphynx
 
 
 
 
56
  """
57
  )
58
 
 
4
  from PIL import Image
5
  import json
6
 
7
+ # 🔹 Load class names
8
  with open("cat_class_names.json", "r") as f:
9
  SUPPORTED_CLASSES = json.load(f)
10
 
11
+ # 🔹 Load model
12
  model = tf.keras.models.load_model("cat_breed_model.keras")
13
 
14
+ # 🔹 Confidence threshold (adjustable)
15
+ CONFIDENCE_THRESHOLD = 0.75
16
+
17
  def predict(image: Image.Image):
18
  try:
19
  if image is None:
20
+ return {"Error": "No image provided"}
21
 
22
+ # Preprocess image
23
  img = image.resize((224, 224)).convert("RGB")
24
  x = np.array(img) / 255.0
25
  x = np.expand_dims(x, axis=0)
26
 
27
  # Predict
28
  preds = model.predict(x)[0] # shape: (13,)
29
+ predicted_index = np.argmax(preds)
30
+ confidence = preds[predicted_index]
31
+ predicted_class = SUPPORTED_CLASSES[predicted_index]
32
+
33
+ # Check threshold
34
+ if confidence < CONFIDENCE_THRESHOLD:
35
+ return {"Unknown or Unsupported Cat Breed": float(confidence)}
36
+ else:
37
+ return {predicted_class: float(confidence)}
38
 
39
  except Exception as e:
40
  print(f"Prediction error: {e}")
41
+ return {"Error": "Prediction failed"}
42
 
43
+ # 🔹 Gradio interface
44
  iface = gr.Interface(
45
  fn=predict,
46
  inputs=gr.Image(type="pil"),
47
  outputs=gr.Label(num_top_classes=1, label="Predicted Class"),
48
  title="Cat Breed Classifier",
49
+ description="Upload a cat image to classify its breed. If it's not a cat or not in the 12 supported breeds, it will return 'Unknown'.",
50
  article="""
 
 
51
  **Supported Cat Breeds:**
52
  - Abyssinian
53
  - Bengal
 
61
  - Russian Blue
62
  - Siamese
63
  - Sphynx
64
+
65
+ Also supports:
66
+ - `not_cat` (e.g., dogs, random objects)
67
+ - `Unknown` (if image doesn’t match any known cat confidently)
68
  """
69
  )
70