Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -308,13 +308,16 @@ async def predict(image):
|
|
308 |
if isinstance(image, np.ndarray):
|
309 |
image = Image.fromarray(image)
|
310 |
|
311 |
-
#
|
312 |
dogs = await detect_multiple_dogs(image)
|
313 |
|
314 |
-
if len(dogs)
|
315 |
-
|
316 |
-
|
317 |
-
|
|
|
|
|
|
|
318 |
else:
|
319 |
# Multiple dogs detected
|
320 |
return await process_multiple_dogs_result(dogs, image)
|
@@ -322,25 +325,6 @@ async def predict(image):
|
|
322 |
except Exception as e:
|
323 |
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
324 |
|
325 |
-
async def process_single_dog_result(top1_prob, topk_breeds, topk_probs_percent, image):
|
326 |
-
if top1_prob >= 0.5:
|
327 |
-
breed = topk_breeds[0]
|
328 |
-
description = get_dog_description(breed)
|
329 |
-
formatted_description = format_description(description, breed)
|
330 |
-
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
331 |
-
elif top1_prob >= 0.2:
|
332 |
-
explanation = (
|
333 |
-
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
334 |
-
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
335 |
-
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
336 |
-
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
337 |
-
"Click on a button to view more information about the breed."
|
338 |
-
)
|
339 |
-
breed_buttons = [f"More about {breed}" for breed in topk_breeds[:3]]
|
340 |
-
return explanation, image, gr.update(visible=True, choices=breed_buttons), gr.update(visible=False), gr.update(visible=False)
|
341 |
-
else:
|
342 |
-
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)
|
343 |
-
|
344 |
async def process_multiple_dogs_result(dogs, image):
|
345 |
annotated_image = image.copy()
|
346 |
draw = ImageDraw.Draw(annotated_image)
|
@@ -358,7 +342,7 @@ async def process_multiple_dogs_result(dogs, image):
|
|
358 |
if top1_prob >= 0.5:
|
359 |
breed = topk_breeds[0]
|
360 |
description = get_dog_description(breed)
|
361 |
-
explanations.append(format_description(description, breed, is_multi_dog=True, dog_number=i))
|
362 |
else:
|
363 |
explanation = f"""
|
364 |
Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
|
@@ -377,6 +361,30 @@ Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
|
|
377 |
else:
|
378 |
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
379 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
380 |
async def show_details(choice):
|
381 |
if not choice:
|
382 |
return "Please select a breed to view details."
|
|
|
308 |
if isinstance(image, np.ndarray):
|
309 |
image = Image.fromarray(image)
|
310 |
|
311 |
+
# Always use YOLO to detect dogs
|
312 |
dogs = await detect_multiple_dogs(image)
|
313 |
|
314 |
+
if len(dogs) == 0:
|
315 |
+
return "No dogs detected in the image. Please upload a clear image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
316 |
+
elif len(dogs) == 1:
|
317 |
+
# Single dog detected
|
318 |
+
cropped_image, _, _ = dogs[0]
|
319 |
+
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
320 |
+
return await process_single_dog_result(top1_prob, topk_breeds, topk_probs_percent, image, dogs[0][2])
|
321 |
else:
|
322 |
# Multiple dogs detected
|
323 |
return await process_multiple_dogs_result(dogs, image)
|
|
|
325 |
except Exception as e:
|
326 |
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
327 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
328 |
async def process_multiple_dogs_result(dogs, image):
|
329 |
annotated_image = image.copy()
|
330 |
draw = ImageDraw.Draw(annotated_image)
|
|
|
342 |
if top1_prob >= 0.5:
|
343 |
breed = topk_breeds[0]
|
344 |
description = get_dog_description(breed)
|
345 |
+
explanations.append(f"Dog {i}: **{breed}** ({top1_prob*100:.2f}% confidence)\n\n{format_description(description, breed, is_multi_dog=True, dog_number=i)}")
|
346 |
else:
|
347 |
explanation = f"""
|
348 |
Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
|
|
|
361 |
else:
|
362 |
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
363 |
|
364 |
+
async def process_single_dog_result(top1_prob, topk_breeds, topk_probs_percent, image, box):
|
365 |
+
annotated_image = image.copy()
|
366 |
+
draw = ImageDraw.Draw(annotated_image)
|
367 |
+
draw.rectangle(box, outline="red", width=3)
|
368 |
+
draw.text((box[0], box[1]), "Dog", fill="yellow", font=ImageFont.load_default())
|
369 |
+
|
370 |
+
if top1_prob >= 0.5:
|
371 |
+
breed = topk_breeds[0]
|
372 |
+
description = get_dog_description(breed)
|
373 |
+
formatted_description = format_description(description, breed)
|
374 |
+
return formatted_description, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
375 |
+
elif top1_prob >= 0.2:
|
376 |
+
explanation = (
|
377 |
+
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
378 |
+
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
379 |
+
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
380 |
+
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
381 |
+
"Click on a button to view more information about the breed."
|
382 |
+
)
|
383 |
+
breed_buttons = [f"More about {breed}" for breed in topk_breeds[:3]]
|
384 |
+
return explanation, annotated_image, gr.update(visible=True, choices=breed_buttons), gr.update(visible=False), gr.update(visible=False)
|
385 |
+
else:
|
386 |
+
return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
387 |
+
|
388 |
async def show_details(choice):
|
389 |
if not choice:
|
390 |
return "Please select a breed to view details."
|