Skip to content

using cv2 dnn interface to run yolov8 model#24396

Merged
asmorkalov merged 16 commits intoopencv:4.xfrom
Tsai-chia-hsiang:yolov8cv
Nov 16, 2023
Merged

using cv2 dnn interface to run yolov8 model#24396
asmorkalov merged 16 commits intoopencv:4.xfrom
Tsai-chia-hsiang:yolov8cv

Conversation

@Tsai-chia-hsiang
Copy link
Copy Markdown
Contributor

@Tsai-chia-hsiang Tsai-chia-hsiang commented Oct 13, 2023

This is a sample code for using opencv dnn interface to run ultralytics yolov8 model for object detection.

Pull Request Readiness Checklist

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

  • I agree to contribute to the project under Apache 2 License.
  • 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
  • The PR is proposed to the proper branch
  • [] 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.
  • [] The feature is well documented and sample code can be built with the project CMake

@Tsai-chia-hsiang Tsai-chia-hsiang marked this pull request as draft October 18, 2023 06:27
@Tsai-chia-hsiang Tsai-chia-hsiang marked this pull request as ready for review October 18, 2023 06:28
@Tsai-chia-hsiang
Copy link
Copy Markdown
Contributor Author

@dkurt
Hi, during refactoring the code, we got a question about line 54:
image

However, according to the comment above it, shoudn't it be score = detection[4:] ? Since detection[0,1,2,3] are the bbox x,y,w,h and [4: ] are the score.

Or Do I misunderstand the usage of this line ?

Thanks

@dkurt dkurt self-assigned this Nov 7, 2023
@dkurt
Copy link
Copy Markdown
Member

dkurt commented Nov 9, 2023

@Tsai-chia-hsiang, made some changes with sample update and common parts unification. Please give your comment on the padding preprocessing (I removed it for now). If you don't mind, we can introduce it in the next PR.

Also, I have replaced onnx URL to Ultralytics's .pt model with a comment how to convert to ONNX.

Now sample works with the following commands:
YOLOv8 (Ultralytics):

python samples/dnn/object_detection.py yolov8 --model ~/yolov8net.onnx --input kodim23.png

YOLOv4 (Darknet):

python samples/dnn/object_detection.py yolo --model ~/yolov4.weights --input kodim23.png

SSD (Caffe):

python samples/dnn/object_detection.py ssd_caffe --input kodim23.png

@Tsai-chia-hsiang
Copy link
Copy Markdown
Contributor Author

Tsai-chia-hsiang commented Nov 9, 2023

@dkurt
Ok, so should I open another PR ? or just add the comment and commit in this PR is fine ?
Thanks.

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Nov 9, 2023

@Tsai-chia-hsiang, I recommend to keep this PR just enable YOLOv8 postprocessing. Need more time to think about the pre-processing proposal.

@Tsai-chia-hsiang
Copy link
Copy Markdown
Contributor Author

OK, I see.

Thanks!

@dkurt dkurt requested a review from asmorkalov November 9, 2023 12:28
@asmorkalov
Copy link
Copy Markdown
Contributor

 python samples/dnn/object_detection.py yolov8 --model ./samples/yolov8net.onnx --input ./samples/data/lena.jpg 
[ WARN:0@0.020] global samples.cpp:61 findFile cv::samples::findFile('object_detection_classes_yolov4.txt') => ''
Traceback (most recent call last):
  File "samples/dnn/object_detection.py", line 90, in <module>
    net = cv.dnn.readNet(args.model, args.config, args.framework)
cv2.error: OpenCV(4.8.0-dev) /home/alexander/Projects/OpenCV/opencv-master/modules/dnn/src/onnx/onnx_importer.cpp:279: error: (-210:Unsupported format or combination of formats) Failed to parse ONNX model: ./samples/yolov8net.onnx in function 'ONNXImporter'

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Nov 9, 2023

@asmorkalov , dis you convert a model to ONNX or used https://github.com/JasonLin1110/ultralytics_yolov8_onnx_model/blob/main/yolov8net.onnx ? I tested second option, let me try again.

Update: convert step worked for me too. Can you please check that ./samples/yolov8net.onnx is a correct path? Maybe ./samples/yolov8n.onnx? Also, please make sure Torch for CPU is used (pip install torch torchvisoon --index-url https://download.pytorch.org/whl/cpu)

@dkurt dkurt mentioned this pull request Nov 12, 2023
4 tasks
@dkurt dkurt added this to the 4.9.0 milestone Nov 14, 2023
Tsai-chai-hsiang and others added 15 commits November 14, 2023 14:14
add yolov8 to object_detection.py and models.yml
change yoloV8 readNetFromONNX to readNet, change lastLayer.type == 'Identity' to args.postprocessing == 'yolov8'
add padding image to blobFromImage.
Solve case yolov8 doen't have config(args.config == None), so cv.samples.findFile(args.config) will return error
add a if else in nms_indices = nms_indices[:, 0] if len(nms_indices) else [], because yolov8's nms_indices is 1-d array
Correct yolov8 onnx's url and classes
minScore, minClassLoc, x
@dkurt dkurt mentioned this pull request Nov 15, 2023
4 tasks
@asmorkalov
Copy link
Copy Markdown
Contributor

Ok. I found out the issue reason.

# model.export(format="onnx")
yolov8:
load_info:
url: "https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.pt"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ONNX link is required here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to rename model: "yolov8net.onnx" to model: "yolov8n.pt".

config for download_models.py contains link to pt, but not onnx. The file is renamed to .onnx as it's defined in yaml config.

I have not seen entry for YOLOv8 in download_models.py except https://github.com/CVHub520/X-AnyLabeling/releases/download/v0.1.0/yolov8n.onnx which is not an official repo. Can we keep a link to .pt?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, excuse me. I just missed that there is another https://github.com/opencv/opencv/blob/4.x/samples/dnn/download_models.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced to URL from opencv_extra

Copy link
Copy Markdown
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
Tested manually with current OpenCV 4.x, Ubuntu 18.04 and ONNX model by link in yaml config.

@asmorkalov asmorkalov merged commit 83d70b0 into opencv:4.x Nov 16, 2023
@dkurt
Copy link
Copy Markdown
Member

dkurt commented Nov 16, 2023

@asmorkalov, thanks for testing!

IskXCr pushed a commit to Haosonn/opencv that referenced this pull request Dec 20, 2023
Using cv2 dnn interface to run yolov8 model opencv#24396

This is a sample code for using opencv dnn interface to run ultralytics yolov8 model for object detection.

### 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
- [] 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.
- [] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
Using cv2 dnn interface to run yolov8 model opencv#24396

This is a sample code for using opencv dnn interface to run ultralytics yolov8 model for object detection.

### 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
- [] 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.
- [] The feature is well documented and sample code can be built with the project CMake
@asmorkalov asmorkalov mentioned this pull request Jan 19, 2024
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
Using cv2 dnn interface to run yolov8 model opencv#24396

This is a sample code for using opencv dnn interface to run ultralytics yolov8 model for object detection.

### 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
- [] 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.
- [] The feature is well documented and sample code can be built with the project CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants