Theingh02 commited on
Commit
3b27c00
·
verified ·
1 Parent(s): d1d3f58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -26
app.py CHANGED
@@ -1,34 +1,56 @@
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()
 
 
 
 
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()