DawnC commited on
Commit
cdaf020
1 Parent(s): 499f204

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -18
app.py CHANGED
@@ -302,7 +302,7 @@ async def detect_multiple_dogs(image):
302
 
303
  async def predict(image):
304
  if image is None:
305
- return "Please upload an image to start.", None
306
 
307
  try:
308
  if isinstance(image, np.ndarray):
@@ -311,9 +311,11 @@ async def predict(image):
311
  dogs = await detect_multiple_dogs(image)
312
 
313
  if len(dogs) == 0:
314
- return "No dogs detected. Please upload a clear image of a dog.", None
315
 
316
  explanations = []
 
 
317
  annotated_image = image.copy()
318
  draw = ImageDraw.Draw(annotated_image)
319
  try:
@@ -336,6 +338,7 @@ async def predict(image):
336
  breed = topk_breeds[0]
337
  description = get_dog_description(breed)
338
  explanation = f"**Dog {i}: {breed}**\n\n{format_description(description, breed)}"
 
339
  else:
340
  explanation = f"""
341
  **Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:**
@@ -343,26 +346,22 @@ async def predict(image):
343
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
344
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
345
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
346
-
347
- [More about {topk_breeds[0]}](#breed_{i}_1)
348
- [More about {topk_breeds[1]}](#breed_{i}_2)
349
- [More about {topk_breeds[2]}](#breed_{i}_3)
350
  """
351
-
352
- explanations.append(explanation)
 
 
 
 
353
 
354
  final_explanation = "\n\n---\n\n".join(explanations)
355
-
356
- # 添加隱藏的詳細信息部分
357
- for i, (_, _, _) in enumerate(dogs, 1):
358
- for j, breed in enumerate(topk_breeds, 1):
359
- description = get_dog_description(breed)
360
- final_explanation += f"\n\n<div id='breed_{i}_{j}' style='display:none;'>\n\n{format_description(description, breed)}\n\n</div>"
361
-
362
- return final_explanation, annotated_image
363
 
364
  except Exception as e:
365
- return f"An error occurred: {e}", None
 
 
 
366
 
367
  # Gradio 界面設置
368
  with gr.Blocks(css="""
@@ -381,8 +380,20 @@ with gr.Blocks(css="""
381
  output_image = gr.Image(label="Annotated Image")
382
 
383
  output = gr.Markdown(label="Prediction Results")
 
 
 
 
 
 
 
 
384
 
385
- input_image.change(predict, inputs=input_image, outputs=[output, output_image])
 
 
 
 
386
 
387
  gr.Examples(
388
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
 
302
 
303
  async def predict(image):
304
  if image is None:
305
+ return "Please upload an image to start.", None, [], []
306
 
307
  try:
308
  if isinstance(image, np.ndarray):
 
311
  dogs = await detect_multiple_dogs(image)
312
 
313
  if len(dogs) == 0:
314
+ return "No dogs detected. Please upload a clear image of a dog.", None, [], []
315
 
316
  explanations = []
317
+ buttons = []
318
+ button_details = []
319
  annotated_image = image.copy()
320
  draw = ImageDraw.Draw(annotated_image)
321
  try:
 
338
  breed = topk_breeds[0]
339
  description = get_dog_description(breed)
340
  explanation = f"**Dog {i}: {breed}**\n\n{format_description(description, breed)}"
341
+ explanations.append(explanation)
342
  else:
343
  explanation = f"""
344
  **Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:**
 
346
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
347
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
348
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
 
 
 
 
349
  """
350
+ explanations.append(explanation)
351
+ for j, breed in enumerate(topk_breeds, 1):
352
+ button_text = f"More about Dog {i}: {breed}"
353
+ buttons.append(button_text)
354
+ description = get_dog_description(breed)
355
+ button_details.append(format_description(description, breed))
356
 
357
  final_explanation = "\n\n---\n\n".join(explanations)
358
+ return final_explanation, annotated_image, buttons, button_details
 
 
 
 
 
 
 
359
 
360
  except Exception as e:
361
+ return f"An error occurred: {e}", None, [], []
362
+
363
+ def show_breed_details(evt: gr.SelectData, details):
364
+ return details[evt.index]
365
 
366
  # Gradio 界面設置
367
  with gr.Blocks(css="""
 
380
  output_image = gr.Image(label="Annotated Image")
381
 
382
  output = gr.Markdown(label="Prediction Results")
383
+ breed_buttons = gr.Radio([], label="Select breed for more details", visible=False)
384
+ breed_details = gr.Markdown(label="Breed Details")
385
+
386
+ input_image.change(
387
+ predict,
388
+ inputs=input_image,
389
+ outputs=[output, output_image, breed_buttons, breed_details]
390
+ )
391
 
392
+ breed_buttons.select(
393
+ show_breed_details,
394
+ inputs=[breed_details],
395
+ outputs=[breed_details]
396
+ )
397
 
398
  gr.Examples(
399
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],