Bladeren bron

refactor boxes_convert.py

quarrying 3 jaren geleden
bovenliggende
commit
2daa4919e0
2 gewijzigde bestanden met toevoegingen van 20 en 19 verwijderingen
  1. 17 16
      khandy/boxes/boxes_convert.py
  2. 3 3
      khandy/boxes/boxes_filter.py

+ 17 - 16
khandy/boxes/boxes_convert.py

@@ -6,7 +6,7 @@ def convert_xyxy_to_xywh(boxes, copy=True):
     """
     if copy:
         boxes = boxes.copy()
-    boxes[:, 2:4] -= boxes[:, 0:2]
+    boxes[..., 2:4] -= boxes[..., 0:2]
     return boxes
 
 
@@ -15,7 +15,7 @@ def convert_xywh_to_xyxy(boxes, copy=True):
     """
     if copy:
         boxes = boxes.copy()
-    boxes[:, 2:4] += boxes[:, 0:2]
+    boxes[..., 2:4] += boxes[..., 0:2]
     return boxes
 
 
@@ -24,7 +24,7 @@ def convert_xywh_to_cxcywh(boxes, copy=True):
     """
     if copy:
         boxes = boxes.copy()
-    boxes[:, 0:2] += boxes[:, 2:4] * 0.5
+    boxes[..., 0:2] += boxes[..., 2:4] * 0.5
     return boxes
     
     
@@ -33,23 +33,27 @@ def convert_cxcywh_to_xywh(boxes, copy=True):
     """
     if copy:
         boxes = boxes.copy()
-    boxes[:, 0:2] -= boxes[:, 2:4] * 0.5
+    boxes[..., 0:2] -= boxes[..., 2:4] * 0.5
     return boxes
     
     
 def convert_xyxy_to_cxcywh(boxes, copy=True):
     """Convert [x_min, y_min, x_max, y_max] format to [cx, cy, width, height] format.
     """
-    boxes = convert_xyxy_to_xywh(boxes, copy)
-    boxes = convert_xywh_to_cxcywh(boxes, False)
+    if copy:
+        boxes = boxes.copy()
+    boxes[..., 2:4] -= boxes[..., 0:2]
+    boxes[..., 0:2] += boxes[..., 2:4] * 0.5
     return boxes
 
 
 def convert_cxcywh_to_xyxy(boxes, copy=True):
-    """Convert [x_min, y_min, x_max, y_max] format to [cx, cy, width, height] format.
+    """Convert [cx, cy, width, height] format to [x_min, y_min, x_max, y_max] format.
     """
-    boxes = convert_cxcywh_to_xywh(boxes, copy)
-    boxes = convert_xywh_to_xyxy(boxes, False)
+    if copy:
+        boxes = boxes.copy()
+    boxes[..., 0:2] -= boxes[..., 2:4] * 0.5
+    boxes[..., 2:4] += boxes[..., 0:2]
     return boxes
 
 
@@ -57,13 +61,10 @@ def convert_boxes_format(boxes, in_fmt, out_fmt, copy=True):
     """Converts boxes from given in_fmt to out_fmt.
 
     Supported in_fmt and out_fmt are:
-
-    'xyxy': boxes are represented via corners, x1, y1 being top left and x2, y2 being bottom right.
-
-    'xywh' : boxes are represented via corner, width and height, x1, y2 being top left, w, h being width and height.
-
-    'cxcywh' : boxes are represented via centre, width and height, cx, cy being center of box, w, h
-    being width and height.
+        'xyxy': boxes are represented via corners, x1, y1 being top left and x2, y2 being bottom right.
+        'xywh' : boxes are represented via corner, width and height, x1, y2 being top left, w, h being width and height.
+        'cxcywh' : boxes are represented via centre, width and height, cx, cy being center of box, w, h
+            being width and height.
 
     Args:
         boxes: boxes which will be converted.

+ 3 - 3
khandy/boxes/boxes_filter.py

@@ -96,12 +96,12 @@ def non_max_suppression(boxes, scores, thresh, classes=None, ratio_type="iou"):
         min_y_maxs = np.minimum(y_maxs[i], y_maxs[order[1:]])
         widths = np.maximum(0, min_x_maxs - max_x_mins)
         heights = np.maximum(0, min_y_maxs - max_y_mins)
-        intersect_area = widths * heights
+        intersect_areas = widths * heights
         
         if ratio_type in ["union", 'iou']:
-            ratio = intersect_area / (areas[i] + areas[order[1:]] - intersect_area)
+            ratio = intersect_areas / (areas[i] + areas[order[1:]] - intersect_areas)
         elif ratio_type == "min":
-            ratio = intersect_area / np.minimum(areas[i], areas[order[1:]])
+            ratio = intersect_areas / np.minimum(areas[i], areas[order[1:]])
         else:
             raise ValueError('Unsupported ratio_type. Got {}'.format(ratio_type))