-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObject.py
153 lines (125 loc) · 4.85 KB
/
Object.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
import numpy as np
from cfg import Params
from utils.projections import project_by_depth, project_by_ground_plane
class Object:
def __init__(self, category, left, top, width, height, corner_3d=None,
score=None, mask=None, x_3d=None, y_3d=None, z_3d=None, trun=None, oclu=None, alpha=None,
matched=False, encode_mask=False):
# from object detection
self.category = category
self.left = left
self.top = top
self.width = width
self.height = height
self.x1_2d = left
self.x2_2d = left + width
self.y1_2d = top
self.y2_2d = top + height
self.score = score
# from mrcnn
self.mask = mask
self.encode_mask = encode_mask
self.matched = matched # Ture is there is a matching with ground truth
self.matched_id = None # matched ground truth id
# from 3d gt
self.x_3d = x_3d
self.y_3d = y_3d
self.z_3d = z_3d
self.trun = trun
self.oclu = oclu
self.alpha = alpha
self.corner_3d = corner_3d
# from depthmap
self.depth = None
self.depth_conf = None
# from gpe
self.ground_plane = None
# from 2d to 3d projection
self.x_gnd_2d = int((self.x1_2d + self.x2_2d) / 2.0)
self.y_gnd_2d = self.y2_2d
self.x_3d_proj_d = None
self.y_3d_proj_d = None
self.z_3d_proj_d = None
self.x_3d_proj_gp = None
self.y_3d_proj_gp = None
self.z_3d_proj_gp = None
# from tracking
self.tracklet_id = None
self.x_3d_trk = None
self.y_3d_trk = None
self.z_3d_trk = None
self.x_3d_final = None
self.y_3d_final = None
self.z_3d_final = None
# from evaluation
self.eval_metrics = {}
def __str__(self):
return "Object <%s>: depth = %s, eval_metrics = %s" % (self.category, self.depth, self.eval_metrics)
def set_mask(self, mask):
self.mask = mask
def set_depth(self, depth, depth_conf=None):
self.depth = depth
self.depth_conf = depth_conf
def set_matched(self, matched_id):
"""set matched=True when this detection is matched with a ground truth"""
self.matched = True
self.matched_id = matched_id
def set_tracklet_id(self, tracklet_id):
self.tracklet_id = tracklet_id
def set_tracklet_refine_loc(self, x_3d, y_3d, z_3d, apply_final=True):
self.x_3d_trk = x_3d
self.y_3d_trk = y_3d
self.z_3d_trk = z_3d
if apply_final:
self.x_3d_final = self.x_3d_trk
self.y_3d_final = self.y_3d_trk
self.z_3d_final = self.z_3d_trk
def set_eval_metrics(self, metric_name, metric_value):
self.eval_metrics[metric_name] = metric_value
def project_by_depth(self, K, apply_final=True):
"""
project 2d points to 3d using camera intrinsics and depth from depthmap
:param K: intrinsic matrix
:return: pc_3d: object 3d point in camera coordinates, depth_conf
"""
puv = np.array([self.x_gnd_2d, self.y_gnd_2d])
puv = puv.reshape((2, 1))
pc_3d = project_by_depth(puv, K, self.depth)
pc_3d = np.squeeze(pc_3d)
self.x_3d_proj_d = pc_3d[0]
self.y_3d_proj_d = pc_3d[1]
self.z_3d_proj_d = pc_3d[2]
if apply_final:
self.x_3d_final = self.x_3d_proj_d
self.y_3d_final = self.y_3d_proj_d
self.z_3d_final = self.z_3d_proj_d
return pc_3d, self.depth_conf
def project_by_ground_plane(self, K, ground_plane=None, apply_final=True):
"""
project 2d points to 3d using camera intrinsics and ground plane
:param K: intrinsic matrix
:param ground_plane: ground plane for this object
:return: pc_3d: object 3d point in camera coordinates
"""
# input ground plane has higher priority
if ground_plane is None:
ground_plane = self.ground_plane
if ground_plane is None:
raise ValueError("Ground plane does not exist!")
puv = np.array([self.x_gnd_2d, self.y_gnd_2d])
puv = puv.reshape((2, 1))
pc_3d = project_by_ground_plane(puv, K, ground_plane)
pc_3d = np.squeeze(pc_3d)
self.x_3d_proj_gp = pc_3d[0]
self.y_3d_proj_gp = pc_3d[1]
self.z_3d_proj_gp = pc_3d[2]
if apply_final:
if self.depth_conf < Params['DEPTH_CONF_THRES']:
self.x_3d_final = self.x_3d_proj_gp
self.y_3d_final = self.y_3d_proj_gp
self.z_3d_final = self.z_3d_proj_gp
elif self.z_3d_final > Params['MAX_CONF_DEPTH']:
self.x_3d_final = self.x_3d_proj_gp
self.y_3d_final = self.y_3d_proj_gp
self.z_3d_final = self.z_3d_proj_gp
return pc_3d