2020-10-29 00:34:31 -03:00
|
|
|
/**
|
|
|
|
|
* @file
|
2021-01-22 20:14:25 -03:00
|
|
|
* @brief Source file for the TrackedObjectBBox class
|
2020-10-29 00:34:31 -03:00
|
|
|
* @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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-01-18 14:52:01 -03:00
|
|
|
#include "TrackedObjectBBox.h"
|
|
|
|
|
#include "Clip.h"
|
2020-10-29 00:34:31 -03:00
|
|
|
#include <algorithm>
|
2020-12-12 20:23:34 -03:00
|
|
|
#include <fstream>
|
|
|
|
|
#include <iostream>
|
2020-10-29 00:34:31 -03:00
|
|
|
#include <functional>
|
2020-12-12 20:23:34 -03:00
|
|
|
|
2020-10-29 00:34:31 -03:00
|
|
|
using namespace std;
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Default Constructor that sets the bounding-box displacement as 0 and the scales as 1 for the first frame
|
2021-01-18 14:52:01 -03:00
|
|
|
TrackedObjectBBox::TrackedObjectBBox() : delta_x(0.0), delta_y(0.0), scale_x(1.0), scale_y(1.0), rotation(0.0)
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
2020-11-12 21:25:27 -03:00
|
|
|
this->TimeScale = 1.0;
|
2020-10-29 00:34:31 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-04 15:45:33 -03:00
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Add a BBox to the BoxVec map
|
2021-01-18 14:52:01 -03:00
|
|
|
void TrackedObjectBBox::AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle)
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
|
|
|
|
// Check if the given frame number is valid
|
2020-10-29 00:34:31 -03:00
|
|
|
if (_frame_num < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Instantiate a new bounding-box
|
2020-12-14 18:08:44 -03:00
|
|
|
BBox newBBox = BBox(_cx, _cy, _width, _height, _angle);
|
2020-10-29 00:34:31 -03:00
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Get the time of given frame
|
2020-11-12 21:25:27 -03:00
|
|
|
double time = this->FrameNToTime(_frame_num, 1.0);
|
2020-12-12 20:23:34 -03:00
|
|
|
// Create an iterator that points to the BoxVec pair indexed by the time of given frame
|
|
|
|
|
auto BBoxIterator = BoxVec.find(time);
|
|
|
|
|
|
|
|
|
|
if (BBoxIterator != BoxVec.end())
|
|
|
|
|
{
|
|
|
|
|
// There is a bounding-box indexed by the time of given frame, update-it
|
|
|
|
|
BBoxIterator->second = newBBox;
|
|
|
|
|
}
|
2020-10-29 00:34:31 -03:00
|
|
|
else
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
|
|
|
|
// There isn't a bounding-box indexed by the time of given frame, insert a new one
|
|
|
|
|
BoxVec.insert({time, newBBox});
|
|
|
|
|
}
|
2020-10-29 00:34:31 -03:00
|
|
|
}
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Get the size of BoxVec map
|
2021-01-18 14:52:01 -03:00
|
|
|
int64_t TrackedObjectBBox::GetLength() const
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
|
|
|
|
if (BoxVec.empty())
|
2020-10-29 00:34:31 -03:00
|
|
|
return 0;
|
2020-12-12 20:23:34 -03:00
|
|
|
if (BoxVec.size() == 1)
|
2020-10-29 00:34:31 -03:00
|
|
|
return 1;
|
|
|
|
|
return BoxVec.size();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Check if there is a bounding-box in the given frame
|
2021-01-22 20:03:05 -03:00
|
|
|
bool TrackedObjectBBox::Contains(int64_t frame_num) const
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
|
|
|
|
// Get the time of given frame
|
2020-11-12 21:25:27 -03:00
|
|
|
double time = this->FrameNToTime(frame_num, 1.0);
|
2020-12-12 20:23:34 -03:00
|
|
|
// Create an iterator that points to the BoxVec pair indexed by the time of given frame (or the closest time)
|
2020-11-12 21:25:27 -03:00
|
|
|
auto it = BoxVec.lower_bound(time);
|
2021-01-22 20:03:05 -03:00
|
|
|
if (it == BoxVec.end()){
|
2020-12-12 20:23:34 -03:00
|
|
|
// BoxVec pair not found
|
2020-11-12 21:25:27 -03:00
|
|
|
return false;
|
2021-01-22 20:03:05 -03:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check if there is a bounding-box in the exact frame number
|
|
|
|
|
bool TrackedObjectBBox::ExactlyContains(int64_t frame_number) const
|
|
|
|
|
{
|
|
|
|
|
// Get the time of given frame
|
|
|
|
|
double time = FrameNToTime(frame_number, 1.0);
|
|
|
|
|
// Create an iterator that points to the BoxVec pair indexed by the exact time of given frame
|
|
|
|
|
auto it = BoxVec.find(time);
|
|
|
|
|
if (it == BoxVec.end()){
|
|
|
|
|
// BoxVec pair not found
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-11-12 21:25:27 -03:00
|
|
|
return true;
|
2020-10-29 00:34:31 -03:00
|
|
|
}
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Remove a bounding-box from the BoxVec map
|
2021-01-18 14:52:01 -03:00
|
|
|
void TrackedObjectBBox::RemoveBox(int64_t frame_number)
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
|
|
|
|
// Get the time of given frame
|
2020-11-12 21:25:27 -03:00
|
|
|
double time = this->FrameNToTime(frame_number, 1.0);
|
2020-12-12 20:23:34 -03:00
|
|
|
// Create an iterator that points to the BoxVec pair indexed by the time of given frame
|
2020-10-29 00:34:31 -03:00
|
|
|
auto it = BoxVec.find(time);
|
2020-12-12 20:23:34 -03:00
|
|
|
if (it != BoxVec.end())
|
|
|
|
|
{
|
|
|
|
|
// The BoxVec pair exists, so remove it
|
2020-11-12 21:25:27 -03:00
|
|
|
BoxVec.erase(time);
|
2020-10-29 00:34:31 -03:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Return a bounding-box from BoxVec with it's properties adjusted by the Keyframes
|
2021-01-18 14:52:01 -03:00
|
|
|
BBox TrackedObjectBBox::GetBox(int64_t frame_number)
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
|
|
|
|
// Get the time position of the given frame.
|
2020-11-12 21:25:27 -03:00
|
|
|
double time = this->FrameNToTime(frame_number, this->TimeScale);
|
2020-12-12 20:23:34 -03:00
|
|
|
|
|
|
|
|
// Return a iterator pointing to the BoxVec pair indexed by time or to the pair indexed
|
|
|
|
|
// by the closest upper time value.
|
|
|
|
|
auto currentBBoxIterator = BoxVec.lower_bound(time);
|
|
|
|
|
|
|
|
|
|
// Check if there is a pair indexed by time, returns an empty bbox if there isn't.
|
|
|
|
|
if (currentBBoxIterator == BoxVec.end())
|
|
|
|
|
{
|
|
|
|
|
// Create and return an empty bounding-box object
|
|
|
|
|
BBox emptyBBox;
|
|
|
|
|
return emptyBBox;
|
2020-11-12 21:25:27 -03:00
|
|
|
}
|
2020-12-12 20:23:34 -03:00
|
|
|
|
|
|
|
|
// Check if the iterator matches a BBox indexed by time or points to the first element of BoxVec
|
|
|
|
|
if ((currentBBoxIterator->first == time) || (currentBBoxIterator == BoxVec.begin()))
|
|
|
|
|
{
|
|
|
|
|
// Get the BBox indexed by time
|
|
|
|
|
BBox currentBBox = currentBBoxIterator->second;
|
|
|
|
|
|
|
|
|
|
// Adjust the BBox properties by the Keyframes values
|
2020-12-14 18:08:44 -03:00
|
|
|
currentBBox.cx += this->delta_x.GetValue(frame_number);
|
|
|
|
|
currentBBox.cy += this->delta_y.GetValue(frame_number);
|
2020-12-12 20:23:34 -03:00
|
|
|
currentBBox.width *= this->scale_x.GetValue(frame_number);
|
|
|
|
|
currentBBox.height *= this->scale_y.GetValue(frame_number);
|
2020-12-14 18:08:44 -03:00
|
|
|
currentBBox.angle += this->rotation.GetValue(frame_number);
|
2020-11-12 21:25:27 -03:00
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
return currentBBox;
|
2020-10-29 00:34:31 -03:00
|
|
|
}
|
2020-11-12 21:25:27 -03:00
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// BBox indexed by the closest upper time
|
|
|
|
|
BBox currentBBox = currentBBoxIterator->second;
|
|
|
|
|
// BBox indexed by the closet lower time
|
|
|
|
|
BBox previousBBox = prev(currentBBoxIterator, 1)->second;
|
2020-10-29 00:34:31 -03:00
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Interpolate a BBox in the middle of previousBBox and currentBBox
|
|
|
|
|
BBox interpolatedBBox = InterpolateBoxes(prev(currentBBoxIterator, 1)->first, currentBBoxIterator->first,
|
|
|
|
|
previousBBox, currentBBox, time);
|
|
|
|
|
|
|
|
|
|
// Adjust the BBox properties by the Keyframes values
|
2020-12-14 18:08:44 -03:00
|
|
|
interpolatedBBox.cx += this->delta_x.GetValue(frame_number);
|
|
|
|
|
interpolatedBBox.cy += this->delta_y.GetValue(frame_number);
|
2020-12-12 20:23:34 -03:00
|
|
|
interpolatedBBox.width *= this->scale_x.GetValue(frame_number);
|
|
|
|
|
interpolatedBBox.height *= this->scale_y.GetValue(frame_number);
|
2020-12-14 18:08:44 -03:00
|
|
|
interpolatedBBox.angle += this->rotation.GetValue(frame_number);
|
2020-12-12 20:23:34 -03:00
|
|
|
|
|
|
|
|
return interpolatedBBox;
|
2020-10-29 00:34:31 -03:00
|
|
|
}
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Interpolate the bouding-boxes properties
|
2021-01-18 14:52:01 -03:00
|
|
|
BBox TrackedObjectBBox::InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target)
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
2020-12-14 18:08:44 -03:00
|
|
|
// Interpolate the x-coordinate of the center point
|
|
|
|
|
Point cx_left(t1, left.cx, openshot::InterpolationType::LINEAR);
|
|
|
|
|
Point cx_right(t2, right.cx, openshot::InterpolationType::LINEAR);
|
|
|
|
|
Point cx = InterpolateBetween(cx_left, cx_right, target, 0.01);
|
2020-10-29 00:34:31 -03:00
|
|
|
|
2020-12-14 18:08:44 -03:00
|
|
|
// Interpolate de y-coordinate of the center point
|
|
|
|
|
Point cy_left(t1, left.cy, openshot::InterpolationType::LINEAR);
|
|
|
|
|
Point cy_right(t2, right.cy, openshot::InterpolationType::LINEAR);
|
|
|
|
|
Point cy = InterpolateBetween(cy_left, cy_right, target, 0.01);
|
2020-12-12 20:23:34 -03:00
|
|
|
|
2020-12-14 18:08:44 -03:00
|
|
|
// Interpolate the width
|
|
|
|
|
Point width_left(t1, left.width, openshot::InterpolationType::LINEAR);
|
|
|
|
|
Point width_right(t2, right.width, openshot::InterpolationType::LINEAR);
|
|
|
|
|
Point width = InterpolateBetween(width_left, width_right, target, 0.01);
|
2020-12-12 20:23:34 -03:00
|
|
|
|
2020-12-14 18:08:44 -03:00
|
|
|
// Interpolate the height
|
|
|
|
|
Point height_left(t1, left.height, openshot::InterpolationType::LINEAR);
|
|
|
|
|
Point height_right(t2, right.height, openshot::InterpolationType::LINEAR);
|
|
|
|
|
Point height = InterpolateBetween(height_left, height_right, target, 0.01);
|
2020-10-29 00:34:31 -03:00
|
|
|
|
2020-12-14 18:08:44 -03:00
|
|
|
// Interpolate the rotation angle
|
|
|
|
|
Point angle_left(t1, left.angle, openshot::InterpolationType::LINEAR);
|
|
|
|
|
Point angle_right(t1, right.angle, openshot::InterpolationType::LINEAR);
|
|
|
|
|
Point angle = InterpolateBetween(angle_left, angle_right, target, 0.01);
|
2020-12-12 20:23:34 -03:00
|
|
|
|
2020-12-14 18:08:44 -03:00
|
|
|
// Create a bounding box with the interpolated points
|
|
|
|
|
BBox interpolatedBox(cx.co.Y, cy.co.Y, width.co.Y, height.co.Y, angle.co.Y);
|
2020-10-29 00:34:31 -03:00
|
|
|
|
2020-12-14 18:08:44 -03:00
|
|
|
return interpolatedBox;
|
2020-10-29 00:34:31 -03:00
|
|
|
}
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Update object's BaseFps
|
2021-01-18 14:52:01 -03:00
|
|
|
void TrackedObjectBBox::SetBaseFPS(Fraction fps){
|
2020-11-12 21:25:27 -03:00
|
|
|
this->BaseFps = fps;
|
2020-10-29 00:34:31 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Return the object's BaseFps
|
2021-01-18 14:52:01 -03:00
|
|
|
Fraction TrackedObjectBBox::GetBaseFPS(){
|
2020-11-12 21:25:27 -03:00
|
|
|
return BaseFps;
|
2020-10-29 00:34:31 -03:00
|
|
|
}
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Get the time of the given frame
|
2021-01-22 20:03:05 -03:00
|
|
|
double TrackedObjectBBox::FrameNToTime(int64_t frame_number, double time_scale) const{
|
2020-12-12 20:23:34 -03:00
|
|
|
double time = ((double)frame_number) * this->BaseFps.Reciprocal().ToDouble() * (1.0 / time_scale);
|
2020-10-29 00:34:31 -03:00
|
|
|
|
|
|
|
|
return time;
|
|
|
|
|
}
|
2020-11-12 21:25:27 -03:00
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Update the TimeScale member variable
|
2021-01-18 14:52:01 -03:00
|
|
|
void TrackedObjectBBox::ScalePoints(double time_scale){
|
2020-11-12 21:25:27 -03:00
|
|
|
this->TimeScale = time_scale;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Load the bounding-boxes information from the protobuf file
|
2021-01-18 14:52:01 -03:00
|
|
|
bool TrackedObjectBBox::LoadBoxData(std::string inputFilePath)
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
|
|
|
|
// Variable to hold the loaded data
|
2021-01-14 15:00:04 -03:00
|
|
|
pb_tracker::Tracker bboxMessage;
|
2020-12-12 20:23:34 -03:00
|
|
|
|
|
|
|
|
// Read the existing tracker message.
|
|
|
|
|
fstream input(inputFilePath, ios::in | ios::binary);
|
|
|
|
|
|
2021-01-22 19:28:16 -03:00
|
|
|
// Check if it was able to read the protobuf data
|
2020-12-12 20:23:34 -03:00
|
|
|
if (!bboxMessage.ParseFromIstream(&input))
|
|
|
|
|
{
|
|
|
|
|
cerr << "Failed to parse protobuf message." << endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->clear();
|
|
|
|
|
|
|
|
|
|
// Iterate over all frames of the saved message
|
|
|
|
|
for (size_t i = 0; i < bboxMessage.frame_size(); i++)
|
|
|
|
|
{
|
|
|
|
|
// Get data of the i-th frame
|
2021-01-14 15:00:04 -03:00
|
|
|
const pb_tracker::Frame &pbFrameData = bboxMessage.frame(i);
|
2020-12-12 20:23:34 -03:00
|
|
|
|
|
|
|
|
// Get frame number
|
|
|
|
|
size_t frame_number = pbFrameData.id();
|
|
|
|
|
|
|
|
|
|
// Get bounding box data from current frame
|
2021-01-14 15:00:04 -03:00
|
|
|
const pb_tracker::Frame::Box &box = pbFrameData.bounding_box();
|
2020-12-12 20:23:34 -03:00
|
|
|
|
2020-12-14 18:08:44 -03:00
|
|
|
float width = box.x2() - box.x1();
|
|
|
|
|
float height = box.y2() - box.y1();
|
|
|
|
|
float cx = box.x1() + width/2;
|
|
|
|
|
float cy = box.y1() + height/2;
|
|
|
|
|
float angle = 0.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( (cx >= 0.0) && (cy >= 0.0) && (width >= 0.0) && (height >= 0.0) )
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
|
|
|
|
// The bounding-box properties are valid, so add it to the BoxVec map
|
2020-12-14 18:08:44 -03:00
|
|
|
this->AddBox(frame_number, cx, cy, width, height, angle);
|
2020-12-12 20:23:34 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Show the time stamp from the last update in tracker data file
|
|
|
|
|
if (bboxMessage.has_last_updated())
|
|
|
|
|
{
|
|
|
|
|
cout << " Loaded Data. Saved Time Stamp: " << TimeUtil::ToString(bboxMessage.last_updated()) << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete all global objects allocated by libprotobuf.
|
|
|
|
|
google::protobuf::ShutdownProtobufLibrary();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-11-12 21:25:27 -03:00
|
|
|
|
2020-12-22 21:32:36 -03:00
|
|
|
// Clear the BoxVec map
|
2021-01-18 14:52:01 -03:00
|
|
|
void TrackedObjectBBox::clear()
|
2020-12-22 21:32:36 -03:00
|
|
|
{
|
|
|
|
|
BoxVec.clear();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-12 21:25:27 -03:00
|
|
|
// Generate JSON string of this object
|
2021-01-18 14:52:01 -03:00
|
|
|
std::string TrackedObjectBBox::Json() const
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
|
|
|
|
// Return formatted string
|
|
|
|
|
return JsonValue().toStyledString();
|
2020-11-12 21:25:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate Json::Value for this object
|
2021-01-18 14:52:01 -03:00
|
|
|
Json::Value TrackedObjectBBox::JsonValue() const
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
|
|
|
|
// Create root json object
|
|
|
|
|
Json::Value root;
|
2020-11-12 21:25:27 -03:00
|
|
|
|
2020-12-22 21:32:36 -03:00
|
|
|
// Object's properties
|
|
|
|
|
root["box_id"] = Id();
|
2020-11-12 21:25:27 -03:00
|
|
|
root["BaseFPS"]["num"] = BaseFps.num;
|
2020-12-12 20:23:34 -03:00
|
|
|
root["BaseFPS"]["den"] = BaseFps.den;
|
2020-12-22 21:32:36 -03:00
|
|
|
root["TimeScale"] = TimeScale;
|
2021-02-04 15:45:33 -03:00
|
|
|
root["child_clip_id"] = ChildClipId();
|
2020-11-12 21:25:27 -03:00
|
|
|
|
2020-12-22 21:32:36 -03:00
|
|
|
// Keyframe's properties
|
2020-11-12 21:25:27 -03:00
|
|
|
root["delta_x"] = delta_x.JsonValue();
|
|
|
|
|
root["delta_y"] = delta_y.JsonValue();
|
|
|
|
|
root["scale_x"] = scale_x.JsonValue();
|
|
|
|
|
root["scale_y"] = scale_y.JsonValue();
|
|
|
|
|
root["rotation"] = rotation.JsonValue();
|
2021-02-04 15:09:13 -03:00
|
|
|
root["visible"] = visible.JsonValue();
|
2021-03-18 12:25:22 -03:00
|
|
|
root["draw_box"] = draw_box.JsonValue();
|
2020-12-12 20:23:34 -03:00
|
|
|
|
|
|
|
|
// return JsonValue
|
|
|
|
|
return root;
|
2020-11-12 21:25:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load JSON string into this object
|
2021-01-18 14:52:01 -03:00
|
|
|
void TrackedObjectBBox::SetJson(const std::string value)
|
2020-12-12 20:23:34 -03:00
|
|
|
{
|
|
|
|
|
// 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)");
|
|
|
|
|
}
|
|
|
|
|
return;
|
2020-11-12 21:25:27 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load Json::Value into this object
|
2021-01-18 14:52:01 -03:00
|
|
|
void TrackedObjectBBox::SetJsonValue(const Json::Value root)
|
2020-12-22 21:32:36 -03:00
|
|
|
{
|
2020-11-12 21:25:27 -03:00
|
|
|
|
2021-01-22 20:14:25 -03:00
|
|
|
// Set the Id by the given JSON object
|
2021-02-04 15:57:30 -03:00
|
|
|
if (!root["box_id"].isNull() && root["box_id"].asString() != "None")
|
2021-01-18 14:52:01 -03:00
|
|
|
Id(root["box_id"].asString());
|
2020-12-22 21:32:36 -03:00
|
|
|
|
|
|
|
|
// Set the BaseFps by the given JSON object
|
2020-12-12 20:23:34 -03:00
|
|
|
if (!root["BaseFPS"].isNull() && root["BaseFPS"].isObject())
|
2020-12-22 21:32:36 -03:00
|
|
|
{
|
2020-11-12 21:25:27 -03:00
|
|
|
if (!root["BaseFPS"]["num"].isNull())
|
2020-12-12 20:23:34 -03:00
|
|
|
BaseFps.num = (int)root["BaseFPS"]["num"].asInt();
|
2020-11-12 21:25:27 -03:00
|
|
|
if (!root["BaseFPS"]["den"].isNull())
|
2020-12-12 20:23:34 -03:00
|
|
|
BaseFps.den = (int)root["BaseFPS"]["den"].asInt();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 21:32:36 -03:00
|
|
|
// Set the TimeScale by the given JSON object
|
2020-12-12 20:23:34 -03:00
|
|
|
if (!root["TimeScale"].isNull())
|
|
|
|
|
{
|
|
|
|
|
double scale = (double)root["TimeScale"].asDouble();
|
2020-11-12 21:33:53 -03:00
|
|
|
this->ScalePoints(scale);
|
2020-12-12 20:23:34 -03:00
|
|
|
}
|
|
|
|
|
|
2021-01-22 20:14:25 -03:00
|
|
|
// Set the protobuf data path by the given JSON object
|
2020-12-22 21:32:36 -03:00
|
|
|
if (!root["protobuf_data_path"].isNull())
|
|
|
|
|
protobufDataPath = root["protobuf_data_path"].asString();
|
2020-12-12 20:23:34 -03:00
|
|
|
|
2021-02-04 15:45:33 -03:00
|
|
|
// Set the id of the child clio
|
|
|
|
|
if (!root["child_clip_id"].isNull())
|
|
|
|
|
ChildClipId(root["child_clip_id"].asString());
|
|
|
|
|
|
2020-12-12 20:23:34 -03:00
|
|
|
// Set the Keyframes by the given JSON object
|
|
|
|
|
if (!root["delta_x"].isNull())
|
|
|
|
|
delta_x.SetJsonValue(root["delta_x"]);
|
|
|
|
|
if (!root["delta_y"].isNull())
|
|
|
|
|
delta_y.SetJsonValue(root["delta_y"]);
|
|
|
|
|
if (!root["scale_x"].isNull())
|
|
|
|
|
scale_x.SetJsonValue(root["scale_x"]);
|
|
|
|
|
if (!root["scale_y"].isNull())
|
|
|
|
|
scale_y.SetJsonValue(root["scale_y"]);
|
|
|
|
|
if (!root["rotation"].isNull())
|
|
|
|
|
rotation.SetJsonValue(root["rotation"]);
|
2021-02-04 15:09:13 -03:00
|
|
|
if (!root["visible"].isNull())
|
|
|
|
|
visible.SetJsonValue(root["visible"]);
|
2021-03-18 12:25:22 -03:00
|
|
|
if (!root["draw_box"].isNull())
|
|
|
|
|
draw_box.SetJsonValue(root["draw_box"]);
|
2021-02-04 15:09:13 -03:00
|
|
|
|
2020-11-12 21:25:27 -03:00
|
|
|
return;
|
2020-12-14 18:08:44 -03:00
|
|
|
}
|
2020-12-22 21:32:36 -03:00
|
|
|
|
|
|
|
|
// Get all properties for a specific frame (perfect for a UI to display the current state
|
|
|
|
|
// of all properties at any time)
|
2021-01-18 14:52:01 -03:00
|
|
|
Json::Value TrackedObjectBBox::PropertiesJSON(int64_t requested_frame) const
|
2020-12-22 21:32:36 -03:00
|
|
|
{
|
|
|
|
|
Json::Value root;
|
|
|
|
|
|
|
|
|
|
BBox box = GetBox(requested_frame);
|
|
|
|
|
|
2021-01-22 20:14:25 -03:00
|
|
|
// Add the ID of this object to the JSON object
|
2020-12-22 21:32:36 -03:00
|
|
|
root["box_id"] = add_property_json("Box ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
|
2021-01-22 20:03:05 -03:00
|
|
|
|
2021-02-04 15:45:33 -03:00
|
|
|
// Add the ID of this object's child clip to the JSON object
|
|
|
|
|
root["child_clip_id"] = add_property_json("Child Clip ID", 0.0, "string", ChildClipId(), NULL, -1, -1, false, requested_frame);
|
|
|
|
|
|
2020-12-22 21:32:36 -03:00
|
|
|
// Add the data of given frame bounding-box to the JSON object
|
|
|
|
|
root["x1"] = add_property_json("X1", box.cx-(box.width/2), "float", "", NULL, 0.0, 1.0, false, requested_frame);
|
|
|
|
|
root["y1"] = add_property_json("Y1", box.cy-(box.height/2), "float", "", NULL, 0.0, 1.0, false, requested_frame);
|
|
|
|
|
root["x2"] = add_property_json("X2", box.cx+(box.width/2), "float", "", NULL, 0.0, 1.0, false, requested_frame);
|
|
|
|
|
root["y2"] = add_property_json("Y2", box.cy+(box.height/2), "float", "", NULL, 0.0, 1.0, false, requested_frame);
|
|
|
|
|
|
|
|
|
|
// Add the bounding-box Keyframes to the JSON object
|
|
|
|
|
root["delta_x"] = add_property_json("Displacement X-axis", delta_x.GetValue(requested_frame), "float", "", &delta_x, -1.0, 1.0, false, requested_frame);
|
|
|
|
|
root["delta_y"] = add_property_json("Displacement Y-axis", delta_y.GetValue(requested_frame), "float", "", &delta_y, -1.0, 1.0, false, requested_frame);
|
|
|
|
|
root["scale_x"] = add_property_json("Scale (Width)", scale_x.GetValue(requested_frame), "float", "", &scale_x, -1.0, 1.0, false, requested_frame);
|
|
|
|
|
root["scale_y"] = add_property_json("Scale (Height)", scale_y.GetValue(requested_frame), "float", "", &scale_y, -1.0, 1.0, false, requested_frame);
|
|
|
|
|
root["rotation"] = add_property_json("Rotation", rotation.GetValue(requested_frame), "float", "", &rotation, 0, 360, false, requested_frame);
|
2021-02-04 15:09:13 -03:00
|
|
|
root["visible"] = add_property_json("Visible", visible.GetValue(requested_frame), "int", "", &visible, 0, 1, false, requested_frame);
|
2021-03-18 12:25:22 -03:00
|
|
|
|
|
|
|
|
root["draw_box"] = add_property_json("Draw Box", draw_box.GetValue(requested_frame), "int", "", &draw_box, -1, 1.0, false, requested_frame);
|
|
|
|
|
root["draw_box"]["choices"].append(add_property_choice_json("Off", 0, draw_box.GetValue(requested_frame)));
|
|
|
|
|
root["draw_box"]["choices"].append(add_property_choice_json("On", 1, draw_box.GetValue(requested_frame)));
|
2020-12-22 21:32:36 -03:00
|
|
|
|
|
|
|
|
// Return formatted string
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Generate JSON for a property
|
2021-01-18 14:52:01 -03:00
|
|
|
Json::Value TrackedObjectBBox::add_property_json(std::string name, float value, std::string type, std::string memo, const Keyframe* keyframe, float min_value, float max_value, bool readonly, int64_t requested_frame) const {
|
2020-12-22 21:32:36 -03:00
|
|
|
|
|
|
|
|
// Requested Point
|
|
|
|
|
const Point requested_point(requested_frame, requested_frame);
|
|
|
|
|
|
|
|
|
|
// Create JSON Object
|
|
|
|
|
Json::Value prop = Json::Value(Json::objectValue);
|
|
|
|
|
prop["name"] = name;
|
|
|
|
|
prop["value"] = value;
|
|
|
|
|
prop["memo"] = memo;
|
|
|
|
|
prop["type"] = type;
|
|
|
|
|
prop["min"] = min_value;
|
|
|
|
|
prop["max"] = max_value;
|
|
|
|
|
if (keyframe) {
|
|
|
|
|
prop["keyframe"] = keyframe->Contains(requested_point);
|
|
|
|
|
prop["points"] = int(keyframe->GetCount());
|
|
|
|
|
Point closest_point = keyframe->GetClosestPoint(requested_point);
|
|
|
|
|
prop["interpolation"] = closest_point.interpolation;
|
|
|
|
|
prop["closest_point_x"] = closest_point.co.X;
|
|
|
|
|
prop["previous_point_x"] = keyframe->GetPreviousPoint(closest_point).co.X;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
prop["keyframe"] = false;
|
|
|
|
|
prop["points"] = 0;
|
|
|
|
|
prop["interpolation"] = CONSTANT;
|
|
|
|
|
prop["closest_point_x"] = -1;
|
|
|
|
|
prop["previous_point_x"] = -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prop["readonly"] = readonly;
|
|
|
|
|
prop["choices"] = Json::Value(Json::arrayValue);
|
|
|
|
|
|
|
|
|
|
// return JsonValue
|
|
|
|
|
return prop;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 20:14:25 -03:00
|
|
|
// Return a map that contains the bounding box properties and it's keyframes indexed by their names
|
2021-01-19 16:03:51 -03:00
|
|
|
std::map<std::string, float> TrackedObjectBBox::GetBoxValues(int64_t frame_number) const {
|
2020-12-22 21:32:36 -03:00
|
|
|
|
|
|
|
|
// Create the map
|
|
|
|
|
std::map<std::string, float> boxValues;
|
|
|
|
|
|
|
|
|
|
// Get bounding box of the current frame
|
|
|
|
|
BBox box = GetBox(frame_number);
|
|
|
|
|
|
|
|
|
|
// Save the bounding box properties
|
|
|
|
|
boxValues["cx"] = box.cx;
|
|
|
|
|
boxValues["cy"] = box.cy;
|
|
|
|
|
boxValues["w"] = box.width;
|
|
|
|
|
boxValues["h"] = box.height;
|
|
|
|
|
boxValues["ang"] = box.angle;
|
|
|
|
|
|
|
|
|
|
// Save the keyframes values
|
|
|
|
|
boxValues["sx"] = this->scale_x.GetValue(frame_number);
|
|
|
|
|
boxValues["sy"] = this->scale_y.GetValue(frame_number);
|
|
|
|
|
boxValues["dx"] = this->delta_x.GetValue(frame_number);
|
|
|
|
|
boxValues["dy"] = this->delta_y.GetValue(frame_number);
|
|
|
|
|
boxValues["r"] = this->rotation.GetValue(frame_number);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return boxValues;
|
2021-01-18 14:52:01 -03:00
|
|
|
}
|
|
|
|
|
|
2021-01-22 20:14:25 -03:00
|
|
|
// Return a map that contains the properties of this object's parent clip
|
2021-01-19 16:03:51 -03:00
|
|
|
std::map<std::string, float> TrackedObjectBBox::GetParentClipProperties(int64_t frame_number) const {
|
2021-01-18 14:52:01 -03:00
|
|
|
|
|
|
|
|
// Get the parent clip of this object as a Clip pointer
|
|
|
|
|
Clip* parentClip = (Clip *) ParentClip();
|
|
|
|
|
|
|
|
|
|
// Calculate parentClip's frame number
|
|
|
|
|
long parentClip_start_position = round( parentClip->Position() * parentClip->info.fps.ToDouble() ) + 1;
|
|
|
|
|
long parentClip_start_frame = ( parentClip->Start() * parentClip->info.fps.ToDouble() ) + 1;
|
2021-01-18 15:30:11 -03:00
|
|
|
float parentClip_frame_number = round(frame_number - parentClip_start_position) + parentClip_start_frame;
|
2021-01-18 14:52:01 -03:00
|
|
|
|
|
|
|
|
// Get parentClip's Keyframes
|
|
|
|
|
float parentClip_location_x = parentClip->location_x.GetValue(parentClip_frame_number);
|
|
|
|
|
float parentClip_location_y = parentClip->location_y.GetValue(parentClip_frame_number);
|
|
|
|
|
float parentClip_scale_x = parentClip->scale_x.GetValue(parentClip_frame_number);
|
|
|
|
|
float parentClip_scale_y = parentClip->scale_y.GetValue(parentClip_frame_number);
|
|
|
|
|
float parentClip_rotation = parentClip->rotation.GetValue(parentClip_frame_number);
|
|
|
|
|
|
2021-01-22 20:14:25 -03:00
|
|
|
// Initialize the parent clip properties map
|
2021-01-18 14:52:01 -03:00
|
|
|
std::map<std::string, float> parentClipProperties;
|
|
|
|
|
|
2021-01-22 20:14:25 -03:00
|
|
|
// Set the map properties
|
2021-01-18 14:52:01 -03:00
|
|
|
parentClipProperties["frame_number"] = parentClip_frame_number;
|
|
|
|
|
parentClipProperties["timeline_frame_number"] = frame_number;
|
|
|
|
|
parentClipProperties["location_x"] = parentClip_location_x;
|
|
|
|
|
parentClipProperties["location_y"] = parentClip_location_y;
|
|
|
|
|
parentClipProperties["scale_x"] = parentClip_scale_x;
|
|
|
|
|
parentClipProperties["scale_y"] = parentClip_scale_y;
|
|
|
|
|
parentClipProperties["rotation"] = parentClip_rotation;
|
|
|
|
|
|
|
|
|
|
return parentClipProperties;
|
2020-12-22 21:32:36 -03:00
|
|
|
}
|