DawnC commited on
Commit
a678f0c
·
1 Parent(s): 1ac521d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -41
app.py CHANGED
@@ -330,50 +330,48 @@ async def predict(image):
330
  return explanation, image, gr.update(visible=True, choices=breed_buttons), gr.update(visible=False), gr.update(visible=False)
331
 
332
  elif top1_prob < 0.2:
333
- return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
334
-
335
- # If confidence is low, use YOLO to check for multiple dogs
336
- dogs = await detect_multiple_dogs(image)
337
-
338
- if len(dogs) > 1:
339
- # Multiple dogs detected, process each one
340
- explanations = []
341
- buttons = []
342
- annotated_image = image.copy()
343
- draw = ImageDraw.Draw(annotated_image)
344
- font = ImageFont.load_default()
345
-
346
- for i, (cropped_image, _, box) in enumerate(dogs, 1):
347
- top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
348
-
349
- draw.rectangle(box, outline="red", width=3)
350
- draw.text((box[0], box[1]), f"Dog {i}", fill="yellow", font=font)
351
-
352
- if top1_prob >= 0.5:
353
- breed = topk_breeds[0]
354
- description = get_dog_description(breed)
355
- explanations.append(format_description(description, breed, is_multi_dog=True, dog_number=i))
 
 
 
 
 
 
 
 
 
 
 
 
 
356
  else:
357
- explanation = f"""
358
- Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
359
- 1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
360
- 2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
361
- 3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
362
- """
363
- explanations.append(explanation)
364
- for breed in topk_breeds:
365
- buttons.append(f"More about Dog {i}: {breed}")
366
 
367
- final_explanation = "\n\n---\n\n".join(explanations)
368
-
369
- if buttons:
370
- return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), gr.update(visible=False), gr.update(visible=False)
371
  else:
372
- return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
373
-
374
- else:
375
- # No dogs detected or only one dog (already processed)
376
- return "No additional dogs detected.", image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
377
 
378
  except Exception as e:
379
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
330
  return explanation, image, gr.update(visible=True, choices=breed_buttons), gr.update(visible=False), gr.update(visible=False)
331
 
332
  elif top1_prob < 0.2:
333
+ # Only use YOLO if the confidence is very low
334
+ dogs = await detect_multiple_dogs(image)
335
+
336
+ if len(dogs) > 1:
337
+ # Multiple dogs detected, process each one
338
+ explanations = []
339
+ buttons = []
340
+ annotated_image = image.copy()
341
+ draw = ImageDraw.Draw(annotated_image)
342
+ font = ImageFont.load_default()
343
+
344
+ for i, (cropped_image, _, box) in enumerate(dogs, 1):
345
+ top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
346
+
347
+ draw.rectangle(box, outline="red", width=3)
348
+ draw.text((box[0], box[1]), f"Dog {i}", fill="yellow", font=font)
349
+
350
+ if top1_prob >= 0.5:
351
+ breed = topk_breeds[0]
352
+ description = get_dog_description(breed)
353
+ explanations.append(format_description(description, breed, is_multi_dog=True, dog_number=i))
354
+ else:
355
+ explanation = f"""
356
+ Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
357
+ 1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
358
+ 2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
359
+ 3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
360
+ """
361
+ explanations.append(explanation)
362
+ for breed in topk_breeds:
363
+ buttons.append(f"More about Dog {i}: {breed}")
364
+
365
+ final_explanation = "\n\n---\n\n".join(explanations)
366
+
367
+ if buttons:
368
+ return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), gr.update(visible=False), gr.update(visible=False)
369
  else:
370
+ return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
 
 
 
 
 
 
 
371
 
 
 
 
 
372
  else:
373
+ # No dogs detected or only one dog with low confidence
374
+ return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
 
 
375
 
376
  except Exception as e:
377
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)