Created
July 28, 2022 20:24
-
-
Save wyojustin/418724c14d4096f5470e487ff908e79c to your computer and use it in GitHub Desktop.
capture aruco codes from webcam using opencv2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import cv2.aruco as aruco | |
markerSize = 4 | |
totalMarkers=100 | |
key = getattr(aruco, f'DICT_{markerSize}X{markerSize}_{totalMarkers}') | |
arucoDict = aruco.Dictionary_get(key) | |
def findArucoMarkers(img, draw=True): | |
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
arucoParam = aruco.DetectorParameters_create() | |
bboxs, ids, rejected = aruco.detectMarkers(gray, arucoDict, parameters = arucoParam) | |
_bboxs, _ids, _rejected = aruco.detectMarkers(255-gray, arucoDict, parameters = arucoParam) | |
if draw: | |
cv2.aruco.drawDetectedMarkers(img, bboxs, ids) | |
cv2.aruco.drawDetectedMarkers(img, _bboxs, _ids) | |
return ids, _ids | |
# define a video capture object | |
vid = cv2.VideoCapture(0) | |
while(True): | |
# Capture the video frame | |
# by frame | |
ret, frame = vid.read() | |
# Display the resulting frame | |
findArucoMarkers(frame) | |
cv2.imshow('frame', frame) | |
# the 'q' button is set as the | |
# quitting button you may use any | |
# desired button of your choice | |
if cv2.waitKey(1) & 0xFF == ord('q'): | |
break | |
# After the loop release the cap object | |
vid.release() | |
# Destroy all the windows | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment