Skip to main content
emobility.academy
B.Tech / BELane keeping & lateralBeginner About 4 weeksADAS & AUTOSAR

Lane Departure Warning System with Camera Lane Detection

Build a camera-based lane departure warning system in Python and OpenCV that detects lane lines, estimates the vehicle's lateral offset and time to line crossing, and raises a warning when the car drifts without signalling. It is tested on recorded dashcam footage.

Tools: Python · OpenCV

Lane Departure Warning System with Camera Lane Detection: Students studying an electric car with DIYguru trainers in the workshop
Representative photo from DIYguru's EV lab, not of this specific project.

The problem

Unintended lane departure from fatigue or distraction is a common cause of run-off-road and side-swipe crashes. A lane departure warning (LDW) system is one of the simplest ADAS functions, yet getting it right means handling worn markings, shadows and curves while keeping false warnings low enough that drivers do not switch it off.

Objectives

  • Detect left and right lane boundaries in each frame with a classical OpenCV pipeline.
  • Convert lane positions into a lateral offset and a time-to-line-crossing (TLC) estimate.
  • Design warning logic with hysteresis and suppression when a turn indicator is assumed active.
  • Measure detection rate, false-warning rate and warning lead time on labelled clips.

What you need

Tools and toolboxes

  • Python — The processing pipeline, warning logic and evaluation scripts.
  • OpenCV — Colour thresholding, Canny edges, perspective warp, HoughLinesP and drawing the overlay.
  • NumPy — Polynomial lane fitting with numpy.polyfit and offset calculations.
  • Matplotlib — Plots of lateral offset, TLC and warning flags over time.

Software

  • Python 3 with OpenCV, NumPy and Matplotlib
  • A video annotation tool such as CVAT (optional) for labelling departure events

Extra parts and data

  • Dashcam or smartphone footage mounted at the windscreen centre — The system needs forward road video; record it as a passenger or from a parked, supervised test area, never by operating the laptop while driving.

Key concepts

Lateral offset
The distance between the vehicle's centreline and the centre of the lane, computed from the lane boundaries at the bottom of the image after converting pixels to metres.
Time to line crossing (TLC)
The distance from the wheel to the lane boundary divided by the lateral velocity towards it. Warning on a TLC threshold gives earlier, more consistent alerts than warning on offset alone.
Inverse perspective mapping
A homography that warps the road region into a top-down view, where lane lines become nearly parallel and pixel distances map linearly to metres.
Hysteresis
Using separate thresholds to switch a warning on and off so it does not flicker when the offset hovers near a single threshold.

Step-by-step guide

  1. 1

    Define the requirements and ODD

    Write down the operational design domain: daytime, dry roads, visible painted markings, speeds roughly 40 to 100 km/h, straight roads and gentle curves. Define the warning requirement, for example a warning when TLC falls below 1 second or the wheel crosses the line, suppressed when the indicator is on. Set targets for detection rate of departures, false warnings per hour and a maximum processing latency per frame. Note what is out of scope, such as night rain and unmarked roads, so the evaluation stays honest.

    Checkpoint: A one-page requirements table listing ODD, warning rule, targets and exclusions.

  2. 2

    Collect and label footage

    Gather 30 to 60 minutes of forward video from a fixed windscreen mount, recorded by a passenger or from a closed area, plus public clips if needed. Measure the camera height and the lane width on a known road to calibrate scale later. Label each clip with the frame ranges where a wheel crosses a marking, and mark intentional lane changes separately. Split the clips into a tuning set and a held-out test set by drive, not by frame, so nearly identical frames do not appear in both.

    Checkpoint: Labelled departure events in a CSV, with tuning and test clips kept in separate folders.

  3. 3

    Build the bird's-eye view

    Pick four points on a straight-road frame that form a rectangle on the ground, such as lane-line corners, and compute the homography with cv2.getPerspectiveTransform. Warp each frame with cv2.warpPerspective into a top-down image. Use the known lane width (about 3.5 m on many Indian highways, but measure your own) and dash spacing to derive metres per pixel in both directions. Check that straight lane lines appear parallel and vertical in the warped image across several frames.

    Checkpoint: Warped frames where straight lane lines are parallel, with a recorded metres-per-pixel scale.

  4. 4

    Detect lane pixels

    Convert the frame to HLS and threshold the lightness and saturation channels to pick out white and yellow paint, then combine with a Sobel-x or Canny edge mask to reject flat bright areas. Apply a region-of-interest mask to ignore the sky and bonnet. In the warped binary image, find the base of each lane with a column histogram and track it upwards with a sliding-window search. Tune thresholds on the tuning clips only, and log frames where detection fails.

    Checkpoint: Binary lane masks that show both boundaries on most tuning frames, with failure frames listed.

  5. 5

    Fit lanes and track over time

    Fit a second-order polynomial to the left and right lane pixels with numpy.polyfit. Reject a fit if the lane width or curvature jumps beyond physical limits, and smooth accepted fits with an exponential moving average across frames. When one side is lost, infer it from the other side plus the expected lane width for a limited number of frames. Draw the fitted lane back on the original frame with the inverse homography for visual checking.

    Checkpoint: An overlay video where the fitted lane follows the markings smoothly with no wild jumps.

  6. 6

    Compute offset and TLC

    At the bottom row of the warped image, compute the lane centre and the camera centre, then convert the difference to metres to get the lateral offset. Subtract half the vehicle width to get the distance from each front wheel to its boundary. Differentiate the filtered offset over time to obtain lateral velocity, and compute TLC as distance divided by velocity when the vehicle moves towards a line. Plot offset and TLC against time for several tuning clips.

    Checkpoint: Offset and TLC plots that show TLC dropping smoothly before each labelled departure.

  7. 7

    Design the warning logic

    Raise a warning when TLC drops below the threshold or the wheel distance becomes negative, provided lane confidence is high and speed is above a minimum. Clear it only when TLC rises above a higher release threshold. Simulate an indicator input from the labels so intentional lane changes suppress the warning. Add a short minimum-on time so the warning is audible and a refractory time to avoid repeated alerts. Display a coloured overlay and log warning onset times.

    Checkpoint: Warnings fire on labelled departures in the tuning clips and are suppressed on marked lane changes.

  8. 8

    Evaluate on the test set

    Run the frozen pipeline on the held-out clips. Count true warnings (onset before or within 0.5 s of the labelled crossing), missed departures and false warnings, and convert false warnings to a rate per hour. Record warning lead time relative to the crossing and processing time per frame on your laptop. Break results down by condition, such as faded paint, shadows and curves, so weaknesses are visible rather than averaged away.

    Checkpoint: A results table with recall, false warnings per hour, lead time and frame time, split by condition.

  9. 9

    Report and demonstrate

    Write up the ODD, pipeline, parameter choices and test results, including example frames of failures. Explain the trade-off between earlier warnings and more false alarms using a curve of recall against false-warning rate as the TLC threshold varies. Prepare a short demo video with the overlay and warning indicator. State clearly that the system was tested only on recorded footage and is not suitable for use in a vehicle without much further validation.

    Checkpoint: A report with the threshold trade-off curve, failure examples and a demo video.

Testing & validation

  • Departure recall and false warnings per hour on held-out clips.
  • Warning lead time relative to the labelled line crossing.
  • Lane detection success rate per condition (faded paint, shadow, curve).
  • Processing time per frame on the target laptop.

Expected results

A working LDW pipeline that overlays the detected lane, reports lateral offset and TLC, and warns on unintended drift, with measured recall, false-warning rate and lead time on recorded test footage.

Take it further

  • Replace the classical detector with a small lane-segmentation network and compare robustness.
  • Add a Kalman filter on lane parameters for smoother tracking through gaps.
  • Port the pipeline to a Raspberry Pi and measure frame rate.
  • Feed the offset to a lane keeping controller in simulation.

Viva and review questions

Why warn on TLC rather than on lateral offset alone?

Offset ignores how fast the car is drifting. TLC combines distance and lateral speed, so a fast drift warns earlier and a slow drift near the line does not trigger needless alerts.

Why use a bird's-eye view?

In the top-down image lane lines are nearly parallel and pixel distances map linearly to metres, which makes polynomial fitting and offset measurement much simpler.

How do you avoid warnings during intentional lane changes?

The logic suppresses warnings when the indicator is active; in this project the indicator signal is simulated from labels, and in a car it would come from the body CAN network.

Learn it at DIYguru

These DIYguru courses and programmes teach what this project needs.

Related EV projects

All 100 ADAS & AUTOSAR projects