From 8eb1b37d480f743da73d4977cf284fc22e27a6e6 Mon Sep 17 00:00:00 2001 From: Ayush Chaurasia Date: Thu, 25 Aug 2022 15:26:58 +0530 Subject: [PATCH 1/6] add setup.py --- json2yolo/__init__.py | 3 + .../general_json2yolo.py | 70 +------------------ .../labelbox_json2yolo.py | 2 +- utils.py => json2yolo/utils.py | 66 +++++++++++++++++ setup.py | 19 +++++ 5 files changed, 91 insertions(+), 69 deletions(-) create mode 100644 json2yolo/__init__.py rename general_json2yolo.py => json2yolo/general_json2yolo.py (83%) rename labelbox_json2yolo.py => json2yolo/labelbox_json2yolo.py (98%) rename utils.py => json2yolo/utils.py (71%) create mode 100644 setup.py diff --git a/json2yolo/__init__.py b/json2yolo/__init__.py new file mode 100644 index 0000000..0534a3f --- /dev/null +++ b/json2yolo/__init__.py @@ -0,0 +1,3 @@ +from json2yolo.general_json2yolo import convert_coco_json, convert_ath_json, convert_vott_json, convert_infolks_json + +__all__ = ["convert_coco_json", "convert_ath_json", "convert_vott_json", "convert_infolks_json"] \ No newline at end of file diff --git a/general_json2yolo.py b/json2yolo/general_json2yolo.py similarity index 83% rename from general_json2yolo.py rename to json2yolo/general_json2yolo.py index f7a3989..22ee1ef 100644 --- a/general_json2yolo.py +++ b/json2yolo/general_json2yolo.py @@ -4,7 +4,7 @@ import pandas as pd from PIL import Image -from utils import * +from json2yolo.utils import * # Convert INFOLKS JSON file into YOLO-format labels ---------------------------- @@ -295,78 +295,12 @@ def convert_coco_json(json_dir='../coco/annotations/', use_segments=False, cls91 with open((fn / f).with_suffix('.txt'), 'a') as file: file.write(('%g ' * len(line)).rstrip() % line + '\n') -def min_index(arr1, arr2): - """Find a pair of indexes with the shortest distance. - Args: - arr1: (N, 2). - arr2: (M, 2). - Return: - a pair of indexes(tuple). - """ - # (N, M) - dis = ((arr1[:, None, :] - arr2[None, :, :]) ** 2).sum(-1) - index = np.unravel_index(np.argmin(dis, axis=None), dis.shape) - return index - - -def merge_multi_segment(segments): - """Merge multi segments to one list. - Find the coordinates with min distance between each segment, - then connect these coordinates with one thin line to merge all - segments into one. - - Args: - segments(List(List)): original segmentations in coco's json file. - like [segmentation1, segmentation2,...], - each segmentation is a list of coordinates. - """ - s = [] - segments = [np.array(i).reshape(-1, 2) for i in segments] - idx_list = [[] for _ in range(len(segments))] - - # record the indexes with min distance between each segment - for i in range(1, len(segments)): - idx1, idx2 = min_index(segments[i - 1], segments[i]) - idx_list[i - 1].append(idx1) - idx_list[i].append(idx2) - - # use two round to connect all the segments - for k in range(2): - # forward connection - if k == 0: - for i, idx in enumerate(idx_list): - # middle segments have two indexes - # reverse the index of middle segments - if len(idx) == 2 and idx[0] > idx[1]: - idx = idx[::-1] - segments[i] = segments[i][::-1, :] - - segments[i] = np.roll(segments[i], -idx[0], axis=0) - segments[i] = np.concatenate([segments[i], segments[i][0:1]]) - # deal with the first segment and the last one - if i == 0 or i == (len(idx_list) - 1): - s.append(segments[i]) - # deal with the middle ones - else: - idx = [0, idx[1] - idx[0]] - s.append(segments[i][idx[0]:idx[1] + 1]) - - # backward connection - else: - for i in range(len(idx_list) - 1, -1, -1): - if i != 0 and i != (len(idx_list) - 1): - idx = idx_list[i] - nidx = abs(idx[1] - idx[0]) - s.append(segments[i][nidx:]) - return s - - if __name__ == '__main__': source = 'COCO' if source == 'COCO': - convert_coco_json('../../Downloads/Objects365') # directory with *.json + convert_coco_json('data/train') # directory with *.json elif source == 'infolks': # Infolks https://infolks.info/ convert_infolks_json(name='out', diff --git a/labelbox_json2yolo.py b/json2yolo/labelbox_json2yolo.py similarity index 98% rename from labelbox_json2yolo.py rename to json2yolo/labelbox_json2yolo.py index 843aaf8..4ef3d12 100644 --- a/labelbox_json2yolo.py +++ b/json2yolo/labelbox_json2yolo.py @@ -7,7 +7,7 @@ from PIL import Image from tqdm import tqdm -from utils import make_dirs +from json2yolo.utils import make_dirs def convert(file, zip=True): diff --git a/utils.py b/json2yolo/utils.py similarity index 71% rename from utils.py rename to json2yolo/utils.py index 2fcbf71..aad7f61 100644 --- a/utils.py +++ b/json2yolo/utils.py @@ -164,3 +164,69 @@ def coco91_to_coco80_class(): # converts 80-index (val2014) to 91-index (paper) 51, 52, 53, 54, 55, 56, 57, 58, 59, None, 60, None, None, 61, None, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, None, 73, 74, 75, 76, 77, 78, 79, None] return x + + +def min_index(arr1, arr2): + """Find a pair of indexes with the shortest distance. + Args: + arr1: (N, 2). + arr2: (M, 2). + Return: + a pair of indexes(tuple). + """ + # (N, M) + dis = ((arr1[:, None, :] - arr2[None, :, :]) ** 2).sum(-1) + index = np.unravel_index(np.argmin(dis, axis=None), dis.shape) + return index + + +def merge_multi_segment(segments): + """Merge multi segments to one list. + Find the coordinates with min distance between each segment, + then connect these coordinates with one thin line to merge all + segments into one. + + Args: + segments(List(List)): original segmentations in coco's json file. + like [segmentation1, segmentation2,...], + each segmentation is a list of coordinates. + """ + s = [] + segments = [np.array(i).reshape(-1, 2) for i in segments] + idx_list = [[] for _ in range(len(segments))] + + # record the indexes with min distance between each segment + for i in range(1, len(segments)): + idx1, idx2 = min_index(segments[i - 1], segments[i]) + idx_list[i - 1].append(idx1) + idx_list[i].append(idx2) + + # use two round to connect all the segments + for k in range(2): + # forward connection + if k == 0: + for i, idx in enumerate(idx_list): + # middle segments have two indexes + # reverse the index of middle segments + if len(idx) == 2 and idx[0] > idx[1]: + idx = idx[::-1] + segments[i] = segments[i][::-1, :] + + segments[i] = np.roll(segments[i], -idx[0], axis=0) + segments[i] = np.concatenate([segments[i], segments[i][0:1]]) + # deal with the first segment and the last one + if i == 0 or i == (len(idx_list) - 1): + s.append(segments[i]) + # deal with the middle ones + else: + idx = [0, idx[1] - idx[0]] + s.append(segments[i][idx[0]:idx[1] + 1]) + + # backward connection + else: + for i in range(len(idx_list) - 1, -1, -1): + if i != 0 and i != (len(idx_list) - 1): + idx = idx_list[i] + nidx = abs(idx[1] - idx[0]) + s.append(segments[i][nidx:]) + return s diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..36043d6 --- /dev/null +++ b/setup.py @@ -0,0 +1,19 @@ +from setuptools import setup + +setup(name='json2yolo', + version='0.0.1', + author='Ultralytics', + packages=['json2yolo'], + description='Dataset converter for yolo', + license='GPL', + install_requires=[ + "numpy", + "opencv-python>=4.1.2", + "pandas", + "Pillow", + "pyYAML", + "requests", + "tqdm", + + ], +) \ No newline at end of file From c519ebece989e297af1bf8a58a1c123fbf862dc3 Mon Sep 17 00:00:00 2001 From: Ayush Chaurasia Date: Thu, 25 Aug 2022 15:28:01 +0530 Subject: [PATCH 2/6] update --- requirements.txt | 9 --------- 1 file changed, 9 deletions(-) delete mode 100755 requirements.txt diff --git a/requirements.txt b/requirements.txt deleted file mode 100755 index a8a0038..0000000 --- a/requirements.txt +++ /dev/null @@ -1,9 +0,0 @@ -# pip install -r requirements.txt - -numpy -opencv-python>=4.1.2 -pandas -Pillow -pyYAML -requests -tqdm From f7f74d1a838bd0cc7578801f37cfac58f7965da9 Mon Sep 17 00:00:00 2001 From: Ayush Chaurasia Date: Thu, 25 Aug 2022 15:32:44 +0530 Subject: [PATCH 3/6] Update __init__.py --- json2yolo/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json2yolo/__init__.py b/json2yolo/__init__.py index 0534a3f..e22c9d6 100644 --- a/json2yolo/__init__.py +++ b/json2yolo/__init__.py @@ -1,3 +1,3 @@ from json2yolo.general_json2yolo import convert_coco_json, convert_ath_json, convert_vott_json, convert_infolks_json -__all__ = ["convert_coco_json", "convert_ath_json", "convert_vott_json", "convert_infolks_json"] \ No newline at end of file +__all__ = ["convert_coco_json", "convert_ath_json", "convert_vott_json", "convert_infolks_json"] From 0bedc8167c9735b01f108909608028884d8058de Mon Sep 17 00:00:00 2001 From: Ayush Chaurasia Date: Thu, 25 Aug 2022 15:33:20 +0530 Subject: [PATCH 4/6] Update general_json2yolo.py --- json2yolo/general_json2yolo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/json2yolo/general_json2yolo.py b/json2yolo/general_json2yolo.py index 22ee1ef..745521e 100644 --- a/json2yolo/general_json2yolo.py +++ b/json2yolo/general_json2yolo.py @@ -300,7 +300,7 @@ def convert_coco_json(json_dir='../coco/annotations/', use_segments=False, cls91 source = 'COCO' if source == 'COCO': - convert_coco_json('data/train') # directory with *.json + convert_coco_json('../../Downloads/Objects365') # directory with *.json elif source == 'infolks': # Infolks https://infolks.info/ convert_infolks_json(name='out', From b756b439631635521b02b429b474ccf0dc70fd01 Mon Sep 17 00:00:00 2001 From: Ayush Chaurasia Date: Thu, 25 Aug 2022 15:33:41 +0530 Subject: [PATCH 5/6] Update setup.py --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 36043d6..564bed0 100644 --- a/setup.py +++ b/setup.py @@ -16,4 +16,4 @@ "tqdm", ], -) \ No newline at end of file +) From 2a666ce7e021fe7284723dadf0569c1b992f220b Mon Sep 17 00:00:00 2001 From: Ayush Chaurasia Date: Fri, 26 Aug 2022 00:21:00 +0530 Subject: [PATCH 6/6] Update setup.py --- setup.py | 1 - 1 file changed, 1 deletion(-) diff --git a/setup.py b/setup.py index 564bed0..c1b14e3 100644 --- a/setup.py +++ b/setup.py @@ -14,6 +14,5 @@ "pyYAML", "requests", "tqdm", - ], )