Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -300,7 +300,7 @@ def _predict_single_dog(image):
|
|
| 300 |
# dogs.append((cropped_image, confidence, xyxy))
|
| 301 |
# return dogs
|
| 302 |
|
| 303 |
-
async def detect_multiple_dogs(image, conf_threshold=0.
|
| 304 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 305 |
dogs = []
|
| 306 |
for box in results.boxes:
|
|
@@ -432,10 +432,10 @@ async def predict(image):
|
|
| 432 |
if isinstance(image, np.ndarray):
|
| 433 |
image = Image.fromarray(image)
|
| 434 |
|
| 435 |
-
#
|
| 436 |
-
dogs = await detect_multiple_dogs(image, conf_threshold=0.
|
| 437 |
|
| 438 |
-
#
|
| 439 |
if len(dogs) <= 1:
|
| 440 |
return await process_single_dog(image)
|
| 441 |
|
|
@@ -473,9 +473,9 @@ async def predict(image):
|
|
| 473 |
buttons[0] if len(buttons) > 0 else gr.update(visible=False),
|
| 474 |
buttons[1] if len(buttons) > 1 else gr.update(visible=False),
|
| 475 |
buttons[2] if len(buttons) > 2 else gr.update(visible=False),
|
| 476 |
-
gr.update(visible=
|
| 477 |
else:
|
| 478 |
-
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=
|
| 479 |
|
| 480 |
except Exception as e:
|
| 481 |
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
@@ -490,7 +490,7 @@ async def process_single_dog(image):
|
|
| 490 |
|
| 491 |
if top1_prob >= 0.5:
|
| 492 |
formatted_description = format_description(description, breed)
|
| 493 |
-
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=
|
| 494 |
else:
|
| 495 |
explanation = (
|
| 496 |
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
|
@@ -534,10 +534,12 @@ with gr.Blocks() as iface:
|
|
| 534 |
|
| 535 |
back_button = gr.Button("Back", visible=False)
|
| 536 |
|
|
|
|
|
|
|
| 537 |
input_image.change(
|
| 538 |
predict,
|
| 539 |
inputs=input_image,
|
| 540 |
-
outputs=[output, output_image, btn1, btn2, btn3, back_button]
|
| 541 |
)
|
| 542 |
|
| 543 |
for btn in [btn1, btn2, btn3]:
|
|
@@ -548,8 +550,9 @@ with gr.Blocks() as iface:
|
|
| 548 |
)
|
| 549 |
|
| 550 |
back_button.click(
|
| 551 |
-
lambda: (
|
| 552 |
-
|
|
|
|
| 553 |
)
|
| 554 |
|
| 555 |
gr.Examples(
|
|
@@ -559,5 +562,6 @@ with gr.Blocks() as iface:
|
|
| 559 |
|
| 560 |
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>')
|
| 561 |
|
|
|
|
| 562 |
if __name__ == "__main__":
|
| 563 |
iface.launch()
|
|
|
|
| 300 |
# dogs.append((cropped_image, confidence, xyxy))
|
| 301 |
# return dogs
|
| 302 |
|
| 303 |
+
async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.5):
|
| 304 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 305 |
dogs = []
|
| 306 |
for box in results.boxes:
|
|
|
|
| 432 |
if isinstance(image, np.ndarray):
|
| 433 |
image = Image.fromarray(image)
|
| 434 |
|
| 435 |
+
# 嘗試檢測多隻狗,使用較低的閾值以提高檢測率
|
| 436 |
+
dogs = await detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.5)
|
| 437 |
|
| 438 |
+
# 如果只檢測到一隻或沒有檢測到狗,使用單狗處理邏輯
|
| 439 |
if len(dogs) <= 1:
|
| 440 |
return await process_single_dog(image)
|
| 441 |
|
|
|
|
| 473 |
buttons[0] if len(buttons) > 0 else gr.update(visible=False),
|
| 474 |
buttons[1] if len(buttons) > 1 else gr.update(visible=False),
|
| 475 |
buttons[2] if len(buttons) > 2 else gr.update(visible=False),
|
| 476 |
+
gr.update(visible=False))
|
| 477 |
else:
|
| 478 |
+
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 479 |
|
| 480 |
except Exception as e:
|
| 481 |
return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
|
|
|
| 490 |
|
| 491 |
if top1_prob >= 0.5:
|
| 492 |
formatted_description = format_description(description, breed)
|
| 493 |
+
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
| 494 |
else:
|
| 495 |
explanation = (
|
| 496 |
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
|
|
|
| 534 |
|
| 535 |
back_button = gr.Button("Back", visible=False)
|
| 536 |
|
| 537 |
+
initial_output = gr.State()
|
| 538 |
+
|
| 539 |
input_image.change(
|
| 540 |
predict,
|
| 541 |
inputs=input_image,
|
| 542 |
+
outputs=[output, output_image, btn1, btn2, btn3, back_button, initial_output]
|
| 543 |
)
|
| 544 |
|
| 545 |
for btn in [btn1, btn2, btn3]:
|
|
|
|
| 550 |
)
|
| 551 |
|
| 552 |
back_button.click(
|
| 553 |
+
lambda initial: (initial, gr.update(visible=False)),
|
| 554 |
+
inputs=[initial_output],
|
| 555 |
+
outputs=[output, back_button]
|
| 556 |
)
|
| 557 |
|
| 558 |
gr.Examples(
|
|
|
|
| 562 |
|
| 563 |
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>')
|
| 564 |
|
| 565 |
+
|
| 566 |
if __name__ == "__main__":
|
| 567 |
iface.launch()
|