DawnC commited on
Commit
cf21437
·
verified ·
1 Parent(s): a69ec49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -22
app.py CHANGED
@@ -426,17 +426,15 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.5):
426
 
427
  async def predict(image):
428
  if image is None:
429
- return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
430
 
431
  try:
432
  if isinstance(image, np.ndarray):
433
  image = Image.fromarray(image)
434
 
435
- # 嘗試檢測多隻狗,進一步降低閾值以提高檢測率
436
- dogs = await detect_multiple_dogs(image, conf_threshold=0.05) # 降低閾值以檢測更多狗
437
 
438
  if len(dogs) <= 1:
439
- # 單狗情境
440
  return await process_single_dog(image)
441
 
442
  # 多狗情境
@@ -469,28 +467,51 @@ async def predict(image):
469
  final_explanation = "\n\n".join(explanations)
470
  if buttons:
471
  final_explanation += "\n\nClick on a button to view more information about the breed."
 
 
 
 
 
472
  return (final_explanation, annotated_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=True)) # 顯示 back 按鈕
 
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)
 
 
482
 
483
  async def process_single_dog(image):
484
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
485
  if top1_prob < 0.2:
486
- 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), gr.update(visible=False)
 
 
 
 
 
487
 
488
  breed = topk_breeds[0]
489
  description = get_dog_description(breed)
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"
@@ -499,22 +520,31 @@ async def process_single_dog(image):
499
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
500
  "Click on a button to view more information about the breed."
501
  )
502
- return (explanation, image,
503
- gr.update(visible=True, value=f"More about {topk_breeds[0]}"),
504
- gr.update(visible=True, value=f"More about {topk_breeds[1]}"),
505
- gr.update(visible=True, value=f"More about {topk_breeds[2]}"),
506
- gr.update(visible=True)) # 顯示 back 按鈕
507
-
508
- def show_details(choice, previous_output):
 
 
 
 
 
 
509
  if not choice:
510
- return previous_output, gr.update(visible=True)
511
 
512
  try:
513
  breed = choice.split("More about ")[-1]
514
  description = get_dog_description(breed)
515
- return format_description(description, breed), gr.update(visible=True)
 
516
  except Exception as e:
517
- return f"An error occurred while showing details: {e}", gr.update(visible=True)
 
 
518
 
519
  # 介面部分
520
  with gr.Blocks() as iface:
@@ -545,12 +575,16 @@ with gr.Blocks() as iface:
545
  for btn in [btn1, btn2, btn3]:
546
  btn.click(
547
  show_details,
548
- inputs=[btn, output],
549
- outputs=[output, back_button]
550
  )
551
 
552
  back_button.click(
553
- lambda x: x,
 
 
 
 
554
  inputs=[initial_state],
555
  outputs=[output, btn1, btn2, btn3, back_button]
556
  )
@@ -562,5 +596,6 @@ with gr.Blocks() as iface:
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
  if __name__ == "__main__":
566
  iface.launch()
 
426
 
427
  async def predict(image):
428
  if image is None:
429
+ 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
430
 
431
  try:
432
  if isinstance(image, np.ndarray):
433
  image = Image.fromarray(image)
434
 
435
+ dogs = await detect_multiple_dogs(image, conf_threshold=0.05)
 
436
 
437
  if len(dogs) <= 1:
 
438
  return await process_single_dog(image)
439
 
440
  # 多狗情境
 
467
  final_explanation = "\n\n".join(explanations)
468
  if buttons:
469
  final_explanation += "\n\nClick on a button to view more information about the breed."
470
+ initial_state = {
471
+ "explanation": final_explanation,
472
+ "buttons": buttons,
473
+ "show_back": True
474
+ }
475
  return (final_explanation, annotated_image,
476
  buttons[0] if len(buttons) > 0 else gr.update(visible=False),
477
  buttons[1] if len(buttons) > 1 else gr.update(visible=False),
478
  buttons[2] if len(buttons) > 2 else gr.update(visible=False),
479
+ gr.update(visible=True),
480
+ initial_state)
481
  else:
482
+ initial_state = {
483
+ "explanation": final_explanation,
484
+ "buttons": [],
485
+ "show_back": False
486
+ }
487
+ return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
488
 
489
  except Exception as e:
490
+ error_msg = f"An error occurred: {str(e)}"
491
+ print(error_msg) # 添加日誌輸出
492
+ return error_msg, None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
493
 
494
  async def process_single_dog(image):
495
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
496
  if top1_prob < 0.2:
497
+ initial_state = {
498
+ "explanation": "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.",
499
+ "buttons": [],
500
+ "show_back": False
501
+ }
502
+ return initial_state["explanation"], None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
503
 
504
  breed = topk_breeds[0]
505
  description = get_dog_description(breed)
506
 
507
  if top1_prob >= 0.5:
508
  formatted_description = format_description(description, breed)
509
+ initial_state = {
510
+ "explanation": formatted_description,
511
+ "buttons": [],
512
+ "show_back": False
513
+ }
514
+ return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
515
  else:
516
  explanation = (
517
  f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
 
520
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
521
  "Click on a button to view more information about the breed."
522
  )
523
+ buttons = [
524
+ gr.update(visible=True, value=f"More about {topk_breeds[0]}"),
525
+ gr.update(visible=True, value=f"More about {topk_breeds[1]}"),
526
+ gr.update(visible=True, value=f"More about {topk_breeds[2]}")
527
+ ]
528
+ initial_state = {
529
+ "explanation": explanation,
530
+ "buttons": buttons,
531
+ "show_back": True
532
+ }
533
+ return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
534
+
535
+ def show_details(choice, previous_output, initial_state):
536
  if not choice:
537
+ return previous_output, gr.update(visible=True), initial_state
538
 
539
  try:
540
  breed = choice.split("More about ")[-1]
541
  description = get_dog_description(breed)
542
+ formatted_description = format_description(description, breed)
543
+ return formatted_description, gr.update(visible=True), initial_state
544
  except Exception as e:
545
+ error_msg = f"An error occurred while showing details: {e}"
546
+ print(error_msg) # 添加日誌輸出
547
+ return error_msg, gr.update(visible=True), initial_state
548
 
549
  # 介面部分
550
  with gr.Blocks() as iface:
 
575
  for btn in [btn1, btn2, btn3]:
576
  btn.click(
577
  show_details,
578
+ inputs=[btn, output, initial_state],
579
+ outputs=[output, back_button, initial_state]
580
  )
581
 
582
  back_button.click(
583
+ lambda state: (state["explanation"],
584
+ state["buttons"][0] if len(state["buttons"]) > 0 else gr.update(visible=False),
585
+ state["buttons"][1] if len(state["buttons"]) > 1 else gr.update(visible=False),
586
+ state["buttons"][2] if len(state["buttons"]) > 2 else gr.update(visible=False),
587
+ gr.update(visible=state["show_back"])),
588
  inputs=[initial_state],
589
  outputs=[output, btn1, btn2, btn3, back_button]
590
  )
 
596
 
597
  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>')
598
 
599
+
600
  if __name__ == "__main__":
601
  iface.launch()