Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -19,7 +19,7 @@ logging.basicConfig(level=logging.DEBUG)
|
|
19 |
logger = logging.getLogger(__name__)
|
20 |
|
21 |
|
22 |
-
model_yolo = YOLO('
|
23 |
|
24 |
|
25 |
dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staffordshire_Terrier",
|
@@ -166,95 +166,58 @@ async def predict_single_dog(image):
|
|
166 |
return top1_prob, topk_breeds, topk_probs_percent
|
167 |
|
168 |
|
169 |
-
|
170 |
-
# results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
171 |
-
# dogs = []
|
172 |
-
# boxes = []
|
173 |
-
# for box in results.boxes:
|
174 |
-
# if box.cls == 16: # COCO dataset class for dog is 16
|
175 |
-
# xyxy = box.xyxy[0].tolist()
|
176 |
-
# confidence = box.conf.item()
|
177 |
-
# boxes.append((xyxy, confidence))
|
178 |
-
|
179 |
-
# if not boxes:
|
180 |
-
# dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
181 |
-
# else:
|
182 |
-
# nms_boxes = non_max_suppression(boxes, iou_threshold)
|
183 |
-
|
184 |
-
# for box, confidence in nms_boxes:
|
185 |
-
# x1, y1, x2, y2 = box
|
186 |
-
# w, h = x2 - x1, y2 - y1
|
187 |
-
# x1 = max(0, x1 - w * 0.05)
|
188 |
-
# y1 = max(0, y1 - h * 0.05)
|
189 |
-
# x2 = min(image.width, x2 + w * 0.05)
|
190 |
-
# y2 = min(image.height, y2 + h * 0.05)
|
191 |
-
# cropped_image = image.crop((x1, y1, x2, y2))
|
192 |
-
# dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
193 |
-
|
194 |
-
# return dogs
|
195 |
-
|
196 |
-
|
197 |
-
# def non_max_suppression(boxes, iou_threshold):
|
198 |
-
# keep = []
|
199 |
-
# boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
200 |
-
# while boxes:
|
201 |
-
# current = boxes.pop(0)
|
202 |
-
# keep.append(current)
|
203 |
-
# boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
|
204 |
-
# return keep
|
205 |
-
|
206 |
-
# def calculate_iou(box1, box2):
|
207 |
-
# x1 = max(box1[0], box2[0])
|
208 |
-
# y1 = max(box1[1], box2[1])
|
209 |
-
# x2 = min(box1[2], box2[2])
|
210 |
-
# y2 = min(box1[3], box2[3])
|
211 |
-
|
212 |
-
# intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
213 |
-
# area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
214 |
-
# area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
215 |
-
|
216 |
-
# iou = intersection / float(area1 + area2 - intersection)
|
217 |
-
# return iou
|
218 |
-
|
219 |
-
|
220 |
-
def weighted_nms(boxes, scores, iou_threshold=0.5, score_threshold=0.05):
|
221 |
-
keep = nms(boxes, scores, iou_threshold)
|
222 |
-
keep = keep[scores[keep] > score_threshold]
|
223 |
-
return boxes[keep], scores[keep]
|
224 |
-
|
225 |
-
async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.45):
|
226 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
227 |
dogs = []
|
228 |
boxes = []
|
229 |
-
scores = []
|
230 |
-
|
231 |
for box in results.boxes:
|
232 |
if box.cls == 16: # COCO dataset class for dog is 16
|
233 |
-
xyxy = box.xyxy[0]
|
234 |
-
confidence = box.conf
|
235 |
-
boxes.append(xyxy)
|
236 |
-
scores.append(confidence)
|
237 |
|
238 |
if not boxes:
|
239 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
240 |
else:
|
241 |
-
|
242 |
-
scores = torch.stack(scores).squeeze(-1)
|
243 |
-
nms_boxes, nms_scores = weighted_nms(boxes, scores, iou_threshold=iou_threshold, score_threshold=0.1)
|
244 |
|
245 |
-
for box,
|
246 |
-
x1, y1, x2, y2 = box
|
247 |
w, h = x2 - x1, y2 - y1
|
248 |
x1 = max(0, x1 - w * 0.05)
|
249 |
y1 = max(0, y1 - h * 0.05)
|
250 |
x2 = min(image.width, x2 + w * 0.05)
|
251 |
y2 = min(image.height, y2 + h * 0.05)
|
252 |
cropped_image = image.crop((x1, y1, x2, y2))
|
253 |
-
dogs.append((cropped_image,
|
254 |
|
255 |
return dogs
|
256 |
|
257 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
258 |
async def process_single_dog(image):
|
259 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
260 |
if top1_prob < 0.15:
|
|
|
19 |
logger = logging.getLogger(__name__)
|
20 |
|
21 |
|
22 |
+
model_yolo = YOLO('yolov8l.pt')
|
23 |
|
24 |
|
25 |
dog_breeds = ["Afghan_Hound", "African_Hunting_Dog", "Airedale", "American_Staffordshire_Terrier",
|
|
|
166 |
return top1_prob, topk_breeds, topk_probs_percent
|
167 |
|
168 |
|
169 |
+
async def detect_multiple_dogs(image, conf_threshold=0.35, iou_threshold=0.55):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
171 |
dogs = []
|
172 |
boxes = []
|
|
|
|
|
173 |
for box in results.boxes:
|
174 |
if box.cls == 16: # COCO dataset class for dog is 16
|
175 |
+
xyxy = box.xyxy[0].tolist()
|
176 |
+
confidence = box.conf.item()
|
177 |
+
boxes.append((xyxy, confidence))
|
|
|
178 |
|
179 |
if not boxes:
|
180 |
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
181 |
else:
|
182 |
+
nms_boxes = non_max_suppression(boxes, iou_threshold)
|
|
|
|
|
183 |
|
184 |
+
for box, confidence in nms_boxes:
|
185 |
+
x1, y1, x2, y2 = box
|
186 |
w, h = x2 - x1, y2 - y1
|
187 |
x1 = max(0, x1 - w * 0.05)
|
188 |
y1 = max(0, y1 - h * 0.05)
|
189 |
x2 = min(image.width, x2 + w * 0.05)
|
190 |
y2 = min(image.height, y2 + h * 0.05)
|
191 |
cropped_image = image.crop((x1, y1, x2, y2))
|
192 |
+
dogs.append((cropped_image, confidence, [x1, y1, x2, y2]))
|
193 |
|
194 |
return dogs
|
195 |
|
196 |
|
197 |
+
def non_max_suppression(boxes, iou_threshold):
|
198 |
+
keep = []
|
199 |
+
boxes = sorted(boxes, key=lambda x: x[1], reverse=True)
|
200 |
+
while boxes:
|
201 |
+
current = boxes.pop(0)
|
202 |
+
keep.append(current)
|
203 |
+
boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
|
204 |
+
return keep
|
205 |
+
|
206 |
+
def calculate_iou(box1, box2):
|
207 |
+
x1 = max(box1[0], box2[0])
|
208 |
+
y1 = max(box1[1], box2[1])
|
209 |
+
x2 = min(box1[2], box2[2])
|
210 |
+
y2 = min(box1[3], box2[3])
|
211 |
+
|
212 |
+
intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
213 |
+
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
214 |
+
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
215 |
+
|
216 |
+
iou = intersection / float(area1 + area2 - intersection)
|
217 |
+
return iou
|
218 |
+
|
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.15:
|