-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconvert_single_annotations_in_one.py
41 lines (34 loc) · 1.38 KB
/
convert_single_annotations_in_one.py
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
# %% imports
import os
folder_path = 'txt-files'
frame_width = 3840
frame_height = 2160
# %% action
out_path = os.path.join(folder_path, 'annotations.txt')
out_file = open(out_path, 'w')
for folder in os.walk(folder_path):
folder, folders, files = folder
for filename in files:
if filename == 'classes.txt' or filename == 'annotations.txt': continue
filepath = os.path.join(folder, filename)
index, ext = os.path.splitext(filename)
if ext != '.txt': continue
image_path = os.path.join(folder, f'{index}.png')
with open(filepath, 'r') as file:
lines = file.readlines()
for line in lines:
# todo: find correct alignment of values
_, x, y, width, height = line.split(' ')
x = float(x) * frame_width
y = float(y) * frame_height
width = float(width) * frame_width
height = float(height) * frame_height
half_width = width / 2
half_height = height / 2
left = round(x - half_width)
right = round(x + half_width)
top = round(y - half_height)
bottom = round(y + half_height)
output_str = f'{image_path} {left},{top},{right},{bottom},0\n'
out_file.write(output_str)
out_file.close()