A high-performance C++ implementation of YOLOv11 object detection using ONNX Runtime and OpenCV.
- Fast and efficient object detection using YOLOv11
- Support for both CPU and GPU inference (CUDA)
- Video processing capabilities
- Dynamic confidence and IoU thresholds
- Visual performance metrics (FPS counter)
- Semi-transparent bounding box masks for cleaner visualization
- CMake 3.12+
- C++17 compatible compiler
- OpenCV 4.x
- ONNX Runtime 1.17+
- CUDA Toolkit (optional, for GPU acceleration)
git clone https://github.com/yourusername/yolov11cpp.git
cd yolov11cpp
mkdir build
cd build
cmake ..
make -j$(nproc)
- Export your YOLOv11 model to ONNX format using Ultralytics:
# If using Python/Ultralytics
yolo export model=yolov11s.pt format=onnx opset=12 simplify=True
- Place your ONNX model and class names file in the project directory:
cp path/to/best.onnx ./
cp path/to/classes.txt ./
./yolov11_detector [options]
--model
: Path to the ONNX model file (default: "./best.onnx")--classes
: Path to the class names file (default: "./classes.txt")--input
: Path to input video file or camera device index (default: "./input.mov")--output
: Path for output video file (default: "./output.mp4")--gpu
: Use GPU acceleration if available (default: false)--conf
: Confidence threshold (default: 0.25)--iou
: IoU threshold for NMS (default: 0.45)
# Process a video file with custom thresholds
./yolov11_detector --input=test_video.mp4 --output=result.mp4 --conf=0.3 --iou=0.4
# Use webcam (device 0) with GPU acceleration
./yolov11_detector --input=0 --gpu=true
You can modify the default settings by editing the constants in:
src/camera_inference.cpp
- Main application settingssrc/ia/YOLO11.hpp
- Detection parameters and algorithmssrc/ia/tools/Config.hpp
- Debug and timing configurations
Enable debugging by uncommenting these lines in src/ia/tools/Config.hpp
:
// Enable debug messages
#define DEBUG_MODE
// Enable performance timing
#define TIMING_MODE
If you notice differences in detection accuracy compared to the Python implementation:
- Verify your ONNX model is exported correctly with proper settings
- Check that preprocessing matches Ultralytics implementation (RGB conversion, normalization)
- Confirm your class names file is correct and in the expected format
- Try adjusting the confidence and IoU thresholds to match Ultralytics defaults (0.25 and 0.45)
- For CPU optimization, ensure
ORT_ENABLE_ALL
optimization is enabled - For GPU usage, verify CUDA toolkit and ONNX Runtime with CUDA support are installed
- Reduce input image resolution for better performance