-
Notifications
You must be signed in to change notification settings - Fork 5
/
annotations.py
64 lines (51 loc) · 2.21 KB
/
annotations.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import argparse
import os
import shutil
import cv2
def process(images, labels, output):
annotations = {}
with open(labels, 'r') as file:
lines = file.readlines()
for line in lines:
line = line.rstrip()
if line.endswith('.jpg'):
filename = os.path.splitext(os.path.basename(line))[0]
annotations[filename] = []
image_path = os.path.join(images, line)
shutil.copy2(image_path, output)
img = cv2.imread(image_path)
height, width, _ = img.shape
continue
data = line.split(' ')
if len(data) == 1:
continue
data = [max(0, int(x)) for x in line.split(' ')]
if data[0] > width or data[1] > height or data[2] > width or data[3] > height:
continue
cx = (data[0] + data[2] / 2) / width
cy = (data[1] + data[3] / 2) / height
w = data[2] / width
h = data[3] / height
label = f'0 {cx} {cy} {w} {h}'
if label not in annotations[filename]:
annotations[filename].append(label)
for filename in annotations:
with open(os.path.join(output, filename) + '.txt', 'w') as file:
for row in annotations[filename]:
file.write(row + '\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--train-images', default='data/WIDER_train/images')
parser.add_argument('--val-images', default='data/WIDER_val/images')
parser.add_argument('--train-labels', default='data/wider_face_split/wider_face_train_bbx_gt.txt')
parser.add_argument('--val-labels', default='data/wider_face_split/wider_face_val_bbx_gt.txt')
parser.add_argument('--output', default='data/widerface')
opt = parser.parse_args()
train_output = os.path.join(opt.output, 'train')
val_output = os.path.join(opt.output, 'val')
if not os.path.exists(train_output):
os.makedirs(train_output)
if not os.path.exists(val_output):
os.makedirs(val_output)
process(opt.train_images, opt.train_labels, train_output)
process(opt.val_images, opt.val_labels, val_output)