import cv2 import cv2.aruco as aruco # 设置ArUco字典 aruco_dict = aruco.Dictionary_get(aruco.DICT_4X4_50) # 设置ArUco参数 parameters = aruco.DetectorParameters_create() # 打开视频捕捉 cap = cv2.VideoCapture(0) # 如果要从文件中读取视频,请将参数更改为视频文件名 while True: # 读取视频帧 ret, frame = cap.read() # 将图像转换为灰度图像 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 检测ArUco标记 corners, ids, rejected = aruco.detectMarkers(gray, aruco_dict, parameters=parameters) # 在图像中绘制检测到的标记 frame = aruco.drawDetectedMarkers(frame, corners, ids) # 如果检测到标记 if ids is not None: # 遍历每个检测到的标记 for i in range(len(ids)): # 获取标记ID marker_id = ids[i][0] # 在图像中心绘制标记ID x = int(corners[i][0][0][0]) y = int(corners[i][0][0][1]) cv2.putText(frame, str(marker_id), (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA) # 输出标记ID print("Detected marker with ID:", marker_id) # 显示帧 cv2.imshow('ArUco Marker Detection', frame) # 检查按键,如果按下'q'键则退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放视频捕捉和关闭窗口 cap.release() cv2.destroyAllWindows()