Object Detection with OpenCV and CUDA
This tutorial covers setting up object detection capabilities on your FEI JetRacer using OpenCV with CUDA acceleration and YOLOv4-Tiny for real-time performance.
Overview
We'll use:
- OpenCV with CUDA: For accelerated computer vision operations
- YOLOv4-Tiny: A lightweight object detection model optimized for embedded devices
- cuDNN: NVIDIA's deep learning acceleration library
Note
If you are using our pre-configured JetRacer image, some of this setup is already complete!
You can skip the OpenCV with CUDA installation steps below and go directly to Installation Verification.
Manual Installation
Step 1: Install OpenCV with CUDA Support
We'll use the Qengineering installation script which automates the complex process of building OpenCV with CUDA and cuDNN support.
Download the Installation Script
cd ~
git clone https://github.com/Qengineering/Install-OpenCV-Jetson-Nano.git
cd Install-OpenCV-Jetson-Nano
Prepare Your System
Before running the installation script, ensure your system is ready:
sudo apt update && sudo apt upgrade -y
sudo apt autoremove -y
Free up disk space and increase swap:
sudo systemctl disable nvzramconfig
sudo fallocate -l 4G /var/swapfile
sudo chmod 600 /var/swapfile
sudo mkswap /var/swapfile
sudo swapon /var/swapfile
sudo swapon -s
Run the Installation Script
Warning: This process can take 2-4 hours to complete. Ensure stable power supply and don't interrupt the process.
chmod +x ./OpenCV-4-11-0.sh
./OpenCV-4-11-0.sh
The script will:
- Install all necessary dependencies
- Download OpenCV source code
- Configure build with CUDA support
- Compile OpenCV (this takes the longest time)
- Install the compiled libraries
Verify Installation
After installation completes, verify OpenCV with CUDA support:
python3 -c "import cv2; print('OpenCV version:', cv2.__version__); print('CUDA devices:', cv2.cuda.getCudaEnabledDeviceCount())"
You should see output similar to:
OpenCV version: 4.11.1
CUDA devices: 1
Step 2: Configure Environment
Ensure the OpenCV installation is properly configured in your DonkeyCar environment:
source ~/env/bin/activate
cd ~/mycar
Test OpenCV in your virtual environment:
python3 -c "import cv2; print('OpenCV loaded successfully with version:', cv2.__version__)"
Step 3: Setup YOLOv4-Tiny
Now we'll add YOLOv4-Tiny object detection model to your DonkeyCar setup.
Create Models Directory
cd ~/mycar
mkdir -p models
cd models
Download YOLOv4-Tiny Files
Download the pre-trained YOLOv4-Tiny model files:
wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4-tiny.weights
wget https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-tiny.cfg
wget https://raw.githubusercontent.com/AlexeyAB/darknet/master/data/coco.names
Verify Model Files
Check that all required files are present:
ls -la ~/mycar/models/
You should see:
yolov4-tiny.weights(approximately 23MB)yolov4-tiny.cfg(configuration file)coco.names(class labels file)
Test Object Detection
Test by running the DonkeyCar web interface:
cd ~/mycar
python manage.py drive
You should see the object detection in the camera feed in the web interface.
Performance Tips
Optimize for Real-time Performance
-
Resolution Tuning: Balance between accuracy and speed by adjusting input resolution
-
Detection Frequency: Run detection every few frames rather than every frame
-
ROI Processing: Only process regions of interest in the image
Troubleshooting
OpenCV Installation Issues
If the installation script fails:
- Insufficient storage: Ensure at least 16GB free space
- Memory issues: Increase swap file size
- Power issues: Use adequate power supply
CUDA Not Working
If CUDA acceleration isn't working:
# Check CUDA installation
nvcc --version
# Verify cuDNN
cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
Model Loading Errors
If YOLO models fail to load:
-
Verify file integrity:
md5sum ~/mycar/models/yolov4-tiny.weights -
Check file permissions:
chmod 644 ~/mycar/models/*