-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_classifier.py
More file actions
68 lines (57 loc) · 1.96 KB
/
Copy pathdata_classifier.py
File metadata and controls
68 lines (57 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import shutil
import json
import math
"""
Splits the data into train, valid, test datasets to be further processed
Download the data from the gdrive and have them in the carrying, normal, threat folders
"""
TRAIN_RATIO = 0.8
VALID_RATIO = 0.1
# train ratio is just 1 - those two
carrying_path = os.path.join(os.path.dirname(__file__), "carrying")
normal_path = os.path.join(os.path.dirname(__file__), "normal")
threat_path = os.path.join(os.path.dirname(__file__), "threat")
carrying_files = os.listdir(carrying_path)
normal_files = os.listdir(normal_path)
threat_files = os.listdir(threat_path)
annotations = {}
pwd = os.path.dirname(__file__)
data_dir = os.path.join(pwd, "data")
os.mkdir(data_dir)
test_dir = os.path.join(data_dir, "test")
os.mkdir(test_dir)
train_dir = os.path.join(data_dir, "train")
os.mkdir(train_dir)
valid_dir = os.path.join(data_dir, "valid")
os.mkdir(valid_dir)
for classification in ["carrying", "normal", "threat"]:
path = os.path.join(pwd, classification)
files = os.listdir(path)
length = len(files)
train_end_index = math.floor(TRAIN_RATIO * length)
valid_end_index = math.floor((TRAIN_RATIO + VALID_RATIO) * length)
train_set = files[:train_end_index]
valid_set = files[train_end_index:valid_end_index]
test_set = files[valid_end_index:]
for file in train_set:
annotations[file] = classification
shutil.move(
os.path.join(path, file),
os.path.join(train_dir, file)
)
for file in valid_set:
annotations[file] = classification
shutil.move(
os.path.join(path, file),
os.path.join(valid_dir, file)
)
for file in test_set:
annotations[file] = classification
shutil.move(
os.path.join(path, file),
os.path.join(test_dir, file)
)
os.rmdir(path)
with open(os.path.join(data_dir, "annotations.json"), mode="w") as json_f:
json.dump(annotations, json_f, indent=2)