Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -176,21 +176,61 @@ async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4):
|
|
| 176 |
dogs.append((cropped_image, confidence, xyxy))
|
| 177 |
return dogs
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
async def predict(image):
|
| 181 |
if image is None:
|
| 182 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
|
| 183 |
-
|
| 184 |
try:
|
| 185 |
if isinstance(image, np.ndarray):
|
| 186 |
image = Image.fromarray(image)
|
| 187 |
-
|
| 188 |
dogs = await detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4)
|
| 189 |
|
| 190 |
if len(dogs) <= 1:
|
| 191 |
return await process_single_dog(image)
|
| 192 |
|
| 193 |
-
# 多狗情境
|
| 194 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 195 |
explanations = []
|
| 196 |
buttons = []
|
|
@@ -223,7 +263,8 @@ async def predict(image):
|
|
| 223 |
initial_state = {
|
| 224 |
"explanation": final_explanation,
|
| 225 |
"buttons": buttons,
|
| 226 |
-
"show_back": True
|
|
|
|
| 227 |
}
|
| 228 |
return (final_explanation, annotated_image,
|
| 229 |
buttons[0] if len(buttons) > 0 else gr.update(visible=False),
|
|
@@ -235,10 +276,10 @@ async def predict(image):
|
|
| 235 |
initial_state = {
|
| 236 |
"explanation": final_explanation,
|
| 237 |
"buttons": [],
|
| 238 |
-
"show_back": False
|
|
|
|
| 239 |
}
|
| 240 |
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 241 |
-
|
| 242 |
except Exception as e:
|
| 243 |
error_msg = f"An error occurred: {str(e)}"
|
| 244 |
print(error_msg) # 添加日誌輸出
|
|
@@ -397,47 +438,6 @@ async def predict(image):
|
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
-
async def process_single_dog(image):
|
| 401 |
-
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
| 402 |
-
if top1_prob < 0.2:
|
| 403 |
-
initial_state = {
|
| 404 |
-
"explanation": "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.",
|
| 405 |
-
"buttons": [],
|
| 406 |
-
"show_back": False
|
| 407 |
-
}
|
| 408 |
-
return initial_state["explanation"], None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 409 |
-
|
| 410 |
-
breed = topk_breeds[0]
|
| 411 |
-
description = get_dog_description(breed)
|
| 412 |
-
|
| 413 |
-
if top1_prob >= 0.5:
|
| 414 |
-
formatted_description = format_description(description, breed)
|
| 415 |
-
initial_state = {
|
| 416 |
-
"explanation": formatted_description,
|
| 417 |
-
"buttons": [],
|
| 418 |
-
"show_back": False
|
| 419 |
-
}
|
| 420 |
-
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 421 |
-
else:
|
| 422 |
-
explanation = (
|
| 423 |
-
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
| 424 |
-
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
| 425 |
-
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
| 426 |
-
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
| 427 |
-
"Click on a button to view more information about the breed."
|
| 428 |
-
)
|
| 429 |
-
buttons = [
|
| 430 |
-
gr.update(visible=True, value=f"More about {topk_breeds[0]}"),
|
| 431 |
-
gr.update(visible=True, value=f"More about {topk_breeds[1]}"),
|
| 432 |
-
gr.update(visible=True, value=f"More about {topk_breeds[2]}")
|
| 433 |
-
]
|
| 434 |
-
initial_state = {
|
| 435 |
-
"explanation": explanation,
|
| 436 |
-
"buttons": buttons,
|
| 437 |
-
"show_back": True
|
| 438 |
-
}
|
| 439 |
-
return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
|
| 440 |
-
|
| 441 |
def show_details(choice, previous_output, initial_state):
|
| 442 |
if not choice:
|
| 443 |
return previous_output, gr.update(visible=True), initial_state
|
|
@@ -446,13 +446,25 @@ def show_details(choice, previous_output, initial_state):
|
|
| 446 |
breed = choice.split("More about ")[-1]
|
| 447 |
description = get_dog_description(breed)
|
| 448 |
formatted_description = format_description(description, breed)
|
|
|
|
| 449 |
return formatted_description, gr.update(visible=True), initial_state
|
| 450 |
except Exception as e:
|
| 451 |
error_msg = f"An error occurred while showing details: {e}"
|
| 452 |
print(error_msg) # 添加日誌輸出
|
| 453 |
return error_msg, gr.update(visible=True), initial_state
|
| 454 |
|
| 455 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 456 |
with gr.Blocks() as iface:
|
| 457 |
gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
| 458 |
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>")
|
|
@@ -486,12 +498,9 @@ with gr.Blocks() as iface:
|
|
| 486 |
)
|
| 487 |
|
| 488 |
back_button.click(
|
| 489 |
-
|
| 490 |
-
state["buttons"][0] if len(state["buttons"]) > 0 else gr.update(visible=False),
|
| 491 |
-
state["buttons"][1] if len(state["buttons"]) > 1 else gr.update(visible=False),
|
| 492 |
-
gr.update(visible=state["show_back"])),
|
| 493 |
inputs=[initial_state],
|
| 494 |
-
outputs=[output, btn1, btn2, btn3, back_button]
|
| 495 |
)
|
| 496 |
|
| 497 |
gr.Examples(
|
|
@@ -501,5 +510,5 @@ with gr.Blocks() as iface:
|
|
| 501 |
|
| 502 |
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>')
|
| 503 |
|
| 504 |
-
|
| 505 |
-
|
|
|
|
| 176 |
dogs.append((cropped_image, confidence, xyxy))
|
| 177 |
return dogs
|
| 178 |
|
| 179 |
+
async def process_single_dog(image):
|
| 180 |
+
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
| 181 |
+
if top1_prob < 0.2:
|
| 182 |
+
initial_state = {
|
| 183 |
+
"explanation": "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.",
|
| 184 |
+
"buttons": [],
|
| 185 |
+
"show_back": False,
|
| 186 |
+
"image": None
|
| 187 |
+
}
|
| 188 |
+
return initial_state["explanation"], None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 189 |
+
|
| 190 |
+
breed = topk_breeds[0]
|
| 191 |
+
description = get_dog_description(breed)
|
| 192 |
+
|
| 193 |
+
if top1_prob >= 0.5:
|
| 194 |
+
formatted_description = format_description(description, breed)
|
| 195 |
+
initial_state = {
|
| 196 |
+
"explanation": formatted_description,
|
| 197 |
+
"buttons": [],
|
| 198 |
+
"show_back": False,
|
| 199 |
+
"image": image
|
| 200 |
+
}
|
| 201 |
+
return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
| 202 |
+
else:
|
| 203 |
+
explanation = (
|
| 204 |
+
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
| 205 |
+
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
| 206 |
+
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
| 207 |
+
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
| 208 |
+
"Click on a button to view more information about the breed."
|
| 209 |
+
)
|
| 210 |
+
buttons = [
|
| 211 |
+
gr.update(visible=True, value=f"More about {topk_breeds[0]}"),
|
| 212 |
+
gr.update(visible=True, value=f"More about {topk_breeds[1]}"),
|
| 213 |
+
gr.update(visible=True, value=f"More about {topk_breeds[2]}")
|
| 214 |
+
]
|
| 215 |
+
initial_state = {
|
| 216 |
+
"explanation": explanation,
|
| 217 |
+
"buttons": buttons,
|
| 218 |
+
"show_back": True,
|
| 219 |
+
"image": image
|
| 220 |
+
}
|
| 221 |
+
return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
|
| 222 |
|
| 223 |
async def predict(image):
|
| 224 |
if image is None:
|
| 225 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
|
|
|
|
| 226 |
try:
|
| 227 |
if isinstance(image, np.ndarray):
|
| 228 |
image = Image.fromarray(image)
|
|
|
|
| 229 |
dogs = await detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4)
|
| 230 |
|
| 231 |
if len(dogs) <= 1:
|
| 232 |
return await process_single_dog(image)
|
| 233 |
|
|
|
|
| 234 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 235 |
explanations = []
|
| 236 |
buttons = []
|
|
|
|
| 263 |
initial_state = {
|
| 264 |
"explanation": final_explanation,
|
| 265 |
"buttons": buttons,
|
| 266 |
+
"show_back": True,
|
| 267 |
+
"image": annotated_image
|
| 268 |
}
|
| 269 |
return (final_explanation, annotated_image,
|
| 270 |
buttons[0] if len(buttons) > 0 else gr.update(visible=False),
|
|
|
|
| 276 |
initial_state = {
|
| 277 |
"explanation": final_explanation,
|
| 278 |
"buttons": [],
|
| 279 |
+
"show_back": False,
|
| 280 |
+
"image": annotated_image
|
| 281 |
}
|
| 282 |
return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
|
|
|
|
| 283 |
except Exception as e:
|
| 284 |
error_msg = f"An error occurred: {str(e)}"
|
| 285 |
print(error_msg) # 添加日誌輸出
|
|
|
|
| 438 |
|
| 439 |
|
| 440 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
def show_details(choice, previous_output, initial_state):
|
| 442 |
if not choice:
|
| 443 |
return previous_output, gr.update(visible=True), initial_state
|
|
|
|
| 446 |
breed = choice.split("More about ")[-1]
|
| 447 |
description = get_dog_description(breed)
|
| 448 |
formatted_description = format_description(description, breed)
|
| 449 |
+
initial_state["explanation"] = formatted_description
|
| 450 |
return formatted_description, gr.update(visible=True), initial_state
|
| 451 |
except Exception as e:
|
| 452 |
error_msg = f"An error occurred while showing details: {e}"
|
| 453 |
print(error_msg) # 添加日誌輸出
|
| 454 |
return error_msg, gr.update(visible=True), initial_state
|
| 455 |
|
| 456 |
+
def go_back(state):
|
| 457 |
+
buttons = state.get("buttons", [])
|
| 458 |
+
return (
|
| 459 |
+
state["explanation"],
|
| 460 |
+
state.get("image", None),
|
| 461 |
+
buttons[0] if len(buttons) > 0 else gr.update(visible=False),
|
| 462 |
+
buttons[1] if len(buttons) > 1 else gr.update(visible=False),
|
| 463 |
+
buttons[2] if len(buttons) > 2 else gr.update(visible=False),
|
| 464 |
+
gr.update(visible=state["show_back"]),
|
| 465 |
+
state
|
| 466 |
+
)
|
| 467 |
+
|
| 468 |
with gr.Blocks() as iface:
|
| 469 |
gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
| 470 |
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>")
|
|
|
|
| 498 |
)
|
| 499 |
|
| 500 |
back_button.click(
|
| 501 |
+
go_back,
|
|
|
|
|
|
|
|
|
|
| 502 |
inputs=[initial_state],
|
| 503 |
+
outputs=[output, output_image, btn1, btn2, btn3, back_button, initial_state]
|
| 504 |
)
|
| 505 |
|
| 506 |
gr.Examples(
|
|
|
|
| 510 |
|
| 511 |
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>')
|
| 512 |
|
| 513 |
+
if __name__ == "__main__":
|
| 514 |
+
iface.launch()
|