2020-07-09 20:26:01 -03:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Track an object selected by the user
|
|
|
|
|
* @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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2020-06-26 10:29:08 -03:00
|
|
|
#include "../include/CVTracker.h"
|
|
|
|
|
|
2020-07-16 21:10:02 -03:00
|
|
|
// Constructor
|
|
|
|
|
CVTracker::CVTracker(std::string processInfoJson, ProcessingController &processingController)
|
|
|
|
|
: processingController(&processingController){
|
|
|
|
|
SetJson(processInfoJson);
|
2020-06-26 10:29:08 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-08 22:31:26 -03:00
|
|
|
// Set desirable tracker method
|
2020-07-16 21:10:02 -03:00
|
|
|
cv::Ptr<cv::Tracker> CVTracker::selectTracker(std::string trackerType){
|
2020-07-02 19:09:04 -03:00
|
|
|
cv::Ptr<cv::Tracker> t;
|
2020-07-09 20:26:01 -03:00
|
|
|
|
|
|
|
|
if (trackerType == "BOOSTING")
|
|
|
|
|
t = cv::TrackerBoosting::create();
|
|
|
|
|
if (trackerType == "MIL")
|
|
|
|
|
t = cv::TrackerMIL::create();
|
|
|
|
|
if (trackerType == "KCF")
|
|
|
|
|
t = cv::TrackerKCF::create();
|
|
|
|
|
if (trackerType == "TLD")
|
|
|
|
|
t = cv::TrackerTLD::create();
|
|
|
|
|
if (trackerType == "MEDIANFLOW")
|
|
|
|
|
t = cv::TrackerMedianFlow::create();
|
|
|
|
|
if (trackerType == "GOTURN")
|
|
|
|
|
t = cv::TrackerGOTURN::create();
|
|
|
|
|
if (trackerType == "MOSSE")
|
|
|
|
|
t = cv::TrackerMOSSE::create();
|
|
|
|
|
if (trackerType == "CSRT")
|
|
|
|
|
t = cv::TrackerCSRT::create();
|
2020-06-26 10:29:08 -03:00
|
|
|
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 21:10:02 -03:00
|
|
|
// Track object in the hole clip or in a given interval
|
|
|
|
|
void CVTracker::trackClip(openshot::Clip& video, size_t start, size_t end, bool process_interval){
|
2020-07-08 11:49:46 -03:00
|
|
|
|
|
|
|
|
bool trackerInit = false;
|
2020-07-16 21:10:02 -03:00
|
|
|
size_t frame;
|
|
|
|
|
|
|
|
|
|
if(!process_interval || end == 0 || end-start <= 0){
|
|
|
|
|
// Get total number of frames in video
|
|
|
|
|
end = video.Reader()->info.video_length;
|
|
|
|
|
}
|
2020-07-08 22:31:26 -03:00
|
|
|
|
|
|
|
|
// Loop through video
|
2020-07-16 21:10:02 -03:00
|
|
|
for (frame = start; frame < end; frame++)
|
2020-07-08 11:49:46 -03:00
|
|
|
{
|
2020-07-16 21:10:02 -03:00
|
|
|
|
|
|
|
|
// Stop the feature tracker process
|
|
|
|
|
if(processingController->ShouldStop()){
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout<<"Frame: "<<frame<<"\n";
|
|
|
|
|
size_t frame_number = frame;
|
2020-07-08 22:31:26 -03:00
|
|
|
// Get current frame
|
2020-07-08 11:49:46 -03:00
|
|
|
std::shared_ptr<openshot::Frame> f = video.GetFrame(frame_number);
|
|
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Grab OpenCV Mat image
|
2020-07-08 11:49:46 -03:00
|
|
|
cv::Mat cvimage = f->GetImageCV();
|
|
|
|
|
|
2020-07-08 22:31:26 -03:00
|
|
|
// Pass the first frame to initialize the tracker
|
2020-07-08 11:49:46 -03:00
|
|
|
if(!trackerInit){
|
2020-07-08 22:31:26 -03:00
|
|
|
|
|
|
|
|
// Initialize the tracker
|
2020-07-16 21:10:02 -03:00
|
|
|
initTracker(cvimage, frame_number);
|
2020-07-08 11:49:46 -03:00
|
|
|
|
|
|
|
|
trackerInit = true;
|
|
|
|
|
}
|
|
|
|
|
else{
|
2020-07-09 20:26:01 -03:00
|
|
|
// Update the object tracker according to frame
|
2020-07-08 11:49:46 -03:00
|
|
|
trackerInit = trackFrame(cvimage, frame_number);
|
|
|
|
|
|
|
|
|
|
// Draw box on image
|
|
|
|
|
FrameData fd = GetTrackedData(frame_number);
|
|
|
|
|
|
|
|
|
|
}
|
2020-07-16 21:10:02 -03:00
|
|
|
// Update progress
|
|
|
|
|
processingController->SetProgress(uint(100*frame_number/end));
|
2020-07-08 11:49:46 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Initialize the tracker
|
2020-07-16 21:10:02 -03:00
|
|
|
bool CVTracker::initTracker(cv::Mat &frame, size_t frameId){
|
|
|
|
|
|
2020-06-27 17:37:12 -03:00
|
|
|
// Create new tracker object
|
2020-07-16 21:10:02 -03:00
|
|
|
tracker = selectTracker(trackerType);
|
2020-06-26 10:29:08 -03:00
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Initialize tracker
|
2020-06-26 10:29:08 -03:00
|
|
|
tracker->init(frame, bbox);
|
|
|
|
|
|
2020-06-27 17:37:12 -03:00
|
|
|
// Add new frame data
|
2020-06-27 19:30:15 -03:00
|
|
|
trackedDataById[frameId] = FrameData(frameId, 0, bbox.x, bbox.y, bbox.x+bbox.width, bbox.y+bbox.height);
|
2020-06-27 17:37:12 -03:00
|
|
|
|
2020-06-26 10:29:08 -03:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Update the object tracker according to frame
|
2020-07-16 21:10:02 -03:00
|
|
|
bool CVTracker::trackFrame(cv::Mat &frame, size_t frameId){
|
2020-06-26 10:29:08 -03:00
|
|
|
// Update the tracking result
|
|
|
|
|
bool ok = tracker->update(frame, bbox);
|
|
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Add frame number and box coords if tracker finds the object
|
|
|
|
|
// Otherwise add only frame number
|
2020-06-26 10:29:08 -03:00
|
|
|
if (ok)
|
|
|
|
|
{
|
2020-06-27 17:37:12 -03:00
|
|
|
// Add new frame data
|
2020-06-27 19:30:15 -03:00
|
|
|
trackedDataById[frameId] = FrameData(frameId, 0, bbox.x, bbox.y, bbox.x+bbox.width, bbox.y+bbox.height);
|
2020-06-26 10:29:08 -03:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-27 17:37:12 -03:00
|
|
|
// Add new frame data
|
2020-06-27 19:30:15 -03:00
|
|
|
trackedDataById[frameId] = FrameData(frameId);
|
2020-06-26 10:29:08 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
2020-06-27 17:37:12 -03:00
|
|
|
|
2020-07-16 21:10:02 -03:00
|
|
|
bool CVTracker::SaveTrackedData(){
|
2020-06-27 17:37:12 -03:00
|
|
|
// Create tracker message
|
|
|
|
|
libopenshottracker::Tracker trackerMessage;
|
|
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Iterate over all frames data and save in protobuf message
|
2020-07-16 21:10:02 -03:00
|
|
|
for(std::map<size_t,FrameData>::iterator it=trackedDataById.begin(); it!=trackedDataById.end(); ++it){
|
2020-06-27 19:30:15 -03:00
|
|
|
FrameData fData = it->second;
|
2020-06-27 17:37:12 -03:00
|
|
|
libopenshottracker::Frame* pbFrameData;
|
|
|
|
|
AddFrameDataToProto(trackerMessage.add_frame(), fData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add timestamp
|
|
|
|
|
*trackerMessage.mutable_last_updated() = TimeUtil::SecondsToTimestamp(time(NULL));
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Write the new message to disk.
|
2020-07-16 21:10:02 -03:00
|
|
|
std::fstream output(protobuf_data_path, ios::out | ios::trunc | ios::binary);
|
2020-06-27 17:37:12 -03:00
|
|
|
if (!trackerMessage.SerializeToOstream(&output)) {
|
|
|
|
|
cerr << "Failed to write protobuf message." << endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete all global objects allocated by libprotobuf.
|
|
|
|
|
google::protobuf::ShutdownProtobufLibrary();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add frame tracked data into protobuf message.
|
|
|
|
|
void CVTracker::AddFrameDataToProto(libopenshottracker::Frame* pbFrameData, FrameData& fData) {
|
|
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Save frame number and rotation
|
|
|
|
|
pbFrameData->set_id(fData.frame_id);
|
|
|
|
|
pbFrameData->set_rotation(0);
|
2020-06-27 17:37:12 -03:00
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
libopenshottracker::Frame::Box* box = pbFrameData->mutable_bounding_box();
|
|
|
|
|
// Save bounding box data
|
2020-06-27 17:37:12 -03:00
|
|
|
box->set_x1(fData.x1);
|
|
|
|
|
box->set_y1(fData.y1);
|
|
|
|
|
box->set_x2(fData.x2);
|
|
|
|
|
box->set_y2(fData.y2);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Load protobuf data file
|
2020-07-16 21:10:02 -03:00
|
|
|
bool CVTracker::LoadTrackedData(){
|
2020-07-09 20:26:01 -03:00
|
|
|
// Create tracker message
|
2020-06-27 17:37:12 -03:00
|
|
|
libopenshottracker::Tracker trackerMessage;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// Read the existing tracker message.
|
2020-07-16 21:10:02 -03:00
|
|
|
fstream input(protobuf_data_path, ios::in | ios::binary);
|
2020-06-27 17:37:12 -03:00
|
|
|
if (!trackerMessage.ParseFromIstream(&input)) {
|
|
|
|
|
cerr << "Failed to parse protobuf message." << endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Make sure the trackedData is empty
|
2020-06-27 19:30:15 -03:00
|
|
|
trackedDataById.clear();
|
2020-06-27 17:37:12 -03:00
|
|
|
|
|
|
|
|
// Iterate over all frames of the saved message
|
2020-07-16 21:10:02 -03:00
|
|
|
for (size_t i = 0; i < trackerMessage.frame_size(); i++) {
|
2020-06-27 17:37:12 -03:00
|
|
|
const libopenshottracker::Frame& pbFrameData = trackerMessage.frame(i);
|
|
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Load frame and rotation data
|
2020-07-16 21:10:02 -03:00
|
|
|
size_t id = pbFrameData.id();
|
2020-06-27 17:37:12 -03:00
|
|
|
float rotation = pbFrameData.rotation();
|
|
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Load bounding box data
|
2020-06-27 17:37:12 -03:00
|
|
|
const libopenshottracker::Frame::Box& box = pbFrameData.bounding_box();
|
|
|
|
|
int x1 = box.x1();
|
|
|
|
|
int y1 = box.y1();
|
|
|
|
|
int x2 = box.x2();
|
|
|
|
|
int y2 = box.y2();
|
|
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Assign data to tracker map
|
2020-06-27 19:30:15 -03:00
|
|
|
trackedDataById[id] = FrameData(id, rotation, x1, y1, x2, y2);
|
2020-06-27 17:37:12 -03:00
|
|
|
}
|
|
|
|
|
|
2020-07-09 20:26:01 -03:00
|
|
|
// Show the time stamp from the last update in tracker data file
|
2020-06-27 17:37:12 -03:00
|
|
|
if (trackerMessage.has_last_updated()) {
|
|
|
|
|
cout << " Loaded Data. Saved Time Stamp: " << TimeUtil::ToString(trackerMessage.last_updated()) << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete all global objects allocated by libprotobuf.
|
|
|
|
|
google::protobuf::ShutdownProtobufLibrary();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-06-27 19:30:15 -03:00
|
|
|
|
2020-07-08 22:31:26 -03:00
|
|
|
// Get tracker info for the desired frame
|
2020-07-16 21:10:02 -03:00
|
|
|
FrameData CVTracker::GetTrackedData(size_t frameId){
|
2020-06-27 19:30:15 -03:00
|
|
|
|
2020-07-08 22:31:26 -03:00
|
|
|
// Check if the tracker info for the requested frame exists
|
2020-06-27 19:30:15 -03:00
|
|
|
if ( trackedDataById.find(frameId) == trackedDataById.end() ) {
|
|
|
|
|
|
|
|
|
|
return FrameData();
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
return trackedDataById[frameId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2020-07-16 21:10:02 -03:00
|
|
|
|
|
|
|
|
// Load JSON string into this object
|
|
|
|
|
void CVTracker::SetJson(const std::string value) {
|
|
|
|
|
// Parse JSON string into JSON objects
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
const Json::Value root = openshot::stringToJson(value);
|
|
|
|
|
// Set all values that match
|
|
|
|
|
|
|
|
|
|
SetJsonValue(root);
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception& e)
|
|
|
|
|
{
|
|
|
|
|
// Error parsing JSON (or missing keys)
|
|
|
|
|
// throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
|
|
|
|
|
std::cout<<"JSON is invalid (missing keys or invalid data types)"<<std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load Json::Value into this object
|
|
|
|
|
void CVTracker::SetJsonValue(const Json::Value root) {
|
|
|
|
|
|
|
|
|
|
// Set data from Json (if key is found)
|
|
|
|
|
if (!root["protobuf_data_path"].isNull()){
|
|
|
|
|
protobuf_data_path = (root["protobuf_data_path"].asString());
|
|
|
|
|
}
|
|
|
|
|
if (!root["tracker_type"].isNull()){
|
|
|
|
|
trackerType = (root["tracker_type"].asString());
|
|
|
|
|
}
|
|
|
|
|
if (!root["bbox"].isNull()){
|
|
|
|
|
double x = root["bbox"]["x"].asDouble();
|
|
|
|
|
double y = root["bbox"]["y"].asDouble();
|
|
|
|
|
double w = root["bbox"]["w"].asDouble();
|
|
|
|
|
double h = root["bbox"]["h"].asDouble();
|
|
|
|
|
cv::Rect2d prev_bbox(x,y,w,h);
|
|
|
|
|
bbox = prev_bbox;
|
|
|
|
|
}
|
|
|
|
|
}
|