Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
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 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
356 |
else:
|
357 |
-
|
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 |
-
|
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)
|