diff --git a/modules/tracking/samples/multiTracker_test.cpp b/modules/tracking/samples/multiTracker_dataset.cpp similarity index 60% rename from modules/tracking/samples/multiTracker_test.cpp rename to modules/tracking/samples/multiTracker_dataset.cpp index 68cefc7e3..78fc96e33 100644 --- a/modules/tracking/samples/multiTracker_test.cpp +++ b/modules/tracking/samples/multiTracker_dataset.cpp @@ -39,6 +39,7 @@ // //M*/ +#include "opencv2/datasets/track_vot.hpp" #include #include #include @@ -47,20 +48,29 @@ using namespace std; using namespace cv; +using namespace cv::datasets; #define NUM_TEST_FRAMES 100 -#define TEST_VIDEO_INDEX 15 //TLD Dataset Video Index from 1-10 for TLD and 1-60 for VOT -//#define RECORD_VIDEO_FLG static Mat image; static bool paused; -static bool selectObject = false; +static bool selectObjects = false; static bool startSelection = false; +vector boundingBoxes; +int targetsCnt = 0; +int targetsNum = 0; 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*) { - if (!selectObject) + if (!selectObjects) { switch (event) { @@ -75,16 +85,24 @@ static void onMouse(int event, int x, int y, int, void*) //sei with and height of the bounding box boundingBox.width = std::abs(x - boundingBox.x); boundingBox.height = std::abs(y - boundingBox.y); - paused = false; - selectObject = true; + boundingBoxes.push_back(boundingBox); + targetsCnt++; + if (targetsCnt == targetsNum) + { + paused = false; + selectObjects = true; + } + startSelection = false; break; case EVENT_MOUSEMOVE: - if (startSelection && !selectObject) + if (startSelection && !selectObjects) { //draw the bounding box Mat 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); imshow("Tracking API", currentFrame); } @@ -93,12 +111,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\"" + "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" + "Example:\n" + "./example_tracking_multiTracker_dataset \n" + << endl; + + cout << "\n\nHot keys: \n" + "\tq - quit the program\n" + "\tp - pause video\n"; +} + +int main(int argc, char *argv[]) { - // - // "MIL", "BOOSTING", "MEDIANFLOW", "TLD" - // - char* tracker_algorithm_name = (char*)"TLD"; + CommandLineParser parser(argc, argv, keys); + string tracker_algorithm = parser.get(0); + string datasetRootPath = parser.get(1); + int datasetID = parser.get(2); + targetsNum = parser.get(3); + if (tracker_algorithm.empty() || datasetRootPath.empty() || targetsNum < 1) + { + help(); + return -1; + } Mat frame; paused = false; @@ -106,45 +144,20 @@ int main() setMouseCallback("Tracking API", onMouse, 0); MultiTrackerTLD mt; + //Init Dataset + Ptr dataset = TRACK_vot::create(); + dataset->load(datasetRootPath); + dataset->initDataset(datasetID); - //Get the first frame - ////Open the capture - // 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(); + //Read first frame + dataset->getNextFrame(frame); frame.copyTo(image); - - // Setup output video -#ifdef RECORD_VIDEO_FLG - String outputFilename = "test.avi"; - VideoWriter outputVideo; - outputVideo.open(outputFilename, -1, 15, Size(image.cols, image.rows)); - - if (!outputVideo.isOpened()) - { - std::cout << "!!! Output video could not be opened" << std::endl; - getchar(); - return 0; - - } -#endif - - rectangle(image, boundingBox, Scalar(255, 0, 0), 2, 1); + for (int i = 0; i < (int)boundingBoxes.size(); i++) + rectangle(image, boundingBoxes[i], Scalar(255, 0, 0), 2, 1); imshow("Tracking API", image); - bool initialized = false; + paused = true; int frameCounter = 0; //Time measurment @@ -152,65 +165,58 @@ int main() 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) { - //cap >> frame; - frame = tld::tld_getNextDatasetFrame(); - if (frame.empty()) - { - break; + //Time measurment + int64 e1 = getTickCount(); + if (initialized){ + dataset->getNextFrame(frame); + if (frame.empty()){ + break; + } + frame.copyTo(image); } - frame.copyTo(image); - if (selectObject) + if (!initialized && selectObjects) { - if (!initialized) + //Initialize the tracker and add targets + for (int i = 0; i < (int)boundingBoxes.size(); i++) { - //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; + if (!mt.addTarget(frame, boundingBoxes[i], tracker_algorithm)) + { + cout << "Trackers Init Error!!!"; + return 0; + } + rectangle(frame, boundingBoxes[i], mt.colors[0], 2, 1); } - else + initialized = true; + } + else if (initialized) + { + //Update all targets + if (mt.update(frame)) { - //updates the tracker - if (mt.update(frame)) + for (int i = 0; i < mt.targetNum; i++) { - for (int i = 0; i < mt.targetNum; i++) - rectangle(frame, mt.boundingBoxes[i], mt.colors[i], 2, 1); + rectangle(frame, mt.boundingBoxes[i], mt.colors[i], 2, 1); } } } imshow("Tracking API", frame); - -#ifdef RECORD_VIDEO_FLG - outputVideo << frame; -#endif - - + frameCounter++; //Time measurment int64 e2 = getTickCount(); double t1 = (e2 - e1) / getTickFrequency(); cout << frameCounter << "\tframe : " << t1 * 1000.0 << "ms" << endl; - - //waitKey(0); } + + char c = (char)waitKey(2); + if (c == 'q') + break; + if (c == 'p') + paused = !paused; + + //waitKey(0); } //Time measurment