From cd7af07a092020aa09225f92554ce86f0acff782 Mon Sep 17 00:00:00 2001 From: ThibautDeTandt Date: Wed, 29 Mar 2023 15:21:34 +0200 Subject: [PATCH 1/2] added getpolygons functions for Image segmentation --- labelbox_json2yolo.py | 58 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/labelbox_json2yolo.py b/labelbox_json2yolo.py index 843aaf8..741d4f8 100644 --- a/labelbox_json2yolo.py +++ b/labelbox_json2yolo.py @@ -9,6 +9,34 @@ from utils import make_dirs +def getPolygons(url): + # Download the image from the URL + with urllib.request.urlopen(url) as url_response: + img_array = np.asarray(bytearray(url_response.read()), dtype=np.uint8) + image = cv2.imdecode(img_array, cv2.IMREAD_COLOR) + + h, w = image.shape[:2] + line_width = int((h + w) * 0.5 * 0.0025) + #convert to grayscale + gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + contours, _ = cv2.findContours(gray_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_KCOS) + + contours_approx = [] + polygons = [] + #Get polygons + for contour in contours: + epsilon = 0.001 * cv2.arcLength(contour, True) + contour_approx = cv2.approxPolyDP(contour, epsilon, True) + + contours_approx.append(contour_approx) + polygon = (contour_approx.flatten().reshape(-1, 2) / np.array([w, h])).tolist() + polygons.append(polygon) + cv2.drawContours(image, contours_approx, -1, 128, line_width) + #format the data correctly + result = [[f"{x},{y}" for x, y in sublist] for sublist in polygons] + polygons = [[float(x) for pair in sublist for x in pair.split(',')] for sublist in result] + + return polygons def convert(file, zip=True): # Convert Labelbox JSON labels to YOLO labels @@ -26,6 +54,36 @@ def convert(file, zip=True): image_path = save_dir / 'images' / img['External ID'] im.save(image_path, quality=95, subsampling=0) + + ###################################### + #for Image Segmentation + ###################################### + + # for img in tqdm(data, desc=f'Converting {file}'): + # im_path = img['Labeled Data'] + # im = Image.open(requests.get(im_path, stream=True).raw if im_path.startswith('http') else im_path) # open + # width, height = im.size # image size + # label_path = save_dir / 'labels' / Path(img['External ID']).with_suffix('.txt').name + # print(label_path) + # image_path = save_dir / 'images' / img['External ID'] + # print(image_path) + # im.save(image_path, quality=95, subsampling=0) + + # for label in img['Label']['objects']: + # + # polygon = getPolygons(label['instanceURI']) + # # class + # cls = label['value'] # class name + # if cls not in names: + # names.append(cls) + + # line = names.index(cls), *polygon[0] # YOLO format (class_index, polygon) + # with open(label_path, 'a') as f: + # f.write(('%g ' * len(line)).rstrip() % line + '\n') + + ###################################### + #for Object detection + ###################################### for label in img['Label']['objects']: # box top, left, h, w = label['bbox'].values() # top, left, height, width From 7b1d8b765e6072169a33e47eb16aadaf1fd1f7e6 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Wed, 29 Mar 2023 13:23:46 +0000 Subject: [PATCH 2/2] 'Refactored by Sourcery' --- labelbox_json2yolo.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/labelbox_json2yolo.py b/labelbox_json2yolo.py index 741d4f8..240249a 100644 --- a/labelbox_json2yolo.py +++ b/labelbox_json2yolo.py @@ -27,16 +27,17 @@ def getPolygons(url): for contour in contours: epsilon = 0.001 * cv2.arcLength(contour, True) contour_approx = cv2.approxPolyDP(contour, epsilon, True) - + contours_approx.append(contour_approx) polygon = (contour_approx.flatten().reshape(-1, 2) / np.array([w, h])).tolist() polygons.append(polygon) cv2.drawContours(image, contours_approx, -1, 128, line_width) #format the data correctly result = [[f"{x},{y}" for x, y in sublist] for sublist in polygons] - polygons = [[float(x) for pair in sublist for x in pair.split(',')] for sublist in result] - - return polygons + return [ + [float(x) for pair in sublist for x in pair.split(',')] + for sublist in result + ] def convert(file, zip=True): # Convert Labelbox JSON labels to YOLO labels