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>
|
2021-04-18 00:32:03 -04:00
|
|
|
#include "CVTracker.h"
|
|
|
|
|
#include "CVStabilization.h"
|
|
|
|
|
#include "CVObjectDetection.h"
|
2020-06-26 10:29:08 -03:00
|
|
|
|
2021-04-18 00:32:03 -04:00
|
|
|
#include "Clip.h"
|
|
|
|
|
#include "EffectBase.h"
|
|
|
|
|
#include "EffectInfo.h"
|
|
|
|
|
#include "Frame.h"
|
|
|
|
|
#include "CrashHandler.h"
|
2020-06-26 10:29:08 -03:00
|
|
|
|
|
|
|
|
using namespace openshot;
|
2020-07-21 21:42:09 -03:00
|
|
|
using namespace std;
|
2020-06-27 19:30:15 -03:00
|
|
|
|
2020-07-26 16:19:55 -03:00
|
|
|
/*
|
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
The following methods are just for getting JSON info to the pre-processing effects
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
string jsonFormat(string key, string value, string type="string"); // Format variables to the needed JSON format
|
|
|
|
|
string trackerJson(cv::Rect2d r, bool onlyProtoPath); // Set variable values for tracker effect
|
|
|
|
|
string stabilizerJson(bool onlyProtoPath); // Set variable values for stabilizer effect
|
|
|
|
|
string objectDetectionJson(bool onlyProtoPath); // Set variable values for object detector effect
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
*/
|
|
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Show the pre-processed clip on the screen
|
2020-07-09 20:26:01 -03:00
|
|
|
void displayClip(openshot::Clip &r9){
|
2020-07-21 21:42:09 -03:00
|
|
|
|
2020-07-02 19:09:04 -03:00
|
|
|
// Opencv display window
|
|
|
|
|
cv::namedWindow("Display Image", cv::WINDOW_NORMAL );
|
2021-04-18 00:32:03 -04:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Get video lenght
|
2020-07-02 19:09:04 -03:00
|
|
|
int videoLenght = r9.Reader()->info.video_length;
|
|
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Loop through the clip and show it with the effects, if any
|
2020-07-02 19:09:04 -03:00
|
|
|
for (long int frame = 0; frame < videoLenght; frame++)
|
|
|
|
|
{
|
|
|
|
|
int frame_number = frame;
|
2020-07-21 21:42:09 -03:00
|
|
|
// Get the frame
|
2020-07-02 19:09:04 -03:00
|
|
|
std::shared_ptr<openshot::Frame> f = r9.GetFrame(frame_number);
|
2020-07-21 21:42:09 -03:00
|
|
|
// Grab OpenCV::Mat image
|
2020-07-02 19:09:04 -03:00
|
|
|
cv::Mat cvimage = f->GetImageCV();
|
2020-07-26 16:19:55 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Display the frame
|
2020-07-02 20:18:01 -03:00
|
|
|
cv::imshow("Display Image", cvimage);
|
2020-07-21 21:42:09 -03:00
|
|
|
|
|
|
|
|
// Press ESC on keyboard to exit
|
2020-07-02 19:09:04 -03:00
|
|
|
char c=(char)cv::waitKey(25);
|
|
|
|
|
if(c==27)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-07-21 21:42:09 -03:00
|
|
|
// Destroy all remaining windows
|
|
|
|
|
cv::destroyAllWindows();
|
|
|
|
|
}
|
2020-07-02 19:09:04 -03:00
|
|
|
|
2020-06-26 10:29:08 -03:00
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Set pre-processing effects
|
2020-07-30 21:39:20 -03:00
|
|
|
bool TRACK_DATA = true;
|
2020-07-26 16:19:55 -03:00
|
|
|
bool SMOOTH_VIDEO = false;
|
2020-07-30 21:39:20 -03:00
|
|
|
bool OBJECT_DETECTION_DATA = false;
|
2020-06-27 19:30:15 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Get media path
|
|
|
|
|
std::stringstream path;
|
2020-07-28 21:01:57 -03:00
|
|
|
path << TEST_MEDIA_PATH << ((OBJECT_DETECTION_DATA) ? "run.mp4" : "test.avi");
|
2020-07-30 21:39:20 -03:00
|
|
|
// run.mp4 --> Used for object detector
|
|
|
|
|
// test.avi --> Used for tracker and stabilizer
|
2020-06-26 10:29:08 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Thread controller just for the pre-processing constructors, it won't be used
|
|
|
|
|
ProcessingController processingController;
|
2020-06-26 10:29:08 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Open clip
|
|
|
|
|
openshot::Clip r9(path.str());
|
|
|
|
|
r9.Open();
|
2020-06-26 10:29:08 -03:00
|
|
|
|
2020-07-26 16:19:55 -03:00
|
|
|
// Apply tracking effect on the clip
|
2020-07-21 21:42:09 -03:00
|
|
|
if(TRACK_DATA){
|
2020-07-16 21:10:02 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Take the bounding box coordinates
|
|
|
|
|
cv::Mat roi = r9.GetFrame(0)->GetImageCV();
|
|
|
|
|
cv::Rect2d r = cv::selectROI(roi);
|
|
|
|
|
cv::destroyAllWindows();
|
2020-07-16 21:10:02 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Create a tracker object by passing a JSON string and a thread controller, this last one won't be used
|
|
|
|
|
// JSON info: path to save the tracked data, type of tracker and bbox coordinates
|
|
|
|
|
CVTracker tracker(trackerJson(r, false), processingController);
|
2020-07-16 21:10:02 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Start the tracking
|
2020-11-28 18:01:58 -03:00
|
|
|
tracker.trackClip(r9, 0, 0, true);
|
2020-07-21 21:42:09 -03:00
|
|
|
// Save the tracked data
|
|
|
|
|
tracker.SaveTrackedData();
|
2020-07-16 21:10:02 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Create a tracker effect
|
|
|
|
|
EffectBase* e = EffectInfo().CreateEffect("Tracker");
|
2020-07-16 21:10:02 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Pass a JSON string with the saved tracked data
|
|
|
|
|
// The effect will read and save the tracking in a map::<frame,data_struct>
|
|
|
|
|
e->SetJson(trackerJson(r, true));
|
|
|
|
|
// Add the effect to the clip
|
|
|
|
|
r9.AddEffect(e);
|
|
|
|
|
}
|
2020-07-16 21:10:02 -03:00
|
|
|
|
2020-07-26 16:19:55 -03:00
|
|
|
// Apply stabilizer effect on the clip
|
2020-07-21 21:42:09 -03:00
|
|
|
if(SMOOTH_VIDEO){
|
2020-07-16 21:10:02 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Create a stabilizer object by passing a JSON string and a thread controller, this last one won't be used
|
|
|
|
|
// JSON info: path to save the stabilized data and smoothing window value
|
|
|
|
|
CVStabilization stabilizer(stabilizerJson(false), processingController);
|
2020-07-16 21:10:02 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Start the stabilization
|
2020-07-26 16:19:55 -03:00
|
|
|
stabilizer.stabilizeClip(r9, 0, 100, true);
|
2020-07-21 21:42:09 -03:00
|
|
|
// Save the stabilization data
|
|
|
|
|
stabilizer.SaveStabilizedData();
|
2020-07-16 21:10:02 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Create a stabilizer effect
|
|
|
|
|
EffectBase* e = EffectInfo().CreateEffect("Stabilizer");
|
2020-07-16 21:10:02 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Pass a JSON string with the saved stabilized data
|
|
|
|
|
// The effect will read and save the stabilization in a map::<frame,data_struct>
|
|
|
|
|
e->SetJson(stabilizerJson(true));
|
|
|
|
|
// Add the effect to the clip
|
|
|
|
|
r9.AddEffect(e);
|
|
|
|
|
}
|
2020-07-16 21:10:02 -03:00
|
|
|
|
2020-07-26 16:19:55 -03:00
|
|
|
// Apply object detection effect on the clip
|
2020-07-21 21:42:09 -03:00
|
|
|
if(OBJECT_DETECTION_DATA){
|
2020-07-26 16:19:55 -03:00
|
|
|
|
|
|
|
|
// Create a object detection object by passing a JSON string and a thread controller, this last one won't be used
|
|
|
|
|
// JSON info: path to save the detection data, processing devicee, model weights, model configuration and class names
|
|
|
|
|
CVObjectDetection objectDetection(objectDetectionJson(false), processingController);
|
|
|
|
|
|
|
|
|
|
// Start the object detection
|
|
|
|
|
objectDetection.detectObjectsClip(r9, 0, 100, true);
|
|
|
|
|
// Save the object detection data
|
2020-07-30 21:39:20 -03:00
|
|
|
objectDetection.SaveObjDetectedData();
|
2020-07-26 16:19:55 -03:00
|
|
|
|
|
|
|
|
// Create a object detector effect
|
|
|
|
|
EffectBase* e = EffectInfo().CreateEffect("Object Detector");
|
|
|
|
|
|
|
|
|
|
// Pass a JSON string with the saved detections data
|
|
|
|
|
// The effect will read and save the detections in a map::<frame,data_struct>
|
|
|
|
|
|
|
|
|
|
e->SetJson(objectDetectionJson(true));
|
|
|
|
|
// Add the effect to the clip
|
|
|
|
|
r9.AddEffect(e);
|
2020-07-21 21:42:09 -03:00
|
|
|
}
|
2020-06-26 10:29:08 -03:00
|
|
|
|
2020-07-21 21:42:09 -03:00
|
|
|
// Show the pre-processed clip on the screen
|
|
|
|
|
displayClip(r9);
|
|
|
|
|
|
|
|
|
|
// Close timeline
|
|
|
|
|
r9.Close();
|
2020-06-26 10:29:08 -03:00
|
|
|
|
|
|
|
|
std::cout << "Completed successfully!" << std::endl;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-07-26 16:19:55 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
The following methods are just for getting JSON info to the pre-processing effects
|
|
|
|
|
|
|
|
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string jsonFormat(string key, string value, string type){
|
|
|
|
|
stringstream jsonFormatMessage;
|
|
|
|
|
jsonFormatMessage << ( "\"" + key + "\": " );
|
|
|
|
|
|
|
|
|
|
if(type == "string")
|
|
|
|
|
jsonFormatMessage << ( "\"" + value + "\"" );
|
|
|
|
|
if(type == "rstring")
|
|
|
|
|
jsonFormatMessage << value;
|
|
|
|
|
if(type == "int")
|
|
|
|
|
jsonFormatMessage << stoi(value);
|
|
|
|
|
if(type == "float")
|
|
|
|
|
jsonFormatMessage << (float)stof(value);
|
|
|
|
|
if(type == "double")
|
|
|
|
|
jsonFormatMessage << (double)stof(value);
|
|
|
|
|
if (type == "bool")
|
|
|
|
|
jsonFormatMessage << ((value == "true" || value == "1") ? "true" : "false");
|
|
|
|
|
|
|
|
|
|
return jsonFormatMessage.str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return JSON string for the tracker effect
|
|
|
|
|
string trackerJson(cv::Rect2d r, bool onlyProtoPath){
|
|
|
|
|
|
2021-04-18 00:32:03 -04:00
|
|
|
// Define path to save tracked data
|
2020-07-26 16:19:55 -03:00
|
|
|
string protobufDataPath = "kcf_tracker.data";
|
|
|
|
|
// Set the tracker
|
|
|
|
|
string tracker = "KCF";
|
|
|
|
|
|
|
|
|
|
// Construct all the composition of the JSON string
|
|
|
|
|
string protobuf_data_path = jsonFormat("protobuf_data_path", protobufDataPath);
|
2020-11-28 18:01:58 -03:00
|
|
|
string trackerType = jsonFormat("tracker-type", tracker);
|
2020-07-26 16:19:55 -03:00
|
|
|
string bboxCoords = jsonFormat(
|
2021-04-18 00:32:03 -04:00
|
|
|
"region",
|
|
|
|
|
"{" + jsonFormat("x", to_string(r.x), "int") +
|
|
|
|
|
"," + jsonFormat("y", to_string(r.y), "int") +
|
2020-11-28 18:01:58 -03:00
|
|
|
"," + jsonFormat("width", to_string(r.width), "int") +
|
2021-04-18 00:32:03 -04:00
|
|
|
"," + jsonFormat("height", to_string(r.height), "int") +
|
2020-11-28 18:01:58 -03:00
|
|
|
"," + jsonFormat("first-frame", to_string(0), "int") +
|
2020-07-26 16:19:55 -03:00
|
|
|
"}",
|
2021-04-18 00:32:03 -04:00
|
|
|
"rstring");
|
2020-07-26 16:19:55 -03:00
|
|
|
|
|
|
|
|
// Return only the the protobuf path in JSON format
|
|
|
|
|
if(onlyProtoPath)
|
|
|
|
|
return "{" + protobuf_data_path + "}";
|
|
|
|
|
// Return all the parameters for the pre-processing effect
|
|
|
|
|
else
|
|
|
|
|
return "{" + protobuf_data_path + "," + trackerType + "," + bboxCoords + "}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return JSON string for the stabilizer effect
|
|
|
|
|
string stabilizerJson(bool onlyProtoPath){
|
|
|
|
|
|
2021-04-18 00:32:03 -04:00
|
|
|
// Define path to save stabilized data
|
2020-07-26 16:19:55 -03:00
|
|
|
string protobufDataPath = "example_stabilizer.data";
|
|
|
|
|
// Set smoothing window value
|
|
|
|
|
string smoothingWindow = "30";
|
|
|
|
|
|
|
|
|
|
// Construct all the composition of the JSON string
|
|
|
|
|
string protobuf_data_path = jsonFormat("protobuf_data_path", protobufDataPath);
|
|
|
|
|
string smoothing_window = jsonFormat("smoothing_window", smoothingWindow, "int");
|
|
|
|
|
|
|
|
|
|
// Return only the the protobuf path in JSON format
|
|
|
|
|
if(onlyProtoPath)
|
|
|
|
|
return "{" + protobuf_data_path + "}";
|
|
|
|
|
// Return all the parameters for the pre-processing effect
|
|
|
|
|
else
|
|
|
|
|
return "{" + protobuf_data_path + "," + smoothing_window + "}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string objectDetectionJson(bool onlyProtoPath){
|
|
|
|
|
|
2021-04-18 00:32:03 -04:00
|
|
|
// Define path to save object detection data
|
2020-07-26 16:19:55 -03:00
|
|
|
string protobufDataPath = "example_object_detection.data";
|
|
|
|
|
// Define processing device
|
|
|
|
|
string processingDevice = "GPU";
|
|
|
|
|
// Set path to model configuration file
|
|
|
|
|
string modelConfiguration = "yolov3.cfg";
|
2021-04-18 00:32:03 -04:00
|
|
|
// Set path to model weights
|
2020-07-26 16:19:55 -03:00
|
|
|
string modelWeights = "yolov3.weights";
|
|
|
|
|
// Set path to class names file
|
|
|
|
|
string classesFile = "obj.names";
|
|
|
|
|
|
|
|
|
|
// Construct all the composition of the JSON string
|
|
|
|
|
string protobuf_data_path = jsonFormat("protobuf_data_path", protobufDataPath);
|
|
|
|
|
string processing_device = jsonFormat("processing_device", processingDevice);
|
|
|
|
|
string model_configuration = jsonFormat("model_configuration", modelConfiguration);
|
|
|
|
|
string model_weights = jsonFormat("model_weights", modelWeights);
|
|
|
|
|
string classes_file = jsonFormat("classes_file", classesFile);
|
|
|
|
|
|
|
|
|
|
// Return only the the protobuf path in JSON format
|
|
|
|
|
if(onlyProtoPath)
|
|
|
|
|
return "{" + protobuf_data_path + "}";
|
|
|
|
|
else
|
|
|
|
|
return "{" + protobuf_data_path + "," + processing_device + "," + model_configuration + ","
|
|
|
|
|
+ model_weights + "," + classes_file + "}";
|
|
|
|
|
}
|