DawnC commited on
Commit
5f74c5e
·
verified ·
1 Parent(s): ecc483b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -19
app.py CHANGED
@@ -249,7 +249,6 @@ def get_akc_breeds_link():
249
  # if __name__ == "__main__":
250
  # iface.launch()
251
 
252
- # 格式化狗的品種描述函數
253
  def format_description(description, breed, is_multi_dog=False, dog_number=None):
254
  if isinstance(description, dict):
255
  formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items() if key != "Breed"])
@@ -302,7 +301,6 @@ async def detect_multiple_dogs(image):
302
  print(f"Error in detect_multiple_dogs: {e}")
303
  return []
304
 
305
- # 主預測函數
306
  async def predict(image):
307
  if image is None:
308
  return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
@@ -311,23 +309,34 @@ async def predict(image):
311
  if isinstance(image, np.ndarray):
312
  image = Image.fromarray(image)
313
 
314
- # 使用 YOLO 偵測有無多隻狗
315
- dogs = await detect_multiple_dogs(image)
316
 
317
- if len(dogs) == 1:
318
- # 如果只有一隻狗,直接預測品種,不使用 YOLO
319
- top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
320
  breed = topk_breeds[0]
321
  description = get_dog_description(breed)
322
  formatted_description = format_description(description, breed)
323
-
324
- if top1_prob < 0.2:
325
- 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)
326
-
327
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
328
 
329
- elif len(dogs) > 1:
330
- # 如果偵測到多隻狗,使用 YOLO 進行分割並預測
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
331
  explanations = []
332
  buttons = []
333
  annotated_image = image.copy()
@@ -343,7 +352,7 @@ async def predict(image):
343
  if top1_prob >= 0.5:
344
  breed = topk_breeds[0]
345
  description = get_dog_description(breed)
346
- explanations.append(f"Dog {i}: **{breed}**\n\n{format_description(description, breed, is_multi_dog=True, dog_number=i)}")
347
  else:
348
  explanation = f"""
349
  Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
@@ -363,19 +372,21 @@ Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
363
  return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
364
 
365
  else:
366
- # 未偵測到狗
367
- return "No dogs detected. Please upload a clear image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible(False))
368
 
369
  except Exception as e:
370
- return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible(False))
371
 
372
- # 顯示選擇的品種詳細信息
373
  async def show_details(choice):
374
  if not choice:
375
  return "Please select a breed to view details."
376
 
377
  try:
378
- _, breed = choice.split(": ", 1)
 
 
 
379
  description = get_dog_description(breed)
380
  return format_description(description, breed)
381
  except Exception as e:
 
249
  # if __name__ == "__main__":
250
  # iface.launch()
251
 
 
252
  def format_description(description, breed, is_multi_dog=False, dog_number=None):
253
  if isinstance(description, dict):
254
  formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items() if key != "Breed"])
 
301
  print(f"Error in detect_multiple_dogs: {e}")
302
  return []
303
 
 
304
  async def predict(image):
305
  if image is None:
306
  return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
309
  if isinstance(image, np.ndarray):
310
  image = Image.fromarray(image)
311
 
312
+ # First, try to predict without YOLO
313
+ top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
314
 
315
+ if top1_prob >= 0.5:
 
 
316
  breed = topk_breeds[0]
317
  description = get_dog_description(breed)
318
  formatted_description = format_description(description, breed)
 
 
 
 
319
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
320
 
321
+ elif top1_prob >= 0.2:
322
+ explanation = (
323
+ f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
324
+ f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
325
+ f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
326
+ f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
327
+ "Click on a button to view more information about the breed."
328
+ )
329
+ breed_buttons = [f"More about {breed}" for breed in topk_breeds[:3]]
330
+ return explanation, image, gr.update(visible=True, choices=breed_buttons), gr.update(visible=False), gr.update(visible=False)
331
+
332
+ elif top1_prob < 0.2:
333
+ 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)
334
+
335
+ # If confidence is low, use YOLO to check for multiple dogs
336
+ dogs = await detect_multiple_dogs(image)
337
+
338
+ if len(dogs) > 1:
339
+ # Multiple dogs detected, process each one
340
  explanations = []
341
  buttons = []
342
  annotated_image = image.copy()
 
352
  if top1_prob >= 0.5:
353
  breed = topk_breeds[0]
354
  description = get_dog_description(breed)
355
+ explanations.append(format_description(description, breed, is_multi_dog=True, dog_number=i))
356
  else:
357
  explanation = f"""
358
  Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
 
372
  return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
373
 
374
  else:
375
+ # No dogs detected or only one dog (already processed)
376
+ return "No additional dogs detected.", image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
377
 
378
  except Exception as e:
379
+ return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
380
 
 
381
  async def show_details(choice):
382
  if not choice:
383
  return "Please select a breed to view details."
384
 
385
  try:
386
+ if "Dog" in choice:
387
+ _, breed = choice.split(": ", 1)
388
+ else:
389
+ _, breed = choice.split("More about ", 1)
390
  description = get_dog_description(breed)
391
  return format_description(description, breed)
392
  except Exception as e: