Skip to content

Add DaSiamRPN tracker sample of c++ version#19078

Merged
alalek merged 3 commits intoopencv:masterfrom
zihaomu:dasiamrpn_tracker_c_plus_plus
Jan 24, 2021
Merged

Add DaSiamRPN tracker sample of c++ version#19078
alalek merged 3 commits intoopencv:masterfrom
zihaomu:dasiamrpn_tracker_c_plus_plus

Conversation

@zihaomu
Copy link
Copy Markdown
Member

@zihaomu zihaomu commented Dec 11, 2020

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

  • 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 other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to 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
Xforce_builders_only=docs,linux,win64

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};
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.

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 ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thank you for pointing this out, the new code has added the mouse to select the initial box.

@ieliz
Copy link
Copy Markdown
Contributor

ieliz commented Dec 11, 2020

@alalek, for now, as I see it, C++ version is placed here samples/dnn/dasiamrpn_tracker.cpp .
Can we choose a more suitable place for the C++ version or we should wait for the PR: opencv/opencv_contrib#2753 ?

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

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

Thank you for contribution!

@zihaomu zihaomu requested a review from alalek December 22, 2020 12:22
Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

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

Well done!

VideoCapture cap;
Mat image;

bool openSuccess = parser.has("input") ? cap.open(parser.get<String>("input")) : cap.open(0);
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.

Please use samples::findFileOrKeep() helper for VideoCapture input source (to support GStreamer pipelines or URLs)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks. In the new version, I have used samples::findFileOrKeep().

Mat image;

bool openSuccess = parser.has("input") ? cap.open(parser.get<String>("input")) : cap.open(0);
CV_Assert(openSuccess);
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 is better to print the message with video file name instead of just CV_Assert()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, fixed.

@zihaomu zihaomu requested a review from alalek December 29, 2020 02:21
Comment on lines +162 to +163
siamRPN.setPreferableBackend(5);
siamRPN.setPreferableTarget(DNN_TARGET_CUDA);
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.

5

Avoid magic numbers.

DNN_TARGET_CUDA

Please make this as sample command-line parameter. See here


BTW, There are multiple networks used.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, fixed.

Comment on lines +82 to +100
CommandLineParser parser(argc, argv, keys);

if (argc == 1 || parser.has("help"))
{
parser.printMessage();
return 0;
}

std::string inputName = parser.get<String>("input");
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.

fix indentation (4 spaces)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, fixed

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)));
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.

.at() is evaluated 3 times here.

Use this code instead:

float& v = src.at<float>(index);
v = fmax(v, 1.0f / v);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thank you for your careful review. I have used your comment in new version.

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 );
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.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, fixed.

@zihaomu zihaomu force-pushed the dasiamrpn_tracker_c_plus_plus branch from 9e3a3a2 to f751c9c Compare January 7, 2021 15:43
@zihaomu zihaomu requested a review from alalek January 8, 2021 07:04
@zihaomu zihaomu force-pushed the dasiamrpn_tracker_c_plus_plus branch from f751c9c to c25d8c2 Compare January 16, 2021 13:11
@zihaomu zihaomu force-pushed the dasiamrpn_tracker_c_plus_plus branch from c25d8c2 to 31c8767 Compare January 17, 2021 00:54
Comment on lines +170 to +175
siamRPN.setPreferableBackend(backend);
siamRPN.setPreferableTarget(target);
siamKernelR1.setPreferableBackend(backend);
siamKernelR1.setPreferableTarget(target);
siamKernelCL1.setPreferableBackend(backend);
siamKernelCL1.setPreferableTarget(target);
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.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thank you for pointing out this. I will fix it soon.

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.

I added 2 commits which include this fix.
Please fetch latest changes from the branch in your fork.

return anchors;
}

Mat getSubwindow(Mat &img, const Rect2f &targetBox, float originalSize, Scalar avgChans)
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.

I propose to rename the function to copySubwindow to stress that it's not ROI, but deep copy.

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.

This name is similar to .py sample.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thank you for reviewing my code, Should I change this in .cpp file, @alalek @asmorkalov ?

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.

I believe we can merge PR in its current state (when CI pass checks).

yMax = yMax + topPad;
yMin = yMin + topPad;

if(topPad == 0 && bottomPad ==0 && leftPad == 0 && rightPad == 0)
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.

bottomPad ==0 space is missing.

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.

applied clang-format

// Parse command line arguments.
CommandLineParser parser(argc, argv, keys);

if (argc == 1 || parser.has("help"))
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.

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.

@asmorkalov
Copy link
Copy Markdown
Contributor

The sample works for me on Ubuntu 18.04.

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

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

Added commits with coding style and UX fixes.

return anchors;
}

Mat getSubwindow(Mat &img, const Rect2f &targetBox, float originalSize, Scalar avgChans)
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.

This name is similar to .py sample.

yMax = yMax + topPad;
yMin = yMin + topPad;

if(topPad == 0 && bottomPad ==0 && leftPad == 0 && rightPad == 0)
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.

applied clang-format

@alalek alalek merged commit 13a1105 into opencv:master Jan 24, 2021
@alalek alalek mentioned this pull request Apr 9, 2021
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
…_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
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.

4 participants