Skip to content
tkdyun.xyz

Raspberry Pi4 AI Car

CNN lane-following + YOLOv5 obstacle detection at 20 Hz

Embedded AI

Year
2024
Role
Team • Lead • 3 Members
Stack
PyTorch · YOLOv5 · OpenCV · Raspberry Pi · RPi.GPIO

Overview

The car uses two models: a small CNN that predicts steering angles, and YOLOv5, which detects obstacles, crosswalks, and other objects. I built the driving road myself and collected a balanced training dataset via remote control. I improved the small CNN's training efficiency and performance through image augmentation and image format conversion, built an automatic labeling pipeline for the steering angle prediction model, and also carried out image labeling for training the YOLO model.

Preview

Pipeline — AI & software

1. Image Acquisition & Multi-threading

Training data was collected directly via remote control, with steering-angle labeling performed automatically. I built several driving tracks and trained on data collected across a variety of situations — going straight, turning left, and turning right. Since the network is an MLP, I found that the distribution of the labeled data has a large effect on its performance, so I paid close attention to balanced image distribution to avoid steering bias — applying data augmentation techniques such as rotation and brightness/color adjustment to improve the network's generalization.
In addition, I labeled data for object detection — obstacles, crosswalks, and so on — to train the YOLO model. Separately from detecting the objects themselves, proper autonomous driving also requires stopping at an appropriate distance from an object. I wrote a rule-based algorithm that computes the distance to an object from the bounding box YOLO detects, and uses that to decide whether to keep driving or stop.

Image distribution

The system uses OpenCV to capture video frames and Python's threading module for parallelism. The main thread handles CNN inference and motor control, while a background thread runs YOLO asynchronously so that the heavy object-detection computation doesn't block the driving logic.

2. Steering Angle Prediction (Custom CNN)

A custom PyTorch CNN inspired by NVIDIA's End-to-End Autonomous Driving model. The input image is cropped to remove parts unnecessary for prediction, then resized to 200×66. Through this process, the network is set up to take in only the region that matters most for predicting the steering angle — and I found that cropping and resizing the image alone had a large effect on training efficiency and performance.
After that, the image is converted from RGB to YUV color space, filtered with Gaussian Blur, and then goes through thresholding to highlight lane features.

RGB packs brightness and color information together in every channel, which makes it hard to isolate lane edges from color noise. YUV separates luminance (Y) from chrominance (U, V) — I leaned on the fact that lane markings are usually distinguished more by brightness contrast than by hue. In practice, I found that a model trained on RGB input indoors saw its performance drop sharply outdoors under strong sunlight, and that using the YUV format made lane features easier to learn and more robust to lighting changes than RGB.

YUV image

RGB → YUV → Threshold → RGB pipeline

The full transform, in order: resize to 200×66, convert BGR → YUV, apply a 3×3 Gaussian blur to suppress high-frequency noise, binary-inverse-threshold each channel at 130 (pixel ≤ 130 → 255, else 0), then convert YUV → RGB so the result can feed the network (and be visualized) as a standard 3-channel image.

# BGR → YUV
yuv = cv2.cvtColor(image, cv2.COLOR_BGR2YUV)

# Gaussian Blur — suppress high-frequency noise before thresholding
blurred = cv2.GaussianBlur(yuv, (3, 3), 0)

# Binary inverse threshold, applied per-channel on Y/U/V
_, thresholded = cv2.threshold(
    blurred, 130, 255, cv2.THRESH_BINARY_INV
)

# YUV → RGB — back to a standard 3-channel image for the network / visualization
processed_rgb = cv2.cvtColor(thresholded, cv2.COLOR_YUV2RGB)
Notes
  • YUV visualization is misleading. Passing the raw yuv array straight to plt.imshow() makes Matplotlib interpret its three channels as R/G/B — the colors shown do not represent what the YUV image actually looks like, it's purely a debugging artifact of the plotting call.
  • The final YUV → RGB step is lossy. Thresholding already discards information from the original image, so converting the thresholded YUV data back to RGB does not reconstruct the original photo — it just re-represents the already processed (edge-highlighted) data in RGB channels.

3. Real-Time Object Detection (YOLOv5)

YOLOv5 identifies crossroad, emptyBottle, andlaneBottle targets. Bounding box sizes and positions from the background thread are passed via thread-safe queues. If an obstacle is detected within a distance threshold, the system automatically slows or stops, overriding the CNN's driving commands.

YOLO training images

4. Motor Control

Using RPi.GPIO, PWM signals are dispatched to the left and right motors. Duty cycles are adjusted to execute maneuvers smoothly, completing the end-to-end autonomous driving loop.

Search the site

Choose a destination, or type to search.