Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
from fastai.vision.all import *
|
3 |
|
4 |
-
|
5 |
-
'Abyssinian', 'Bengal', 'Birman', 'Bombay', 'British_Shorthair',
|
6 |
-
'Egyptian_Mau', 'Main_Coon', 'Persian', 'Ragdoll', 'Russian_Blue',
|
7 |
-
'Siamese', 'Sphynx'
|
8 |
-
]
|
9 |
|
10 |
def get_labels(path):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
else:
|
18 |
-
# Handle the case where the regex doesn't match
|
19 |
-
print(f"Could not match pattern in {path.name}")
|
20 |
-
return []
|
21 |
-
|
22 |
-
learn = load_learner('dog_cat_multi.pkl')
|
23 |
|
24 |
def predict(image):
|
25 |
# Transform the image
|
26 |
pil_image = PILImage.create(image)
|
27 |
-
|
28 |
# Predict
|
29 |
-
preds,
|
30 |
-
|
31 |
# Apply the threshold
|
32 |
-
threshold = 0.
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
breed_info = f"The breed is {'/'.join(breeds)}." if breeds else "The breed is not identified."
|
40 |
-
return f"This is a {pet_type}. {breed_info}"
|
41 |
-
else:
|
42 |
-
return "This is not a cat, nor a dog."
|
43 |
|
44 |
|
45 |
# Define the Gradio interface
|
@@ -50,7 +53,7 @@ iface = gr.Interface(
|
|
50 |
live=True,
|
51 |
title="Cat and Dog Image Classifier",
|
52 |
description="Upload an image of a cat or a dog, and the model will identify the type and breed.",
|
53 |
-
article="This model has been trained on the Oxford Pets dataset and might not recognize all types dog and cat breeds. For best results, use clear images."
|
54 |
)
|
55 |
|
56 |
|
|
|
1 |
import gradio as gr
|
2 |
from fastai.vision.all import *
|
3 |
|
4 |
+
def is_cat(x): return x[0].isupper()
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def get_labels(path):
|
7 |
+
pet_type = 'cat' if is_cat(path.name) else 'dog'
|
8 |
+
breed = RegexLabeller(r'(.+)_\d+.jpg$')(path.name)
|
9 |
+
|
10 |
+
return [pet_type, breed]
|
11 |
+
|
12 |
+
learn = load_learner('dog_cat_multi_v3.pkl')
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
def predict(image):
|
15 |
# Transform the image
|
16 |
pil_image = PILImage.create(image)
|
17 |
+
|
18 |
# Predict
|
19 |
+
preds, mask, probs = learn.predict(pil_image)
|
20 |
+
|
21 |
# Apply the threshold
|
22 |
+
threshold = 0.570
|
23 |
+
classes_with_probs = [(learn.dls.vocab[i], probs[i].item()) for i in range(len(mask)) if probs[i] > threshold]
|
24 |
+
|
25 |
+
# Check if the prediction includes "dog" or "cat"
|
26 |
+
pet_type = None
|
27 |
+
breed = 'unknown' # Default to 'unknown' if no breed is identified
|
28 |
+
pet_prob = 0
|
29 |
+
breed_prob = 0
|
30 |
+
for class_name, prob in classes_with_probs:
|
31 |
+
if class_name == 'dog' or class_name == 'cat':
|
32 |
+
pet_type = class_name
|
33 |
+
pet_prob = prob
|
34 |
+
else:
|
35 |
+
breed = class_name
|
36 |
+
breed_prob = prob
|
37 |
+
|
38 |
+
# Check if pet_type is None (i.e., neither dog nor cat)
|
39 |
+
if pet_type is None:
|
40 |
+
return "This is not a cat, nor a dog."
|
41 |
|
42 |
+
result = f"This is a {pet_type}, the breed is {breed if breed else 'unknown'}.\n" \
|
43 |
+
f"The probability for it being a {pet_type} is {pet_prob * 100:.2f}%, the probability of being the breed is {breed_prob * 100:.2f}%."
|
44 |
|
45 |
+
return result
|
|
|
|
|
|
|
|
|
46 |
|
47 |
|
48 |
# Define the Gradio interface
|
|
|
53 |
live=True,
|
54 |
title="Cat and Dog Image Classifier",
|
55 |
description="Upload an image of a cat or a dog, and the model will identify the type and breed.",
|
56 |
+
article="This model has been trained on the Oxford Pets dataset and might not recognize all types dog and cat breeds. For best results, use clear images of pets."
|
57 |
)
|
58 |
|
59 |
|