20#include "trackerdata.pb.h"
21#include <google/protobuf/util/time_util.h>
23using google::protobuf::util::TimeUtil;
34 : delta_x(0.0), delta_y(0.0),
35 scale_x(1.0), scale_y(1.0), rotation(0.0),
36 background_alpha(0.0), background_corner(12),
37 stroke_width(2) , stroke_alpha(0.7),
38 stroke(Red, Green, Blue, Alfa),
39 background(0, 0, 255, Alfa)
41 this->TimeScale = 1.0;
52 BBox newBBox =
BBox(_cx, _cy, _width, _height, _angle);
57 auto BBoxIterator =
BoxVec.find(time);
59 if (BBoxIterator !=
BoxVec.end())
62 BBoxIterator->second = newBBox;
67 BoxVec.insert({time, newBBox});
87 auto it =
BoxVec.lower_bound(time);
101 auto it =
BoxVec.find(time);
115 auto it =
BoxVec.find(time);
128 double time = this->
FrameNToTime(frame_number, this->TimeScale);
132 auto currentBBoxIterator =
BoxVec.lower_bound(time);
135 if (currentBBoxIterator ==
BoxVec.end())
143 if ((currentBBoxIterator->first == time) || (currentBBoxIterator ==
BoxVec.begin()))
146 BBox currentBBox = currentBBoxIterator->second;
159 BBox currentBBox = currentBBoxIterator->second;
161 BBox previousBBox = prev(currentBBoxIterator, 1)->second;
164 BBox interpolatedBBox =
InterpolateBoxes(prev(currentBBoxIterator, 1)->first, currentBBoxIterator->first,
165 previousBBox, currentBBox, time);
174 return interpolatedBBox;
208 return interpolatedBox;
224 double time = ((double)frame_number) * this->BaseFps.
Reciprocal().
ToDouble() * (1.0 / time_scale);
231 this->TimeScale = time_scale;
240 pb_tracker::Tracker bboxMessage;
243 std::fstream input(inputFilePath, ios::in | ios::binary);
246 if (!bboxMessage.ParseFromIstream(&input))
248 std::cerr <<
"Failed to parse protobuf message." << std::endl;
255 for (
size_t i = 0; i < bboxMessage.frame_size(); i++)
258 const pb_tracker::Frame &pbFrameData = bboxMessage.frame(i);
261 size_t frame_number = pbFrameData.id();
264 const pb_tracker::Frame::Box &box = pbFrameData.bounding_box();
266 float width = box.x2() - box.x1();
267 float height = box.y2() - box.y1();
268 float cx = box.x1() + width/2;
269 float cy = box.y1() + height/2;
273 if ( (cx >= 0.0) && (cy >= 0.0) && (width >= 0.0) && (height >= 0.0) )
276 this->
AddBox(frame_number, cx, cy, width, height, angle);
281 if (bboxMessage.has_last_updated())
283 std::cout <<
" Loaded Data. Saved Time Stamp: "
284 << TimeUtil::ToString(bboxMessage.last_updated()) << std::endl;
288 google::protobuf::ShutdownProtobufLibrary();
313 root[
"box_id"] =
Id();
314 root[
"BaseFPS"][
"num"] = BaseFps.
num;
315 root[
"BaseFPS"][
"den"] = BaseFps.
den;
316 root[
"TimeScale"] = TimeScale;
347 catch (
const std::exception &e)
350 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
360 if (!root[
"box_id"].isNull() && root[
"box_id"].asString() !=
"")
361 Id(root[
"box_id"].asString());
364 if (!root[
"BaseFPS"].isNull() && root[
"BaseFPS"].isObject())
366 if (!root[
"BaseFPS"][
"num"].isNull())
367 BaseFps.
num = (int)root[
"BaseFPS"][
"num"].asInt();
368 if (!root[
"BaseFPS"][
"den"].isNull())
369 BaseFps.
den = (
int)root[
"BaseFPS"][
"den"].asInt();
372 if (!root[
"TimeScale"].isNull())
374 double scale = (double)root[
"TimeScale"].asDouble();
378 if (!root[
"protobuf_data_path"].isNull())
382 if (!root[
"delta_x"].isNull())
384 if (!root[
"delta_y"].isNull())
386 if (!root[
"scale_x"].isNull())
388 if (!root[
"scale_y"].isNull())
390 if (!root[
"rotation"].isNull())
392 if (!root[
"visible"].isNull())
394 if (!root[
"draw_box"].isNull())
396 if (!root[
"stroke"].isNull())
398 if (!root[
"background_alpha"].isNull())
400 if (!root[
"background_corner"].isNull())
402 if (!root[
"background"].isNull())
404 if (!root[
"stroke_width"].isNull())
406 if (!root[
"stroke_alpha"].isNull())
420 root[
"box_id"] =
add_property_json(
"Box ID", 0.0,
"string",
Id(), NULL, -1, -1,
true, requested_frame);
440 root[
"stroke"] =
add_property_json(
"Border", 0.0,
"color",
"", NULL, 0, 255,
false, requested_frame);
450 root[
"background"] =
add_property_json(
"Background", 0.0,
"color",
"", NULL, 0, 255,
false, requested_frame);
464 const Point requested_point(requested_frame, requested_frame);
467 Json::Value prop = Json::Value(Json::objectValue);
469 prop[
"value"] = value;
472 prop[
"min"] = min_value;
473 prop[
"max"] = max_value;
475 prop[
"keyframe"] = keyframe->
Contains(requested_point);
476 prop[
"points"] = int(keyframe->
GetCount());
479 prop[
"closest_point_x"] = closest_point.
co.
X;
483 prop[
"keyframe"] =
false;
486 prop[
"closest_point_x"] = -1;
487 prop[
"previous_point_x"] = -1;
490 prop[
"readonly"] = readonly;
491 prop[
"choices"] = Json::Value(Json::arrayValue);
501 std::map<std::string, float> boxValues;
507 boxValues[
"cx"] = box.
cx;
508 boxValues[
"cy"] = box.
cy;
509 boxValues[
"w"] = box.
width;
510 boxValues[
"h"] = box.
height;
511 boxValues[
"ang"] = box.
angle;
Header file for Clip class.
Header file for the TrackedObjectBBox class.
openshot::Keyframe blue
Curve representing the red value (0 - 255)
openshot::Keyframe red
Curve representing the red value (0 - 255)
openshot::Keyframe green
Curve representing the green value (0 - 255)
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Json::Value JsonValue() const
Generate Json::Value for this object.
double X
The X value of the coordinate (usually representing the frame #)
double Y
The Y value of the coordinate (usually representing the value of the property being animated)
This class represents a fraction.
int num
Numerator for the fraction.
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
int den
Denominator for the fraction.
Exception for invalid JSON.
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
bool Contains(Point p) const
Does this keyframe contain a specific point.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Point GetPreviousPoint(Point p) const
Get previous point (.
double GetValue(int64_t index) const
Get the value at a specific index.
Json::Value JsonValue() const
Generate Json::Value for this object.
int64_t GetCount() const
Get the number of points (i.e. # of points)
Point GetClosestPoint(Point p) const
Get current point (or closest point to the right) from the X coordinate (i.e. the frame number)
A Point is the basic building block of a key-frame curve.
Coordinate co
This is the primary coordinate.
InterpolationType interpolation
This is the interpolation mode.
This class contains the properties of a tracked object and functions to manipulate it.
Keyframe delta_x
X-direction displacement Keyframe.
bool LoadBoxData(std::string inputFilePath)
Load the bounding-boxes information from the protobuf file.
Color stroke
Border line color.
Keyframe rotation
Rotation Keyframe.
int64_t GetLength() const
Get the size of BoxVec map.
Keyframe stroke_width
Thickness of border line.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
void AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle) override
Add a BBox to the BoxVec map.
TrackedObjectBBox()
Default Constructor.
void SetBaseFPS(Fraction fps)
Update object's BaseFps.
void SetJson(const std::string value) override
Load JSON string into this object.
Json::Value 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
void clear()
Clear the BoxVec map.
Keyframe delta_y
Y-direction displacement Keyframe.
Color background
Background fill color.
std::map< double, BBox > BoxVec
Index the bounding-box by time of each frame.
std::string Json() const override
Get and Set JSON methods.
bool ExactlyContains(int64_t frame_number) const override
Check if there is a bounding-box in the exact frame number.
void ScalePoints(double scale) override
Update the TimeScale member variable.
BBox InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target)
Interpolate the bouding-boxes properties.
Fraction GetBaseFPS()
Return the object's BaseFps.
Json::Value PropertiesJSON(int64_t requested_frame) const override
Keyframe background_alpha
Background box opacity.
void RemoveBox(int64_t frame_number)
Remove a bounding-box from the BoxVec map.
bool Contains(int64_t frame_number) const
Check if there is a bounding-box in the given frame.
std::map< std::string, float > GetBoxValues(int64_t frame_number) const override
Return a map that contains the bounding box properties and it's keyframes indexed by their names.
Json::Value JsonValue() const override
Generate Json::Value for this object.
Keyframe scale_y
Y-direction scale Keyframe.
std::string protobufDataPath
Path to the protobuf file that holds the bounding box points across the frames.
Keyframe stroke_alpha
Stroke box opacity.
double FrameNToTime(int64_t frame_number, double time_scale) const
Get the time of the given frame.
Keyframe scale_x
X-direction scale Keyframe.
BBox GetBox(int64_t frame_number)
Return a bounding-box from BoxVec with it's properties adjusted by the Keyframes.
Keyframe background_corner
Radius of rounded corners.
Keyframe draw_box
Keyframe to determine if a specific box is drawn (or hidden)
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
Keyframe visible
Keyframe to track if a box is visible in the current frame (read-only)
std::string Id() const
Get the id of this object.
This namespace is the default namespace for all code in the openshot library.
double InterpolateBetween(Point const &left, Point const &right, double target, double allowed_error)
Interpolate two points using the right Point's interpolation method.
const Json::Value stringToJson(const std::string value)
@ CONSTANT
Constant curves jump from their previous position to a new one (with no interpolation).
@ LINEAR
Linear curves are angular, straight lines between two points.
This struct holds the information of a bounding-box.
float cy
y-coordinate of the bounding box center
float height
bounding box height
float cx
x-coordinate of the bounding box center
float width
bounding box width
float angle
bounding box rotation angle [degrees]