AttributeError:模块“cv2.aruco”没有属性“Dictionary_get”
即使安装后
import numpy as np
import cv2, PIL
from cv2 import aruco
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
vid = cv2.VideoCapture(0)
while (True):
ret, frame = vid.read()
#cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
aruco_dict = aruco.Dictionary_get(aruco.DICT_6X6_250)
parameters = aruco.DetectorParameters()
corners, ids, rejectedImgPoints = aruco.detectMarkers(gray, aruco_dict, parameters=parameters)
frame_markers = aruco.drawDetectedMarkers(frame.copy(), corners, ids)
plt.figure()
plt.imshow(frame_markers)
for i in range(len(ids)):
c = corners[i][0]
plt.plot([c[:, 0].mean()], [c[:, 1].mean()], "o", label = "id={0}".format(ids[i]))
plt.legend()
plt.show()
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
查找和标记 aruco 的正常示例
4.7.x 的 API 发生了变化,我更新了一个小片段。现在您需要实例化 ArucoDetector 对象。
import cv2 as cv
dictionary = cv.aruco.getPredefinedDictionary(cv.aruco.DICT_4X4_250)
parameters = cv.aruco.DetectorParameters()
detector = cv.aruco.ArucoDetector(dictionary, parameters)
frame = cv.imread(...)
markerCorners, markerIds, rejectedCandidates = detector.detectMarkers(frame)
从新版本安装旧版本的 opencv-contrib-python 后,它工作正常
pip install opencv-contrib-python==4.6.0.66
我使用Python 3.8,试试这个:
pip uninstall opencv-contrib-python opencv-python
然后安装:
pip install opencv-contrib-python==4.7.0.68 opencv-python==4.7.0.68
该问题是由于 opencv 版本 4.7.0 中的 aruco api 更改所致。下面的代码可以处理旧版本和新版本的 opencv。
import cv2
from packaging import version # Installed with setuptools, so should already be installed in your env.
if version.parse(cv2.__version__) >= version.parse("4.7.0"):
dictionary = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_5X5_250)
detectorParams = cv2.aruco.DetectorParameters()
detector = cv2.aruco.ArucoDetector(dictionary, detectorParams)
marker_corners, marker_ids, rejected_candidates = detector.detectMarkers(image)
else:
dictionary = cv2.aruco.Dictionary_get(cv2.aruco.DICT_5X5_250)
detectorParams = cv2.aruco.DetectorParameters_create()
marker_corners, marker_ids, rejected_candidates = cv2.aruco.detectMarkers(
image, dictionary, parameters=detectorParams
)