Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -193,17 +193,62 @@ async def predict_single_dog(image):
|
|
| 193 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
| 194 |
return top1_prob, topk_breeds, topk_probs_percent
|
| 195 |
|
| 196 |
-
async def detect_multiple_dogs(image, conf_threshold=0.
|
| 197 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 198 |
dogs = []
|
|
|
|
| 199 |
for box in results.boxes:
|
| 200 |
if box.cls == 16: # COCO dataset class for dog is 16
|
| 201 |
xyxy = box.xyxy[0].tolist()
|
| 202 |
confidence = box.conf.item()
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
return dogs
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
async def process_single_dog(image):
|
| 209 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
|
@@ -412,9 +457,6 @@ async def predict(image):
|
|
| 412 |
|
| 413 |
dogs = await detect_multiple_dogs(image)
|
| 414 |
|
| 415 |
-
if len(dogs) == 0:
|
| 416 |
-
dogs = [(image, 1.0, [0, 0, image.width, image.height])]
|
| 417 |
-
|
| 418 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 419 |
explanations = []
|
| 420 |
buttons = []
|
|
@@ -452,7 +494,7 @@ async def predict(image):
|
|
| 452 |
"is_multi_dog": len(dogs) > 1,
|
| 453 |
"dogs_info": explanations
|
| 454 |
}
|
| 455 |
-
return final_explanation, annotated_image, gr.update(visible=
|
| 456 |
else:
|
| 457 |
initial_state = {
|
| 458 |
"explanation": final_explanation,
|
|
@@ -469,6 +511,7 @@ async def predict(image):
|
|
| 469 |
print(error_msg)
|
| 470 |
return error_msg, None, gr.update(visible=False, choices=[]), None
|
| 471 |
|
|
|
|
| 472 |
def show_details(choice, previous_output, initial_state):
|
| 473 |
if not choice:
|
| 474 |
return previous_output, gr.update(visible=True), initial_state
|
|
|
|
| 193 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
| 194 |
return top1_prob, topk_breeds, topk_probs_percent
|
| 195 |
|
| 196 |
+
async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.5):
|
| 197 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
| 198 |
dogs = []
|
| 199 |
+
boxes = []
|
| 200 |
for box in results.boxes:
|
| 201 |
if box.cls == 16: # COCO dataset class for dog is 16
|
| 202 |
xyxy = box.xyxy[0].tolist()
|
| 203 |
confidence = box.conf.item()
|
| 204 |
+
boxes.append(xyxy)
|
| 205 |
+
|
| 206 |
+
# 如果沒有檢測到狗,使用整張圖片
|
| 207 |
+
if not boxes:
|
| 208 |
+
dogs.append((image, 1.0, [0, 0, image.width, image.height]))
|
| 209 |
+
else:
|
| 210 |
+
# 合併重疊的框
|
| 211 |
+
merged_boxes = merge_boxes(boxes)
|
| 212 |
+
for box in merged_boxes:
|
| 213 |
+
cropped_image = image.crop((box[0], box[1], box[2], box[3]))
|
| 214 |
+
dogs.append((cropped_image, 1.0, box))
|
| 215 |
+
|
| 216 |
return dogs
|
| 217 |
|
| 218 |
+
def merge_boxes(boxes, iou_threshold=0.5):
|
| 219 |
+
merged = []
|
| 220 |
+
while boxes:
|
| 221 |
+
base_box = boxes.pop(0)
|
| 222 |
+
i = 0
|
| 223 |
+
while i < len(boxes):
|
| 224 |
+
if calculate_iou(base_box, boxes[i]) > iou_threshold:
|
| 225 |
+
base_box = merge_two_boxes(base_box, boxes.pop(i))
|
| 226 |
+
else:
|
| 227 |
+
i += 1
|
| 228 |
+
merged.append(base_box)
|
| 229 |
+
return merged
|
| 230 |
+
|
| 231 |
+
def calculate_iou(box1, box2):
|
| 232 |
+
x1 = max(box1[0], box2[0])
|
| 233 |
+
y1 = max(box1[1], box2[1])
|
| 234 |
+
x2 = min(box1[2], box2[2])
|
| 235 |
+
y2 = min(box1[3], box2[3])
|
| 236 |
+
|
| 237 |
+
intersection = max(0, x2 - x1) * max(0, y2 - y1)
|
| 238 |
+
area1 = (box1[2] - box1[0]) * (box1[3] - box1[1])
|
| 239 |
+
area2 = (box2[2] - box2[0]) * (box2[3] - box2[1])
|
| 240 |
+
|
| 241 |
+
iou = intersection / float(area1 + area2 - intersection)
|
| 242 |
+
return iou
|
| 243 |
+
|
| 244 |
+
def merge_two_boxes(box1, box2):
|
| 245 |
+
return [
|
| 246 |
+
min(box1[0], box2[0]),
|
| 247 |
+
min(box1[1], box2[1]),
|
| 248 |
+
max(box1[2], box2[2]),
|
| 249 |
+
max(box1[3], box2[3])
|
| 250 |
+
]
|
| 251 |
+
|
| 252 |
|
| 253 |
async def process_single_dog(image):
|
| 254 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
|
|
|
| 457 |
|
| 458 |
dogs = await detect_multiple_dogs(image)
|
| 459 |
|
|
|
|
|
|
|
|
|
|
| 460 |
color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
| 461 |
explanations = []
|
| 462 |
buttons = []
|
|
|
|
| 494 |
"is_multi_dog": len(dogs) > 1,
|
| 495 |
"dogs_info": explanations
|
| 496 |
}
|
| 497 |
+
return final_explanation, annotated_image, gr.update(visible=true, choices=buttons), initial_state
|
| 498 |
else:
|
| 499 |
initial_state = {
|
| 500 |
"explanation": final_explanation,
|
|
|
|
| 511 |
print(error_msg)
|
| 512 |
return error_msg, None, gr.update(visible=False, choices=[]), None
|
| 513 |
|
| 514 |
+
|
| 515 |
def show_details(choice, previous_output, initial_state):
|
| 516 |
if not choice:
|
| 517 |
return previous_output, gr.update(visible=True), initial_state
|