0

I'm trying to build an autonomous driving car with the Raspberry Pi - Therefore I try to learn from Udacity's Nanodegree examples.

The following Code is from some GitHub repositories and I just changed the code to work with the PI-CAM. Because the Udacity example Codes work all with .mp4 videos.

When I try to run the following code on the Raspberry PI with the Thonny IDE, sometimes it works for a few seconds or a minute and sometimes it won't even start running.

You can see the whole program here.

def draw_lines(img, lines, thickness=5):
    global rightSlope, leftSlope, rightIntercept, leftIntercept
    rightColor=[0,0,255]
    leftColor=[255,0,0]

    #this is used to filter out the outlying lines that can affect the average
    #We then use the slope we determined to find the y-intercept of the filtered lines by solving for b in y=mx+b
    for line in lines:
        for x1,y1,x2,y2 in line:
            slope = (y1-y2)/(x1-x2)
            if slope > 0.3:
                if x1 > 500 :
                    yintercept = y2 - (slope*x2)                    
                    rightSlope.append(slope)
                    rightIntercept.append(yintercept)
                else: None                
            elif slope < -0.3:
                if x1 < 600:
                    yintercept = y2 - (slope*x2)                    
                    leftSlope.append(slope)
                    leftIntercept.append(yintercept)    

     ...               

lines are defined in this part:

def hough_lines(img, rho, theta, threshold, min_line_len, max_line_gap):
    """
    `img` should be the output of a Canny transform.
    """
    lines = cv2.HoughLinesP(img, rho, theta, threshold, np.array([]), minLineLength=min_line_len, maxLineGap=max_line_gap)
    line_img = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8)
    draw_lines(line_img, lines)
    return line_img

def linedetect(img):
    return hough_lines(img, 1, np.pi/180, 10, 20, 100)

This is the error I get when I execute the code :

/usr/local/lib/python3.5/dist-packages/numpy/core/fromnumeric.py:3118: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/usr/local/lib/python3.5/dist-packages/numpy/core/_methods.py:85: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
version1_for_PI.py:160: RuntimeWarning: divide by zero encountered in int_scalars
  slope = (y1-y2)/(x1-x2)
Traceback (most recent call last):
  File "/home/pi/Desktop/version-1/version1_for_PI.py", line 244, in <module>
    myline = hough_lines(canny, 1, np.pi/180, 10, 20, 5)
  File "/home/pi/Desktop/version-1/version1_for_PI.py", line 209, in hough_lines
    draw_lines(line_img, lines)
  File "/home/pi/Desktop/version-1/version1_for_PI.py", line 158, in draw_lines
    for line in lines:
TypeError: 'NoneType' object is not iterable

Dan Beri
  • 53
  • 1
  • 1
  • 4
  • 2
    You've shown far too much irrelevant code - we don't need any of the rest of that function after the part where the error happens - but you haven't shown any of the actually relevant code, ie where lines is coming from. – Daniel Roseman Jun 08 '19 at 10:33
  • 1
    The error means that `lines` is `None`. So, find where you're calling your function and fix it there (the code that has the problem isn't in your post). – Tom Karzes Jun 08 '19 at 10:39
  • So what is your question? Do you expect something else to happen when `cv2.HoughLinesP` returns `None`? – Stop harming Monica Jun 08 '19 at 10:51

1 Answers1

0

Your "lines" parameter is None - which is not an "iterable" typed object in python (such as lists, sets, etc). You should either make sure that the "lines" you pass to the method are not None - or add some logic to ignore it:

if not lines: # means that lines == None
  return 0 # or return something else

Another good option is to capture an exception and handle it properly.

Tom
  • 1,105
  • 8
  • 17