-
Notifications
You must be signed in to change notification settings - Fork 1
/
change_detection_boxes.py
216 lines (124 loc) · 7.48 KB
/
change_detection_boxes.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
215
216
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 31 20:00:48 2021
@author: Admin
"""
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 10 15:58:20 2021
@author: Admin
"""
import cv2
import numpy as np
import itertools
from XYZ_WFK import camera_pos, calcWFKfromPhotoMatrix, calcPhotogrammetryMatrixFromSolvePnp
import time
from collections import namedtuple
from bbox.metrics import jaccard_index_2d
from bbox import BBox2D, XYXY
width = 5.37 ## mm
height = 4.04 ## mm
Xpixel= width/1920
Ypixel= height/1080
pixel = (Xpixel + Ypixel) / 2
class Detects:
diction = {}
def __init__(self,
num,
box_list,
camera_matrix,
rotation_matrix,
projection_matrix,
tvecs,
dist,
frame,
camera):
self.x = box_list[0]
self.y = box_list[1]
self.w = box_list[2]
self.h = box_list[3]
self.list = box_list
self.dist = dist
self.frame = frame
self.id = num
self.box = BBox2D(self.list)
self.cam_id = camera
# exterior openCV orientation
self.rotation_matrix = rotation_matrix
self.projection_matrix = projection_matrix
self.tvecs = tvecs
self.down_mid_x = (self.x + self.w / 2.0)
self.down_mid_y = (self.y + self.h)
self.camera_matrix = camera_matrix
# interior orientation
self.xo = (self.camera_matrix[0, 2] - 1920.0 / 2.0)
self.yo = (-self.camera_matrix[1, 2] + 1080.0 / 2.0)
self.f = ((self.camera_matrix[0, 0] + self.camera_matrix[1, 1]) / 2.0)
# exterior photogrammetric orientation
self.gXo, self.gYo, self.gZo = camera_pos(self.rotation_matrix, self.tvecs)
# self.omega, self.phi, self.kappa = calcWFKfromPhotoMatrix(calcPhotogrammetryMatrixFromSolvePnp(self.rotation_matrix))
self.photogram_rot_mat = calcPhotogrammetryMatrixFromSolvePnp(self.rotation_matrix)
# ground coordinate system
self.X = None
self.Y = None
self.down_mid_x_corrected = (self.down_mid_x - 1920.0 / 2.0)
self.down_mid_y_corrected = (1080.0 / 2.0 - self.down_mid_y )
# print(self.down_mid_x_corrected, self.down_mid_y_corrected)
# self.x_corrected = self.x - 1920 / 2
# self.y_corrected = 1080/2 - self.y
def triangulation_z(self, z = 0.000):
X = self.gXo + (z - self.gZo) * ((self.photogram_rot_mat[0, 0]*(self.down_mid_x_corrected - self.xo) + \
self.photogram_rot_mat[1, 0]*(self.down_mid_y_corrected - self.yo) - \
self.photogram_rot_mat[2, 0]*self.f) / (self.photogram_rot_mat[0, 2]*(self.down_mid_x_corrected - self.xo)+ \
self.photogram_rot_mat[1, 2]*(self.down_mid_y_corrected - self.yo)- \
self.photogram_rot_mat[2, 2]*self.f))
Y = self.gYo + (z - self.gZo)* ((self.photogram_rot_mat[0, 1]*(self.down_mid_x_corrected - self.xo) + \
self.photogram_rot_mat[1, 1]*(self.down_mid_y_corrected - self.yo) - \
self.photogram_rot_mat[2, 1]*self.f) / (self.photogram_rot_mat[0, 2]*(self.down_mid_x_corrected - self.xo)+ \
self.photogram_rot_mat[1, 2]*(self.down_mid_y_corrected - self.yo)- \
self.photogram_rot_mat[2, 2]*self.f))
self.X = X
self.Y = Y
# print(self.X, self.Y)
coordinates = np.float32(np.array([self.X, self.Y, z]))
mid_down = np.int32(cv2.projectPoints(coordinates.reshape(1, -1), cv2.Rodrigues(self.rotation_matrix)[0], self.tvecs, self.camera_matrix, np.zeros((1,5)) )[0])
# cv2.circle(self.frame, (mid_down[0][0][0], mid_down[0][0][1]), 10, (0, 0, 0), -1)
# print(mid_down)
return(self)
def correlation_distance(detection1, detection2):
dist = np.sqrt((detection1.X - detection2.X)**2 + (detection1.Y - detection2.Y)**2)
# print(dist)
Detects.diction[detection1.id , detection2.id] = dist
def compute_iou(detection1, detection2):
x = jaccard_index_2d(detection1.box, detection2.box)
if x > 0.0001:
print(x)
xmin = min(detection1.x, detection2.x)
ymin = min(detection1.y, detection2.y)
xmax = max(detection1.x + detection1.w, detection2.x + detection2.w)
ymax = max(detection1.y + detection1.h, detection2.y + detection2.h)
w = xmax - xmin
h = ymax - ymin
if detection1.cam_id == 'L':
det = Detects(detection1.id , [xmin, ymin, w, h], detection1.camera_matrix,
detection1.rotation_matrix, detection1.projection_matrix, detection1.tvecs,
detection1.dist, detection1.frame, 'L')
else:
det = Detects(detection1.id , [xmin, ymin, w, h], detection1.camera_matrix,
detection1.rotation_matrix, detection1.projection_matrix, detection1.tvecs,
detection1.dist, detection1.frame, 'R')
return(det)
else:
return([detection1, detection2])
def plot_2d(frame, detections, cam_num):
# cv2.namedWindow(f'Camera{number}', cv2.WINDOW_NORMAL)
for i in detections:
cv2.rectangle(frame, (i.x, i.y), (i.x + i.w, i.y + i.h), (0,255,0),2)
cv2.rectangle(frame,(i.x, i.y-45), (i.x + 100, i.y), (0, 255,0), -1)
cv2.putText(frame, f'ID:{i.id}', (i.x, i.y-5), 1, 3, (0, 0, 255), 2)
cv2.imshow(f'Camera{cam_num}', frame)
if cv2.waitKey(1) & 0xFF==27:
cv2.destroyAllWindows()
break
else:
pass