-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
78 lines (47 loc) · 1.6 KB
/
Copy pathmain.py
File metadata and controls
78 lines (47 loc) · 1.6 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
69
70
71
72
73
74
75
76
77
78
from os import path
from glob import glob
import numpy as np
import aug
LOCAL_DIR = path.dirname(path.realpath(__file__))
DATASET = path.join(LOCAL_DIR, "v1")
folders = ["train", "valid", "test"]
augmentation_number = 20
def chunks(xs, n):
n = max(1, n)
return [xs[i:i+n] for i in range(0, len(xs), n)]
for folder in folders:
labels_dir = path.join(LOCAL_DIR, f"v1/{folder}/labels")
images_dir = path.join(LOCAL_DIR, f"v1/{folder}/images")
labels = list(glob(path.join(labels_dir, "*.txt")))
print(labels)
for label in labels:
print(label)
image = glob(path.join(images_dir, path.basename(label)[:-4]) + "*")[0]
with open(label) as f:
c = f.read()
points = {}
for i in c.split("\n"):
points[i.split(" ")[0]] = chunks(i.split(" ")[1:], 2)
points_all = []
points_lens = {}
for k,v in points.items():
points_all += v
points_lens[k]=len(v)
imgs, pts = aug.Generate(image, np.array(points_all, dtype=np.float64), augmentation_number)
for index in range(augmentation_number):
augmented_image = path.join(images_dir, f"{index}-{path.basename(image)}")
augmented_label = path.join(labels_dir, f"{index}-{path.basename(label)}")
pt = list(pts[index])
augmented_label_text = []
for k,v in points_lens.items():
text = []
for i in pt[:v]:
text.append(" ".join([str(x) for x in i]))
augmented_label_text.append(f"{int(k)} {' '.join(text)}")
#print(augmented_label_text)
del pt[:v]
#write to label & img
with open(augmented_label, "w") as f:
f.write("\n".join(augmented_label_text))
imgs[index].save(augmented_image)
#print(imgs, pts)