Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -167,37 +167,98 @@ async def predict_single_dog(image):
|
|
167 |
return top1_prob, topk_breeds, topk_probs_percent
|
168 |
|
169 |
|
170 |
-
async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
171 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
172 |
dogs = []
|
173 |
boxes = []
|
|
|
174 |
for box in results.boxes:
|
175 |
if box.cls == 16: # COCO dataset class for dog is 16
|
176 |
xyxy = box.xyxy[0].tolist()
|
177 |
confidence = box.conf.item()
|
178 |
boxes.append((xyxy, confidence))
|
179 |
-
|
|
|
180 |
if not boxes:
|
181 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
182 |
else:
|
183 |
nms_boxes = non_max_suppression(boxes, iou_threshold)
|
184 |
|
|
|
185 |
for box, confidence in nms_boxes:
|
186 |
x1, y1, x2, y2 = box
|
187 |
w, h = x2 - x1, y2 - y1
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
cropped_image = image.crop((x1, y1, x2, y2))
|
193 |
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
194 |
-
|
195 |
-
return dogs
|
196 |
|
|
|
197 |
|
198 |
-
|
|
|
199 |
keep = []
|
200 |
-
boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
|
|
201 |
while boxes:
|
202 |
current = boxes.pop(0)
|
203 |
keep.append(current)
|
@@ -210,14 +271,17 @@ def calculate_iou(box1, box2):
|
|
210 |
x2 = min(box1[2], box2[2])
|
211 |
y2 = min(box1[3], box2[3])
|
212 |
|
|
|
213 |
intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
214 |
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
215 |
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
216 |
|
|
|
217 |
iou = intersection / float(area1 + area2 - intersection)
|
218 |
return iou
|
219 |
|
220 |
|
|
|
221 |
async def process_single_dog(image):
|
222 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
223 |
if top1_prob < 0.2:
|
|
|
167 |
return top1_prob, topk_breeds, topk_probs_percent
|
168 |
|
169 |
|
170 |
+
# async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.4):
|
171 |
+
# results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
172 |
+
# dogs = []
|
173 |
+
# boxes = []
|
174 |
+
# for box in results.boxes:
|
175 |
+
# if box.cls == 16: # COCO dataset class for dog is 16
|
176 |
+
# xyxy = box.xyxy[0].tolist()
|
177 |
+
# confidence = box.conf.item()
|
178 |
+
# boxes.append((xyxy, confidence))
|
179 |
+
|
180 |
+
# if not boxes:
|
181 |
+
# dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
182 |
+
# else:
|
183 |
+
# nms_boxes = non_max_suppression(boxes, iou_threshold)
|
184 |
+
|
185 |
+
# for box, confidence in nms_boxes:
|
186 |
+
# x1, y1, x2, y2 = box
|
187 |
+
# w, h = x2 - x1, y2 - y1
|
188 |
+
# x1 = max(0, x1 - w * 0.05)
|
189 |
+
# y1 = max(0, y1 - h * 0.05)
|
190 |
+
# x2 = min(image.width, x2 + w * 0.05)
|
191 |
+
# y2 = min(image.height, y2 + h * 0.05)
|
192 |
+
# cropped_image = image.crop((x1, y1, x2, y2))
|
193 |
+
# dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
194 |
+
|
195 |
+
# return dogs
|
196 |
+
|
197 |
+
|
198 |
+
# def non_max_suppression(boxes, iou_threshold):
|
199 |
+
# keep = []
|
200 |
+
# boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
201 |
+
# while boxes:
|
202 |
+
# current = boxes.pop(0)
|
203 |
+
# keep.append(current)
|
204 |
+
# boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
|
205 |
+
# return keep
|
206 |
+
|
207 |
+
# def calculate_iou(box1, box2):
|
208 |
+
# x1 = max(box1[0], box2[0])
|
209 |
+
# y1 = max(box1[1], box2[1])
|
210 |
+
# x2 = min(box1[2], box2[2])
|
211 |
+
# y2 = min(box1[3], box2[3])
|
212 |
+
|
213 |
+
# intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
214 |
+
# area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
215 |
+
# area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
216 |
+
|
217 |
+
# iou = intersection / float(area1 + area2 - intersection)
|
218 |
+
# return iou
|
219 |
+
|
220 |
+
|
221 |
+
async def detect_multiple_dogs(image, conf_threshold=0.15, iou_threshold=0.3):
|
222 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
223 |
dogs = []
|
224 |
boxes = []
|
225 |
+
|
226 |
for box in results.boxes:
|
227 |
if box.cls == 16: # COCO dataset class for dog is 16
|
228 |
xyxy = box.xyxy[0].tolist()
|
229 |
confidence = box.conf.item()
|
230 |
boxes.append((xyxy, confidence))
|
231 |
+
|
232 |
+
# 如果沒有檢測到任何狗
|
233 |
if not boxes:
|
234 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
235 |
else:
|
236 |
nms_boxes = non_max_suppression(boxes, iou_threshold)
|
237 |
|
238 |
+
# 進一步優化處理重疊框邏輯
|
239 |
for box, confidence in nms_boxes:
|
240 |
x1, y1, x2, y2 = box
|
241 |
w, h = x2 - x1, y2 - y1
|
242 |
+
|
243 |
+
# 計算高度和寬度的比率,如果比例異常,則認定為重疊框需要拆分
|
244 |
+
aspect_ratio = h / w if w != 0 else 1
|
245 |
+
if aspect_ratio > 1.5 or aspect_ratio < 0.5:
|
246 |
+
# 假設重疊度過高,可以進一步裁切框
|
247 |
+
x1 = max(0, x1 - w * 0.05)
|
248 |
+
y1 = max(0, y1 - h * 0.05)
|
249 |
+
x2 = min(image.width, x2 + w * 0.05)
|
250 |
+
y2 = min(image.height, y2 + h * 0.05)
|
251 |
+
|
252 |
cropped_image = image.crop((x1, y1, x2, y2))
|
253 |
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
|
|
|
|
254 |
|
255 |
+
return dogs
|
256 |
|
257 |
+
# 增加一個優化的non_max_suppression版本
|
258 |
+
def non_max_suppression(boxes, iou_threshold=0.3):
|
259 |
keep = []
|
260 |
+
boxes = sorted(boxes, key=lambda x: x[1], reverse=True) # 按信心分數排序
|
261 |
+
|
262 |
while boxes:
|
263 |
current = boxes.pop(0)
|
264 |
keep.append(current)
|
|
|
271 |
x2 = min(box1[2], box2[2])
|
272 |
y2 = min(box1[3], box2[3])
|
273 |
|
274 |
+
# 計算交集面積
|
275 |
intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
276 |
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
277 |
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
278 |
|
279 |
+
# 計算IOU
|
280 |
iou = intersection / float(area1 + area2 - intersection)
|
281 |
return iou
|
282 |
|
283 |
|
284 |
+
|
285 |
async def process_single_dog(image):
|
286 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
287 |
if top1_prob < 0.2:
|