6

AttributeError: module 'cv2.aruco' has no attribute 'Dictionary_get'

even after installing

  • opencv-python
  • opencv-contrib-python
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()

normal example for finding and marking aruco

Markus
  • 5,976
  • 5
  • 6
  • 21
Harikrishnan M
  • 95
  • 1
  • 1
  • 10

2 Answers2

20

API changed for 4.7.x, I have updated a small snippet. Now you need to instantiate ArucoDetector object.

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)
not7CD
  • 376
  • 1
  • 10
  • Nice answer, thanks! I had this issue in 4.6.0.66 which was weird because the dictionary and parameter creation were as you suggest, but `ArucoDetector` class did not exist. However when I tried calling the detector in the old way like `aruco.detectMarkers` it didn't complain, it just crashed with no info! My solution was `pip install --upgrade opencv-python` and `pip install --upgrade opencv-contrib-python` and then your solution worked. – adamconkey Jan 11 '23 at 18:29
  • I am having a similar issue. Im trying to migrate over to using new Aruco/Charuco API changes in 4.7.0. But the following code just crashes out with no info too! `detector = cv2.aruco.CharucoDetector(board, cv2.aruco.CharucoParameters(), cv2.aruco.DetectorParameters(), cv2.aruco.RefineParameters())` – Diarmaid O Cualain Jan 26 '23 at 15:41
  • Uh oh, this answer is getting popular. Unfortunately, I'm no open-cv expert. And only managed to get my use case working. My general suggestion would be to backtrack to something simple like my example. And then add args like a board, refined params, and see where it crashes. – not7CD Jan 28 '23 at 14:16
  • what about " module 'cv2.aruco' has no attribute 'estimatePoseSingleMarkers'" – M lab Jul 31 '23 at 09:57
3

After installing an older version of opencv-contrib-python from the new version it worked fine

pip install opencv-contrib-python==4.6.0.66

https://pypi.org/project/opencv-contrib-python/4.6.0.66/

Markus
  • 5,976
  • 5
  • 6
  • 21
Harikrishnan M
  • 95
  • 1
  • 1
  • 10
  • Thank you Harikrishnan M! This is good workaround for another error: "AttributeError: module 'cv2.aruco' has no attribute 'GridBoard_create'" in opencv-python and opencv-contrib-python 4.7.x version. – Volkov Maxim Jan 20 '23 at 14:28