代码如下:
import numpy as np
import cv2 as cv
import glob
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
arucoDict = cv.aruco.Dictionary_get(cv.aruco.DICT_5X5_100)
arucoParams = cv.aruco.DetectorParameters_create()
(corners, ids, rejected) = cv.aruco.detectMarkers(gray, arucoDict,
parameters=arucoParams)
print(corners)
img = frame
if(len(corners)>0):
imgpts = [np.int32(corners).reshape(-1, 2)]
img = cv.drawContours(frame, imgpts, -1, (0, 255, 0), -3)
# Display the resulting frame
cv.imshow('frame', img)
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
效果如下:
所用的二维码如下: