Search code examples
pythonopencvcomputer-visionyolo

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. 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.

What exactly does i = i[0] even do? And what is another way to doing it.


Solution

  • 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.

    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 can't really tell why this might be happening without more details. Maybe try removing that line completely and see if it works.