<jupyter_start><jupyter_text>0. Load all dependencies<jupyter_code># Importing some useful packages

from importlib import reload
from matplotlib.patches import Polygon
from moviepy.editor import VideoFileClip
from IPython.display import HTML

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
import sys
import os
import glob
import pickle
import cv2

sys.path.append("../../") # go to parent dir

# Visualizations will be shown in the notebook:

%matplotlib inline


# Import local deps

import src.helpers.line as L
import src.helpers.imageProcessing as IP
import src.helpers.plot as P
import src.helpers.cameraCalibration as C
import src.helpers.laneFinder as LF
import src.helpers.imagePreprocessingPipeline as IPP
import src.helpers.polyFitPipeline as PFP


if 'src.helpers.imageProcessing' in sys.modules:
    print("Reloading Modules...")
    
    reload(L)
    reload(IP)
    reload(P)
    reload(C)
    reload(LF)
    reload(IPP)
    reload(PFP)

    print("\nModules Reloaded!")<jupyter_output>Reloading Modules...

Modules Reloaded!<jupyter_text>1. Load camera calibration and calculate M transform matrix<jupyter_code># Load an image

anyImage = mpimg.imread("../../input/images/001 - Yellow Straight.jpg")


# Get image count & dimensions

WIDTH = anyImage.shape[1]
HEIGHT = anyImage.shape[0]


# Load camera calibration

data = pickle.load(open('../../output/data/calibration.p', 'rb'))
mtx = data["mtx"]
dist = data["dist"]


# Calculate perpective transform matrixs

MIDDLE = WIDTH // 2
H_MARGIN_TOP = 50
H_MARGIN_BOTTOM = 215
CORRECTION = 15
DST_MARGIN_H = 400
DST_MARGIN_TOP = 100

srcPoints = np.array([
    (MIDDLE - H_MARGIN_TOP + CORRECTION - 10, 450),
    (MIDDLE + H_MARGIN_TOP + CORRECTION - 22, 450), 
    (WIDTH - H_MARGIN_BOTTOM + CORRECTION - 5, 700), 
    (H_MARGIN_BOTTOM + CORRECTION, 700)
], np.float32)

dstPoints = np.array([
    (DST_MARGIN_H, DST_MARGIN_TOP),
    (WIDTH - DST_MARGIN_H, DST_MARGIN_TOP),
    (WIDTH - DST_MARGIN_H, HEIGHT),
    (DST_MARGIN_H, HEIGHT)
], np.float32)

M, Minv = C.getM(srcPoints, dstPoints)<jupyter_output><empty_output><jupyter_text>2. Create video processing pipeline<jupyter_code># IMAGE PROCESSING:

line_L = L.Line()
line_R = L.Line()

def process_image(imageRGB):
    # NOTE: The output you return should be a color image (3 channel) for processing video below
    # TODO: put your pipeline here,
    # you should return the final output (image where lines are drawn on lanes)
    
    imgUndistorted = C.undistort(imageRGB, mtx, dist)
    imgBinary = IPP.pipeline(imgUndistorted, M)
    
    return PFP.pipeline(imgUndistorted, imgBinary, Minv, line_L, line_R)


# VIDEO PROCESSING:

outputDir = "../../output/videos/"
videosDir = "../../input/videos/"
videosNames = os.listdir(videosDir)

videosNames = ["004 - Advanced Project.mp4"]

def process_video(videoName):
    print("--------\n\nProcessing %s:" % videoName)
    
    line_L.reset()
    line_R.reset()
    
    inputClip = VideoFileClip(videosDir + videoName)
    outputClip = inputClip.fl_image(process_image) # NOTE: this function expects color images!
    %time outputClip.write_videofile(outputDir + videoName, audio=False)
    
    return outputClip
    
print("Available videos:\n")

for videoName in videosNames: print(videoName)<jupyter_output>Available videos:

004 - Advanced Project.mp4<jupyter_text>3. Process videos<jupyter_code>for videoName in videosNames:
    output = process_video(videoName)<jupyter_output>--------

Processing 004 - Advanced Project.mp4:
[MoviePy] >>>> Building video ../../output/videos/004 - Advanced Project.mp4
[MoviePy] Writing video ../../output/videos/004 - Advanced Project.mp4
{'fraction_non_alphanumeric': 0.11165311653116532, 'fraction_numerical': 0.01788617886178862, 'mean_word_length': 4.028610354223433, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '552004', 'license': 'MIT', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1261, 'n_words': 326, 'path': 'src/notebooks/Video Processing.ipynb', 'repo_name': 'Danziger/CarND-T1-P4-Advanced-Lane-Lines-Detection', 'size': '3690'}