Orkhan Hasanli commited on
Commit
3ecb1be
·
1 Parent(s): 5ce2fb0

Expanding by 10% person/car crop to make sure objects centers are still within person/car bbox

Browse files
Files changed (1) hide show
  1. dfine_jina_pipeline.py +23 -2
dfine_jina_pipeline.py CHANGED
@@ -108,6 +108,25 @@ def box_center_inside(box, crop_box):
108
  )
109
 
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  def squarify_crop_box(bx1, by1, bx2, by2, img_w, img_h):
112
  """
113
  Expand the shorter side to match the longer (same ratio / square), centered, clamped to image.
@@ -360,10 +379,11 @@ def main():
360
  for gidx, grp in enumerate(top_groups):
361
  x1, y1, x2, y2 = grp["box"]
362
  group_box = [x1, y1, x2, y2]
 
363
 
364
  inside = [
365
  d for d in detections
366
- if box_center_inside(d["box"], group_box) and d["cls"] not in person_car_ids
367
  ]
368
  inside = deduplicate_by_iou(inside, iou_threshold=0.9)
369
 
@@ -554,10 +574,11 @@ def run_single_image(
554
  for gidx, grp in enumerate(top_groups):
555
  x1, y1, x2, y2 = grp["box"]
556
  group_box = [x1, y1, x2, y2]
 
557
 
558
  inside = [
559
  d for d in detections
560
- if box_center_inside(d["box"], group_box) and d["cls"] not in person_car_ids
561
  ]
562
  inside = deduplicate_by_iou(inside, iou_threshold=0.9)
563
 
 
108
  )
109
 
110
 
111
+ def expand_box_by_margin(box, margin_ratio, img_w, img_h):
112
+ """Expand box [x1,y1,x2,y2] by margin_ratio (e.g. 0.1 = 10%) on all sides, clamped to image."""
113
+ x1, y1, x2, y2 = box
114
+ w, h = x2 - x1, y2 - y1
115
+ if w <= 0 or h <= 0:
116
+ return box
117
+ mx = w * margin_ratio
118
+ my = h * margin_ratio
119
+ x1 = max(0, x1 - mx)
120
+ y1 = max(0, y1 - my)
121
+ x2 = min(img_w, x2 + mx)
122
+ y2 = min(img_h, y2 + my)
123
+ return [x1, y1, x2, y2]
124
+
125
+
126
+ # 10% margin on person/car group box when testing if object center is inside (avoids missing boundary objects)
127
+ PERSON_CAR_GROUP_MARGIN = 0.10
128
+
129
+
130
  def squarify_crop_box(bx1, by1, bx2, by2, img_w, img_h):
131
  """
132
  Expand the shorter side to match the longer (same ratio / square), centered, clamped to image.
 
379
  for gidx, grp in enumerate(top_groups):
380
  x1, y1, x2, y2 = grp["box"]
381
  group_box = [x1, y1, x2, y2]
382
+ group_box_with_margin = expand_box_by_margin(group_box, PERSON_CAR_GROUP_MARGIN, img_w, img_h)
383
 
384
  inside = [
385
  d for d in detections
386
+ if box_center_inside(d["box"], group_box_with_margin) and d["cls"] not in person_car_ids
387
  ]
388
  inside = deduplicate_by_iou(inside, iou_threshold=0.9)
389
 
 
574
  for gidx, grp in enumerate(top_groups):
575
  x1, y1, x2, y2 = grp["box"]
576
  group_box = [x1, y1, x2, y2]
577
+ group_box_with_margin = expand_box_by_margin(group_box, PERSON_CAR_GROUP_MARGIN, img_w, img_h)
578
 
579
  inside = [
580
  d for d in detections
581
+ if box_center_inside(d["box"], group_box_with_margin) and d["cls"] not in person_car_ids
582
  ]
583
  inside = deduplicate_by_iou(inside, iou_threshold=0.9)
584