This example demonstrates how to perform inference using YOLOv5 models in C++ with OpenCV's DNN API.
This repository was originally created by Matin Ghorbani and is available at this original repository
git clone https://github.com/matin-ghorbani/Yolov5-CPP.git
First you need to download the weight
that you want from here
Then you need to convert the .pt
format to .onnx
format
To export YOLOv5 models:
First install ultralytics
pip install ultralytics
Then clone the ultralytics repository and convert the .pt
format to .onnx
format with this command:
git clone https://github.com/ultralytics/yolov5.git
cd yolov5
python3 export.py --weights YOUR_WEIGHT.pt --img 480 640 --include onnx --opset 12
Finally, Copy the YOUR_WEIGHT.onnx
file to assets/models
directory.
cd Yolov5-CPP/
mkdir build
cd build/
cmake ..
make
- On Image:
./run YOUR_IMG.JPG
- On Video:
./run YOUR_VIDEO.MP4
- On Webcam:
./run YOUR_WEBCAM_ID
Note that the example networks are exported with rectangular (640x480) resolutions