DawnC commited on
Commit
31492de
·
verified ·
1 Parent(s): 9d8bbd3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -21
app.py CHANGED
@@ -283,21 +283,32 @@ def _predict_single_dog(image):
283
  topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
284
  return top1_prob, topk_breeds, topk_probs_percent
285
 
286
- async def detect_multiple_dogs(image, conf_threshold=0.3):
287
- # 調整 YOLO 模型的置信度閾值
288
- return await asyncio.to_thread(_detect_multiple_dogs, image, conf_threshold)
289
-
290
- def _detect_multiple_dogs(image, conf_threshold):
291
- results = model_yolo(image, conf=conf_threshold)
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  dogs = []
293
- for result in results:
294
- for box in result.boxes:
295
- if box.cls == 16: # COCO 資料集中狗的類別是 16
296
- xyxy = box.xyxy[0].tolist()
297
- confidence = box.conf.item()
298
- if confidence >= conf_threshold: # 只保留置信度高於閾值的框
299
- cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
300
- dogs.append((cropped_image, confidence, xyxy))
301
  return dogs
302
 
303
 
@@ -421,10 +432,11 @@ async def predict(image):
421
  if isinstance(image, np.ndarray):
422
  image = Image.fromarray(image)
423
 
424
- # 嘗試檢測多隻狗,進一步降低閾值以提高檢測率
425
- dogs = await detect_multiple_dogs(image, conf_threshold=0.1)
426
- if len(dogs) == 0:
427
- # 單狗情境
 
428
  return await process_single_dog(image)
429
 
430
  # 多狗情境
@@ -461,9 +473,9 @@ async def predict(image):
461
  buttons[0] if len(buttons) > 0 else gr.update(visible=False),
462
  buttons[1] if len(buttons) > 1 else gr.update(visible=False),
463
  buttons[2] if len(buttons) > 2 else gr.update(visible=False),
464
- gr.update(visible=False))
465
  else:
466
- return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
467
 
468
  except Exception as e:
469
  return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
@@ -495,7 +507,7 @@ async def process_single_dog(image):
495
 
496
  def show_details(choice, previous_output):
497
  if not choice:
498
- return previous_output, gr.update(visible=False)
499
 
500
  try:
501
  breed = choice.split("More about ")[-1]
 
283
  topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
284
  return top1_prob, topk_breeds, topk_probs_percent
285
 
286
+ # async def detect_multiple_dogs(image, conf_threshold=0.3):
287
+ # # 調整 YOLO 模型的置信度閾值
288
+ # return await asyncio.to_thread(_detect_multiple_dogs, image, conf_threshold)
289
+
290
+ # def _detect_multiple_dogs(image, conf_threshold):
291
+ # results = model_yolo(image, conf=conf_threshold)
292
+ # dogs = []
293
+ # for result in results:
294
+ # for box in result.boxes:
295
+ # if box.cls == 16: # COCO 資料集中狗的類別是 16
296
+ # xyxy = box.xyxy[0].tolist()
297
+ # confidence = box.conf.item()
298
+ # if confidence >= conf_threshold: # 只保留置信度高於閾值的框
299
+ # cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
300
+ # dogs.append((cropped_image, confidence, xyxy))
301
+ # return dogs
302
+
303
+ async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.5):
304
+ results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
305
  dogs = []
306
+ for box in results.boxes:
307
+ if box.cls == 16: # COCO 資料集中狗的類別是 16
308
+ xyxy = box.xyxy[0].tolist()
309
+ confidence = box.conf.item()
310
+ cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
311
+ dogs.append((cropped_image, confidence, xyxy))
 
 
312
  return dogs
313
 
314
 
 
432
  if isinstance(image, np.ndarray):
433
  image = Image.fromarray(image)
434
 
435
+ # 嘗試檢測多隻狗,使用較高的閾值以避免錯誤檢測
436
+ dogs = await detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.5)
437
+
438
+ # 如果只檢測到一隻狗,使用單狗處理邏輯
439
+ if len(dogs) <= 1:
440
  return await process_single_dog(image)
441
 
442
  # 多狗情境
 
473
  buttons[0] if len(buttons) > 0 else gr.update(visible=False),
474
  buttons[1] if len(buttons) > 1 else gr.update(visible=False),
475
  buttons[2] if len(buttons) > 2 else gr.update(visible=False),
476
+ gr.update(visible=True))
477
  else:
478
+ return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
479
 
480
  except Exception as e:
481
  return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
507
 
508
  def show_details(choice, previous_output):
509
  if not choice:
510
+ return previous_output, gr.update(visible=True)
511
 
512
  try:
513
  breed = choice.split("More about ")[-1]