OpenShot Library | libopenshot  0.4.0
TrackedObjectBBox.h
Go to the documentation of this file.
1 
10 // Copyright (c) 2008-2019 OpenShot Studios, LLC
11 //
12 // SPDX-License-Identifier: LGPL-3.0-or-later
13 
14 #ifndef OPENSHOT_TRACKEDOBJECTBBOX_H
15 #define OPENSHOT_TRACKEDOBJECTBBOX_H
16 
17 #include "TrackedObjectBase.h"
18 
19 #include "Color.h"
20 #include "Exceptions.h"
21 #include "Fraction.h"
22 #include "Json.h"
23 #include "KeyFrame.h"
24 
25 namespace openshot
26 {
37  struct BBox
38  {
39  float cx = -1;
40  float cy = -1;
41  float width = -1;
42  float height = -1;
43  float angle = -1;
44 
46  BBox() {}
47 
54  BBox(float _cx, float _cy, float _width, float _height, float _angle)
55  {
56  cx = _cx;
57  cy = _cy;
58  width = _width;
59  height = _height;
60  angle = _angle;
61  }
62 
63 
65  std::string Json() const
66  {
67  return JsonValue().toStyledString();
68  }
69 
71  Json::Value JsonValue() const
72  {
73  Json::Value root;
74  root["cx"] = cx;
75  root["cy"] = cy;
76  root["width"] = width;
77  root["height"] = height;
78  root["angle"] = angle;
79 
80  return root;
81  }
82 
84  void SetJson(const std::string value)
85  {
86  // Parse JSON string into JSON objects
87  try
88  {
89  const Json::Value root = openshot::stringToJson(value);
90  // Set all values that match
91  SetJsonValue(root);
92  }
93  catch (const std::exception &e)
94  {
95  // Error parsing JSON (or missing keys)
96  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
97  }
98  }
99 
101  void SetJsonValue(const Json::Value root)
102  {
103 
104  // Set data from Json (if key is found)
105  if (!root["cx"].isNull())
106  cx = root["cx"].asDouble();
107  if (!root["cy"].isNull())
108  cy = root["cy"].asDouble();
109  if (!root["width"].isNull())
110  width = root["width"].asDouble();
111  if (!root["height"].isNull())
112  height = root["height"].asDouble();
113  if (!root["angle"].isNull())
114  angle = root["angle"].asDouble();
115  }
116  };
117 
131  {
132  private:
133  Fraction BaseFps;
134  double TimeScale;
135 
136  public:
137  std::map<double, BBox> BoxVec;
149 
150  std::string protobufDataPath;
151 
154  TrackedObjectBBox(int Red, int Green, int Blue, int Alfa);
155 
157  void AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle) override;
158 
160  void SetBaseFPS(Fraction fps);
161 
164 
166  void ScalePoints(double scale) override;
167 
169  bool Contains(int64_t frame_number) const;
171  bool ExactlyContains(int64_t frame_number) const override;
172 
174  int64_t GetLength() const;
175 
177  void RemoveBox(int64_t frame_number);
178 
180  BBox GetBox(int64_t frame_number);
182  BBox GetBox(int64_t frame_number) const
183  {
184  return const_cast<TrackedObjectBBox *>(this)->GetBox(frame_number);
185  }
186 
188  bool LoadBoxData(std::string inputFilePath);
189 
191  double FrameNToTime(int64_t frame_number, double time_scale) const;
192 
194  BBox InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target);
195 
197  void clear();
198 
200  std::string Json() const override;
201  Json::Value JsonValue() const override;
202  void SetJson(const std::string value) override;
203  void SetJsonValue(const Json::Value root) override;
204 
207  Json::Value PropertiesJSON(int64_t requested_frame) const override;
208 
209  // Generate JSON for a property
210  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;
211 
213  std::map<std::string, float> GetBoxValues(int64_t frame_number) const override;
214  };
215 } // namespace openshot
216 
217 #endif
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition: Json.cpp:16
openshot::TrackedObjectBBox::PropertiesJSON
Json::Value PropertiesJSON(int64_t requested_frame) const override
Definition: TrackedObjectBBox.cpp:413
openshot::TrackedObjectBBox::stroke_alpha
Keyframe stroke_alpha
Stroke box opacity.
Definition: TrackedObjectBBox.h:146
openshot::TrackedObjectBBox::rotation
Keyframe rotation
Rotation Keyframe.
Definition: TrackedObjectBBox.h:142
openshot::TrackedObjectBBox::AddBox
void AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle) override
Add a BBox to the BoxVec map.
Definition: TrackedObjectBBox.cpp:45
openshot::BBox::height
float height
bounding box height
Definition: TrackedObjectBBox.h:42
Fraction.h
Header file for Fraction class.
openshot::TrackedObjectBBox::Contains
bool Contains(int64_t frame_number) const
Check if there is a bounding-box in the given frame.
Definition: TrackedObjectBBox.cpp:82
openshot::TrackedObjectBBox::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: TrackedObjectBBox.cpp:307
openshot::TrackedObjectBBox::background
Color background
Background fill color.
Definition: TrackedObjectBBox.h:148
openshot::TrackedObjectBBox::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: TrackedObjectBBox.cpp:356
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
openshot::BBox::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: TrackedObjectBBox.h:71
openshot::TrackedObjectBBox::add_property_json
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
Definition: TrackedObjectBBox.cpp:461
openshot::TrackedObjectBBox::FrameNToTime
double FrameNToTime(int64_t frame_number, double time_scale) const
Get the time of the given frame.
Definition: TrackedObjectBBox.cpp:223
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:30
openshot::BBox::cy
float cy
y-coordinate of the bounding box center
Definition: TrackedObjectBBox.h:40
openshot::TrackedObjectBBox::ScalePoints
void ScalePoints(double scale) override
Update the TimeScale member variable.
Definition: TrackedObjectBBox.cpp:230
openshot::TrackedObjectBase
This abstract class is the base class of all Tracked Objects.
Definition: TrackedObjectBase.h:35
openshot::TrackedObjectBBox::GetBox
BBox GetBox(int64_t frame_number) const
Const-cast of the GetBox function, so that it can be called inside other cont function.
Definition: TrackedObjectBBox.h:182
openshot::TrackedObjectBBox::Json
std::string Json() const override
Get and Set JSON methods.
Definition: TrackedObjectBBox.cpp:300
openshot::TrackedObjectBBox::GetBaseFPS
Fraction GetBaseFPS()
Return the object's BaseFps.
Definition: TrackedObjectBBox.cpp:218
openshot::BBox::BBox
BBox()
Blank constructor.
Definition: TrackedObjectBBox.h:46
KeyFrame.h
Header file for the Keyframe class.
openshot::TrackedObjectBBox::scale_y
Keyframe scale_y
Y-direction scale Keyframe.
Definition: TrackedObjectBBox.h:141
openshot::Color
This class represents a color (used on the timeline and clips)
Definition: Color.h:27
openshot::BBox::angle
float angle
bounding box rotation angle [degrees]
Definition: TrackedObjectBBox.h:43
openshot::TrackedObjectBBox
This class contains the properties of a tracked object and functions to manipulate it.
Definition: TrackedObjectBBox.h:130
openshot::Keyframe
A Keyframe is a collection of Point instances, which is used to vary a number or property over time.
Definition: KeyFrame.h:53
openshot::TrackedObjectBBox::scale_x
Keyframe scale_x
X-direction scale Keyframe.
Definition: TrackedObjectBBox.h:140
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:217
openshot::TrackedObjectBBox::GetBox
BBox GetBox(int64_t frame_number)
Return a bounding-box from BoxVec with it's properties adjusted by the Keyframes.
Definition: TrackedObjectBBox.cpp:125
openshot::TrackedObjectBBox::TrackedObjectBBox
TrackedObjectBBox()
Default Constructor.
Definition: TrackedObjectBBox.cpp:28
openshot::BBox::width
float width
bounding box width
Definition: TrackedObjectBBox.h:41
openshot::BBox::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: TrackedObjectBBox.h:101
openshot::TrackedObjectBBox::InterpolateBoxes
BBox InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target)
Interpolate the bouding-boxes properties.
Definition: TrackedObjectBBox.cpp:178
openshot::TrackedObjectBBox::delta_y
Keyframe delta_y
Y-direction displacement Keyframe.
Definition: TrackedObjectBBox.h:139
openshot::TrackedObjectBBox::RemoveBox
void RemoveBox(int64_t frame_number)
Remove a bounding-box from the BoxVec map.
Definition: TrackedObjectBBox.cpp:110
openshot::TrackedObjectBBox::BoxVec
std::map< double, BBox > BoxVec
Index the bounding-box by time of each frame.
Definition: TrackedObjectBBox.h:137
openshot::TrackedObjectBBox::GetBoxValues
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.
Definition: TrackedObjectBBox.cpp:498
openshot::BBox::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: TrackedObjectBBox.h:84
openshot::TrackedObjectBBox::delta_x
Keyframe delta_x
X-direction displacement Keyframe.
Definition: TrackedObjectBBox.h:138
openshot::BBox
This struct holds the information of a bounding-box.
Definition: TrackedObjectBBox.h:37
openshot::TrackedObjectBBox::LoadBoxData
bool LoadBoxData(std::string inputFilePath)
Load the bounding-boxes information from the protobuf file.
Definition: TrackedObjectBBox.cpp:235
openshot::TrackedObjectBBox::clear
void clear()
Clear the BoxVec map.
Definition: TrackedObjectBBox.cpp:294
openshot::TrackedObjectBBox::SetBaseFPS
void SetBaseFPS(Fraction fps)
Update object's BaseFps.
Definition: TrackedObjectBBox.cpp:212
openshot::TrackedObjectBBox::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: TrackedObjectBBox.cpp:338
openshot::TrackedObjectBBox::stroke
Color stroke
Border line color.
Definition: TrackedObjectBBox.h:147
openshot::TrackedObjectBBox::ExactlyContains
bool ExactlyContains(int64_t frame_number) const override
Check if there is a bounding-box in the exact frame number.
Definition: TrackedObjectBBox.cpp:96
openshot::BBox::Json
std::string Json() const
Generate JSON string of this object.
Definition: TrackedObjectBBox.h:65
openshot::TrackedObjectBBox::protobufDataPath
std::string protobufDataPath
Path to the protobuf file that holds the bounding box points across the frames.
Definition: TrackedObjectBBox.h:150
openshot::BBox::BBox
BBox(float _cx, float _cy, float _width, float _height, float _angle)
Definition: TrackedObjectBBox.h:54
Color.h
Header file for Color class.
openshot::BBox::cx
float cx
x-coordinate of the bounding box center
Definition: TrackedObjectBBox.h:39
Json.h
Header file for JSON class.
openshot::TrackedObjectBBox::stroke_width
Keyframe stroke_width
Thickness of border line.
Definition: TrackedObjectBBox.h:145
TrackedObjectBase.h
Header file for the TrackedObjectBase class.
openshot::TrackedObjectBBox::background_corner
Keyframe background_corner
Radius of rounded corners.
Definition: TrackedObjectBBox.h:144
Exceptions.h
Header file for all Exception classes.
openshot::TrackedObjectBBox::background_alpha
Keyframe background_alpha
Background box opacity.
Definition: TrackedObjectBBox.h:143
openshot::TrackedObjectBBox::GetLength
int64_t GetLength() const
Get the size of BoxVec map.
Definition: TrackedObjectBBox.cpp:72