Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -504,8 +504,54 @@ async def predict(image):
|
|
| 504 |
elif len(dogs) == 1:
|
| 505 |
return await process_single_dog(dogs[0][0])
|
| 506 |
else:
|
| 507 |
-
|
| 508 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 509 |
|
| 510 |
except Exception as e:
|
| 511 |
error_msg = f"An error occurred: {str(e)}"
|
|
|
|
| 504 |
elif len(dogs) == 1:
|
| 505 |
return await process_single_dog(dogs[0][0])
|
| 506 |
else:
|
| 507 |
+
# ๅค็ๆ
ๅข
|
| 508 |
+
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 509 |
+
explanations = []
|
| 510 |
+
buttons = []
|
| 511 |
+
annotated_image = image.copy()
|
| 512 |
+
draw = ImageDraw.Draw(annotated_image)
|
| 513 |
+
font = ImageFont.load_default()
|
| 514 |
+
|
| 515 |
+
for i, (cropped_image, confidence, box) in enumerate(dogs):
|
| 516 |
+
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
| 517 |
+
color = color_list[i % len(color_list)]
|
| 518 |
+
draw.rectangle(box, outline=color, width=3)
|
| 519 |
+
draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
| 520 |
+
|
| 521 |
+
breed = topk_breeds[0]
|
| 522 |
+
if top1_prob >= 0.5:
|
| 523 |
+
description = get_dog_description(breed)
|
| 524 |
+
formatted_description = format_description(description, breed)
|
| 525 |
+
explanations.append(f"Dog {i+1}: {formatted_description}")
|
| 526 |
+
elif top1_prob >= 0.2:
|
| 527 |
+
dog_explanation = f"Dog {i+1}: Top 3 possible breeds:\n"
|
| 528 |
+
dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
|
| 529 |
+
explanations.append(dog_explanation)
|
| 530 |
+
buttons.extend([gr.update(visible=True, value=f"Dog {i+1}: More about {breed}") for breed in topk_breeds[:3]])
|
| 531 |
+
else:
|
| 532 |
+
explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
| 533 |
+
|
| 534 |
+
final_explanation = "\n\n".join(explanations)
|
| 535 |
+
if buttons:
|
| 536 |
+
final_explanation += "\n\nClick on a button to view more information about the breed."
|
| 537 |
+
initial_state = {
|
| 538 |
+
"explanation": final_explanation,
|
| 539 |
+
"buttons": buttons,
|
| 540 |
+
"show_back": True
|
| 541 |
+
}
|
| 542 |
+
return (final_explanation, annotated_image,
|
| 543 |
+
buttons[0] if len(buttons) > 0 else gr.update(visible=False),
|
| 544 |
+
buttons[1] if len(buttons) > 1 else gr.update(visible=False),
|
| 545 |
+
buttons[2] if len(buttons) > 2 else gr.update(visible=False),
|
| 546 |
+
gr.update(visible=True),
|
| 547 |
+
initial_state)
|
| 548 |
+
else:
|
| 549 |
+
initial_state = {
|
| 550 |
+
"explanation": final_explanation,
|
| 551 |
+
"buttons": [],
|
| 552 |
+
"show_back": False
|
| 553 |
+
}
|
| 554 |
+
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 555 |
|
| 556 |
except Exception as e:
|
| 557 |
error_msg = f"An error occurred: {str(e)}"
|