From 6d25f9e6347f177b960d245ee699b8e1cfa97d46 Mon Sep 17 00:00:00 2001 From: klein179 <roel.klein@wur.nl> Date: Thu, 19 Sep 2024 15:56:27 +0200 Subject: [PATCH] formatting --- box_converter.py | 129 +++++++++++++++++++++++++++-------------------- 1 file changed, 74 insertions(+), 55 deletions(-) diff --git a/box_converter.py b/box_converter.py index 728b422..b4ce970 100755 --- a/box_converter.py +++ b/box_converter.py @@ -4,21 +4,22 @@ import pycocotools.mask def x1y1x2y2_2_x1x2y1y2(this_list): - new_list = [0]*4 - new_list[0] = this_list[0] - new_list[1] = this_list[2] - new_list[2] = this_list[1] - new_list[3] = this_list[3] - return new_list + new_list = [0] * 4 + new_list[0] = this_list[0] + new_list[1] = this_list[2] + new_list[2] = this_list[1] + new_list[3] = this_list[3] + return new_list def x1x2y1y2_2_x1y1x2y2(this_list): - new_list = [0]*4 - new_list[0] = this_list[0] - new_list[1] = this_list[2] - new_list[2] = this_list[1] - new_list[3] = this_list[3] - return new_list + new_list = [0] * 4 + new_list[0] = this_list[0] + new_list[1] = this_list[2] + new_list[2] = this_list[1] + new_list[3] = this_list[3] + return new_list + def xywh2xyxy_yolo(x): y = np.copy(x) @@ -29,15 +30,15 @@ def xywh2xyxy_yolo(x): return y - def xywh2xxyy(list_input): this_list = list_input.copy() - this_list[0] = list_input[0] # x1 - this_list[1] = list_input[0] + list_input[2] # x2 - this_list[2] = list_input[1] # y1 - this_list[3] = list_input[1] + list_input[3] # y2 + this_list[0] = list_input[0] # x1 + this_list[1] = list_input[0] + list_input[2] # x2 + this_list[2] = list_input[1] # y1 + this_list[3] = list_input[1] + list_input[3] # y2 return this_list + def xywh2xyxy(list_input): this_list = list_input.copy() this_list[2] = list_input[0] + list_input[2] @@ -46,56 +47,74 @@ def xywh2xyxy(list_input): this_list[1] = list_input[1] return this_list -def xyxy2xywh(list_input): - x1 = int(list_input[0]) - y1 = int(list_input[1]) - w = abs(int(list_input[2]-list_input[0])) - h = abs(int(list_input[3]-list_input[1])) - return [x1,y1,w,h] + +def xyxy2xywh(listinput): + x1 = int(listinput[0]) + y1 = int(listinput[0]) + w = int(listinput[2] - listinput[0]) + h = int(listinput[3] - listinput[1]) + assert w > 0 + assert h > 0 + return [x1, y1, w, h] def xywh_2_x1y1x2y2(list_input): - x1 = int(list_input[0]) - y1 = int(list_input[1]) - x2 = int(x1 + list_input[2]) - y2 = int(y1 + list_input[3]) - return [x1,y1,x2,y2] - - -def x1y1x2y2_2_cxcywh(list_input,height_img, width_img): - # conversin of [x1,y1,x2,y2] to normalized yolo! [center_x,center_y,width,height] - center_x = (list_input[0]+list_input[2])/2 - center_y = (list_input[1]+list_input[3])/2 - width = (list_input[2]-list_input[0]) - height = (list_input[3]-list_input[1]) - return [center_x/width_img,center_y/height_img,width/width_img,height/height_img] - -def x1y1wh_2_cxcywh(list_input,height_img, width_img): - # conversin of [x1,y1,x2,y2] to normalized yolo! [center_x,center_y,width,height] - center_x = (list_input[0]+list_input[2]*0.5) - center_y = (list_input[1]+list_input[3]*0.5) - width = list_input[2] - height = list_input[3] - return [center_x/width_img,center_y/height_img,width/width_img,height/height_img] + x1 = int(list_input[0]) + y1 = int(list_input[1]) + x2 = int(x1 + list_input[2]) + y2 = int(y1 + list_input[3]) + return [x1, y1, x2, y2] + + +def x1y1x2y2_2_cxcywh(list_input, height_img, width_img): + # conversin of [x1,y1,x2,y2] to normalized yolo! [center_x,center_y,width,height] + center_x = (list_input[0] + list_input[2]) / 2 + center_y = (list_input[1] + list_input[3]) / 2 + width = list_input[2] - list_input[0] + height = list_input[3] - list_input[1] + return [ + center_x / width_img, + center_y / height_img, + width / width_img, + height / height_img, + ] + + +def x1y1wh_2_cxcywh(list_input, height_img, width_img): + # conversin of [x1,y1,x2,y2] to normalized yolo! [center_x,center_y,width,height] + center_x = list_input[0] + list_input[2] * 0.5 + center_y = list_input[1] + list_input[3] * 0.5 + width = list_input[2] + height = list_input[3] + return [ + center_x / width_img, + center_y / height_img, + width / width_img, + height / height_img, + ] + def binary_mask_to_rle(binary_mask): - ## input is numpy array with either 0 or 1 : np.array([0,0,1,1,1,0,1]) - ## output: rle={'counts': [2, 3, 1, 1], 'size': [7]} - rle = {'counts': [], 'size': list(binary_mask.shape)} - counts = rle.get('counts') - for i, (value, elements) in enumerate(groupby(binary_mask.ravel(order='F'))): + ## input is numpy array with either 0 or 1 : np.array([0,0,1,1,1,0,1]) + ## output: rle={'counts': [2, 3, 1, 1], 'size': [7]} + rle = {"counts": [], "size": list(binary_mask.shape)} + counts = rle.get("counts") + for i, (value, elements) in enumerate(groupby(binary_mask.ravel(order="F"))): if i == 0 and value == 1: counts.append(0) counts.append(len(list(elements))) return rle + def rle_to_binary_mask(rle): - ## converts rle={'counts': [2, 3, 1, 1], 'size': [7]} to np.array([0,0,1,1,1,0,1]) - compressed_rle = pycocotools.mask.frPyObjects(rle, rle.get('size')[0], rle.get('size')[1]) - mask = pycocotools.mask.decode(compressed_rle) - return mask + ## converts rle={'counts': [2, 3, 1, 1], 'size': [7]} to np.array([0,0,1,1,1,0,1]) + compressed_rle = pycocotools.mask.frPyObjects( + rle, rle.get("size")[0], rle.get("size")[1] + ) + mask = pycocotools.mask.decode(compressed_rle) + return mask def box2centroid(b): - centroid = [int((b[2]+b[0])/2), int((b[3]+b[1])/2)] + centroid = [int((b[2] + b[0]) / 2), int((b[3] + b[1]) / 2)] return centroid -- GitLab