forked from foamliu/Autoencoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_process.py
More file actions
136 lines (111 loc) · 4 KB
/
pre_process.py
File metadata and controls
136 lines (111 loc) · 4 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
import os
import random
import tarfile
import cv2 as cv
import numpy as np
import scipy.io
from tqdm import tqdm
from config import imsize
def ensure_folder(folder):
if not os.path.exists(folder):
os.makedirs(folder)
def save_train_data(fnames, bboxes):
src_folder = 'data/cars_train'
num_samples = len(fnames)
train_split = 0.8
num_train = int(round(num_samples * train_split))
train_indexes = random.sample(range(num_samples), num_train)
print('train_indexes: '.format(str(train_indexes)))
for i in tqdm(range(num_samples)):
fname = fnames[i]
(x1, y1, x2, y2) = bboxes[i]
src_path = os.path.join(src_folder, fname)
src_image = cv.imread(src_path)
height, width = src_image.shape[:2]
# margins of 16 pixels
margin = 16
x1 = max(0, x1 - margin)
y1 = max(0, y1 - margin)
x2 = min(x2 + margin, width)
y2 = min(y2 + margin, height)
# print(fname)
if i in train_indexes:
dst_folder = 'data/train'
else:
dst_folder = 'data/valid'
dst_path = os.path.join(dst_folder, fname)
crop_image = src_image[y1:y2, x1:x2]
dst_img = cv.resize(src=crop_image, dsize=(img_height, img_width))
cv.imwrite(dst_path, dst_img)
print('\n')
def save_test_data(fnames, bboxes):
src_folder = 'data/cars_test'
dst_folder = 'data/test'
num_samples = len(fnames)
for i in tqdm(range(num_samples)):
fname = fnames[i]
(x1, y1, x2, y2) = bboxes[i]
src_path = os.path.join(src_folder, fname)
src_image = cv.imread(src_path)
height, width = src_image.shape[:2]
# margins of 16 pixels
margin = 16
x1 = max(0, x1 - margin)
y1 = max(0, y1 - margin)
x2 = min(x2 + margin, width)
y2 = min(y2 + margin, height)
# print(fname)
dst_path = os.path.join(dst_folder, fname)
crop_image = src_image[y1:y2, x1:x2]
dst_img = cv.resize(src=crop_image, dsize=(img_height, img_width))
cv.imwrite(dst_path, dst_img)
print('\n')
def process_data(usage):
print("Processing {} data...".format(usage))
cars_annos = scipy.io.loadmat('data/devkit/cars_{}_annos'.format(usage))
annotations = cars_annos['annotations']
annotations = np.transpose(annotations)
fnames = []
bboxes = []
for annotation in annotations:
bbox_x1 = annotation[0][0][0][0]
bbox_y1 = annotation[0][1][0][0]
bbox_x2 = annotation[0][2][0][0]
bbox_y2 = annotation[0][3][0][0]
if usage == 'train':
class_id = annotation[0][4][0][0]
fname = annotation[0][5][0]
else:
fname = annotation[0][4][0]
bboxes.append((bbox_x1, bbox_y1, bbox_x2, bbox_y2))
fnames.append(fname)
if usage == 'train':
save_train_data(fnames, bboxes)
else:
save_test_data(fnames, bboxes)
if __name__ == '__main__':
# parameters
img_width, img_height = imsize, imsize
print('Extracting data/cars_train.tgz...')
# if not os.path.exists('data/cars_train'):
with tarfile.open('data/cars_train.tgz', "r:gz") as tar:
tar.extractall('data')
print('Extracting data/cars_test.tgz...')
# if not os.path.exists('data/cars_test'):
with tarfile.open('data/cars_test.tgz', "r:gz") as tar:
tar.extractall('data')
print('Extracting data/car_devkit.tgz...')
# if not os.path.exists('data/devkit'):
with tarfile.open('data/car_devkit.tgz', "r:gz") as tar:
tar.extractall('data')
cars_meta = scipy.io.loadmat('data/devkit/cars_meta')
class_names = cars_meta['class_names'] # shape=(1, 196)
class_names = np.transpose(class_names)
print('class_names.shape: ' + str(class_names.shape))
print('Sample class_name: [{}]'.format(class_names[8][0][0]))
ensure_folder('data/train')
ensure_folder('data/valid')
ensure_folder('data/test')
process_data('train')
process_data('test')