Add DaSiamRPN tracker sample of c++ version#19078
Conversation
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| Box targetBox = {0,0,0,0}; | ||
| int scoreSize = (instanceSize-exemplarSize)/totalStride+1; | ||
|
|
||
| std::vector<float> initBox= {334.02, 128.36, 438.19, 188.78, 396.39, 260.83, 292.23, 200.41}; |
There was a problem hiding this comment.
As I see here, you are trying to reproduce the original version of the tracker from https://github.com/foolwood/DaSiamRPN/ .
Maybe it is gonna be better to use as an example our version from
https://github.com/opencv/opencv/blob/master/samples/dnn/dasiamrpn_tracker.py ?
There was a problem hiding this comment.
Thank you for pointing this out, the new code has added the mouse to select the initial box.
|
@alalek, for now, as I see it, C++ version is placed here |
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| VideoCapture cap; | ||
| Mat image; | ||
|
|
||
| bool openSuccess = parser.has("input") ? cap.open(parser.get<String>("input")) : cap.open(0); |
There was a problem hiding this comment.
Please use samples::findFileOrKeep() helper for VideoCapture input source (to support GStreamer pipelines or URLs)
There was a problem hiding this comment.
Thanks. In the new version, I have used samples::findFileOrKeep().
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| Mat image; | ||
|
|
||
| bool openSuccess = parser.has("input") ? cap.open(parser.get<String>("input")) : cap.open(0); | ||
| CV_Assert(openSuccess); |
There was a problem hiding this comment.
It is better to print the message with video file name instead of just CV_Assert()
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| siamRPN.setPreferableBackend(5); | ||
| siamRPN.setPreferableTarget(DNN_TARGET_CUDA); |
There was a problem hiding this comment.
5
Avoid magic numbers.
DNN_TARGET_CUDA
Please make this as sample command-line parameter. See here
BTW, There are multiple networks used.
| CommandLineParser parser(argc, argv, keys); | ||
|
|
||
| if (argc == 1 || parser.has("help")) | ||
| { | ||
| parser.printMessage(); | ||
| return 0; | ||
| } | ||
|
|
||
| std::string inputName = parser.get<String>("input"); |
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| for(int j = 0; j < *(p+3); j++) | ||
| { | ||
| index[0] = n, index[1] = k, index[2] = i, index[3] = j; | ||
| src.at<float>(index) = fmax(src.at<float>(index), (float)(1.0/src.at<float>(index))); |
There was a problem hiding this comment.
.at() is evaluated 3 times here.
Use this code instead:
float& v = src.at<float>(index);
v = fmax(v, 1.0f / v);
There was a problem hiding this comment.
Thank you for your careful review. I have used your comment in new version.
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| img(Rect(int(xMin), int(yMin), int(xMax - xMin + 1), int(yMax - yMin + 1))).copyTo(zCrop); | ||
| } else | ||
| { | ||
| copyMakeBorder(img, img, topPad, bottomPad, leftPad, rightPad, BORDER_CONSTANT, avgChans ); |
There was a problem hiding this comment.
img, img
Don't use in-place parameters with non-inplace processing.
It has significant performance penalty.
img is passed as reference here (non-const). Function name (get*) says nothing about modification of passed image.
9e3a3a2 to
f751c9c
Compare
f751c9c to
c25d8c2
Compare
c25d8c2 to
31c8767
Compare
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| siamRPN.setPreferableBackend(backend); | ||
| siamRPN.setPreferableTarget(target); | ||
| siamKernelR1.setPreferableBackend(backend); | ||
| siamKernelR1.setPreferableTarget(target); | ||
| siamKernelCL1.setPreferableBackend(backend); | ||
| siamKernelCL1.setPreferableTarget(target); |
There was a problem hiding this comment.
I propose to set parameters before object selection and print actual used backend to console. Not all target-backend combinations are valid and user can get this information on start.
There was a problem hiding this comment.
Thank you for pointing out this. I will fix it soon.
There was a problem hiding this comment.
I added 2 commits which include this fix.
Please fetch latest changes from the branch in your fork.
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| return anchors; | ||
| } | ||
|
|
||
| Mat getSubwindow(Mat &img, const Rect2f &targetBox, float originalSize, Scalar avgChans) |
There was a problem hiding this comment.
I propose to rename the function to copySubwindow to stress that it's not ROI, but deep copy.
There was a problem hiding this comment.
This name is similar to .py sample.
There was a problem hiding this comment.
Thank you for reviewing my code, Should I change this in .cpp file, @alalek @asmorkalov ?
There was a problem hiding this comment.
I believe we can merge PR in its current state (when CI pass checks).
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| yMax = yMax + topPad; | ||
| yMin = yMin + topPad; | ||
|
|
||
| if(topPad == 0 && bottomPad ==0 && leftPad == 0 && rightPad == 0) |
There was a problem hiding this comment.
bottomPad ==0 space is missing.
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| // Parse command line arguments. | ||
| CommandLineParser parser(argc, argv, keys); | ||
|
|
||
| if (argc == 1 || parser.has("help")) |
There was a problem hiding this comment.
It looks strange. All parameters have default values and I expect that sample should work by default with camera 0, if all models are there.
|
The sample works for me on Ubuntu 18.04. |
- exclude "keys" variable
alalek
left a comment
There was a problem hiding this comment.
Added commits with coding style and UX fixes.
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| return anchors; | ||
| } | ||
|
|
||
| Mat getSubwindow(Mat &img, const Rect2f &targetBox, float originalSize, Scalar avgChans) |
There was a problem hiding this comment.
This name is similar to .py sample.
samples/dnn/dasiamrpn_tracker.cpp
Outdated
| yMax = yMax + topPad; | ||
| yMin = yMin + topPad; | ||
|
|
||
| if(topPad == 0 && bottomPad ==0 && leftPad == 0 && rightPad == 0) |
…_plus Add DaSiamRPN tracker sample of c++ version * add sample dasiamrpn_tracker of c++ version. * samples(dasiamrpn_tracker.cpp): apply clang-format - exclude "keys" variable * samples(dasiamrpn_tracker.cpp): coding style and UX fixes
Hi, This is DaSiamRPN tracker with C++ version. The model used in this example is the same as PR18033.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.