forked from skhadem/3D-BoundingBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Run.py
201 lines (147 loc) · 6.34 KB
/
Run.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
"""
Images must be in ./Kitti/testing/image_2/ and camera matricies in ./Kitti/testing/calib/
Uses YOLO to obtain 2D box, PyTorch to get 3D box, plots both
SPACE bar for next image, any other key to exit
"""
from torch_lib.Dataset import *
from library.Math import *
from library.Plotting import *
from torch_lib import Model, ClassAverages
from yolo.yolo import cv_Yolo
import os
import time
import numpy as np
import cv2
import torch
import torch.nn as nn
from torch.autograd import Variable
from torchvision.models import vgg
import argparse
def str2bool(v):
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
parser = argparse.ArgumentParser()
parser.add_argument("--image-dir", default="eval/image_2/",
help="Relative path to the directory containing images to detect. Default \
is eval/image_2/")
# TODO: support multiple cal matrix input types
parser.add_argument("--cal-dir", default="camera_cal/",
help="Relative path to the directory containing camera calibration form KITTI. \
Default is camera_cal/")
parser.add_argument("--video", action="store_true",
help="Weather or not to advance frame-by-frame as fast as possible. \
By default, this will pull images from ./eval/video")
parser.add_argument("--show-yolo", action="store_true",
help="Show the 2D BoundingBox detecions on a separate image")
parser.add_argument("--hide-debug", action="store_true",
help="Supress the printing of each 3d location")
def plot_regressed_3d_bbox(img, cam_to_img, box_2d, dimensions, alpha, theta_ray, img_2d=None):
# the math! returns X, the corners used for constraint
location, X = calc_location(dimensions, cam_to_img, box_2d, alpha, theta_ray)
orient = alpha + theta_ray
if img_2d is not None:
plot_2d_box(img_2d, box_2d)
plot_3d_box(img, cam_to_img, orient, dimensions, location) # 3d boxes
return location
def main():
FLAGS = parser.parse_args()
# load torch
weights_path = os.path.abspath(os.path.dirname(__file__)) + '/weights'
model_lst = [x for x in sorted(os.listdir(weights_path)) if x.endswith('.pkl')]
if len(model_lst) == 0:
print('No previous model found, please train first!')
exit()
else:
print('Using previous model %s'%model_lst[-1])
my_vgg = vgg.vgg19_bn(pretrained=True)
# TODO: load bins from file or something
model = Model.Model(features=my_vgg.features, bins=2).cuda()
checkpoint = torch.load(weights_path + '/%s'%model_lst[-1])
model.load_state_dict(checkpoint['model_state_dict'])
model.eval()
# load yolo
yolo_path = os.path.abspath(os.path.dirname(__file__)) + '/weights'
yolo = cv_Yolo(yolo_path)
averages = ClassAverages.ClassAverages()
# TODO: clean up how this is done. flag?
angle_bins = generate_bins(2)
image_dir = FLAGS.image_dir
cal_dir = FLAGS.cal_dir
if FLAGS.video:
if FLAGS.image_dir == "eval/image_2/" and FLAGS.cal_dir == "camera_cal/":
image_dir = "eval/video/2011_09_26/image_2/"
cal_dir = "eval/video/2011_09_26/"
img_path = os.path.abspath(os.path.dirname(__file__)) + "/" + image_dir
# using P_rect from global calibration file
calib_path = os.path.abspath(os.path.dirname(__file__)) + "/" + cal_dir
calib_file = calib_path + "calib_cam_to_cam.txt"
# using P from each frame
# calib_path = os.path.abspath(os.path.dirname(__file__)) + '/Kitti/testing/calib/'
try:
ids = [x.split('.')[0] for x in sorted(os.listdir(img_path))]
except:
print("\nError: no images in %s"%img_path)
exit()
for img_id in ids:
start_time = time.time()
img_file = img_path + img_id + ".png"
# P for each frame
# calib_file = calib_path + id + ".txt"
truth_img = cv2.imread(img_file)
img = np.copy(truth_img)
yolo_img = np.copy(truth_img)
detections = yolo.detect(yolo_img)
for detection in detections:
if not averages.recognized_class(detection.detected_class):
continue
# this is throwing when the 2d bbox is invalid
# TODO: better check
try:
detectedObject = DetectedObject(img, detection.detected_class, detection.box_2d, calib_file)
except:
continue
theta_ray = detectedObject.theta_ray
input_img = detectedObject.img
proj_matrix = detectedObject.proj_matrix
box_2d = detection.box_2d
detected_class = detection.detected_class
input_tensor = torch.zeros([1,3,224,224]).cuda()
input_tensor[0,:,:,:] = input_img
[orient, conf, dim] = model(input_tensor)
orient = orient.cpu().data.numpy()[0, :, :]
conf = conf.cpu().data.numpy()[0, :]
dim = dim.cpu().data.numpy()[0, :]
dim += averages.get_item(detected_class)
argmax = np.argmax(conf)
orient = orient[argmax, :]
cos = orient[0]
sin = orient[1]
alpha = np.arctan2(sin, cos)
alpha += angle_bins[argmax]
alpha -= np.pi
if FLAGS.show_yolo:
location = plot_regressed_3d_bbox(img, proj_matrix, box_2d, dim, alpha, theta_ray, truth_img)
else:
location = plot_regressed_3d_bbox(img, proj_matrix, box_2d, dim, alpha, theta_ray)
if not FLAGS.hide_debug:
print('Estimated pose: %s'%location)
if FLAGS.show_yolo:
numpy_vertical = np.concatenate((truth_img, img), axis=0)
cv2.imshow('SPACE for next image, any other key to exit', numpy_vertical)
else:
cv2.imshow('3D detections', img)
if not FLAGS.hide_debug:
print("\n")
print('Got %s poses in %.3f seconds'%(len(detections), time.time() - start_time))
print('-------------')
if FLAGS.video:
cv2.waitKey(1)
else:
if cv2.waitKey(0) != 32: # space bar
exit()
if __name__ == '__main__':
main()