board_camera_bot/aruco_realtime.py

51 lines
1.1 KiB
Python
Raw Normal View History

import sys
import cv2
from cv2 import aruco
# Define the ArUco dictionary
2024-08-11 18:23:47 +00:00
aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_4X4_50)
aruco_params = cv2.aruco.DetectorParameters()
# Specify the RTSP URL
2024-08-11 18:23:47 +00:00
# rtsp_url = sys.argv[1]
2024-08-11 18:23:47 +00:00
# webcam:
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
# Check if frame is not empty
if frame is None:
print("Empty frame, exiting...")
break
# Detect ArUco markers
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
corners, ids, rejectedImgPoints = aruco.detectMarkers(
gray, aruco_dict, parameters=aruco_params
)
# If some markers are found, draw them on the frame
if ids is not None:
frame = cv2.aruco.drawDetectedMarkers(frame, corners, ids)
# Scale frame in half
2024-08-11 18:23:47 +00:00
# frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
# Display the frame
cv2.imshow("Frame", frame)
# Wait for the 'q' key to exit the loop
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# Release the capture object and close all OpenCV windows
cap.release()
cv2.destroyAllWindows()
2024-08-11 18:23:47 +00:00
if __name__ == '__main__':
pass