Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -167,105 +167,43 @@ async def predict_single_dog(image):
|
|
167 |
return top1_prob, topk_breeds, topk_probs_percent
|
168 |
|
169 |
|
170 |
-
|
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.1, 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 |
if not boxes:
|
233 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
234 |
else:
|
235 |
-
|
236 |
-
valid_boxes = [box for box in boxes if (box[0][2] - box[0][0]) * (box[0][3] - box[0][1]) > 0.1 * image.width * image.height]
|
237 |
-
|
238 |
-
nms_boxes = non_max_suppression(valid_boxes, iou_threshold)
|
239 |
|
240 |
for box, confidence in nms_boxes:
|
241 |
x1, y1, x2, y2 = box
|
242 |
w, h = x2 - x1, y2 - y1
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
# 根據框的大小動態調整信心門檻
|
249 |
-
if w * h < 0.05 * image.width * image.height:
|
250 |
-
continue # 過小的框直接跳過
|
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 |
|
258 |
-
def non_max_suppression(boxes, iou_threshold
|
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)
|
265 |
boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
|
266 |
return keep
|
267 |
|
268 |
-
|
269 |
def calculate_iou(box1, box2):
|
270 |
x1 = max(box1[0], box2[0])
|
271 |
y1 = max(box1[1], box2[1])
|
@@ -280,8 +218,6 @@ def calculate_iou(box1, box2):
|
|
280 |
return iou
|
281 |
|
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:
|
|
|
167 |
return top1_prob, topk_breeds, topk_probs_percent
|
168 |
|
169 |
|
170 |
+
async def detect_multiple_dogs(image, conf_threshold=0.2, 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])
|
|
|
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:
|