Skip to content
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

Bounding box fixes for GOTURN tracker #2620

Merged
merged 18 commits into from Sep 5, 2020
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion modules/tracking/include/opencv2/tracking/tracker.hpp
Expand Up @@ -1290,8 +1290,10 @@ class CV_EXPORTS_W TrackerGOTURN : public Tracker
struct CV_EXPORTS Params
{
Params();
void read(const FileNode& /*fn*/);
void read(const FileNode& fn);
void write(FileStorage& /*fs*/) const;
String modelTxt;
String modelBin;
};

/** @brief Constructor
Expand Down
29 changes: 23 additions & 6 deletions modules/tracking/src/gtrTracker.cpp
Expand Up @@ -45,9 +45,19 @@
namespace cv
{

TrackerGOTURN::Params::Params(){}
TrackerGOTURN::Params::Params()
{
modelTxt = "goturn.prototxt";
modelBin = "goturn.caffemodel";
}

void TrackerGOTURN::Params::read(const cv::FileNode& /*fn*/){}
void TrackerGOTURN::Params::read(const cv::FileNode& fn)
{
CV_Assert(!fn["caffemodel"].empty());
CV_Assert(!fn["prototxt"].empty());
modelBin = (String)fn["caffemodel"];
modelTxt = (String)fn["prototxt"];
dkurt marked this conversation as resolved.
Show resolved Hide resolved
}

void TrackerGOTURN::Params::write(cv::FileStorage& /*fs*/) const {}

Expand Down Expand Up @@ -75,7 +85,11 @@ class TrackerGOTURNModel : public TrackerModel{
public:
TrackerGOTURNModel(TrackerGOTURN::Params){}
Rect2d getBoundingBox(){ return boundingBox_; }
void setBoudingBox(Rect2d boundingBox){ boundingBox_ = boundingBox; }
void setBoudingBox(Rect2d boundingBox) {
if (image_.empty())
CV_Error(Error::StsInternal, "Set image first");
boundingBox_ = boundingBox & Rect2d(Point(0, 0), image_.size());
}
Mat getImage(){ return image_; }
void setImage(const Mat& image){ image.copyTo(image_); }
protected:
Expand Down Expand Up @@ -108,9 +122,7 @@ bool TrackerGOTURNImpl::initImpl(const Mat& image, const Rect2d& boundingBox)
((TrackerGOTURNModel*)static_cast<TrackerModel*>(model))->setBoudingBox(boundingBox);

//Load GOTURN architecture from *.prototxt and pretrained weights from *.caffemodel
String modelTxt = "goturn.prototxt";
String modelBin = "goturn.caffemodel";
net = dnn::readNetFromCaffe(modelTxt, modelBin);
net = dnn::readNetFromCaffe(params.modelTxt, params.modelBin);
return true;
}

Expand All @@ -137,6 +149,11 @@ bool TrackerGOTURNImpl::updateImpl(const Mat& image, Rect2d& boundingBox)
targetPatchRect.x = (float)(prevCenter.x - prevBB.width*padTargetPatch / 2.0 + targetPatchRect.width);
targetPatchRect.y = (float)(prevCenter.y - prevBB.height*padTargetPatch / 2.0 + targetPatchRect.height);

targetPatchRect.width = std::min(targetPatchRect.width, (float)prevFrame.cols);
targetPatchRect.height = std::min(targetPatchRect.height, (float)prevFrame.rows);
targetPatchRect.x = std::max(-prevFrame.cols * 0.5f, std::min(targetPatchRect.x, prevFrame.cols * 1.5f));
targetPatchRect.y = std::max(-prevFrame.rows * 0.5f, std::min(targetPatchRect.y, prevFrame.rows * 1.5f));

copyMakeBorder(prevFrame, prevFramePadded, (int)targetPatchRect.height, (int)targetPatchRect.height, (int)targetPatchRect.width, (int)targetPatchRect.width, BORDER_REPLICATE);
targetPatch = prevFramePadded(targetPatchRect).clone();

Expand Down
17 changes: 16 additions & 1 deletion modules/tracking/test/test_main.cpp
Expand Up @@ -3,4 +3,19 @@
// of this distribution and at http://opencv.org/license.html.
#include "test_precomp.hpp"

CV_TEST_MAIN("cv")
static
void initTrackingTests()
{
const char* extraTestDataPath =
#ifdef WINRT
NULL;
#else
getenv("OPENCV_DNN_TEST_DATA_PATH");
#endif
if (extraTestDataPath)
cvtest::addDataSearchPath(extraTestDataPath);

cvtest::addDataSearchSubDirectory(""); // override "cv" prefix below to access without "../dnn" hacks
}

CV_TEST_MAIN("cv", initTrackingTests())
35 changes: 35 additions & 0 deletions modules/tracking/test/test_trackers.cpp
Expand Up @@ -567,6 +567,41 @@ TEST_P(DistanceAndOverlap, Scaled_Data_CSRT)
test.run();
}

TEST(GOTURN, memory_usage)
{
cv::Rect2d roi(145, 70, 85, 85);
cv::Mat frame;

std::string model = cvtest::findDataFile("dnn/gsoc2016-goturn/goturn.prototxt");
std::string weights = cvtest::findDataFile("dnn/gsoc2016-goturn/goturn.caffemodel", false);

std::string dataPath =
"%YAML:1.0\n"
"---\n"
"prototxt: " + model + "\n"
"caffemodel: " + weights + "\n";
Copy link
Member

Choose a reason for hiding this comment

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

Avoid that. We don't testing parameters serialization here.

Use existed straightforward approach:

TrackerMedianFlow::Params parameters;
parameters.maxLevel = 10;
parameters.maxMedianLengthOfDisplacementDifference = 11;
parameters.pointsInGrid = 12;
parameters.winSize = Size(6, 5);
parameters.winSizeNCC = Size(41, 40);
parameters.termCriteria.maxCount = 100;
parameters.termCriteria.epsilon = 0.1;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I implemented usage of the paths via cv::FileNode. So, this is gonna be no used for the test?

Copy link
Member

Choose a reason for hiding this comment

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

Test must be simple and straightforward.

this is gonna be no used for the test

At least not in this test with the name "memory_usage".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please, check last changes:
I was forced to remove cv::FileNode, but instead used cv::TrackerGOTURN::Params params;, as @alalek said.
Win32/win64 builds are passed, so I think this test is ready.

cv::FileStorage goturnStorageRead( dataPath, cv::FileStorage::READ | cv::FileStorage::MEMORY);
cv::FileNode goturnNode = goturnStorageRead.root();

cv::TrackerGOTURN::Params params;
params.modelTxt = model;
params.modelBin = weights;
cv::Ptr<cv::Tracker> tracker = cv::TrackerGOTURN::create(params);
string inputVideo = cvtest::findDataFile("tracking/david/data/david.webm");
cv::VideoCapture video(inputVideo);
tracker->read(goturnNode);

video >> frame;
tracker->init(frame, roi);
string ground_truth_bb;
for (int nframes = 0; nframes < 15; ++nframes)
{
std::cout << "Frame: " << nframes << std::endl;
video >> frame;
tracker->update(frame, roi);
std::cout << "Predicted ROI: " << roi << std::endl;
dkurt marked this conversation as resolved.
Show resolved Hide resolved
}
}
dkurt marked this conversation as resolved.
Show resolved Hide resolved

INSTANTIATE_TEST_CASE_P( Tracking, DistanceAndOverlap, TESTSET_NAMES);

Expand Down