From e686797d614d5d1f963c85c8b7346cb4e8536fcd Mon Sep 17 00:00:00 2001 From: zranguai Date: Sat, 25 Nov 2023 10:58:23 +0800 Subject: [PATCH 1/2] add delete_dirs function --- utils.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/utils.py b/utils.py index 2fcbf71..31f7b43 100644 --- a/utils.py +++ b/utils.py @@ -69,11 +69,25 @@ def split_indices(x, train=0.9, test=0.1, validate=0.0, shuffle=True): # split return v[:i], v[i:j], v[j:k] # return indices +def delete_dirs(str_dir): + # Delete files in folders + ls = os.listdir(str_dir) + for name in ls: + c_path = os.path.join(str_dir, name) + if os.path.isdir(c_path): + delete_dirs(c_path) + else: + if c_path.endswith(".jpg") or c_path.endswith(".bmp") or c_path.endswith(".jpeg") or\ + c_path.endswith(".png") or c_path.endswith(".tif") or c_path.endswith(".tiff") or\ + c_path.endswith(".dng") or c_path.endswith(".txt"): + os.remove(c_path) + + def make_dirs(dir='new_dir/'): # Create folders dir = Path(dir) if dir.exists(): - shutil.rmtree(dir) # delete dir + delete_dirs(dir) # delete dir for p in dir, dir / 'labels', dir / 'images': p.mkdir(parents=True, exist_ok=True) # make dir return dir From 5c621924b5d64614c57cbc590697e9ba864505f7 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Sat, 25 Nov 2023 03:15:29 +0000 Subject: [PATCH 2/2] 'Refactored by Sourcery' --- utils.py | 110 +++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 98 insertions(+), 12 deletions(-) diff --git a/utils.py b/utils.py index 31f7b43..8fd40a6 100644 --- a/utils.py +++ b/utils.py @@ -76,11 +76,10 @@ def delete_dirs(str_dir): c_path = os.path.join(str_dir, name) if os.path.isdir(c_path): delete_dirs(c_path) - else: - if c_path.endswith(".jpg") or c_path.endswith(".bmp") or c_path.endswith(".jpeg") or\ + elif c_path.endswith(".jpg") or c_path.endswith(".bmp") or c_path.endswith(".jpeg") or\ c_path.endswith(".png") or c_path.endswith(".tif") or c_path.endswith(".tiff") or\ c_path.endswith(".dng") or c_path.endswith(".txt"): - os.remove(c_path) + os.remove(c_path) def make_dirs(dir='new_dir/'): @@ -164,17 +163,104 @@ def flatten_recursive_folders(path='../../Downloads/data/sm4/'): # from utils i image = parent / f json = Path(parent.replace('images', 'json')) / str(f).replace(suffix, '.json') - os.system("cp '%s' '%s'" % (json, json_new)) - os.system("cp '%s' '%s'" % (image, image_new)) - # cv2.imwrite(str(image_new), cv2.imread(str(image))) + os.system(f"cp '{json}' '{json_new}'") + os.system(f"cp '{image}' '{image_new}'") + # cv2.imwrite(str(image_new), cv2.imread(str(image))) print('Flattening complete: %g jsons and images' % n) def coco91_to_coco80_class(): # converts 80-index (val2014) to 91-index (paper) - # https://tech.amikelive.com/node-718/what-object-categories-labels-are-in-coco-dataset/ - x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, None, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, None, 24, 25, None, - None, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, None, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, - 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 + return [ + 0, + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + None, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + None, + 24, + 25, + None, + None, + 26, + 27, + 28, + 29, + 30, + 31, + 32, + 33, + 34, + 35, + 36, + 37, + 38, + 39, + None, + 40, + 41, + 42, + 43, + 44, + 45, + 46, + 47, + 48, + 49, + 50, + 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, + ]