forked from xuannianz/ssd_keras
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc.py
214 lines (191 loc) · 9 KB
/
misc.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import os.path as osp
import csv
import cv2
import pandas as pd
import numpy as np
import logging
import sys
import random
from keras.applications import vgg16
logger = logging.getLogger('irf_sh')
logger.setLevel(logging.DEBUG) # default log level
formatter = logging.Formatter("%(asctime)s %(name)-8s %(levelname)-8s %(lineno)-4d %(message)s") # output format
sh = logging.StreamHandler(stream=sys.stdout) # output to standard output
sh.setFormatter(formatter)
logger.addHandler(sh)
def split_labels_csv(images_dir):
labels_csv_path = osp.join(images_dir, 'labels_crowdai.csv')
df = pd.read_csv(labels_csv_path)
class_names = df['Label'].unique().tolist()
csv_reader = csv.reader(open(labels_csv_path, 'r'), delimiter=',')
# 忽略第一行 header
next(csv_reader)
# image file name 作为 key, [[class_id,xmin,ymin,xmax,ymax],...] 作为 value
annotations = {}
for row in csv_reader:
xmin = row[0]
ymin = row[1]
xmax = row[2]
ymax = row[3]
image_filename = row[4]
class_name = row[5]
# because id=0 reversed for background
class_id = class_names.index(class_name) + 1
if image_filename not in annotations:
annotations[image_filename] = []
annotations[image_filename].append([image_filename, xmin, ymin, xmax, ymax, class_id])
# 9218
num_images = len(annotations)
# 6452
num_train_images = int(num_images * 0.7)
# 2766
num_val_images = num_images - num_train_images
logger.debug(
'num_images={}, num_train_images={}, num_val_image={}'.format(num_images, num_train_images, num_val_images))
train_csv_obj = open(osp.join(images_dir, 'train.csv'), 'w')
train_csv_writer = csv.writer(train_csv_obj, delimiter=',')
val_csv_obj = open(osp.join(images_dir, 'val.csv'), 'w')
val_csv_writer = csv.writer(val_csv_obj, delimiter=',')
image_filenames = list(annotations.keys())
random.shuffle(image_filenames)
for image_filename in image_filenames[:num_train_images]:
for row in annotations[image_filename]:
train_csv_writer.writerow(row)
for image_filename in image_filenames[num_train_images:]:
for row in annotations[image_filename]:
val_csv_writer.writerow(row)
# split_labels_csv('/home/adam/.keras/datasets/udacity_self_driving_car/object-detection-crowdai')
def resize_and_split(images_dir, new_images_dir):
labels_csv_path = osp.join(images_dir, 'labels_crowdai.csv')
df = pd.read_csv(labels_csv_path)
class_names = df['Label'].unique().tolist()
csv_reader = csv.reader(open(labels_csv_path, 'r'), delimiter=',')
# 忽略第一行 header
next(csv_reader)
# image file name 作为 key, [[class_id,xmin,ymin,xmax,ymax],...] 作为 value
annotations = {}
for row in csv_reader:
xmin = int(round(int(row[0]) / 4))
ymin = int(round(int(row[1]) / 4))
xmax = int(round(int(row[2]) / 4))
ymax = int(round(int(row[3]) / 4))
image_filename = row[4]
class_name = row[5]
# because id=0 reversed for background
class_id = class_names.index(class_name) + 1
if image_filename not in annotations:
annotations[image_filename] = []
annotations[image_filename].append([image_filename, xmin, ymin, xmax, ymax, class_id])
new_image_path = osp.join(new_images_dir, image_filename)
if not osp.exists(new_image_path):
image = cv2.imread(osp.join(images_dir, image_filename))
image = cv2.resize(image, (480, 300), interpolation=cv2.INTER_LINEAR)
cv2.imwrite(new_image_path, image)
# 9218
num_images = len(annotations)
# 6452
num_train_images = int(num_images * 0.7)
# 2766
num_val_images = num_images - num_train_images
logger.debug(
'num_images={}, num_train_images={}, num_val_image={}'.format(num_images, num_train_images, num_val_images))
train_csv_obj = open(osp.join(new_images_dir, 'train.csv'), 'w')
train_csv_writer = csv.writer(train_csv_obj, delimiter=',')
val_csv_obj = open(osp.join(new_images_dir, 'val.csv'), 'w')
val_csv_writer = csv.writer(val_csv_obj, delimiter=',')
image_filenames = list(annotations.keys())
random.shuffle(image_filenames)
for image_filename in image_filenames[:num_train_images]:
for row in annotations[image_filename]:
train_csv_writer.writerow(row)
for image_filename in image_filenames[num_train_images:]:
for row in annotations[image_filename]:
val_csv_writer.writerow(row)
# resize_and_split('/home/adam/.keras/datasets/udacity_self_driving_car/-detection-crowdai',
# '/home/adam/.keras/datasets/udacity_self_driving_car/object-detection-crowdai-480-300')
def test_flip(image_path):
image = cv2.imread(image_path)
h_flipped_image1 = image[:, ::-1]
h_flipped_image2 = cv2.flip(image, 1)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', image)
cv2.namedWindow('h_flipped_image1', cv2.WINDOW_NORMAL)
cv2.imshow('h_flipped_image1', h_flipped_image1)
cv2.namedWindow('h_flipped_image2', cv2.WINDOW_NORMAL)
cv2.imshow('h_flipped_image2', h_flipped_image2)
cv2.waitKey(0)
# test_flip('/home/adam/.keras/datasets/udacity_self_driving_car/object-detection-crowdai/1479506172970816105.jpg')
def test_scale(image_path):
image = cv2.imread(image_path)
src_image = image.copy()
img_height, img_width = image.shape[:2]
# Compute the rotation matrix.
rotation_matrix = cv2.getRotationMatrix2D(center=(img_width / 2, img_height / 2),
angle=0,
scale=0.8)
image = cv2.warpAffine(image,
M=rotation_matrix,
dsize=(img_width, img_height),
borderMode=cv2.BORDER_CONSTANT,
borderValue=(0, 0, 0))
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', image)
gt_box = (386, 571, 509, 661)
top_left = np.array([gt_box[0], gt_box[1], 1])
bottom_right = np.array([gt_box[2], gt_box[3], 1])
new_top_left = np.dot(rotation_matrix, top_left)
new_bottom_right = np.dot(rotation_matrix, bottom_right)
new_top_left = np.round(new_top_left, decimals=0).astype(np.int).tolist()
new_bottom_right = np.round(new_bottom_right, decimals=0).astype(np.int).tolist()
# cv2.rectangle(src_image, (gt_box[0], gt_box[1]), (gt_box[2], gt_box[3]), (0, 255, 0), 2)
cv2.rectangle(image, tuple(new_top_left), tuple(new_bottom_right), (0, 255, 0), 2)
cv2.namedWindow('scaled_image', cv2.WINDOW_NORMAL)
cv2.imshow('scaled_image', image)
cv2.waitKey(0)
# test_scale('/home/adam/.keras/datasets/udacity_self_driving_car/object-detection-crowdai/1479506172970816105.jpg')
def test_rotate(image_path):
image = cv2.imread(image_path)
src_image = image.copy()
img_height, img_width = image.shape[:2]
# Compute the rotation matrix.
rotation_matrix = cv2.getRotationMatrix2D(center=(img_width / 2, img_height / 2),
angle=90,
scale=1)
cos_angle = np.abs(rotation_matrix[0, 0])
sin_angle = np.abs(rotation_matrix[0, 1])
# Compute the new bounding dimensions of the image.
img_width_new = int(img_height * sin_angle + img_width * cos_angle)
img_height_new = int(img_height * cos_angle + img_width * sin_angle)
# Adjust the rotation matrix to take into account the translation.
rotation_matrix[1, 2] += (img_height_new - img_height) / 2
rotation_matrix[0, 2] += (img_width_new - img_width) / 2
image = cv2.warpAffine(image,
M=rotation_matrix,
dsize=(img_width_new, img_height_new),
borderMode=cv2.BORDER_CONSTANT,
borderValue=(0, 0, 0))
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', image)
cv2.waitKey(0)
# test_rotate('/home/adam/.keras/datasets/udacity_self_driving_car/object-detection-crowdai/1479506172970816105.jpg')
def count_images(labels_csv_path):
csv_reader = csv.reader(open(labels_csv_path, 'r'), delimiter=',')
# 忽略第一行 header
next(csv_reader)
# image file name 作为 key, [[class_id,xmin,ymin,xmax,ymax],...] 作为 value
annotations = {}
for row in csv_reader:
xmin = int(round(int(row[1]) / 4))
xmax = int(round(int(row[2]) / 4))
ymin = int(round(int(row[3]) / 4))
ymax = int(round(int(row[4]) / 4))
image_filename = row[0]
# because id=0 reversed for background
class_id = row[-1]
if image_filename not in annotations:
annotations[image_filename] = []
annotations[image_filename].append([image_filename, xmin, ymin, xmax, ymax, class_id])
# 9218
num_images = len(annotations)
logger.debug('num_images={}'.format(num_images))
count_images('/home/adam/.keras/datasets/udacity_self_driving_car/ssd_datasets/labels_val.csv')