mirror of
https://github.com/opencv/opencv_contrib.git
synced 2025-10-20 21:40:49 +08:00
Update for multiTracker_test.cpp->multiTracker_dataset.cpp
Sample has been renamed: tld_test.cpp->tracker_dataset.cpp Updated to read parameters from cmd: -Algorithm name -Root dataset path -Dataset ID -Number of targets to track
This commit is contained in:
@@ -39,6 +39,7 @@
|
|||||||
//
|
//
|
||||||
//M*/
|
//M*/
|
||||||
|
|
||||||
|
#include "opencv2/datasets/track_vot.hpp"
|
||||||
#include <opencv2/core/utility.hpp>
|
#include <opencv2/core/utility.hpp>
|
||||||
#include <opencv2/tracking.hpp>
|
#include <opencv2/tracking.hpp>
|
||||||
#include <opencv2/videoio.hpp>
|
#include <opencv2/videoio.hpp>
|
||||||
@@ -47,6 +48,7 @@
|
|||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace cv;
|
using namespace cv;
|
||||||
|
using namespace cv::datasets;
|
||||||
|
|
||||||
#define NUM_TEST_FRAMES 100
|
#define NUM_TEST_FRAMES 100
|
||||||
#define TEST_VIDEO_INDEX 7 //TLD Dataset Video Index from 1-10
|
#define TEST_VIDEO_INDEX 7 //TLD Dataset Video Index from 1-10
|
||||||
@@ -54,13 +56,23 @@ using namespace cv;
|
|||||||
|
|
||||||
static Mat image;
|
static Mat image;
|
||||||
static bool paused;
|
static bool paused;
|
||||||
static bool selectObject = false;
|
static bool selectObjects = false;
|
||||||
static bool startSelection = false;
|
static bool startSelection = false;
|
||||||
|
vector<Rect2d> boundingBoxes;
|
||||||
|
int targetsCnt = 0;
|
||||||
|
int targetsNum = 0;
|
||||||
Rect2d boundingBox;
|
Rect2d boundingBox;
|
||||||
|
|
||||||
|
static const char* keys =
|
||||||
|
{ "{@tracker_algorithm | | Tracker algorithm }"
|
||||||
|
"{@dataset_path |true| Dataset path }"
|
||||||
|
"{@dataset_id |1| Dataset path }"
|
||||||
|
"{@target_num |1| Number of targets }"
|
||||||
|
};
|
||||||
|
|
||||||
static void onMouse(int event, int x, int y, int, void*)
|
static void onMouse(int event, int x, int y, int, void*)
|
||||||
{
|
{
|
||||||
if (!selectObject)
|
if (!selectObjects)
|
||||||
{
|
{
|
||||||
switch (event)
|
switch (event)
|
||||||
{
|
{
|
||||||
@@ -75,16 +87,24 @@ static void onMouse(int event, int x, int y, int, void*)
|
|||||||
//sei with and height of the bounding box
|
//sei with and height of the bounding box
|
||||||
boundingBox.width = std::abs(x - boundingBox.x);
|
boundingBox.width = std::abs(x - boundingBox.x);
|
||||||
boundingBox.height = std::abs(y - boundingBox.y);
|
boundingBox.height = std::abs(y - boundingBox.y);
|
||||||
paused = false;
|
boundingBoxes.push_back(boundingBox);
|
||||||
selectObject = true;
|
targetsCnt++;
|
||||||
|
if (targetsCnt == targetsNum)
|
||||||
|
{
|
||||||
|
paused = false;
|
||||||
|
selectObjects = true;
|
||||||
|
}
|
||||||
|
startSelection = false;
|
||||||
break;
|
break;
|
||||||
case EVENT_MOUSEMOVE:
|
case EVENT_MOUSEMOVE:
|
||||||
|
|
||||||
if (startSelection && !selectObject)
|
if (startSelection && !selectObjects)
|
||||||
{
|
{
|
||||||
//draw the bounding box
|
//draw the bounding box
|
||||||
Mat currentFrame;
|
Mat currentFrame;
|
||||||
image.copyTo(currentFrame);
|
image.copyTo(currentFrame);
|
||||||
|
for (int i = 0; i < (int)boundingBoxes.size(); i++)
|
||||||
|
rectangle(currentFrame, boundingBoxes[i], Scalar(255, 0, 0), 2, 1);
|
||||||
rectangle(currentFrame, Point((int)boundingBox.x, (int)boundingBox.y), Point(x, y), Scalar(255, 0, 0), 2, 1);
|
rectangle(currentFrame, Point((int)boundingBox.x, (int)boundingBox.y), Point(x, y), Scalar(255, 0, 0), 2, 1);
|
||||||
imshow("Tracking API", currentFrame);
|
imshow("Tracking API", currentFrame);
|
||||||
}
|
}
|
||||||
@@ -93,12 +113,32 @@ static void onMouse(int event, int x, int y, int, void*)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
static void help()
|
||||||
{
|
{
|
||||||
//
|
cout << "\nThis example shows the functionality of \"Long-term optical tracking API\""
|
||||||
// "MIL", "BOOSTING", "MEDIANFLOW", "TLD"
|
"TLD dataset ID: 1~10, VOT2015 dataset ID: 1~60\n"
|
||||||
//
|
"-- pause video [p] and draw a bounding boxes around the targets to start the tracker\n"
|
||||||
char* tracker_algorithm_name = (char*)"TLD";
|
"Example:\n"
|
||||||
|
"./example_tracking_multiTracker_dataset<tracker_algorithm> <dataset_path> <dataset_id> <number_of_targets>\n"
|
||||||
|
<< endl;
|
||||||
|
|
||||||
|
cout << "\n\nHot keys: \n"
|
||||||
|
"\tq - quit the program\n"
|
||||||
|
"\tp - pause video\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
CommandLineParser parser(argc, argv, keys);
|
||||||
|
string tracker_algorithm = parser.get<string>(0);
|
||||||
|
string datasetRootPath = parser.get<string>(1);
|
||||||
|
int datasetID = parser.get<int>(2);
|
||||||
|
targetsNum = parser.get<int>(3);
|
||||||
|
if (tracker_algorithm.empty() || datasetRootPath.empty() || targetsNum < 1)
|
||||||
|
{
|
||||||
|
help();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
Mat frame;
|
Mat frame;
|
||||||
paused = false;
|
paused = false;
|
||||||
@@ -106,23 +146,13 @@ int main()
|
|||||||
setMouseCallback("Tracking API", onMouse, 0);
|
setMouseCallback("Tracking API", onMouse, 0);
|
||||||
|
|
||||||
MultiTrackerTLD mt;
|
MultiTrackerTLD mt;
|
||||||
|
//Init Dataset
|
||||||
|
Ptr<TRACK_vot> dataset = TRACK_vot::create();
|
||||||
|
dataset->load(datasetRootPath);
|
||||||
|
dataset->initDataset(datasetID);
|
||||||
|
|
||||||
//Get the first frame
|
//Read first frame
|
||||||
////Open the capture
|
dataset->getNextFrame(frame);
|
||||||
// VideoCapture cap(0);
|
|
||||||
// if( !cap.isOpened() )
|
|
||||||
// {
|
|
||||||
// cout << "Video stream error";
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
//cap >> frame;
|
|
||||||
|
|
||||||
//From TLD dataset
|
|
||||||
selectObject = true;
|
|
||||||
Rect2d boundingBox1 = tld::tld_InitDataset(TEST_VIDEO_INDEX, "D:/opencv/VOT 2015", 1);
|
|
||||||
Rect2d boundingBox2(470, 490, 50, 120);
|
|
||||||
|
|
||||||
frame = tld::tld_getNextDatasetFrame();
|
|
||||||
frame.copyTo(image);
|
frame.copyTo(image);
|
||||||
|
|
||||||
// Setup output video
|
// Setup output video
|
||||||
@@ -142,8 +172,8 @@ int main()
|
|||||||
rectangle(image, boundingBox, Scalar(255, 0, 0), 2, 1);
|
rectangle(image, boundingBox, Scalar(255, 0, 0), 2, 1);
|
||||||
imshow("Tracking API", image);
|
imshow("Tracking API", image);
|
||||||
|
|
||||||
|
|
||||||
bool initialized = false;
|
bool initialized = false;
|
||||||
|
paused = true;
|
||||||
int frameCounter = 0;
|
int frameCounter = 0;
|
||||||
|
|
||||||
//Time measurment
|
//Time measurment
|
||||||
@@ -151,64 +181,59 @@ int main()
|
|||||||
|
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
//Time measurment
|
|
||||||
int64 e1 = getTickCount();
|
|
||||||
//Frame num
|
|
||||||
frameCounter++;
|
|
||||||
if (frameCounter == NUM_TEST_FRAMES) break;
|
|
||||||
|
|
||||||
char c = (char)waitKey(2);
|
|
||||||
if (c == 'q' || c == 27)
|
|
||||||
break;
|
|
||||||
if (c == 'p')
|
|
||||||
paused = !paused;
|
|
||||||
|
|
||||||
if (!paused)
|
if (!paused)
|
||||||
{
|
{
|
||||||
//cap >> frame;
|
//Time measurment
|
||||||
frame = tld::tld_getNextDatasetFrame();
|
int64 e1 = getTickCount();
|
||||||
if (frame.empty())
|
if (initialized){
|
||||||
{
|
dataset->getNextFrame(frame);
|
||||||
break;
|
if (frame.empty()){
|
||||||
}
|
break;
|
||||||
frame.copyTo(image);
|
|
||||||
|
|
||||||
if (selectObject)
|
|
||||||
{
|
|
||||||
if (!initialized)
|
|
||||||
{
|
|
||||||
//initializes the tracker
|
|
||||||
mt.addTarget(frame, boundingBox1, tracker_algorithm_name);
|
|
||||||
rectangle(frame, boundingBox1, mt.colors[0], 2, 1);
|
|
||||||
|
|
||||||
|
|
||||||
mt.addTarget(frame, boundingBox2, tracker_algorithm_name);
|
|
||||||
rectangle(frame, boundingBox2, mt.colors[1], 2, 1);
|
|
||||||
initialized = true;
|
|
||||||
}
|
}
|
||||||
else
|
frame.copyTo(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!initialized && selectObjects)
|
||||||
|
{
|
||||||
|
//Initialize the tracker and add targets
|
||||||
|
for (int i = 0; i < (int)boundingBoxes.size(); i++)
|
||||||
{
|
{
|
||||||
//updates the tracker
|
if (!mt.addTarget(frame, boundingBoxes[i], tracker_algorithm))
|
||||||
if (mt.update(frame))
|
|
||||||
{
|
{
|
||||||
for (int i = 0; i < mt.targetNum; i++)
|
cout << "Trackers Init Error!!!";
|
||||||
rectangle(frame, mt.boundingBoxes[i], mt.colors[i], 2, 1);
|
return 0;
|
||||||
|
}
|
||||||
|
rectangle(frame, boundingBoxes[i], mt.colors[0], 2, 1);
|
||||||
|
}
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
else if (initialized)
|
||||||
|
{
|
||||||
|
//Update all targets
|
||||||
|
if (mt.update(frame))
|
||||||
|
{
|
||||||
|
for (int i = 0; i < mt.targetNum; i++)
|
||||||
|
{
|
||||||
|
rectangle(frame, mt.boundingBoxes[i], mt.colors[i], 2, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
imshow("Tracking API", frame);
|
imshow("Tracking API", frame);
|
||||||
|
frameCounter++;
|
||||||
#ifdef RECORD_VIDEO_FLG
|
|
||||||
outputVideo << frame;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
//Time measurment
|
//Time measurment
|
||||||
int64 e2 = getTickCount();
|
int64 e2 = getTickCount();
|
||||||
double t1 = (e2 - e1) / getTickFrequency();
|
double t1 = (e2 - e1) / getTickFrequency();
|
||||||
cout << frameCounter << "\tframe : " << t1 * 1000.0 << "ms" << endl;
|
cout << frameCounter << "\tframe : " << t1 * 1000.0 << "ms" << endl;
|
||||||
//waitKey(0);
|
//waitKey(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char c = (char)waitKey(2);
|
||||||
|
if (c == 'q')
|
||||||
|
break;
|
||||||
|
if (c == 'p')
|
||||||
|
paused = !paused;
|
||||||
|
|
||||||
|
//waitKey(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Time measurment
|
//Time measurment
|
Reference in New Issue
Block a user