DawnC commited on
Commit
a46a577
·
1 Parent(s): 0c6ed24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -14
app.py CHANGED
@@ -308,11 +308,10 @@ async def predict(image):
308
  if isinstance(image, np.ndarray):
309
  image = Image.fromarray(image)
310
 
311
- # 偵測圖片中的多個狗
312
- dogs = await detect_multiple_dogs(image)
313
-
314
- # 處理單一狗情況 (如果 YOLO 沒有檢測到多隻狗)
315
  if len(dogs) == 0:
 
316
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
317
  if top1_prob < 0.2:
318
  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)
@@ -321,7 +320,7 @@ async def predict(image):
321
  description = get_dog_description(breed)
322
  formatted_description = format_description(description, breed)
323
 
324
- # 處理高置信度與低置信度情況
325
  if top1_prob >= 0.5:
326
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
327
  else:
@@ -332,9 +331,9 @@ async def predict(image):
332
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
333
  "Click on a button to view more information about the breed."
334
  )
335
- return explanation, image, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
336
 
337
- # 多狗處理
338
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
339
  explanations = []
340
  visible_buttons = []
@@ -342,7 +341,7 @@ async def predict(image):
342
  draw = ImageDraw.Draw(annotated_image)
343
  font = ImageFont.load_default()
344
 
345
- # 遍歷每一隻狗
346
  for i, (cropped_image, _, box) in enumerate(dogs):
347
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
348
 
@@ -351,7 +350,7 @@ async def predict(image):
351
  draw.rectangle(box, outline=color, width=3)
352
  draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
353
 
354
- # 高置信度返回
355
  if top1_prob >= 0.5:
356
  breed = topk_breeds[0]
357
  description = get_dog_description(breed)
@@ -364,18 +363,18 @@ Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds
364
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
365
  """
366
  explanations.append(explanation)
367
- visible_buttons.extend([gr.update(visible=True, value=f"More about Dog {i+1}: {topk_breeds[0]}"),
368
- gr.update(visible=True, value=f"More about Dog {i+1}: {topk_breeds[1]}"),
369
- gr.update(visible=True, value=f"More about Dog {i+1}: {topk_breeds[2]}")])
370
  else:
371
  explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
372
 
373
  final_explanation = "\n\n".join(explanations)
374
 
375
- return final_explanation, annotated_image, gr.update(visible=True, choices=visible_buttons)
376
 
377
  except Exception as e:
378
- return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
 
379
 
380
  async def show_details(choice):
381
  if not choice:
 
308
  if isinstance(image, np.ndarray):
309
  image = Image.fromarray(image)
310
 
311
+ # 確認只有在多狗的情況下才會執行 YOLO 偵測邏輯
312
+ dogs = await detect_multiple_dogs(image) # 多狗偵測
 
 
313
  if len(dogs) == 0:
314
+ # 單狗情境
315
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
316
  if top1_prob < 0.2:
317
  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)
 
320
  description = get_dog_description(breed)
321
  formatted_description = format_description(description, breed)
322
 
323
+ # 如果置信度高於 0.5,返回結果
324
  if top1_prob >= 0.5:
325
  return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
326
  else:
 
331
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
332
  "Click on a button to view more information about the breed."
333
  )
334
+ return explanation, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
335
 
336
+ # 如果有多隻狗,使用 YOLO 檢測邏輯處理
337
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
338
  explanations = []
339
  visible_buttons = []
 
341
  draw = ImageDraw.Draw(annotated_image)
342
  font = ImageFont.load_default()
343
 
344
+ # 對每一隻狗進行處理
345
  for i, (cropped_image, _, box) in enumerate(dogs):
346
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
347
 
 
350
  draw.rectangle(box, outline=color, width=3)
351
  draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
352
 
353
+ # 如果置信度高於 0.5
354
  if top1_prob >= 0.5:
355
  breed = topk_breeds[0]
356
  description = get_dog_description(breed)
 
363
  3. **{topk_breeds[2]}** ({topk_probs_percent[2]})
364
  """
365
  explanations.append(explanation)
366
+ visible_buttons.extend([f"More about Dog {i+1}: {topk_breeds[0]}", f"More about Dog {i+1}: {topk_breeds[1]}", f"More about Dog {i+1}: {topk_breeds[2]}"])
 
 
367
  else:
368
  explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
369
 
370
  final_explanation = "\n\n".join(explanations)
371
 
372
+ return final_explanation, annotated_image, gr.update(visible=True, choices=visible_buttons), gr.update(visible=False), gr.update(visible=False)
373
 
374
  except Exception as e:
375
+ # 更具體的錯誤訊息
376
+ return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
377
+
378
 
379
  async def show_details(choice):
380
  if not choice: