Frantz103 commited on
Commit
61ad9eb
1 Parent(s): 8a0ff82

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -30
app.py CHANGED
@@ -1,45 +1,48 @@
1
  import gradio as gr
2
  from fastai.vision.all import *
3
 
4
- cat_breeds = [
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
- regex_pattern = r'(.+)_(\d+)\.jpg$'
12
- match = re.match(regex_pattern, str(path.name))
13
- if match:
14
- label = match.group(1)
15
- pet_type = 'cat' if label in cat_breeds else 'dog'
16
- return [pet_type, label]
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, _, probs = learn.predict(pil_image)
30
-
31
  # Apply the threshold
32
- threshold = 0.425
33
- classes = [learn.dls.vocab[i] for i in range(len(probs)) if probs[i] > threshold]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- pet_type = "dog" if "dog" in classes else "cat" if "cat" in classes else None
36
- breeds = [breed for breed in classes if breed not in ["cat", "dog"]]
37
 
38
- if pet_type:
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