gpt4 book ai didi

python - 类型错误 : an integer is required (got type str) in python

转载 作者:太空宇宙 更新时间:2023-11-03 23:09:12 24 4
gpt4 key购买 nike

我在 Python 中使用 OpenCV ArUco。我正在尝试生成不同目录的多个代码。为了一次性生成它,我在循环中使用了这个函数。例如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])

“路径”的输出是:

aruco.DICT_4X4_1000

之后出现错误:

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

如何解决此错误。请帮忙

最佳答案

"aruco.DICT_4X4_1000"是一个字符串,不同于aruco.DICT_4X4_1000,是aruco中的一个属性。

如果您想以编程方式访问 aruco 的属性 DICT_4X4_1000 的值,您 can use :

getattr(aruco, "DICT_4X4_1000")

所以你获取路径的代码应该是:

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

关于python - 类型错误 : an integer is required (got type str) in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53251821/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com