B
B
Bebronyukh Savory2020-05-06 18:47:53
Python
Bebronyukh Savory, 2020-05-06 18:47:53

Why does an error pop up when I move?

Here is the code:

import cv2
import numpy as np
cap = cv2.VideoCapture(0)
maximus = 0

its = []
its2 = []
its3 = []
segment = [(0,0)]
gx = 0

def calc(begin_seg,end_seg):
    lenght_line = end_seg - begin_seg
    center_line = end_seg - lenght_line // 2
    segment.append((center_line,lenght_line))

    return 0

while 1:
    _, frame = cap.read()
    size = (frame.shape[1]//3,frame.shape[0]//3)
   
    frame_small = cv2.resize(frame,size)
    hsv_temp = cv2.cvtColor(frame_small,cv2.COLOR_BGR2HSV)
    bw = cv2.inRange(hsv_temp,(0,0,0),(180,255,84))
    line = bw[bw.shape[0]-20]
    line_zoom = cv2.resize(line,(100,line.shape[0]))
    #print(line)
    segment = []
    f_begin = 0
    for i in range(len(line)):
        '''
        if line[ї] == 255:
            cv2.circle(frame,(i*3,frame.shape[0]-60),3,255)
        '''
        if(line[i]==255) and (f_begin==0):
            f_begin = 1
            begin = i
        elif (line[i]==0) and (f_begin==1):
            f_begin = 0
            end = i
            calc(begin,end)   
    for i in range(np.shape(segment)[0]):
        temp_circle = segment[i] 
        if temp_circle[1] > 1:
           # print( temp_circle )
            its.append(temp_circle[1])
            its2.append(temp_circle[0]*3)


            cv2.circle(frame,(temp_circle[0]*3,frame.shape[0]-60),temp_circle[1],(255,255,255))
            new = max(its)
            gx = its.index(new)
    if len(its) > 1:
        its.remove(max(its))
    else:
        new = 0
        its.append(0)

    new2 = max(its) 
    print(its2[gx],its2[its.index(new2)])

    
    its = []
    its2 = []

    cv2.imshow('CAM0',frame)
    cv2.imshow('B\W',bw)
    cv2.imshow('Zoom',line_zoom.T)
    
    key = cv2.waitKey(20)
    if (key == 27):
        break

cv2.destroyAllWindows()

Here is the error:
Exception has occurred: IndexError
list index out of range
  File "C:\myProjects\Python\cv2\12.py", line 61, in <module>
    print(its2[gx],its2[its.index(new2)])


The program should circle the dark areas and output the x position of the two largest of them.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alan Gibizov, 2020-05-08
@phaggi

The error comes out because the line that causes the error uses two lists its and its2.
Moreover, the list its is taken, it contains the index of some value new2, and then the list its2 is searched for a value with this index. Apparently, there is simply no such index there, and the system reports this.
Why is there no such index in your its2, and most importantly, should it be there? That's the question. You have active work with its above in the code, new values ​​are added to it, and values ​​are removed from there. Accordingly, there is no guarantee that the length of the list its will be equal to the length of the list its2 or somehow synchronized with it.
Therefore, at some point, it turns out that the list its is longer than the list its2 and the maximum value of the list its has an index that is greater than the largest index of the list its2.
What to do with it? I did not analyze the logic of the program in detail, but I probably need to constantly synchronize both lists somehow, then at least they will have the same length and THIS error will not occur.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question