-
Notifications
You must be signed in to change notification settings - Fork 197
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
C++ Demo - Object Detection (NanoDet) #232
Conversation
@fengyuentau I finally finished my research project so I'm super excited to finally start working on these projects! It's somewhat refactored but I can definitely clean it up significantly more if you'd like, such as
I read the guide from OpenCV Coding Style Guide but I may very well have missed some. Please let me know if it's not up to standards and I can clean it up further. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got two warnings building the demo:
cmake --build build
[ 50%] Building CXX object CMakeFiles/opencv_zoo_object_detection_nanodet.dir/demo.cpp.o
/workspace/opencv_zoo/models/object_detection_nanodet/demo.cpp:115:39: warning: left operand of comma operator has no effect [-Wunused-value]
return projection.reshape(0, (4, projection.total() / 4));
^
/workspace/opencv_zoo/models/object_detection_nanodet/demo.cpp:238:14: warning: decomposition declarations are a C++17 extension [-Wc++17-extensions]
auto [classIds, confidences] = getClassIdAndConfidences(scores);
^~~~~~~~~~~~~~~~~~~~~~~
2 warnings generated.
[100%] Linking CXX executable opencv_zoo_object_detection_nanodet
[100%] Built target opencv_zoo_object_detection_nanodet
We use C++11 standard as same as OpenCV 4.x.
Appreciate the review, @fengyuentau! I've addressed the comments, but please flag any issues or suggest improvements. Successfully tested on my system using the following commands:
On a side note, I observed a consistent format across all C++ demos. I refactored some functions, but if strict adherence is preferred, I can revert these changes. Let me know your thoughts. |
Mat confidences = std::get<1>(classIdAndConfidences); | ||
|
||
vector<int> indices; | ||
NMSBoxes(boxesXYXY, confidences, probThreshold, iouThreshold, indices); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also take a look at NMSBoxesBatched
which may simply the code:
opencv_zoo/models/object_detection_yolox/yolox.py
Lines 59 to 64 in fd2da74
# get scores and class indices | |
scores = dets[:, 4:5] * dets[:, 5:] | |
max_scores = np.amax(scores, axis=1) | |
max_scores_idx = np.argmax(scores, axis=1) | |
keep = cv2.dnn.NMSBoxesBatched(boxes_xyxy.tolist(), max_scores.tolist(), max_scores_idx.tolist(), self.confThreshold, self.nmsThreshold) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the suggestion! The code is now cleaner and more efficient, achieving a higher frame rate of 25-26 FPS (see PR description).
…ssID and confidence function, and use NMSBoxesBatched now.
(hope it's ok to ask you here 😅) My interest in long-term open-source projects aligns well with (C++ or Python):
Thanks for your time and guidance @fengyuentau! |
You can find others in https://groups.google.com/g/opencv-gsoc-202x. Normally, mentors will be more interactive as time move to proposal submit stage. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the contribution 👍
This PR adds the CPP equivalent version
demo.cpp
of the existing Python demo for the NanoDet object detection model. TheREADME
is correspondingly updated. The interface matches other cpp demos within the set of ML models. The C++ demo more than doubles the FPS of the equivalent Python demo for a video feed. Issue was pointed out in #135 (comment).Average FPS on my laptop (AMD Ryzen 7 5800H) for a (640 x 480) webcam feed:
Testing
Run the Demo
Output Visualization Images from C++ and Python
Input
C++
Command:
./build/opencv_zoo_object_detection_nanodet -i=scene.jpg
Python
Command:
python3 demo.py -i scene.jpg
Output Video from C++ and Python
C++
Command:
./build/opencv_zoo_object_detection_nanodet
Python
Command:
python3 demo.py
Confirm matching I/O and intermediate values
C++
Python
Test Summary: Both the visualizations and the intermediate values and I/O (values, shapes, and types) are identical between the Python and C++ demos.