1

I am working with OpenCV ArUco in Python. I am trying to generate multiple codes of different directories. To generate it in a single time I am using this function in a loop. For example list1 =[1,2,3,4],comb = [50,100,250,1000],ids = [1,22,3,45]

def generator(bsize,comb,ids):

    bitsize = [bsize]+['X']+[bsize]
    bitz = ''.join(bitsize)

    dicts = ['DICT']+[bitz]+[comb]
    dictionary = '_'.join(dicts)
    print(dictionary)

    path = ['aruco']+[dictionary]
    print(path)
    path = '.'.join(path)
    print(path)

    aruco_dict = aruco.Dictionary_get(path)
    img = aruco.drawMarker(aruco_dict, ids, bsize)
    cv2.imshow('frame',img)

for i in range(0,7):
    generator(list1[i],list2[i],list3[i])

the output of 'path' is:

aruco.DICT_4X4_1000

after that I am getting error:

line 35, in generator
aruco_dict = aruco.Dictionary_get(path)
TypeError: an integer is required (got type str)

How do I resolve this error. Please help

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mahesh Gupta
  • 135
  • 1
  • 13
  • 1
    how were the parameters "given"? the error message seems clear, somewhere the code is getting a string where it expected an integer. there is a difference between '3' and 3 for example. – Paritosh Singh Nov 11 '18 at 18:26
  • you will need to post more of your traceback for us to understand.. maybe edit your question and post your full traceback? – Jamie Lindsey Nov 11 '18 at 18:32
  • As I can understand, `Dictionary_get()` takes int parameter. `join()` method on strings return a string. In your case that is getting stored in `path` variable, finally you are passing that string to `Dictionary_get()`. Just check parameters. – hygull Nov 11 '18 at 18:45

2 Answers2

0

"aruco.DICT_4X4_1000", a string, is different from aruco.DICT_4X4_1000, an attribute in aruco.

If you want to programmatically access aruco's value for the attribute DICT_4X4_1000, you can use:

getattr(aruco, "DICT_4X4_1000")

So your code for getting path should be:

...
path = getattr(aruco, dictionary)
...
Ollin Boer Bohan
  • 2,296
  • 1
  • 8
  • 12
  • Yay! Please [accept the answer](https://stackoverflow.com/help/someone-answers) if you feel it is the best solution to the problem in your question. – Ollin Boer Bohan Nov 11 '18 at 18:56
0

As I can see at http://www.philipzucker.com/aruco-in-opencv/, aruco.DICT_6X6_250 is real constant (int). In your can it is a string "aruco.DICT_6X6_250" and this is main reason of error. For clarification, just try the below 2 statements in place of path = '.'.join(path).

  • Valid

    path = aruco.DICT_4X4_1000

  • Invalid

    path = "aruco.DICT_4X4_1000"

You'll find, the 2nd one is responsible for error.

My suggestion to fix this kind of issue is to create any module named arcuo_constants.py and place the contents like below in that.

arcuo_constants.py

import cv2
import cv2.aruco as aruco

# define all possible constants here
ARUCO_CONSTANTS = {
    "aruco.DICT_6X6_250": aruco.DICT_6X6_250,
    "aruco.DICT_4X4_1000": aruco.DICT_4X4_1000
}

And finally in your code file, you can import and use those values as follows (Let suppose module file is in the same directory).

import aruco_constants

ARUCO_CONSTANTS = aruco_constants.ARUCO_CONSTANTS # A dictionary
# ...
# ...
path = '.'.join(path)
path = ARUCO_CONSTANTS[path]
aruco_dict = aruco.Dictionary_get(path)
# ...
# ...
hygull
  • 8,464
  • 2
  • 43
  • 52