1 importnumpy as np2 importtime3 importcv24 importcv2.aruco as aruco5
6 #with np.load('webcam_calibration_output.npz') as X:
7 #mtx, dist, _, _ = [X[i] for i in ('mtx','dist','rvecs','tvecs')]
8
9 #mtx =
10 #2946.48 0 1980.53
11 #0 2945.41 1129.25
12 #0 0 1
13
14 mtx =np.array([15 [2946.48, 0, 1980.53],16 [ 0, 2945.41, 1129.25],17 [ 0, 0, 1],18 ])19 #我的手机拍棋盘的时候图片大小是 4000 x 2250
20 #ip摄像头拍视频的时候设置的是 1920 x 1080,长宽比是一样的,
21 #ip摄像头设置分辨率的时候注意一下
22
23
24 dist = np.array( [0.226317, -1.21478, 0.00170689, -0.000334551, 1.9892] )25
26 video = "http://admin:admin@192.168.1.2:8081/" #手机ip摄像头
27 #根据ip摄像头在你手机上生成的ip地址更改,右上角可修改图像分辨率
28
29 cap =cv2.VideoCapture(video)30
31
32 font = cv2.FONT_HERSHEY_SIMPLEX #font for displaying text (below)
33
34 #num = 0
35 whileTrue:36 ret, frame =cap.read()37 #operations on the frame come here
38
39 gray =cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)40 aruco_dict =aruco.Dictionary_get(aruco.DICT_6X6_250)41 parameters =aruco.DetectorParameters_create()42
43 '''
44 detectMarkers(...)45 detectMarkers(image, dictionary[, corners[, ids[, parameters[, rejectedI46 mgPoints]]]]) -> corners, ids, rejectedImgPoints47 '''
48
49 #lists of ids and the corners beloning to each id
50 corners, ids, rejectedImgPoints =aruco.detectMarkers(gray,51 aruco_dict,52 parameters=parameters)53
54 #if ids != None:
55 if ids is notNone:56
57 rvec, tvec, _ = aruco.estimatePoseSingleMarkers(corners, 0.05, mtx, dist)58 #Estimate pose of each marker and return the values rvet and tvec---different
59 #from camera coeficcients
60 (rvec-tvec).any() #get rid of that nasty numpy value array error
61
62 #aruco.drawAxis(frame, mtx, dist, rvec, tvec, 0.1) #Draw Axis
63 #aruco.drawDetectedMarkers(frame, corners) #Draw A square around the markers
64
65 for i inrange(rvec.shape[0]):66 aruco.drawAxis(frame, mtx, dist, rvec[i, :, :], tvec[i, :, :], 0.03)67 aruco.drawDetectedMarkers(frame, corners)68 ###### DRAW ID #####
69 #cv2.putText(frame, "Id: " + str(ids), (0,64), font, 1, (0,255,0),2,cv2.LINE_AA)
70
71
72 else:73 ##### DRAW "NO IDS" #####
74 cv2.putText(frame, "No Ids", (0,64), font, 1, (0,255,0),2,cv2.LINE_AA)75
76 #Display the resulting frame
77 cv2.imshow("frame",frame)78
79 key = cv2.waitKey(1)80
81 if key == 27: #按esc键退出
82 print('esc break...')83 cap.release()84 cv2.destroyAllWindows()85 break
86
87 if key == ord(' '): #按空格键保存
88 #num = num + 1
89 #filename = "frames_%s.jpg" % num # 保存一张图像
90 filename = str(time.time())[:10] + ".jpg"
91 cv2.imwrite(filename, frame)