Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add delete_dirs function (Sourcery refactored) #59

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 110 additions & 10 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,24 @@ 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)
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)


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
Expand Down Expand Up @@ -150,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,
]