-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_train.py
More file actions
59 lines (53 loc) · 2.18 KB
/
create_train.py
File metadata and controls
59 lines (53 loc) · 2.18 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
import os
import shutil
WIDTH = 960
HEIGHT = 600
BOUNDING_BOX_SIZE = 160
IMG_SIZE = 640
def get_yolo_dataset(file_path, transform=None):
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
lines = [line.replace('\\', '/').replace('.frame', '.bmp').strip() for line in lines]
miss_list = []
i = 0
miss = 0
countall = 0
while i < len(lines)-miss:
line = lines[i]
from_path = f"./DATASET/FRAMES/{line}"
countall += 1
if os.path.exists(from_path):
print(from_path)
if transform:
transform(from_path)
index = countall-miss
to_path_frame = f"./train/images/{index}.bmp"
to_path_txt = f"./train/labels/{index}.txt"
os.makedirs(os.path.dirname(to_path_frame), exist_ok=True)
os.makedirs(os.path.dirname(to_path_txt), exist_ok=True)
shutil.copy(from_path, to_path_frame)
i += 1
# Check if all labels are 0 for the current image
all_labels_zero = True
with open(to_path_txt, 'w') as label_file:
while i < len(lines) and len(lines[i].split(', ')) > 1:
x, y, label = map(int, lines[i].split(", "))
if label != 0: # Skip the null class
all_labels_zero = False
label_file.write(f"{label-1} {x/WIDTH} {y/HEIGHT} {BOUNDING_BOX_SIZE/WIDTH} {BOUNDING_BOX_SIZE/HEIGHT}\n")
i += 1
# If all labels are 0, delete the files and decrement countall
if all_labels_zero:
os.remove(to_path_frame)
os.remove(to_path_txt)
countall -= 1
else:
miss += 1
i += 1
miss_list.append(from_path)
print('Processed:', countall)
print('Not in dir: ', miss)
with open('misses.txt', 'w', encoding='utf-8') as f:
f.write('\n'.join(miss_list))
file_path = 'DATASET/metadata/set.txt'
get_yolo_dataset(file_path)