-
Notifications
You must be signed in to change notification settings - Fork 157
/
preproc_annot.py
67 lines (59 loc) · 2.02 KB
/
preproc_annot.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
65
66
67
#!/usr/bin/env python
# -*- coding=utf-8 -*-
import pandas as pd
from skimage import io
import config
import os
import warnings
import numpy as np
def main():
# Load annot file
logos_frame = pd.read_csv(config.ANNOT_FILE, header=None, delim_whitespace=True)
print('Num. of annots:', len(logos_frame))
if not os.path.exists(config.CROPPED_IMAGES_DIR):
os.makedirs(config.CROPPED_IMAGES_DIR)
num_cropped_images = 0
annots = []
for i, row in logos_frame.iterrows():
img_name = row[0]
cls_name = row[1]
cls_idx = config.CLASS_NAMES.index(cls_name)
subset = row[2]
x1, y1, x2, y2 = row[3:]
w, h = (x2 - x1), (y2 - y1)
if w == 0 or h == 0:
print('Skip:', img_name)
continue
img = io.imread(os.path.join(config.IMAGES_DIR, img_name))
img_height, img_width, _ = img.shape
x = (x1 + x2) / 2
y = (y1 + y1) / 2
annot = ','.join([img_name, str(x1), str(y1), str(x2), str(y2), str(cls_idx)])
annots.append(annot)
num_cropped_images += 1
np.random.shuffle(annots)
num_train = int(num_cropped_images * 0.8)
with open(config.CROPPED_ANNOT_FILE, 'w') as f:
for annot in annots[:num_train]:
f.writelines(annot)
f.writelines("\n")
seen = set()
num_test = 0
with open(config.CROPPED_ANNOT_FILE_TEST, 'w') as f:
for annot in annots[num_train:]:
img_fn = annot.split(',')[0]
if img_fn in seen:
continue
f.writelines(annot)
f.writelines("\n")
seen.add(img_fn)
num_test += 1
print('Num. of annotations: {}(train) {}(test)'.format(num_train, num_test))
print('Created: {}'.format(config.CROPPED_ANNOT_FILE))
print('Created: {}'.format(config.CROPPED_ANNOT_FILE_TEST))
if __name__ == "__main__":
with warnings.catch_warnings():
# Supress low contrast warnings
warnings.simplefilter("ignore")
# Crop logo images
main()