-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
135 lines (122 loc) · 7.14 KB
/
main.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
from test import Kalman_draw as draw
import cv2
import numpy as np
import time
from src.QRcode_videography_detection import QRcode
from src.pnp import Pnp
from src.KF import KF
from src.yaml_loader import load_config
from yolov5.detect import *
from src.linear_canonical_test import *
if __name__ == '__main__':
# draw.generate_data() for test
filter=KF()
filter_KNet=linear_canonical_test()
pnp = Pnp(2.5, *load_config("hhb_cam.yaml")) # TODO
flag_first = True
if_yolo=True
if_traditional_kalman=False
if_pnp=True
if if_yolo:
opt = parse_opt()
main(opt)
elif if_traditional_kalman:
detector = QRcode()
while True:
dataresult,inputresult=detector.detectcode()
if dataresult:
if flag_first:
time_prev = time.time()
flag_first = False
points=detector.get_points()
detector.show_originPoints(points) # 打印二维码中心点和四个角点在画面坐标系下的坐标(二维)
imagePoints:np.array = Pnp.convertCornerToImagePoints(points)
pnp.solve(imagePoints, pnp.obj_points) # 打印二维码中心点和四个角点在相机坐标系下的坐标(三维)
dT=time.time()-time_prev
time_prev = time.time()
#dT = 0.02
predict_points_3D=filter.update(*pnp.transformedPoints[:,0],0,0,0,dT).tolist()[0]
print("predict:")
print(predict_points_3D)
cv2.circle(inputresult,(int(imagePoints[0][0]),int(imagePoints[0][1])),5,(0,0,255),-1)
predict_points_3D = np.array(predict_points_3D).reshape(-1, 1, 3)
predict_points_2D, _ = cv2.projectPoints(predict_points_3D, pnp.rvec, pnp.tvec, pnp.camera_matrix, pnp.dist_coeffs)
#predict_points_2D形状为(1,1,2)
cv2.circle(inputresult,(int(predict_points_2D[0][0][0]),int(predict_points_2D[0][0][1])),5,(0,255,0),-1)
input_dealed=detector.draw(inputresult)
cv2.imshow("camera",input_dealed)
else:
cv2.imshow("camera",inputresult)
if cv2.waitKey(1) & 0xFF==ord('q'):
break
detector.release()
cv2.destroyAllWindows()
elif if_pnp:
detector = QRcode()
while True:
dataresult,inputresult=detector.detectcode()
if dataresult:
if flag_first:
time_prev = time.time()
flag_first = False
points=detector.get_points()
detector.show_originPoints(points)#打印二维码中心点和四个角点在画面坐标系下的坐标(二维)
imagePoints:np.array = Pnp.convertCornerToImagePoints(points)
# print("imagePoints:",imagePoints)
pnp.solve(imagePoints, pnp.obj_points)#打印二维码中心点和四个角点在相机坐标系下的坐标(三维)
dT=time.time()-time_prev
time_prev = time.time()
#dT = 0.02
print("pnp.transformedPoints:",pnp.transformedPoints)
predict_points_3D_1=filter_KNet.update((pnp.transformedPoints[0][0],pnp.transformedPoints[1][0]))
predict_points_3D = [predict_points_3D_1[0].item(),predict_points_3D_1[1].item(),pnp.transformedPoints[2][0]]
print("predict:")
print(predict_points_3D)
cv2.circle(inputresult,(int(imagePoints[0][0]),int(imagePoints[0][1])),5,(0,0,255),-1)
predict_points_3D = np.array(predict_points_3D).reshape(-1, 1, 3)
predict_points_2D, _ = cv2.projectPoints(predict_points_3D, pnp.rvec, pnp.tvec, pnp.camera_matrix, pnp.dist_coeffs)
#predict_points_2D形状为(1,1,2)
cv2.circle(inputresult,(int(predict_points_2D[0][0][0]),int(predict_points_2D[0][0][1])),5,(0,255,0),-1)
input_dealed=detector.draw(inputresult)
cv2.imshow("camera",input_dealed)
else:
cv2.imshow("camera",inputresult)
if cv2.waitKey(1) & 0xFF==ord('q'):
break
detector.release()
cv2.destroyAllWindows()
else:
detector = QRcode()
while True:
dataresult,inputresult=detector.detectcode()
if dataresult:
if flag_first:
time_prev = time.time()
flag_first = False
points=detector.get_points()
detector.show_originPoints(points)#打印二维码中心点和四个角点在画面坐标系下的坐标(二维)
imagePoints:np.array = Pnp.convertCornerToImagePoints(points)
print("imagePoints:",imagePoints)
pnp.solve(imagePoints, pnp.obj_points)#打印二维码中心点和四个角点在相机坐标系下的坐标(三维)
dT=time.time()-time_prev
time_prev = time.time()
#dT = 0.02
print("pnp.transformedPoints:",pnp.transformedPoints)
predict_points_3D_1=filter_KNet.update((points[0].x,points[0].y))
predict_points_3D = [predict_points_3D_1[0].item(),predict_points_3D_1[1].item(),pnp.transformedPoints[2][0]]
print("predict:")
print(predict_points_3D)
cv2.circle(inputresult,(int(imagePoints[0][0]),int(imagePoints[0][1])),5,(0,0,255),-1)
predict_points_3D = np.array(predict_points_3D).reshape(-1, 1, 3)
predict_points_2D, _ = cv2.projectPoints(predict_points_3D, pnp.rvec, pnp.tvec, pnp.camera_matrix, pnp.dist_coeffs)
#predict_points_2D形状为(1,1,2)
print(predict_points_3D)
cv2.circle(inputresult,(int(predict_points_3D[0][0][0]),int(predict_points_3D[0][0][1])),5,(0,255,0),-1)
input_dealed=detector.draw(inputresult)
cv2.imshow("camera",input_dealed)
else:
cv2.imshow("camera",inputresult)
if cv2.waitKey(1) & 0xFF==ord('q'):
break
detector.release()
cv2.destroyAllWindows()