|
OpenShot Library | libopenshot
0.7.0
|
Go to the documentation of this file.
23 #include "trackerdata.pb.h"
24 #include <google/protobuf/util/time_util.h>
26 using google::protobuf::util::TimeUtil;
37 : delta_x(0.0), delta_y(0.0),
38 scale_x(1.0), scale_y(1.0), rotation(0.0),
39 background_alpha(0.0), background_corner(12),
40 stroke_width(2) , stroke_alpha(0.7),
41 stroke(Red, Green, Blue, Alfa),
42 background(0, 0, 255, Alfa)
44 this->TimeScale = 1.0;
55 BBox newBBox =
BBox(_cx, _cy, _width, _height, _angle);
60 auto BBoxIterator =
BoxVec.find(time);
62 if (BBoxIterator !=
BoxVec.end())
65 BBoxIterator->second = newBBox;
70 BoxVec.insert({time, newBBox});
90 auto it =
BoxVec.lower_bound(time);
104 auto it =
BoxVec.find(time);
118 auto it =
BoxVec.find(time);
131 double time = this->
FrameNToTime(frame_number, this->TimeScale);
135 auto currentBBoxIterator =
BoxVec.lower_bound(time);
138 if (currentBBoxIterator ==
BoxVec.end())
146 if ((currentBBoxIterator->first == time) || (currentBBoxIterator ==
BoxVec.begin()))
149 BBox currentBBox = currentBBoxIterator->second;
162 BBox currentBBox = currentBBoxIterator->second;
164 BBox previousBBox = prev(currentBBoxIterator, 1)->second;
167 BBox interpolatedBBox =
InterpolateBoxes(prev(currentBBoxIterator, 1)->first, currentBBoxIterator->first,
168 previousBBox, currentBBox, time);
177 return interpolatedBBox;
182 const double base_width = std::max(0.0,
static_cast<double>(
stroke_width.
GetValue(frame_number)));
184 if (!parent_clip || image_width <= 0 || image_height <= 0)
187 int target_width = image_width;
188 int target_height = image_height;
197 QSize output_size(image_width, image_height);
198 switch (parent_clip->
scale) {
200 output_size.scale(target_width, target_height, Qt::KeepAspectRatio);
203 output_size.scale(target_width, target_height, Qt::IgnoreAspectRatio);
206 output_size.scale(target_width, target_height, Qt::KeepAspectRatioByExpanding);
212 if (output_size.width() <= 0 || output_size.height() <= 0)
215 const double raster_scale_x =
static_cast<double>(image_width) / output_size.width();
216 const double raster_scale_y =
static_cast<double>(image_height) / output_size.height();
217 return base_width * std::sqrt(raster_scale_x * raster_scale_y);
251 return interpolatedBox;
267 double time = ((double)frame_number) * this->BaseFps.
Reciprocal().
ToDouble() * (1.0 / time_scale);
274 this->TimeScale = time_scale;
283 pb_tracker::Tracker bboxMessage;
286 std::fstream input(inputFilePath, ios::in | ios::binary);
289 if (!bboxMessage.ParseFromIstream(&input))
291 std::cerr <<
"Failed to parse protobuf message." << std::endl;
298 for (
size_t i = 0; i < bboxMessage.frame_size(); i++)
301 const pb_tracker::Frame &pbFrameData = bboxMessage.frame(i);
304 size_t frame_number = pbFrameData.id();
307 const pb_tracker::Frame::Box &box = pbFrameData.bounding_box();
309 float width = box.x2() - box.x1();
310 float height = box.y2() - box.y1();
311 float cx = box.x1() + width/2;
312 float cy = box.y1() + height/2;
316 if ( (cx >= 0.0) && (cy >= 0.0) && (width >= 0.0) && (height >= 0.0) )
319 this->
AddBox(frame_number, cx, cy, width, height, angle);
324 if (bboxMessage.has_last_updated())
326 std::cout <<
" Loaded Data. Saved Time Stamp: "
327 << TimeUtil::ToString(bboxMessage.last_updated()) << std::endl;
331 google::protobuf::ShutdownProtobufLibrary();
356 root[
"box_id"] =
Id();
357 root[
"BaseFPS"][
"num"] = BaseFps.
num;
358 root[
"BaseFPS"][
"den"] = BaseFps.
den;
359 root[
"TimeScale"] = TimeScale;
390 catch (
const std::exception &e)
393 throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
403 if (!root[
"box_id"].isNull() && root[
"box_id"].asString() !=
"")
404 Id(root[
"box_id"].asString());
407 if (!root[
"BaseFPS"].isNull() && root[
"BaseFPS"].isObject())
409 if (!root[
"BaseFPS"][
"num"].isNull())
410 BaseFps.
num = (int)root[
"BaseFPS"][
"num"].asInt();
411 if (!root[
"BaseFPS"][
"den"].isNull())
412 BaseFps.
den = (
int)root[
"BaseFPS"][
"den"].asInt();
415 if (!root[
"TimeScale"].isNull())
417 double scale = (double)root[
"TimeScale"].asDouble();
421 if (!root[
"protobuf_data_path"].isNull())
425 if (!root[
"delta_x"].isNull())
427 if (!root[
"delta_y"].isNull())
429 if (!root[
"scale_x"].isNull())
431 if (!root[
"scale_y"].isNull())
433 if (!root[
"rotation"].isNull())
435 if (!root[
"visible"].isNull())
437 if (!root[
"draw_box"].isNull())
439 if (!root[
"stroke"].isNull())
441 if (!root[
"background_alpha"].isNull())
443 if (!root[
"background_corner"].isNull())
445 if (!root[
"background"].isNull())
447 if (!root[
"stroke_width"].isNull())
449 if (!root[
"stroke_alpha"].isNull())
463 root[
"box_id"] =
add_property_json(
"Box ID", 0.0,
"string",
Id(), NULL, -1, -1,
true, requested_frame);
483 root[
"stroke"] =
add_property_json(
"Border", 0.0,
"color",
"", NULL, 0, 255,
false, requested_frame);
493 root[
"background"] =
add_property_json(
"Background", 0.0,
"color",
"", NULL, 0, 255,
false, requested_frame);
507 const Point requested_point(requested_frame, requested_frame);
510 Json::Value prop = Json::Value(Json::objectValue);
512 prop[
"value"] = value;
515 prop[
"min"] = min_value;
516 prop[
"max"] = max_value;
518 prop[
"keyframe"] = keyframe->
Contains(requested_point);
519 prop[
"points"] = int(keyframe->
GetCount());
522 prop[
"closest_point_x"] = closest_point.
co.
X;
526 prop[
"keyframe"] =
false;
529 prop[
"closest_point_x"] = -1;
530 prop[
"previous_point_x"] = -1;
533 prop[
"readonly"] = readonly;
534 prop[
"choices"] = Json::Value(Json::arrayValue);
544 std::map<std::string, float> boxValues;
550 boxValues[
"cx"] = box.
cx;
551 boxValues[
"cy"] = box.
cy;
552 boxValues[
"w"] = box.
width;
553 boxValues[
"h"] = box.
height;
554 boxValues[
"ang"] = box.
angle;
const Json::Value stringToJson(const std::string value)
Json::Value PropertiesJSON(int64_t requested_frame) const override
Header file for Timeline class.
Keyframe stroke_alpha
Stroke box opacity.
Keyframe rotation
Rotation Keyframe.
void AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle) override
Add a BBox to the BoxVec map.
double Y
The Y value of the coordinate (usually representing the value of the property being animated)
int preview_width
Optional preview width of timeline image. If your preview window is smaller than the timeline,...
Header file for Clip class.
float height
bounding box height
bool Contains(int64_t frame_number) const
Check if there is a bounding-box in the given frame.
Json::Value JsonValue() const override
Generate Json::Value for this object.
Color background
Background fill color.
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
InterpolationType interpolation
This is the interpolation mode.
This namespace is the default namespace for all code in the openshot library.
int preview_height
Optional preview width of timeline image. If your preview window is smaller than the timeline,...
Coordinate co
This is the primary coordinate.
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
double FrameNToTime(int64_t frame_number, double time_scale) const
Get the time of the given frame.
This class represents a clip (used to arrange readers on the timeline)
This class represents a fraction.
float cy
y-coordinate of the bounding box center
void ScalePoints(double scale) override
Update the TimeScale member variable.
void ParentTimeline(openshot::TimelineBase *new_timeline) override
Set associated Timeline pointer.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
std::string Json() const override
Get and Set JSON methods.
Fraction GetBaseFPS()
Return the object's BaseFps.
Json::Value add_property_choice_json(std::string name, int value, int selected_value) const
Generate JSON choice for a property (dropdown properties)
bool Contains(Point p) const
Does this keyframe contain a specific point.
Keyframe draw_box
Keyframe to determine if a specific box is drawn (or hidden)
std::string Id() const
Get the id of this object.
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Keyframe scale_y
Y-direction scale Keyframe.
Json::Value JsonValue() const
Generate Json::Value for this object.
float angle
bounding box rotation angle [degrees]
int num
Numerator for the fraction.
Keyframe visible
Keyframe to track if a box is visible in the current frame (read-only)
This class contains the properties of a tracked object and functions to manipulate it.
int den
Denominator for the fraction.
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
double InterpolateBetween(Point const &left, Point const &right, double target, double allowed_error)
Interpolate two points using the right Point's interpolation method.
Keyframe scale_x
X-direction scale Keyframe.
Exception for invalid JSON.
BBox GetBox(int64_t frame_number)
Return a bounding-box from BoxVec with it's properties adjusted by the Keyframes.
TrackedObjectBBox()
Default Constructor.
float width
bounding box width
@ SCALE_CROP
Scale the clip until both height and width fill the canvas (cropping the overlap)
openshot::Keyframe green
Curve representing the green value (0 - 255)
ClipBase * ParentClip() const
Get and set the parentClip of this object.
BBox InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target)
Interpolate the bouding-boxes properties.
Keyframe delta_y
Y-direction displacement Keyframe.
void RemoveBox(int64_t frame_number)
Remove a bounding-box from the BoxVec map.
std::map< double, BBox > BoxVec
Index the bounding-box by time of each 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.
@ SCALE_FIT
Scale the clip until either height or width fills the canvas (with no cropping)
@ LINEAR
Linear curves are angular, straight lines between two points.
Json::Value JsonValue() const
Generate Json::Value for this object.
Point GetClosestPoint(Point p) const
Get current point (or closest point to the right) from the X coordinate (i.e. the frame number)
Keyframe delta_x
X-direction displacement Keyframe.
This struct holds the information of a bounding-box.
bool LoadBoxData(std::string inputFilePath)
Load the bounding-boxes information from the protobuf file.
void clear()
Clear the BoxVec map.
void SetBaseFPS(Fraction fps)
Update object's BaseFps.
void SetJson(const std::string value) override
Load JSON string into this object.
Color stroke
Border line color.
bool ExactlyContains(int64_t frame_number) const override
Check if there is a bounding-box in the exact frame number.
This class represents a timeline (used for building generic timeline implementations)
std::string protobufDataPath
Path to the protobuf file that holds the bounding box points across the frames.
int64_t GetCount() const
Get the number of points (i.e. # of points)
@ CONSTANT
Constant curves jump from their previous position to a new one (with no interpolation).
openshot::ScaleType scale
The scale determines how a clip should be resized to fit its parent.
Point GetPreviousPoint(Point p) const
Get previous point (.
Header file for the TrackedObjectBBox class.
@ SCALE_NONE
Do not scale the clip.
double ScaledStrokeWidth(int64_t frame_number, int image_width, int image_height) const
Return stroke width adjusted for source-to-output raster scaling.
float cx
x-coordinate of the bounding box center
Keyframe stroke_width
Thickness of border line.
openshot::Keyframe red
Curve representing the red value (0 - 255)
@ SCALE_STRETCH
Scale the clip until both height and width fill the canvas (distort to fit)
Keyframe background_corner
Radius of rounded corners.
A Point is the basic building block of a key-frame curve.
openshot::Keyframe blue
Curve representing the red value (0 - 255)
double X
The X value of the coordinate (usually representing the frame #)
double GetValue(int64_t index) const
Get the value at a specific index.
Keyframe background_alpha
Background box opacity.
int64_t GetLength() const
Get the size of BoxVec map.