-
Notifications
You must be signed in to change notification settings - Fork 25
/
plotting.py
141 lines (115 loc) · 4.51 KB
/
plotting.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
import matplotlib.pyplot as plt
import numpy as np
import cv2
from analyze import dict_dataset_names
dict_dataset_places = {
'oxford_town': 'Oxford Town Urban Street',
'grand_central': 'New York City Grand Central Terminal',
'mall': 'An Indoor Mall'
}
def get_roi_pts(dataset, roi_raw, matrix_c2w):
if dataset == 'oxford_town':
y1, y2, x1, x2 = roi_raw
elif dataset == 'mall':
x1, x2, y1, y2 = roi_raw
elif dataset == 'grand_central':
x1, x2, y1, y2 = roi_raw
else:
raise Exception('Invalid dataset.')
# x1, x2, y1, y2 = roi_raw
pts_world = np.array([[x1, y1], [x1, y2], [x2, y2], [x2, y1], [x1, y1]])
pts_cam = []
for pt_world in pts_world:
pt_cam = np.linalg.inv(matrix_c2w) @ np.array([[pt_world[0]], [pt_world[1]], [1]]).reshape(3)
pts_cam.append(pt_cam / pt_cam[-1])
pts_cam = np.array(pts_cam)
return pts_world, pts_cam[:, :2]
def plot_frame_one_row(dataset, img_raw, pts_roi_cam, pts_roi_world, pts_w, pairs):
b, g, r = cv2.split(img_raw) # get b,g,r
img_raw = cv2.merge([r, g, b]) # switch it to rgb
if dataset == 'oxford_town':
sub_3_lim = (20, -10, 0, 30)
pts_roi_world[:, [0, 1]] = pts_roi_world[:, [1, 0]]
elif dataset == 'mall':
sub_3_lim = (-10, 10, 10, -10)
elif dataset == 'grand_central':
sub_3_lim = (-10, 30, 36, -4)
else:
raise Exception('Invalid dataset.')
# plot
fig = plt.figure(figsize=(8.77, 3.06))
fig.subplots_adjust(left=0.08, bottom=0.15, right=0.98, top=0.90, wspace=0.3)
fig.suptitle('%s (%s)' % (dict_dataset_places[dataset], dict_dataset_names[dataset]))
# subplot 1 - camera view
a = fig.add_subplot(1, 3, (1, 2))
plt.imshow(img_raw)
a.plot(pts_roi_cam[:, 0], pts_roi_cam[:, 1], '--b')
# a.set_title('Video')
a.set_xlabel('x position (pixel)')
a.set_ylabel('y position (pixel)')
# subplot 2 - bird eye view social distancing
a = fig.add_subplot(1, 3, 3)
# a.set_title('BEV - social distancing')
a.plot(pts_roi_world[:, 0], pts_roi_world[:, 1], '--b')
a.plot(pts_w[:, 0], pts_w[:, 1], 'og', alpha=0.5)
for pair in pairs:
data = np.array([pts_w[pair[0]], pts_w[pair[1]]])
a.plot(data[:, 0], data[:, 1], '-r')
a.axis('equal')
a.grid()
a.set_xlabel('x position (meter)')
a.set_ylabel('y position (meter)')
a.set(xlim=(sub_3_lim[0], sub_3_lim[1]), ylim=(sub_3_lim[2], sub_3_lim[3]))
return fig
def plot_frame(dataset, img_raw, img_bev_bkgd_10x, pts_roi_cam, pts_roi_world, pts_w, pairs):
b, g, r = cv2.split(img_raw) # get b,g,r
img_raw = cv2.merge([r, g, b]) # switch it to rgb
b, g, r = cv2.split(img_bev_bkgd_10x) # get b,g,r
img_bev_bkgd_10x = cv2.merge([r, g, b]) # switch it to rgb
if dataset == 'oxford_town':
sub_2_lim = (300, -100, 0, 400)
sub_3_lim = (20, -20, 0, 40)
pts_roi_world[:, [0, 1]] = pts_roi_world[:, [1, 0]]
elif dataset == 'mall':
sub_2_lim = (-150, 150, 100, -200)
sub_3_lim = (-15, 15, 10, -20)
elif dataset == 'grand_central':
sub_2_lim = (-100, 300, 400, -100)
sub_3_lim = (-15, 35, 40, -10)
else:
raise Exception('Invalid dataset.')
ps_w_10x = pts_w * 10
# plot
fig = plt.figure(figsize=(10, 10))
# subplot 1 - camera view
a = fig.add_subplot(2, 1, 1)
plt.imshow(img_raw)
a.plot(pts_roi_cam[:, 0], pts_roi_cam[:, 1], '--b')
a.set_title('Video')
# subplot 2 - bird eye view background
a = fig.add_subplot(2, 2, 3)
plt.imshow(img_bev_bkgd_10x)
a.set_title('BEV')
# a.plot(ps_w_10x[:, 1], ps_w_10x[:, 0], 'or', alpha=0.5)
a.plot(ps_w_10x[:, 0], ps_w_10x[:, 1], 'or', alpha=0.5)
a.axis('equal')
a.grid()
a.set_xlabel('0.1m / pixel')
a.set_ylabel('0.1m / pixel')
a.set(xlim=(sub_2_lim[0], sub_2_lim[1]), ylim=(sub_2_lim[2], sub_2_lim[3]))
# subplot 3 - bird eye view social distancing
a = fig.add_subplot(2, 2, 4)
a.set_title('BEV - social distancing')
# a.plot(pts_w[:, 1], pts_w[:, 0], 'or', alpha=0.5)
a.plot(pts_w[:, 0], pts_w[:, 1], 'or', alpha=0.5)
for pair in pairs:
data = np.array([pts_w[pair[0]], pts_w[pair[1]]])
# a.plot(data[:, 1], data[:, 0], '-g')
a.plot(data[:, 0], data[:, 1], '-g')
a.plot(pts_roi_world[:, 0], pts_roi_world[:, 1], '--b')
a.axis('equal')
a.grid()
a.set_xlabel('meters')
a.set_ylabel('meters')
a.set(xlim=(sub_3_lim[0], sub_3_lim[1]), ylim=(sub_3_lim[2], sub_3_lim[3]))
return fig