简体   繁体   English

TypeError:Python中需要一个整数(类型为str)

[英]TypeError: an integer is required (got type str) in python

I am working with OpenCV ArUco in Python. 我正在Python中使用OpenCV ArUco。 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] 例如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: “ path”的输出为:

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 请帮忙

"aruco.DICT_4X4_1000" , a string, is different from aruco.DICT_4X4_1000 , an attribute in aruco . 字符串"aruco.DICT_4X4_1000"aruco.DICT_4X4_1000中的属性aruco

If you want to programmatically access aruco 's value for the attribute DICT_4X4_1000 , you can use : 如果你想以编程方式访问aruco该属性的值DICT_4X4_1000 ,你可以使用

getattr(aruco, "DICT_4X4_1000")

So your code for getting path should be: 因此,获取path的代码应为:

...
path = getattr(aruco, dictionary)
...

As I can see at http://www.philipzucker.com/aruco-in-opencv/ , aruco.DICT_6X6_250 is real constant (int). 正如我在http://www.philipzucker.com/aruco-in-opencv/中看到的那样, aruco.DICT_6X6_250是实常数(int)。 In your can it is a string "aruco.DICT_6X6_250" and this is main reason of error. 您可以输入一个字符串"aruco.DICT_6X6_250" ,这是错误的主要原因。 For clarification, just try the below 2 statements in place of path = '.'.join(path) . 为了澄清起见,只需尝试以下2条语句代替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的模块,并将其内容放入下面的内容中。

arcuo_constants.py 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)
# ...
# ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM