gpt4 book ai didi

python 错误: tuples indices must be integers or slices not tuples

转载 作者:太空宇宙 更新时间:2023-11-03 22:17:04 25 4
gpt4 key购买 nike

我正在尝试使用 cv2 在图像中显示矩形。但是由于以下错误,我无法在图像上制作它们。

我已将图像转换为 numpy 数组并尝试以不同方式写下顶点,但一般格式是正确的。我不知道从哪里出现元组错误。

def draw_rect(img, dims, color = None):
img = img.copy()
dims = dims.reshape(-1, 4)
if not color:
color = (255, 255, 255) #rgb values for white color

for dim in dims:

x, y = (dim[0], dim[1]) , (dim[2], dim[3])

x = int(x[0]), int(x[1])
y = int(y[0]), int(y[1])
img = cv2.rectangle(img, x, y, color, int(max(img.shape[:,2])/200)) # error
return img
def main():
addr = 'test1_sec0.jpg'
bbox = 'test1_sec0.jpg.csv'
img_show(addr) # used to read and show the image using cv2. Works fine.
dims = bbox_read(bbox) # used to read the boundary boxes. Works fine
img = cv2.imread(addr, 1)
img_data = np.asarray(img, dtype = 'int32')
print(img_data)
plt.imshow(draw_rect(img_data, dims)) # error
plt.show( )


if __name__ == '__main__':
main()

'

 Traceback (most recent call last):
File "ImgAug.py", line 56, in <module>
main()
File "ImgAug.py", line 51, in main
plt.imshow(draw_rect(img_data, dims))
File "ImgAug.py", line 41, in draw_rect
img = cv2.rectangle(img, x[0], x[1], y[0], y[1], color, int(max(img.shape[:,2])/200)) # first argument & variable must be same cause same image should have all the bbox
TypeError: tuple indices must be integers or slices, not tuple

最佳答案

img.shape 是一个元组,但是 img.shape[:,2] 试图用另一个元组索引它,这是无效的:

>>> class X:
... def __getitem__(self, index):
... print(f'The index is: {index}')
...
>>> X()[:,2]
The index is: (slice(None, None, None), 2)

如您所见,something[:,2] 实际上生成了一个元组作为索引。

关于 python 错误: tuples indices must be integers or slices not tuples,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56604260/

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