DawnC commited on
Commit
614e5ca
1 Parent(s): 772eb5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -29
app.py CHANGED
@@ -243,12 +243,13 @@ def _predict_single_dog(image):
243
  # print(error_msg) # 添加日誌輸出
244
  # return error_msg, None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
245
 
246
- async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.4, merge_threshold=0.3):
 
247
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
248
  dogs = []
249
 
250
  image_area = image.width * image.height
251
- min_area_ratio = 0.01 # 最小檢測面積佔整個圖像的比例
252
 
253
  for box in results.boxes:
254
  if box.cls == 16: # COCO 數據集中狗的類別是 16
@@ -256,34 +257,32 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.4, mer
256
  area = (xyxy[2] - xyxy[0]) * (xyxy[3] - xyxy[1])
257
  if area / image_area >= min_area_ratio:
258
  confidence = box.conf.item()
259
- cropped_image = image.crop((xyxy[0], xyxy[1], xyxy[2], xyxy[3]))
260
- dogs.append((cropped_image, confidence, xyxy))
261
 
262
- # 合併重疊的框
263
- merged_dogs = []
264
- while dogs:
265
- base_dog = dogs.pop(0)
266
- base_box = torch.tensor(base_dog[2])
267
- to_merge = [base_dog]
268
 
269
- i = 0
270
- while i < len(dogs):
271
- compare_box = torch.tensor(dogs[i][2])
272
- iou = box_iou(base_box.unsqueeze(0), compare_box.unsqueeze(0)).item()
273
- if iou > merge_threshold:
274
- to_merge.append(dogs.pop(i))
275
- else:
276
- i += 1
 
 
 
 
 
277
 
278
- if len(to_merge) == 1:
279
- merged_dogs.append(base_dog)
280
- else:
281
- merged_box = torch.tensor([dog[2] for dog in to_merge]).mean(0)
282
- merged_confidence = max(dog[1] for dog in to_merge)
283
- merged_image = image.crop(merged_box.tolist())
284
- merged_dogs.append((merged_image, merged_confidence, merged_box.tolist()))
285
 
286
- return merged_dogs if merged_dogs else [(image, 1.0, [0, 0, image.width, image.height])]
 
287
 
288
  async def predict(image):
289
  if image is None:
@@ -295,10 +294,15 @@ async def predict(image):
295
 
296
  dogs = await detect_multiple_dogs(image)
297
 
298
- if len(dogs) == 1:
299
- return await process_single_dog(dogs[0][0])
 
 
 
 
 
300
 
301
- # 多狗情境
302
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
303
  explanations = []
304
  buttons = []
 
243
  # print(error_msg) # 添加日誌輸出
244
  # return error_msg, None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
245
 
246
+
247
+ async def detect_multiple_dogs(image, conf_threshold=0.1, iou_threshold=0.3, merge_threshold=0.5):
248
  results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
249
  dogs = []
250
 
251
  image_area = image.width * image.height
252
+ min_area_ratio = 0.005 # 降低最小面積比例以捕獲更多小型狗狗
253
 
254
  for box in results.boxes:
255
  if box.cls == 16: # COCO 數據集中狗的類別是 16
 
257
  area = (xyxy[2] - xyxy[0]) * (xyxy[3] - xyxy[1])
258
  if area / image_area >= min_area_ratio:
259
  confidence = box.conf.item()
260
+ dogs.append((xyxy, confidence))
 
261
 
262
+ # 使用 NMS 進行後處理
263
+ if dogs:
264
+ boxes = torch.tensor([dog[0] for dog in dogs])
265
+ scores = torch.tensor([dog[1] for dog in dogs])
266
+ keep = nms(boxes, scores, iou_threshold)
 
267
 
268
+ merged_dogs = []
269
+ for i in keep:
270
+ xyxy = boxes[i].tolist()
271
+ confidence = scores[i].item()
272
+ # 擴大邊界框以包含更多上下文
273
+ expanded_xyxy = [
274
+ max(0, xyxy[0] - 20),
275
+ max(0, xyxy[1] - 20),
276
+ min(image.width, xyxy[2] + 20),
277
+ min(image.height, xyxy[3] + 20)
278
+ ]
279
+ cropped_image = image.crop(expanded_xyxy)
280
+ merged_dogs.append((cropped_image, confidence, expanded_xyxy))
281
 
282
+ return merged_dogs
 
 
 
 
 
 
283
 
284
+ # 如果沒有檢測到狗狗,返回整張圖片
285
+ return [(image, 1.0, [0, 0, image.width, image.height])]
286
 
287
  async def predict(image):
288
  if image is None:
 
294
 
295
  dogs = await detect_multiple_dogs(image)
296
 
297
+ # 如果沒有檢測到狗狗或只檢測到一隻,使用整張圖像進行分類
298
+ if len(dogs) <= 1:
299
+ top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
300
+ if top1_prob >= 0.5:
301
+ return await process_single_dog(image)
302
+ else:
303
+ dogs = [(image, 1.0, [0, 0, image.width, image.height])]
304
 
305
+ # 多狗情境處理保持不變
306
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
307
  explanations = []
308
  buttons = []