Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -377,9 +377,44 @@ def _detect_multiple_dogs(image, conf_threshold):
|
|
| 377 |
# return f"An error occurred while showing details: {e}"
|
| 378 |
|
| 379 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 380 |
async def predict(image):
|
| 381 |
if image is None:
|
| 382 |
-
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False)
|
| 383 |
|
| 384 |
try:
|
| 385 |
if isinstance(image, np.ndarray):
|
|
@@ -388,8 +423,8 @@ async def predict(image):
|
|
| 388 |
# ๅ่ฉฆๆชขๆธฌๅค้ป็
|
| 389 |
dogs = await detect_multiple_dogs(image)
|
| 390 |
if len(dogs) == 0:
|
| 391 |
-
# ๅฎ็ๆ
ๅข
|
| 392 |
-
return process_single_dog(image)
|
| 393 |
|
| 394 |
# ๅค็ๆ
ๅข
|
| 395 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
|
@@ -416,29 +451,29 @@ async def predict(image):
|
|
| 416 |
dog_explanation = f"Dog {i+1}: The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n"
|
| 417 |
dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
|
| 418 |
explanations.append(dog_explanation)
|
| 419 |
-
choices.extend([f"Dog {i+1}:
|
| 420 |
|
| 421 |
final_explanation = "\n\n".join(explanations)
|
| 422 |
if choices:
|
| 423 |
final_explanation += "\n\nClick on a button to view more information about the breed."
|
| 424 |
-
return final_explanation, annotated_image, gr.update(visible=True, choices=choices), gr.update(visible=False)
|
| 425 |
else:
|
| 426 |
-
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False)
|
| 427 |
|
| 428 |
except Exception as e:
|
| 429 |
-
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False)
|
| 430 |
|
| 431 |
-
def process_single_dog(image):
|
| 432 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
| 433 |
if top1_prob < 0.2:
|
| 434 |
-
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)
|
| 435 |
|
| 436 |
breed = topk_breeds[0]
|
| 437 |
description = get_dog_description(breed)
|
| 438 |
|
| 439 |
if top1_prob >= 0.5:
|
| 440 |
formatted_description = format_description(description, breed)
|
| 441 |
-
return formatted_description, image, gr.update(visible=False), gr.update(visible=False)
|
| 442 |
else:
|
| 443 |
explanation = (
|
| 444 |
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
|
@@ -447,22 +482,21 @@ def process_single_dog(image):
|
|
| 447 |
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
| 448 |
"Click on a button to view more information about the breed."
|
| 449 |
)
|
| 450 |
-
choices = [f"
|
| 451 |
-
return explanation, image, gr.update(visible=True, choices=choices), gr.update(visible=False)
|
| 452 |
|
| 453 |
def show_details(choice):
|
| 454 |
if not choice:
|
| 455 |
return "Please select a breed to view details."
|
| 456 |
|
| 457 |
try:
|
| 458 |
-
breed = choice.split("
|
| 459 |
description = get_dog_description(breed)
|
| 460 |
return format_description(description, breed)
|
| 461 |
except Exception as e:
|
| 462 |
return f"An error occurred while showing details: {e}"
|
| 463 |
|
| 464 |
-
|
| 465 |
-
|
| 466 |
with gr.Blocks() as iface:
|
| 467 |
gr.HTML("<h1 style='text-align: center;'>๐ถ Dog Breed Classifier ๐</h1>")
|
| 468 |
gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
|
|
@@ -472,21 +506,20 @@ with gr.Blocks() as iface:
|
|
| 472 |
output_image = gr.Image(label="Annotated Image")
|
| 473 |
|
| 474 |
output = gr.Markdown(label="Prediction Results")
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
btn1 = gr.Button("View More 1", visible=False)
|
| 478 |
-
btn2 = gr.Button("View More 2", visible=False)
|
| 479 |
-
btn3 = gr.Button("View More 3", visible=False)
|
| 480 |
|
| 481 |
input_image.change(
|
| 482 |
predict,
|
| 483 |
inputs=input_image,
|
| 484 |
-
outputs=[output, output_image,
|
| 485 |
)
|
| 486 |
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
|
|
|
|
|
|
| 490 |
|
| 491 |
gr.Examples(
|
| 492 |
examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
|
|
|
|
| 377 |
# return f"An error occurred while showing details: {e}"
|
| 378 |
|
| 379 |
|
| 380 |
+
# with gr.Blocks() as iface:
|
| 381 |
+
# gr.HTML("<h1 style='text-align: center;'>๐ถ Dog Breed Classifier ๐</h1>")
|
| 382 |
+
# gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
|
| 383 |
+
|
| 384 |
+
# with gr.Row():
|
| 385 |
+
# input_image = gr.Image(label="Upload a dog image", type="pil")
|
| 386 |
+
# output_image = gr.Image(label="Annotated Image")
|
| 387 |
+
|
| 388 |
+
# output = gr.Markdown(label="Prediction Results")
|
| 389 |
+
|
| 390 |
+
# with gr.Row():
|
| 391 |
+
# btn1 = gr.Button("View More 1", visible=False)
|
| 392 |
+
# btn2 = gr.Button("View More 2", visible=False)
|
| 393 |
+
# btn3 = gr.Button("View More 3", visible=False)
|
| 394 |
+
|
| 395 |
+
# input_image.change(
|
| 396 |
+
# predict,
|
| 397 |
+
# inputs=input_image,
|
| 398 |
+
# outputs=[output, output_image, btn1, btn2, btn3]
|
| 399 |
+
# )
|
| 400 |
+
|
| 401 |
+
# btn1.click(show_details, inputs=btn1, outputs=output)
|
| 402 |
+
# btn2.click(show_details, inputs=btn2, outputs=output)
|
| 403 |
+
# btn3.click(show_details, inputs=btn3, outputs=output)
|
| 404 |
+
|
| 405 |
+
# gr.Examples(
|
| 406 |
+
# examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
|
| 407 |
+
# inputs=input_image
|
| 408 |
+
# )
|
| 409 |
+
|
| 410 |
+
# gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
|
| 411 |
+
|
| 412 |
+
# if __name__ == "__main__":
|
| 413 |
+
# iface.launch()
|
| 414 |
+
|
| 415 |
async def predict(image):
|
| 416 |
if image is None:
|
| 417 |
+
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False)
|
| 418 |
|
| 419 |
try:
|
| 420 |
if isinstance(image, np.ndarray):
|
|
|
|
| 423 |
# ๅ่ฉฆๆชขๆธฌๅค้ป็
|
| 424 |
dogs = await detect_multiple_dogs(image)
|
| 425 |
if len(dogs) == 0:
|
| 426 |
+
# ๅฎ็ๆ
ๅข
|
| 427 |
+
return await process_single_dog(image)
|
| 428 |
|
| 429 |
# ๅค็ๆ
ๅข
|
| 430 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
|
|
|
| 451 |
dog_explanation = f"Dog {i+1}: The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n"
|
| 452 |
dog_explanation += "\n".join([f"{j+1}. **{breed}** ({prob} confidence)" for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3]))])
|
| 453 |
explanations.append(dog_explanation)
|
| 454 |
+
choices.extend([f"Dog {i+1}: {breed}" for breed in topk_breeds[:3]])
|
| 455 |
|
| 456 |
final_explanation = "\n\n".join(explanations)
|
| 457 |
if choices:
|
| 458 |
final_explanation += "\n\nClick on a button to view more information about the breed."
|
| 459 |
+
return final_explanation, annotated_image, gr.update(visible=True, choices=choices), gr.update(visible=False)
|
| 460 |
else:
|
| 461 |
+
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False)
|
| 462 |
|
| 463 |
except Exception as e:
|
| 464 |
+
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False)
|
| 465 |
|
| 466 |
+
async def process_single_dog(image):
|
| 467 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
| 468 |
if top1_prob < 0.2:
|
| 469 |
+
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)
|
| 470 |
|
| 471 |
breed = topk_breeds[0]
|
| 472 |
description = get_dog_description(breed)
|
| 473 |
|
| 474 |
if top1_prob >= 0.5:
|
| 475 |
formatted_description = format_description(description, breed)
|
| 476 |
+
return formatted_description, image, gr.update(visible=False), gr.update(visible=False)
|
| 477 |
else:
|
| 478 |
explanation = (
|
| 479 |
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
|
|
|
| 482 |
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
| 483 |
"Click on a button to view more information about the breed."
|
| 484 |
)
|
| 485 |
+
choices = [f"{breed}" for breed in topk_breeds[:3]]
|
| 486 |
+
return explanation, image, gr.update(visible=True, choices=choices), gr.update(visible=False)
|
| 487 |
|
| 488 |
def show_details(choice):
|
| 489 |
if not choice:
|
| 490 |
return "Please select a breed to view details."
|
| 491 |
|
| 492 |
try:
|
| 493 |
+
breed = choice.split(": ")[-1] # ่็ๅฏ่ฝ็ "Dog X: " ๅ็ถด
|
| 494 |
description = get_dog_description(breed)
|
| 495 |
return format_description(description, breed)
|
| 496 |
except Exception as e:
|
| 497 |
return f"An error occurred while showing details: {e}"
|
| 498 |
|
| 499 |
+
# ไป้ข้จๅ
|
|
|
|
| 500 |
with gr.Blocks() as iface:
|
| 501 |
gr.HTML("<h1 style='text-align: center;'>๐ถ Dog Breed Classifier ๐</h1>")
|
| 502 |
gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
|
|
|
|
| 506 |
output_image = gr.Image(label="Annotated Image")
|
| 507 |
|
| 508 |
output = gr.Markdown(label="Prediction Results")
|
| 509 |
+
breed_choices = gr.Radio([], label="Select breed for more details", visible=False)
|
| 510 |
+
breed_details = gr.Markdown(label="Breed Details")
|
|
|
|
|
|
|
|
|
|
| 511 |
|
| 512 |
input_image.change(
|
| 513 |
predict,
|
| 514 |
inputs=input_image,
|
| 515 |
+
outputs=[output, output_image, breed_choices, breed_details]
|
| 516 |
)
|
| 517 |
|
| 518 |
+
breed_choices.change(
|
| 519 |
+
show_details,
|
| 520 |
+
inputs=breed_choices,
|
| 521 |
+
outputs=breed_details
|
| 522 |
+
)
|
| 523 |
|
| 524 |
gr.Examples(
|
| 525 |
examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
|