DawnC commited on
Commit
3b33804
1 Parent(s): 22931be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -16
app.py CHANGED
@@ -300,6 +300,7 @@ def _detect_multiple_dogs(image, conf_threshold):
300
  dogs.append((cropped_image, confidence, xyxy))
301
  return dogs
302
 
 
303
  async def predict(image):
304
  if image is None:
305
  return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
@@ -333,7 +334,7 @@ async def predict(image):
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,41 +342,26 @@ async def predict(image):
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
-
348
- # 繪製方框
349
  color = color_list[i % len(color_list)]
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
- # 高置信度返回
354
  if top1_prob >= 0.5:
355
  breed = topk_breeds[0]
356
  description = get_dog_description(breed)
357
  explanations.append(f"Dog {i+1}:\n{format_description(description, breed)}")
358
- elif 0.2 <= top1_prob < 0.5:
359
- explanation = f"""
360
- Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds:
361
- 1. **{topk_breeds[0]}** ({topk_probs_percent[0]})
362
- 2. **{topk_breeds[1]}** ({topk_probs_percent[1]})
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
  return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
376
 
377
 
378
-
379
  async def show_details(choice):
380
  if not choice:
381
  return "Please select a breed to view details."
 
300
  dogs.append((cropped_image, confidence, xyxy))
301
  return dogs
302
 
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)
 
334
  )
335
  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]}")
336
 
337
+ # 多狗情境 - 使用 YOLO 檢測並處理多狗
338
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
339
  explanations = []
340
  visible_buttons = []
 
342
  draw = ImageDraw.Draw(annotated_image)
343
  font = ImageFont.load_default()
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
  color = color_list[i % len(color_list)]
348
  draw.rectangle(box, outline=color, width=3)
349
  draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
350
 
 
351
  if top1_prob >= 0.5:
352
  breed = topk_breeds[0]
353
  description = get_dog_description(breed)
354
  explanations.append(f"Dog {i+1}:\n{format_description(description, breed)}")
 
 
 
 
 
 
 
 
 
355
  else:
356
  explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
357
 
358
  final_explanation = "\n\n".join(explanations)
 
359
  return final_explanation, annotated_image, gr.update(visible=True, choices=visible_buttons), gr.update(visible=False), gr.update(visible=False)
360
 
361
  except Exception as e:
362
  return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
363
 
364
 
 
365
  async def show_details(choice):
366
  if not choice:
367
  return "Please select a breed to view details."