DawnC commited on
Commit
96854cb
·
1 Parent(s): 25f1bec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -415,14 +415,14 @@ def _detect_multiple_dogs(image, conf_threshold):
415
 
416
  async def predict(image):
417
  if image is None:
418
- return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
419
 
420
  try:
421
  if isinstance(image, np.ndarray):
422
  image = Image.fromarray(image)
423
 
424
- # 嘗試檢測多隻狗,降低閾值以提高檢測率
425
- dogs = await detect_multiple_dogs(image, conf_threshold=0.2)
426
  if len(dogs) == 0:
427
  # 單狗情境
428
  return await process_single_dog(image)
@@ -460,24 +460,25 @@ async def predict(image):
460
  return (final_explanation, annotated_image,
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
  else:
465
- return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
466
 
467
  except Exception as e:
468
- return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
469
 
470
  async def process_single_dog(image):
471
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
472
  if top1_prob < 0.2:
473
- 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)
474
 
475
  breed = topk_breeds[0]
476
  description = get_dog_description(breed)
477
 
478
  if top1_prob >= 0.5:
479
  formatted_description = format_description(description, breed)
480
- return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
481
  else:
482
  explanation = (
483
  f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
@@ -489,19 +490,19 @@ async def process_single_dog(image):
489
  return (explanation, image,
490
  gr.update(visible=True, value=f"More about {topk_breeds[0]}"),
491
  gr.update(visible=True, value=f"More about {topk_breeds[1]}"),
492
- gr.update(visible=True, value=f"More about {topk_breeds[2]}"))
 
493
 
494
-
495
- def show_details(choice):
496
  if not choice:
497
- return "Please select a breed to view details."
498
 
499
  try:
500
  breed = choice.split("More about ")[-1]
501
  description = get_dog_description(breed)
502
- return format_description(description, breed)
503
  except Exception as e:
504
- return f"An error occurred while showing details: {e}"
505
 
506
  # 介面部分
507
  with gr.Blocks() as iface:
@@ -518,16 +519,26 @@ with gr.Blocks() as iface:
518
  btn1 = gr.Button("View More 1", visible=False)
519
  btn2 = gr.Button("View More 2", visible=False)
520
  btn3 = gr.Button("View More 3", visible=False)
521
-
 
 
522
  input_image.change(
523
  predict,
524
  inputs=input_image,
525
- outputs=[output, output_image, btn1, btn2, btn3]
526
  )
527
 
528
- btn1.click(show_details, inputs=btn1, outputs=output)
529
- btn2.click(show_details, inputs=btn2, outputs=output)
530
- btn3.click(show_details, inputs=btn3, outputs=output)
 
 
 
 
 
 
 
 
531
 
532
  gr.Examples(
533
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
 
415
 
416
  async def predict(image):
417
  if image is None:
418
+ return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
419
 
420
  try:
421
  if isinstance(image, np.ndarray):
422
  image = Image.fromarray(image)
423
 
424
+ # 嘗試檢測多隻狗,進一步降低閾值以提高檢測率
425
+ dogs = await detect_multiple_dogs(image, conf_threshold=0.1)
426
  if len(dogs) == 0:
427
  # 單狗情境
428
  return await process_single_dog(image)
 
460
  return (final_explanation, annotated_image,
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=False))
465
  else:
466
+ return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
467
 
468
  except Exception as e:
469
+ return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
470
 
471
  async def process_single_dog(image):
472
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
473
  if top1_prob < 0.2:
474
+ 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)
475
 
476
  breed = topk_breeds[0]
477
  description = get_dog_description(breed)
478
 
479
  if top1_prob >= 0.5:
480
  formatted_description = format_description(description, breed)
481
+ return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
482
  else:
483
  explanation = (
484
  f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
 
490
  return (explanation, image,
491
  gr.update(visible=True, value=f"More about {topk_breeds[0]}"),
492
  gr.update(visible=True, value=f"More about {topk_breeds[1]}"),
493
+ gr.update(visible=True, value=f"More about {topk_breeds[2]}"),
494
+ gr.update(visible=True))
495
 
496
+ def show_details(choice, previous_output):
 
497
  if not choice:
498
+ return previous_output, gr.update(visible=False)
499
 
500
  try:
501
  breed = choice.split("More about ")[-1]
502
  description = get_dog_description(breed)
503
+ return format_description(description, breed), gr.update(visible=True)
504
  except Exception as e:
505
+ return f"An error occurred while showing details: {e}", gr.update(visible=True)
506
 
507
  # 介面部分
508
  with gr.Blocks() as iface:
 
519
  btn1 = gr.Button("View More 1", visible=False)
520
  btn2 = gr.Button("View More 2", visible=False)
521
  btn3 = gr.Button("View More 3", visible=False)
522
+
523
+ back_button = gr.Button("Back", visible=False)
524
+
525
  input_image.change(
526
  predict,
527
  inputs=input_image,
528
+ outputs=[output, output_image, btn1, btn2, btn3, back_button]
529
  )
530
 
531
+ for btn in [btn1, btn2, btn3]:
532
+ btn.click(
533
+ show_details,
534
+ inputs=[btn, output],
535
+ outputs=[output, back_button]
536
+ )
537
+
538
+ back_button.click(
539
+ lambda: (gr.update(value=""), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False)),
540
+ outputs=[output, btn1, btn2, btn3, back_button]
541
+ )
542
 
543
  gr.Examples(
544
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],