# Run a lane detection model in Real Time with CV2

**URL:** <https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825>\
**Category:** Python\
**Tags:** dnn, highgui, videoio\
**Created:** [June 14, 2021, 6:21am UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825 "2021-06-14T06:21:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yudhisteer\_Chintaram](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/yudhisteer_chintaram/32/2278_2.png) [@Yudhisteer\_Chintaram](https://forum.opencv.org/u/Yudhisteer_Chintaram)\
**Post date:** [June 14, 2021, 6:21am UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825/1 "2021-06-14T06:21:07Z")

</div>

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:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/d/d9d5f4554af21d7f0a4772427fd8cf316dcf1314.png)

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!

---

<div class="post-metadata">

**Author:** ![Yudhisteer\_Chintaram](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/yudhisteer_chintaram/32/2278_2.png) [@Yudhisteer\_Chintaram](https://forum.opencv.org/u/Yudhisteer_Chintaram)\
**Post date:** [June 14, 2021, 6:22am UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825/2 "2021-06-14T06:22:48Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/1/1271b75b31ce7ac46cda4ed818655dacf382ba98.jpeg)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [June 14, 2021, 7:17am UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825/3 "2021-06-14T07:17:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Yudhisteer\_Chintaram](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/yudhisteer_chintaram/32/2278_2.png) [@Yudhisteer\_Chintaram](https://forum.opencv.org/u/Yudhisteer_Chintaram)\
**Post date:** [June 14, 2021, 7:50am UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825/4 "2021-06-14T07:50:41Z")

</div>

```auto
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

```

---

<div class="post-metadata">

**Author:** ![Yudhisteer\_Chintaram](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/yudhisteer_chintaram/32/2278_2.png) [@Yudhisteer\_Chintaram](https://forum.opencv.org/u/Yudhisteer_Chintaram)\
**Post date:** [June 14, 2021, 7:51am UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825/5 "2021-06-14T07:51:34Z")

</div>

```auto
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))

```

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 14, 2021, 6:27pm UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825/6 "2021-06-14T18:27:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Yudhisteer\_Chintaram](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/yudhisteer_chintaram/32/2278_2.png) [@Yudhisteer\_Chintaram](https://forum.opencv.org/u/Yudhisteer_Chintaram)\
**Post date:** [June 14, 2021, 7:02pm UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825/7 "2021-06-14T19:02:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [June 14, 2021, 11:11pm UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825/8 "2021-06-14T23:11:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Yudhisteer\_Chintaram](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/yudhisteer_chintaram/32/2278_2.png) [@Yudhisteer\_Chintaram](https://forum.opencv.org/u/Yudhisteer_Chintaram)\
**Post date:** [June 15, 2021, 6:10am UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825/9 "2021-06-15T06:10:50Z")

</div>

```auto
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?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [June 15, 2021, 6:15am UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825/10 "2021-06-15T06:15:25Z")

</div>

> [@Yudhisteer\_Chintaram](#):
>
> 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)

```

---

<div class="post-metadata">

**Author:** ![Yudhisteer\_Chintaram](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/yudhisteer_chintaram/32/2278_2.png) [@Yudhisteer\_Chintaram](https://forum.opencv.org/u/Yudhisteer_Chintaram)\
**Post date:** [June 15, 2021, 6:23am UTC](https://forum.opencv.org/t/run-a-lane-detection-model-in-real-time-with-cv2/3825/11 "2021-06-15T06:23:26Z")

</div>

Thanks @berak it works!!!
