Run a lane detection model in Real Time with CV2

Hello,

I built a lane detection model in tensorflow. And I have a function named “run” which encompasses model.predict, cv2.resize, … as shown below:

In that way, I just need to call run(image) to get the result.

Now I need to run the model in real-time. That is, I will use my webcam pointed at a road and it will classify the lane. How can I do this with openCV?

Thanks for helping!

please, no screenshots of code or errors, here (replace with TEXT), thank you.

1 Like
def run(input_image):
    h,w,d = input_image.shape
    network_image = input_image.copy()
    network_image = cv2.resize(network_image, (160,80), interpolation=cv2.INTER_AREA)
    network_image = network_image[None,:,:,:]
    prediction = model.predict(network_image)[0]*255
    R,G,B = rgb_channel(prediction)
    blank = np.zeros_like(R).astype(np.uint8)
    lane_image = np.dstack((R,blank, B))
    lane_image = cv2.resize(lane_image, (w,h))
    result = cv2.addWeighted(input_image, 1, lane_image.astype(np.uint8), 1, 0)
    return result
predict_image = mpimg.imread("/content/drive/MyDrive/Image Segmentation/Images/Tourists-Khao-San-Road.jpg")
# plt.imshow(run(predict_image))
# plt.show()


f, (ax1) = plt.subplots(figsize=(20,10))
ax1.imshow(run(predict_image))

I remember that picture. weren’t you having the same issue last time, but you were working in colab, and colab runs on a server, and getting your local webcam feed into colab takes time and bandwidth?

are you now running your code locally?

1 Like

Yes it is locally now. On jupyter notebook.
Can you help please?

jupyter also has trouble showing video, or so I hear. every imshow presents a new picture or something silly like that.

run the code from a terminal, not from inside of jupyter.

then OpenCV’s highgui can show a window that’s capable of updating properly.

cap = cv2.VideoCapture(r"C:\Users\yudishteer.c\Desktop\Image Segmentation\videos\rtknits.mp4")
while(cap.isOpened()):
    ret, frame = cap.read()
    if ret == True:
        run_image = run(frame)
        cv2.imshow("result", run_image)
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
    else:
        break
cap.release()
cv2.destroyAllWindows() 

I used this to run the model live on a video. Now I want to run the model when my webcam is activated. How do I do that?

you only need to change the path to your video to a webcam id (-1,0,1,2,etc) like

cap = cv2.VideoCapture(0)
1 Like

Thanks @berak it works!!!