1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

Merge pull request #25710 from gursimarsingh:improved_object_detection_sample

Merged yolo_detector and object detection sample #25710

Relates to #25006

This pull request merges the yolo_detector.cpp sample with the object_detector.cpp sample. It also beautifies the bounding box display on the output images

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Gursimar Singh
2024-09-18 18:49:46 +05:30
committed by GitHub
parent 46b800f506
commit e823493af1
6 changed files with 684 additions and 790 deletions
+24 -40
View File
@@ -150,13 +150,12 @@ Once we have our ONNX graph of the model, we just simply can run with OpenCV's s
3. Run the following command:
@code{.cpp}
./bin/example_dnn_yolo_detector --input=<path_to_your_input_file> \
--classes=<path_to_class_names_file> \
./bin/example_dnn_object_detection <model_name> --input=<path_to_your_input_file> \
--labels=<path_to_class_names_file> \
--thr=<confidence_threshold> \
--nms=<non_maximum_suppression_threshold> \
--mean=<mean_normalization_value> \
--scale=<scale_factor> \
--yolo=<yolo_model_version> \
--padvalue=<padding_value> \
--paddingmode=<padding_mode> \
--backend=<computation_backend> \
@@ -166,7 +165,7 @@ Once we have our ONNX graph of the model, we just simply can run with OpenCV's s
@endcode
- --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.
- --labels: 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).
@@ -191,43 +190,28 @@ To demonstrate how to run OpenCV YOLO samples without your own pretrained model,
Run the YOLOX detector(with default values):
@code{.sh}
git clone https://github.com/opencv/opencv_extra.git
cd opencv_extra/testdata/dnn
python download_models.py yolox_s_inf_decoder
cd ..
export OPENCV_TEST_DATA_PATH=$(pwd)
cd opencv/samples/dnn
export OPENCV_DOWNLOAD_CACHE_DIR=<path to download the model>
cd ../data
export OPENCV_SAMPLES_DATA_PATH=$(pwd)
python download_models.py yolov8x --save_dir=$OPENCV_DOWNLOAD_CACHE_DIR
cd <build directory of OpenCV>
./bin/example_dnn_yolo_detector
./bin/example_dnn_object_detection yolov8x
@endcode
This will execute the YOLOX detector with your camera.
For YOLOv8 (for instance), follow these additional steps:
@code{.sh}
cd opencv_extra/testdata/dnn
python download_models.py yolov8
cd ..
export OPENCV_TEST_DATA_PATH=$(pwd)
cd opencv/samples/dnn
export OPENCV_DOWNLOAD_CACHE_DIR=<path to download the model>
cd ../data
export OPENCV_SAMPLES_DATA_PATH=$(pwd)
python download_models.py yolov8n --save_dir=$OPENCV_DOWNLOAD_CACHE_DIR
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
./bin/example_dnn_object_detection yolov8n --model=onnx/models/yolov8n.onnx --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/yolov10s.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}
@@ -238,30 +222,30 @@ module this is also quite easy to achieve. Below we will outline the sample impl
- Import required libraries
@snippet samples/dnn/yolo_detector.cpp includes
@snippet samples/dnn/object_detection.cpp includes
- Read ONNX graph and create neural network model:
@snippet samples/dnn/yolo_detector.cpp read_net
@snippet samples/dnn/object_detection.cpp read_net
- Read image and pre-process it:
@snippet samples/dnn/yolo_detector.cpp preprocess_params
@snippet samples/dnn/yolo_detector.cpp preprocess_call
@snippet samples/dnn/yolo_detector.cpp preprocess_call_func
@snippet samples/dnn/object_detection.cpp preprocess_params
@snippet samples/dnn/object_detection.cpp preprocess_call
@snippet samples/dnn/object_detection.cpp preprocess_call_func
- Inference:
@snippet samples/dnn/yolo_detector.cpp forward_buffers
@snippet samples/dnn/yolo_detector.cpp forward
@snippet samples/dnn/object_detection.cpp forward_buffers
@snippet samples/dnn/object_detection.cpp forward
- Post-Processing
All post-processing steps are implemented in function `yoloPostProcess`. Please pay attention,
that NMS step is not included into onnx graph. Sample uses OpenCV function for it.
@snippet samples/dnn/yolo_detector.cpp postprocess
@snippet samples/dnn/object_detection.cpp postprocess
- Draw predicted boxes
@snippet samples/dnn/yolo_detector.cpp draw_boxes
@snippet samples/dnn/object_detection.cpp draw_boxes