I built a Lane Detection using U-Net Architecture which works perfectly fine.
I create a “run” function which does this:
- Take an image as input
- Resize the input to match the model size
- Call model.predict()
- Extract R,G,B channels from prediction
- Rescale to the original image size
- Draw on the input image Return the result
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
So when I need to predict it on a test image, I do this:
f, (ax1) = plt.subplots(figsize=(20,10))
ax1.imshow(run(predict_image))
type or paste code here
Then it returns me the image with the lane being detected by the model:
Now if I need to run it on a video:
from moviepy.editor import VideoFileClip
video_file = "/content/drive/MyDrive/Image Segmentation/videos/video_input.mov"
clip = VideoFileClip(video_file)
white_clip = clip.fl_image(run)
%time white_clip.write_videofile("/content/drive/MyDrive/Image Segmentation/Output/video_output.mp4",audio=False)
Now I want to run the lane detector in real-time using my webcam. How can I do this?
thanks!