DawnC commited on
Commit
ecc483b
1 Parent(s): 3fa059c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -33
app.py CHANGED
@@ -311,49 +311,63 @@ 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) == 0:
318
- 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)
319
-
320
- explanations = []
321
- buttons = []
322
- annotated_image = image.copy()
323
- draw = ImageDraw.Draw(annotated_image)
324
- font = ImageFont.load_default()
325
-
326
- for i, (cropped_image, _, box) in enumerate(dogs, 1):
327
- top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
328
-
329
- # 標註框框標籤更醒目
330
- draw.rectangle(box, outline="red", width=3)
331
- draw.text((box[0], box[1]), f"Dog {i}", fill="yellow", font=font)
332
-
333
- if top1_prob >= 0.5:
334
- breed = topk_breeds[0]
335
- description = get_dog_description(breed)
336
- explanations.append(f"Dog {i}: **{breed}**\n\n{format_description(description, breed, is_multi_dog=True, dog_number=i)}")
337
- else:
338
- explanation = f"""
339
  Dog {i}: Detected with moderate confidence. Here are the top 3 possible breeds:
340
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
341
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
342
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
343
  """
344
- explanations.append(explanation)
345
- for breed in topk_breeds:
346
- buttons.append(f"More about Dog {i}: {breed}")
 
 
 
 
 
 
 
347
 
348
- final_explanation = "\n\n---\n\n".join(explanations)
349
-
350
- if buttons:
351
- return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), gr.update(visible=False), gr.update(visible=False)
352
  else:
353
- return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
354
-
 
355
  except Exception as e:
356
- return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
357
 
358
  # 顯示選擇的品種詳細信息
359
  async def show_details(choice):
 
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()
334
+ draw = ImageDraw.Draw(annotated_image)
335
+ font = ImageFont.load_default()
336
+
337
+ for i, (cropped_image, _, box) in enumerate(dogs, 1):
338
+ top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
339
+
340
+ draw.rectangle(box, outline="red", width=3)
341
+ draw.text((box[0], box[1]), f"Dog {i}", fill="yellow", font=font)
342
+
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:
350
  1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
351
  2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
352
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
353
  """
354
+ explanations.append(explanation)
355
+ for breed in topk_breeds:
356
+ buttons.append(f"More about Dog {i}: {breed}")
357
+
358
+ final_explanation = "\n\n---\n\n".join(explanations)
359
+
360
+ if buttons:
361
+ return final_explanation, annotated_image, gr.update(visible=True, choices=buttons), gr.update(visible=False), gr.update(visible=False)
362
+ else:
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):