Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -185,10 +185,63 @@ async def predict_single_dog(image):
|
|
185 |
return probabilities[0], breeds[:3], relative_probs
|
186 |
|
187 |
|
188 |
-
async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
190 |
dogs = []
|
191 |
boxes = []
|
|
|
|
|
192 |
for box in results.boxes:
|
193 |
if box.cls == 16: # COCO dataset class for dog is 16
|
194 |
xyxy = box.xyxy[0].tolist()
|
@@ -198,31 +251,69 @@ async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
|
|
198 |
if not boxes:
|
199 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
200 |
else:
|
201 |
-
|
|
|
202 |
|
|
|
203 |
for box, confidence in nms_boxes:
|
204 |
x1, y1, x2, y2 = box
|
|
|
205 |
w, h = x2 - x1, y2 - y1
|
206 |
-
x1 = max(0, x1 - w * 0.
|
207 |
-
y1 = max(0, y1 - h * 0.
|
208 |
-
x2 = min(image.width, x2 + w * 0.
|
209 |
-
y2 = min(image.height, y2 + h * 0.
|
210 |
cropped_image = image.crop((x1, y1, x2, y2))
|
211 |
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
212 |
|
213 |
return dogs
|
214 |
|
215 |
-
def
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
224 |
|
225 |
def calculate_iou(box1, box2):
|
|
|
|
|
|
|
226 |
x1 = max(box1[0], box2[0])
|
227 |
y1 = max(box1[1], box2[1])
|
228 |
x2 = min(box1[2], box2[2])
|
|
|
185 |
return probabilities[0], breeds[:3], relative_probs
|
186 |
|
187 |
|
188 |
+
# async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.55):
|
189 |
+
# results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
190 |
+
# dogs = []
|
191 |
+
# boxes = []
|
192 |
+
# for box in results.boxes:
|
193 |
+
# if box.cls == 16: # COCO dataset class for dog is 16
|
194 |
+
# xyxy = box.xyxy[0].tolist()
|
195 |
+
# confidence = box.conf.item()
|
196 |
+
# boxes.append((xyxy, confidence))
|
197 |
+
|
198 |
+
# if not boxes:
|
199 |
+
# dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
200 |
+
# else:
|
201 |
+
# nms_boxes = non_max_suppression(boxes, iou_threshold)
|
202 |
+
|
203 |
+
# for box, confidence in nms_boxes:
|
204 |
+
# x1, y1, x2, y2 = box
|
205 |
+
# w, h = x2 - x1, y2 - y1
|
206 |
+
# x1 = max(0, x1 - w * 0.05)
|
207 |
+
# y1 = max(0, y1 - h * 0.05)
|
208 |
+
# x2 = min(image.width, x2 + w * 0.05)
|
209 |
+
# y2 = min(image.height, y2 + h * 0.05)
|
210 |
+
# cropped_image = image.crop((x1, y1, x2, y2))
|
211 |
+
# dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
212 |
+
|
213 |
+
# return dogs
|
214 |
+
|
215 |
+
# def non_max_suppression(boxes, iou_threshold):
|
216 |
+
# keep = []
|
217 |
+
# boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
218 |
+
# while boxes:
|
219 |
+
# current = boxes.pop(0)
|
220 |
+
# keep.append(current)
|
221 |
+
# boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
|
222 |
+
# return keep
|
223 |
+
|
224 |
+
|
225 |
+
# def calculate_iou(box1, box2):
|
226 |
+
# x1 = max(box1[0], box2[0])
|
227 |
+
# y1 = max(box1[1], box2[1])
|
228 |
+
# x2 = min(box1[2], box2[2])
|
229 |
+
# y2 = min(box1[3], box2[3])
|
230 |
+
|
231 |
+
# intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
232 |
+
# area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
233 |
+
# area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
234 |
+
|
235 |
+
# iou = intersection / float(area1 + area2 - intersection)
|
236 |
+
# return iou
|
237 |
+
|
238 |
+
|
239 |
+
async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55, sigma=0.5):
|
240 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
241 |
dogs = []
|
242 |
boxes = []
|
243 |
+
|
244 |
+
# 收集所有狗的檢測結果
|
245 |
for box in results.boxes:
|
246 |
if box.cls == 16: # COCO dataset class for dog is 16
|
247 |
xyxy = box.xyxy[0].tolist()
|
|
|
251 |
if not boxes:
|
252 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
253 |
else:
|
254 |
+
# 使用SoftNMS替代原有的NMS
|
255 |
+
nms_boxes = soft_nms(boxes, iou_threshold, sigma)
|
256 |
|
257 |
+
# 處理保留的框
|
258 |
for box, confidence in nms_boxes:
|
259 |
x1, y1, x2, y2 = box
|
260 |
+
# 擴大框的範圍以包含更多上下文
|
261 |
w, h = x2 - x1, y2 - y1
|
262 |
+
x1 = max(0, x1 - w * 0.1) # 增加到10%的margin
|
263 |
+
y1 = max(0, y1 - h * 0.1)
|
264 |
+
x2 = min(image.width, x2 + w * 0.1)
|
265 |
+
y2 = min(image.height, y2 + h * 0.1)
|
266 |
cropped_image = image.crop((x1, y1, x2, y2))
|
267 |
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
268 |
|
269 |
return dogs
|
270 |
|
271 |
+
def soft_nms(boxes, iou_threshold=0.55, sigma=0.5, score_threshold=0.25):
|
272 |
+
"""
|
273 |
+
SoftNMS with Gaussian decay
|
274 |
+
"""
|
275 |
+
if not boxes:
|
276 |
+
return []
|
277 |
+
|
278 |
+
# 轉換格式以便處理
|
279 |
+
box_coords = np.array([box[0] for box in boxes])
|
280 |
+
scores = np.array([box[1] for box in boxes])
|
281 |
+
|
282 |
+
# 按照confidence排序
|
283 |
+
indices = np.argsort(scores)[::-1]
|
284 |
+
box_coords = box_coords[indices]
|
285 |
+
scores = scores[indices]
|
286 |
+
|
287 |
+
keep_boxes = []
|
288 |
+
keep_scores = []
|
289 |
+
|
290 |
+
while len(scores) > 0:
|
291 |
+
# 保留最高分數的框
|
292 |
+
keep_boxes.append(box_coords[0].tolist())
|
293 |
+
keep_scores.append(scores[0])
|
294 |
+
|
295 |
+
if len(scores) == 1:
|
296 |
+
break
|
297 |
+
|
298 |
+
# 計算當前最高分框與其他所有框的IoU
|
299 |
+
ious = np.array([calculate_iou(box_coords[0], box) for box in box_coords[1:]])
|
300 |
+
|
301 |
+
# 使用高斯衰減更新分數
|
302 |
+
scores[1:] = scores[1:] * np.exp(-(ious * ious) / sigma)
|
303 |
+
|
304 |
+
# 移除最高分的框並過濾低於閾值的框
|
305 |
+
box_coords = box_coords[1:]
|
306 |
+
scores = scores[1:]
|
307 |
+
mask = scores > score_threshold
|
308 |
+
box_coords = box_coords[mask]
|
309 |
+
scores = scores[mask]
|
310 |
+
|
311 |
+
return list(zip(keep_boxes, keep_scores))
|
312 |
|
313 |
def calculate_iou(box1, box2):
|
314 |
+
"""
|
315 |
+
IoU 計算
|
316 |
+
"""
|
317 |
x1 = max(box1[0], box2[0])
|
318 |
y1 = max(box1[1], box2[1])
|
319 |
x2 = min(box1[2], box2[2])
|