DawnC commited on
Commit
71f3489
1 Parent(s): 6f8b9bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -32
app.py CHANGED
@@ -308,45 +308,74 @@ async def predict(image):
308
  if isinstance(image, np.ndarray):
309
  image = Image.fromarray(image)
310
 
311
- # Always start with single dog prediction
312
- top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
313
 
314
- if top1_prob >= 0.5:
315
- breed = topk_breeds[0]
316
- description = get_dog_description(breed)
317
- formatted_description = format_description(description, breed)
318
- return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
319
-
320
- elif top1_prob >= 0.2:
321
- explanation = (
322
- f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
323
- f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
324
- f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
325
- f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
326
- "Click on a button to view more information about the breed."
327
- )
328
- breed_buttons = [f"More about {breed}" for breed in topk_breeds[:3]]
329
- return explanation, image, gr.update(visible=True, choices=breed_buttons), gr.update(visible=False), gr.update(visible=False)
330
-
331
- else: # top1_prob < 0.2
332
- 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)
333
 
334
  except Exception as e:
335
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
336
 
337
- async def show_details(choice):
338
- if not choice:
339
- return "Please select a breed to view details."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
340
 
341
- try:
342
- if "Dog" in choice:
343
- _, breed = choice.split(": ", 1)
 
 
 
 
 
 
 
 
 
 
344
  else:
345
- _, breed = choice.split("More about ", 1)
346
- description = get_dog_description(breed)
347
- return format_description(description, breed)
348
- except Exception as e:
349
- return f"An error occurred while showing details: {e}"
 
 
 
 
 
 
 
 
 
 
 
350
 
351
  # Gradio 介面設置
352
  with gr.Blocks(css="""
 
308
  if isinstance(image, np.ndarray):
309
  image = Image.fromarray(image)
310
 
311
+ # First, use YOLO to detect multiple dogs
312
+ dogs = await detect_multiple_dogs(image)
313
 
314
+ if len(dogs) <= 1:
315
+ # Single dog or no dog detected, use the original method
316
+ top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
317
+ return process_single_dog_result(top1_prob, topk_breeds, topk_probs_percent, image)
318
+ else:
319
+ # Multiple dogs detected
320
+ return process_multiple_dogs_result(dogs, image)
 
 
 
 
 
 
 
 
 
 
 
 
321
 
322
  except Exception as e:
323
  return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
324
 
325
+ async def process_single_dog_result(top1_prob, topk_breeds, topk_probs_percent, image):
326
+ if top1_prob >= 0.5:
327
+ breed = topk_breeds[0]
328
+ description = get_dog_description(breed)
329
+ formatted_description = format_description(description, breed)
330
+ return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
331
+ elif top1_prob >= 0.2:
332
+ explanation = (
333
+ f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
334
+ f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
335
+ f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
336
+ f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
337
+ "Click on a button to view more information about the breed."
338
+ )
339
+ breed_buttons = [f"More about {breed}" for breed in topk_breeds[:3]]
340
+ return explanation, image, gr.update(visible=True, choices=breed_buttons), gr.update(visible=False), gr.update(visible=False)
341
+ else:
342
+ 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)
343
+
344
+ async def process_multiple_dogs_result(dogs, image):
345
+ annotated_image = image.copy()
346
+ draw = ImageDraw.Draw(annotated_image)
347
+ font = ImageFont.load_default()
348
 
349
+ explanations = []
350
+ buttons = []
351
+
352
+ for i, (cropped_image, _, box) in enumerate(dogs, 1):
353
+ top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
354
+
355
+ draw.rectangle(box, outline="red", width=3)
356
+ draw.text((box[0], box[1]), f"Dog {i}", fill="yellow", font=font)
357
+
358
+ if top1_prob >= 0.5:
359
+ breed = topk_breeds[0]
360
+ description = get_dog_description(breed)
361
+ explanations.append(format_description(description, breed, is_multi_dog=True, dog_number=i))
362
  else:
363
+ explanation = f"""
364
+ Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
365
+ 1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
366
+ 2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
367
+ 3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
368
+ """
369
+ explanations.append(explanation)
370
+ for breed in topk_breeds:
371
+ buttons.append(f"More about Dog {i}: {breed}")
372
+
373
+ final_explanation = "\n\n---\n\n".join(explanations)
374
+
375
+ if buttons:
376
+ return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), gr.update(visible=False), gr.update(visible=False)
377
+ else:
378
+ return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
379
 
380
  # Gradio 介面設置
381
  with gr.Blocks(css="""