[英]what is i = i[0] for? yolo, cv2
I am very new to computer vision with python. I found a script to run a yolo.weights file.我对 python 的计算机视觉非常陌生。我找到了一个脚本来运行 yolo.weights 文件。 Everything runs smoothly until it hits this part:
一切顺利,直到遇到这一部分:
for i in indices:
i = i[0]
box = bbox[i]
x,y,w,h = box[0], box[1], box[2], box[3]
cv2.rectangle(img, (x,y),(x+w,y+h),(0,255,0),2)
cv2.putText(img, f'{classNames[classIds[i]].capitalize()} {int(confs[i]*100)}%' (x+10,y+30), cv2.FONT_HERSHEY_SIMPLEX, 0.9,(255,255,255),2)
It gives the error:它给出了错误:
File "script_location_etc", line 47, in findObjects i = i[0] IndexError: invalid index to scalar variable.文件“script_location_etc”,第 47 行,在 findObjects 中 i = i[0] IndexError:标量变量的索引无效。
What exactly does i = i[0] even do? i = i[0] 到底做了什么? And what is another way to doing it.
另一种方法是什么。
This means that you are trying to retrieve a value from an indexed location in a non iterable type.这意味着您正在尝试从不可迭代类型的索引位置检索值。
i = i[0]
means get the first element of i
and save it in the i
variable. i = i[0]
表示获取i
的第一个元素并将其保存在i
变量中。
So if i was iterable it would work like this:所以如果我是可迭代的,它会像这样工作:
>>> i = `some string`
>>> i = i[0]
>>> print(i)
... `s`
So basically your code is telling you that i
isn't iterable and therefore there is no first element to get.所以基本上你的代码告诉你
i
不可迭代,因此没有第一个元素可以获取。 I can't really tell why this might be happening without more details.如果没有更多详细信息,我真的无法说出为什么会发生这种情况。 Maybe try removing that line completely and see if it works.
也许尝试完全删除该行并查看它是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.