Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -243,25 +243,30 @@ 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.
|
247 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
248 |
dogs = []
|
249 |
boxes = []
|
250 |
confidences = []
|
251 |
|
|
|
|
|
|
|
252 |
for box in results.boxes:
|
253 |
if box.cls == 16: # COCO 數據集中狗的類別是 16
|
254 |
xyxy = box.xyxy[0].tolist()
|
255 |
-
|
256 |
-
|
257 |
-
|
|
|
|
|
258 |
|
259 |
if boxes:
|
260 |
boxes = torch.stack(boxes)
|
261 |
confidences = torch.tensor(confidences)
|
262 |
|
263 |
-
#
|
264 |
-
keep =
|
265 |
|
266 |
for i in keep:
|
267 |
xyxy = boxes[i].tolist()
|
@@ -293,9 +298,32 @@ async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4, me
|
|
293 |
merged_image = image.crop(merged_box.tolist())
|
294 |
merged_dogs.append((merged_image, merged_confidence, merged_box.tolist()))
|
295 |
|
|
|
|
|
|
|
|
|
296 |
return merged_dogs
|
297 |
|
298 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
299 |
|
300 |
async def predict(image):
|
301 |
if image is None:
|
|
|
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.1, iou_threshold=0.5, merge_threshold=0.2):
|
247 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
248 |
dogs = []
|
249 |
boxes = []
|
250 |
confidences = []
|
251 |
|
252 |
+
image_area = image.width * image.height
|
253 |
+
min_area_ratio = 0.005 # 最小檢測面積佔整個圖像的比例
|
254 |
+
|
255 |
for box in results.boxes:
|
256 |
if box.cls == 16: # COCO 數據集中狗的類別是 16
|
257 |
xyxy = box.xyxy[0].tolist()
|
258 |
+
area = (xyxy[2] - xyxy[0]) * (xyxy[3] - xyxy[1])
|
259 |
+
if area / image_area >= min_area_ratio:
|
260 |
+
confidence = box.conf.item()
|
261 |
+
boxes.append(torch.tensor(xyxy))
|
262 |
+
confidences.append(confidence)
|
263 |
|
264 |
if boxes:
|
265 |
boxes = torch.stack(boxes)
|
266 |
confidences = torch.tensor(confidences)
|
267 |
|
268 |
+
# 應用軟 NMS
|
269 |
+
keep = soft_nms(boxes, confidences, iou_threshold=iou_threshold, sigma=0.5)
|
270 |
|
271 |
for i in keep:
|
272 |
xyxy = boxes[i].tolist()
|
|
|
298 |
merged_image = image.crop(merged_box.tolist())
|
299 |
merged_dogs.append((merged_image, merged_confidence, merged_box.tolist()))
|
300 |
|
301 |
+
# 後處理:限制檢測到的狗狗數量
|
302 |
+
if len(merged_dogs) > 5:
|
303 |
+
merged_dogs = sorted(merged_dogs, key=lambda x: x[1], reverse=True)[:5]
|
304 |
+
|
305 |
return merged_dogs
|
306 |
|
307 |
+
# 如果沒有檢測到狗狗,使用備用分類器
|
308 |
+
return await fallback_classifier(image)
|
309 |
+
|
310 |
+
async def fallback_classifier(image):
|
311 |
+
# 使用預訓練的 ResNet 或其他適合的分類器
|
312 |
+
transform = transforms.Compose([
|
313 |
+
transforms.Resize((224, 224)),
|
314 |
+
transforms.ToTensor(),
|
315 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
316 |
+
])
|
317 |
+
img_tensor = transform(image).unsqueeze(0)
|
318 |
+
|
319 |
+
with torch.no_grad():
|
320 |
+
output = fallback_model(img_tensor)
|
321 |
+
confidence, predicted = torch.max(output, 1)
|
322 |
+
|
323 |
+
if confidence.item() > 0.5: # 設置一個合適的閾值
|
324 |
+
return [(image, confidence.item(), [0, 0, image.width, image.height])]
|
325 |
+
else:
|
326 |
+
return []
|
327 |
|
328 |
async def predict(image):
|
329 |
if image is None:
|