-
-
Notifications
You must be signed in to change notification settings - Fork 56.4k
Add sample support of YOLOv9 and YOLOv10 in OpenCV
#25794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
201f642
support for yolov10
Abdurrahheem c2c114b
rename link
Abdurrahheem 6ac640e
fix naming
Abdurrahheem 9975711
add support for yolov9
Abdurrahheem 858a264
add number of classes to arguments
Abdurrahheem 77f7220
add test for yolov10
Abdurrahheem 45d8393
add yolov9 test
Abdurrahheem 3f24608
add instruction on how to run yolov10
Abdurrahheem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,9 @@ model, but the methodology applies to other supported models. | |
|
|
||
| @note Currently, OpenCV supports the following YOLO models: | ||
| - [YOLOX](https://github.com/Megvii-BaseDetection/YOLOX/blob/main), | ||
| - [YoloNas](https://github.com/Deci-AI/super-gradients/tree/master), | ||
| - [YOLONas](https://github.com/Deci-AI/super-gradients/tree/master), | ||
| - [YOLOv10](https://github.com/THU-MIG/yolov10/tree/main), | ||
| - [YOLOv9](https://github.com/WongKinYiu/yolov9), | ||
| - [YOLOv8](https://github.com/ultralytics/ultralytics/tree/main), | ||
| - [YOLOv7](https://github.com/WongKinYiu/yolov7/tree/main), | ||
| - [YOLOv6](https://github.com/meituan/YOLOv6/blob/main), | ||
|
|
@@ -79,7 +81,7 @@ the ONNX graph, a process that we will detail further in the subsequent sections | |
|
|
||
| Now that we know know the parameters of the pre-precessing we can go on and export the model from | ||
| Pytorch to ONNX graph. Since in this tutorial we are using YOLOX as our sample model, lets use its | ||
| export for demonstration purposes (the process is identical for the rest of the YOLO detectors). | ||
| export for demonstration purposes (the process is identical for the rest of the YOLO detectors except `YOLOv10` model, see details on how to export it later in the post). | ||
| To exporting YOLOX we can just use [export script](https://github.com/Megvii-BaseDetection/YOLOX/blob/ac58e0a5e68e57454b7b9ac822aced493b553c53/tools/export_onnx.py). Particularly we need following commands: | ||
|
|
||
| @code{.bash} | ||
|
|
@@ -125,6 +127,20 @@ than YOLOX) in case it is needed. However, usually each YOLO repository has pred | |
| onnx.save(model_simp, args.output_name) | ||
| @endcode | ||
|
|
||
| #### Exporting YOLOv10 model | ||
|
|
||
| In oder to run YOLOv10 one needs to cut off postporcessing with dynamic shapes from torch and then convert it to ONNX. If someone is looking for on how to cut off the postprocessing, there is this [forked branch](https://github.com/Abdurrahheem/yolov10/tree/ash/opencv-export) from official YOLOv10. The forked branch cuts of the postprocessing by [returning output](https://github.com/Abdurrahheem/yolov10/blob/4fdaafd912c8891642bfbe85751ea66ec20f05ad/ultralytics/nn/modules/head.py#L522) of the model before postprocessing procedure itself. To convert torch model to ONNX follow this proceduce. | ||
|
|
||
| @code{.bash} | ||
| git clone [email protected]:Abdurrahheem/yolov10.git | ||
| conda create -n yolov10 python=3.9 | ||
| conda activate yolov10 | ||
| pip install -r requirements.txt | ||
| python export_opencv.py --model=<model-name> --imgsz=<input-img-size> | ||
| @endcode | ||
|
|
||
| By default `--model="yolov10s"` and `--imgsz=(480,640)`. This will generate file `yolov10s.onnx`, which can be use for inference in OpenCV | ||
|
|
||
| ### Running Yolo ONNX detector with OpenCV Sample | ||
|
|
||
| Once we have our ONNX graph of the model, we just simply can run with OpenCV's sample. To that we need to make sure: | ||
|
|
@@ -144,24 +160,25 @@ Once we have our ONNX graph of the model, we just simply can run with OpenCV's s | |
| --padvalue=<padding_value> \ | ||
| --paddingmode=<padding_mode> \ | ||
| --backend=<computation_backend> \ | ||
| --target=<target_computation_device> | ||
| --target=<target_computation_device> \ | ||
| --width=<model_input_width> \ | ||
| --height=<model_input_height> \ | ||
| @endcode | ||
|
|
||
| VIDEO DEMO: | ||
| @youtube{NHtRlndE2cg} | ||
|
|
||
| - --input: File path to your input image or video. If omitted, it will capture frames from a camera. | ||
| - --classes: File path to a text file containing class names for object detection. | ||
| - --thr: Confidence threshold for detection (e.g., 0.5). | ||
| - --nms: Non-maximum suppression threshold (e.g., 0.4). | ||
| - --mean: Mean normalization value (e.g., 0.0 for no mean normalization). | ||
| - --scale: Scale factor for input normalization (e.g., 1.0). | ||
| - --scale: Scale factor for input normalization (e.g., 1.0, 1/255.0, etc). | ||
| - --yolo: YOLO model version (e.g., YOLOv3, YOLOv4, etc.). | ||
| - --padvalue: Padding value used in pre-processing (e.g., 114.0). | ||
| - --paddingmode: Method for handling image resizing and padding. Options: 0 (resize without extra processing), 1 (crop after resize), 2 (resize with aspect ratio preservation). | ||
| - --backend: Selection of computation backend (0 for automatic, 1 for Halide, 2 for OpenVINO, etc.). | ||
| - --target: Selection of target computation device (0 for CPU, 1 for OpenCL, etc.). | ||
| - --device: Camera device number (0 for default camera). If `--input` is not provided camera with index 0 will used by default. | ||
| - --width: Model input width. Not to be confused with the image width. (e.g., 416, 480, 640, 1280, etc). | ||
| - --height: Model input height. Not to be confused with the image height. (e.g., 416, 480, 640, 1280, etc). | ||
|
|
||
| Here `mean`, `scale`, `padvalue`, `paddingmode` should exactly match those that we discussed | ||
| in pre-processing section in order for the model to match result in PyTorch | ||
|
|
@@ -183,7 +200,8 @@ cd <build directory of OpenCV> | |
| ./bin/example_dnn_yolo_detector | ||
| @endcode | ||
|
|
||
| This will execute the YOLOX detector with your camera. For YOLOv8 (for instance), follow these additional steps: | ||
| This will execute the YOLOX detector with your camera. | ||
| For YOLOv8 (for instance), follow these additional steps: | ||
|
|
||
| @code{.sh} | ||
| cd opencv_extra/testdata/dnn | ||
|
|
@@ -195,6 +213,23 @@ cd <build directory of OpenCV> | |
| ./bin/example_dnn_yolo_detector --model=onnx/models/yolov8n.onnx --yolo=yolov8 --mean=0.0 --scale=0.003921568627 --paddingmode=2 --padvalue=144.0 --thr=0.5 --nms=0.4 --rgb=0 | ||
| @endcode | ||
|
|
||
| For YOLOv10, follow these steps: | ||
|
|
||
| @code{.sh} | ||
| cd opencv_extra/testdata/dnn | ||
| python download_models.py yolov10 | ||
| cd .. | ||
| export OPENCV_TEST_DATA_PATH=$(pwd) | ||
| cd <build directory of OpenCV> | ||
|
|
||
| ./bin/example_dnn_yolo_detector --model=onnx/models/yolov8n.onnx --yolo=yolov10 --width=640 --height=480 --scale=0.003921568627 --padvalue=114 | ||
| @endcode | ||
|
|
||
| This will run `YOLOv10` detector on first camera found on your system. If you want to run it on a image/video file, you can use `--input` option to specify the path to the file. | ||
|
|
||
|
|
||
| VIDEO DEMO: | ||
| @youtube{NHtRlndE2cg} | ||
|
|
||
| ### Building a Custom Pipeline | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.