2020-06-26 10:29:08 -03:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for Example Executable (example app for libopenshot)
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
|
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* LICENSE
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2008-2019 OpenShot Studios, LLC
|
|
|
|
|
* <http://www.openshotstudios.com/>. This file is part of
|
|
|
|
|
* OpenShot Library (libopenshot), an open-source project dedicated to
|
|
|
|
|
* delivering high quality video editing and animation solutions to the
|
|
|
|
|
* world. For more information visit <http://www.openshot.org/>.
|
|
|
|
|
*
|
|
|
|
|
* OpenShot Library (libopenshot) is free software: you can redistribute it
|
|
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public License
|
|
|
|
|
* as published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* OpenShot Library (libopenshot) is distributed in the hope that it will be
|
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <memory>
|
2020-06-27 17:37:12 -03:00
|
|
|
// #include <google/protobuf/util/time_util.h>
|
2020-07-02 19:09:04 -03:00
|
|
|
// #include "../../include/CVTracker.h"
|
|
|
|
|
// #include "../../include/CVStabilization.h"
|
2020-06-27 17:37:12 -03:00
|
|
|
// #include "treackerdata.pb.h"
|
2020-06-26 10:29:08 -03:00
|
|
|
|
|
|
|
|
#include "../../include/OpenShot.h"
|
|
|
|
|
#include "../../include/CrashHandler.h"
|
|
|
|
|
|
|
|
|
|
using namespace openshot;
|
2020-07-02 19:09:04 -03:00
|
|
|
// using namespace cv;
|
2020-06-26 10:29:08 -03:00
|
|
|
|
2020-07-02 19:09:04 -03:00
|
|
|
void trackVideo(openshot::Clip &r9){
|
2020-06-27 19:30:15 -03:00
|
|
|
// Opencv display window
|
|
|
|
|
cv::namedWindow("Display Image", cv::WINDOW_NORMAL );
|
|
|
|
|
// Create Tracker
|
|
|
|
|
CVTracker kcfTracker;
|
|
|
|
|
bool trackerInit = false;
|
|
|
|
|
|
2020-07-02 19:09:04 -03:00
|
|
|
for (long int frame = 1200; frame <= 1600; frame++)
|
2020-06-27 19:30:15 -03:00
|
|
|
{
|
|
|
|
|
int frame_number = frame;
|
|
|
|
|
std::shared_ptr<openshot::Frame> f = r9.GetFrame(frame_number);
|
|
|
|
|
|
|
|
|
|
// Grab Mat image
|
|
|
|
|
cv::Mat cvimage = f->GetImageCV();
|
|
|
|
|
|
|
|
|
|
if(!trackerInit){
|
2020-07-02 19:09:04 -03:00
|
|
|
cv::Rect2d bbox = cv::selectROI("Display Image", cvimage);
|
2020-06-27 19:30:15 -03:00
|
|
|
|
|
|
|
|
kcfTracker.initTracker(bbox, cvimage, frame_number);
|
2020-07-02 19:09:04 -03:00
|
|
|
cv::rectangle(cvimage, bbox, cv::Scalar( 255, 0, 0 ), 2, 1 );
|
2020-06-27 19:30:15 -03:00
|
|
|
|
|
|
|
|
trackerInit = true;
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
trackerInit = kcfTracker.trackFrame(cvimage, frame_number);
|
|
|
|
|
|
|
|
|
|
// Draw box on image
|
|
|
|
|
FrameData fd = kcfTracker.GetTrackedData(frame_number);
|
|
|
|
|
// std::cout<< "fd: "<< fd.x1<< " "<< fd.y1 <<" "<<fd.x2<< " "<<fd.y2<<"\n";
|
2020-07-02 19:09:04 -03:00
|
|
|
cv::Rect2d box(fd.x1, fd.y1, fd.x2-fd.x1, fd.y2-fd.y1);
|
|
|
|
|
cv::rectangle(cvimage, box, cv::Scalar( 255, 0, 0 ), 2, 1 );
|
2020-06-27 19:30:15 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cv::imshow("Display Image", cvimage);
|
|
|
|
|
// Press ESC on keyboard to exit
|
2020-07-02 19:09:04 -03:00
|
|
|
char c=(char)cv::waitKey(25);
|
2020-06-27 19:30:15 -03:00
|
|
|
if(c==27)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save tracked data to file
|
|
|
|
|
std::cout << "Saving tracker data!" << std::endl;
|
|
|
|
|
kcfTracker.SaveTrackedData("kcf_tracker.data");
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-02 19:09:04 -03:00
|
|
|
void displayTrackedData(openshot::Clip &r9){
|
2020-06-27 19:30:15 -03:00
|
|
|
// Opencv display window
|
|
|
|
|
cv::namedWindow("Display Image", cv::WINDOW_NORMAL );
|
|
|
|
|
|
|
|
|
|
// Create Tracker
|
|
|
|
|
CVTracker kcfTracker;
|
|
|
|
|
// Load saved data
|
|
|
|
|
if(!kcfTracker.LoadTrackedData("kcf_tracker.data")){
|
|
|
|
|
std::cout<<"Was not possible to load the tracked data\n";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-02 19:09:04 -03:00
|
|
|
for (long int frame = 1200; frame <= 1600; frame++)
|
2020-06-27 19:30:15 -03:00
|
|
|
{
|
|
|
|
|
int frame_number = frame;
|
|
|
|
|
std::shared_ptr<openshot::Frame> f = r9.GetFrame(frame_number);
|
|
|
|
|
|
|
|
|
|
// Grab Mat image
|
|
|
|
|
cv::Mat cvimage = f->GetImageCV();
|
|
|
|
|
|
|
|
|
|
FrameData fd = kcfTracker.GetTrackedData(frame_number);
|
2020-07-02 19:09:04 -03:00
|
|
|
cv::Rect2d box(fd.x1, fd.y1, fd.x2-fd.x1, fd.y2-fd.y1);
|
|
|
|
|
cv::rectangle(cvimage, box, cv::Scalar( 255, 0, 0 ), 2, 1 );
|
2020-06-27 19:30:15 -03:00
|
|
|
|
|
|
|
|
cv::imshow("Display Image", cvimage);
|
|
|
|
|
// Press ESC on keyboard to exit
|
2020-07-02 19:09:04 -03:00
|
|
|
char c=(char)cv::waitKey(25);
|
2020-06-27 19:30:15 -03:00
|
|
|
if(c==27)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-07-02 19:09:04 -03:00
|
|
|
void displayStabilization(openshot::Clip &r9){
|
|
|
|
|
// Opencv display window
|
|
|
|
|
cv::namedWindow("Display Image", cv::WINDOW_NORMAL );
|
|
|
|
|
|
|
|
|
|
int videoLenght = r9.Reader()->info.video_length;
|
|
|
|
|
|
|
|
|
|
for (long int frame = 0; frame < videoLenght; frame++)
|
|
|
|
|
{
|
|
|
|
|
int frame_number = frame;
|
|
|
|
|
std::shared_ptr<openshot::Frame> f = r9.GetFrame(frame_number);
|
|
|
|
|
|
|
|
|
|
// Grab Mat image
|
|
|
|
|
cv::Mat cvimage = f->GetImageCV();
|
|
|
|
|
|
|
|
|
|
cv::imshow("Display Image1", cvimage);
|
|
|
|
|
// Press ESC on keyboard to exit
|
|
|
|
|
char c=(char)cv::waitKey(25);
|
|
|
|
|
if(c==27)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2020-06-27 19:30:15 -03:00
|
|
|
|
2020-06-26 10:29:08 -03:00
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
|
|
2020-07-02 19:09:04 -03:00
|
|
|
bool TRACK_DATA = false;
|
2020-06-28 16:54:11 -03:00
|
|
|
bool LOAD_TRACKED_DATA = false;
|
2020-07-02 19:09:04 -03:00
|
|
|
bool SMOOTH_VIDEO = true;
|
2020-06-27 19:30:15 -03:00
|
|
|
|
2020-06-26 10:29:08 -03:00
|
|
|
|
|
|
|
|
std::string input_filepath = TEST_MEDIA_PATH;
|
2020-07-02 19:09:04 -03:00
|
|
|
input_filepath += "test.avi";
|
2020-06-26 10:29:08 -03:00
|
|
|
|
2020-07-02 19:09:04 -03:00
|
|
|
openshot::Clip r9(input_filepath);
|
2020-06-26 10:29:08 -03:00
|
|
|
r9.Open();
|
|
|
|
|
|
2020-07-02 19:09:04 -03:00
|
|
|
if(TRACK_DATA)
|
2020-06-27 19:30:15 -03:00
|
|
|
trackVideo(r9);
|
2020-07-02 19:09:04 -03:00
|
|
|
if(LOAD_TRACKED_DATA)
|
2020-06-27 19:30:15 -03:00
|
|
|
displayTrackedData(r9);
|
2020-07-02 19:09:04 -03:00
|
|
|
if(SMOOTH_VIDEO){
|
|
|
|
|
CVStabilization stabilization;
|
|
|
|
|
stabilization.ProcessVideo(r9);
|
|
|
|
|
displayStabilization(r9);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-06-27 19:30:15 -03:00
|
|
|
|
2020-06-26 10:29:08 -03:00
|
|
|
|
|
|
|
|
// Close timeline
|
|
|
|
|
r9.Close();
|
|
|
|
|
|
|
|
|
|
std::cout << "Completed successfully!" << std::endl;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|