OpenShot Library | libopenshot  0.7.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 {
28  {
29  int width = 0;
30  int height = 0;
31  std::vector<uint32_t> rle;
32 
33  bool HasData() const { return width > 0 && height > 0 && !rle.empty(); }
34  };
35 
46  struct BBox
47  {
48  float cx = -1;
49  float cy = -1;
50  float width = -1;
51  float height = -1;
52  float angle = -1;
53 
55  BBox() {}
56 
63  BBox(float _cx, float _cy, float _width, float _height, float _angle)
64  {
65  cx = _cx;
66  cy = _cy;
67  width = _width;
68  height = _height;
69  angle = _angle;
70  }
71 
72 
74  std::string Json() const
75  {
76  return JsonValue().toStyledString();
77  }
78 
80  Json::Value JsonValue() const
81  {
82  Json::Value root;
83  root["cx"] = cx;
84  root["cy"] = cy;
85  root["width"] = width;
86  root["height"] = height;
87  root["angle"] = angle;
88 
89  return root;
90  }
91 
93  void SetJson(const std::string value)
94  {
95  // Parse JSON string into JSON objects
96  try
97  {
98  const Json::Value root = openshot::stringToJson(value);
99  // Set all values that match
100  SetJsonValue(root);
101  }
102  catch (const std::exception &e)
103  {
104  // Error parsing JSON (or missing keys)
105  throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
106  }
107  }
108 
110  void SetJsonValue(const Json::Value root)
111  {
112 
113  // Set data from Json (if key is found)
114  if (!root["cx"].isNull())
115  cx = root["cx"].asDouble();
116  if (!root["cy"].isNull())
117  cy = root["cy"].asDouble();
118  if (!root["width"].isNull())
119  width = root["width"].asDouble();
120  if (!root["height"].isNull())
121  height = root["height"].asDouble();
122  if (!root["angle"].isNull())
123  angle = root["angle"].asDouble();
124  }
125  };
126 
140  {
141  private:
142  Fraction BaseFps;
143  double TimeScale;
144 
145  public:
146  std::map<double, BBox> BoxVec;
147  std::map<double, ObjectMaskData> MaskVec;
160 
161  std::string protobufDataPath;
162 
165  TrackedObjectBBox(int Red, int Green, int Blue, int Alfa);
166 
168  void AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle) override;
169  void AddMask(int64_t frame_num, const ObjectMaskData& mask);
170  bool HasMask(int64_t frame_number, int64_t max_frame_gap = 0) const;
171  bool HasMaskData() const;
172  ObjectMaskData GetMask(int64_t frame_number, int64_t max_frame_gap = 0) const;
173 
175  void SetBaseFPS(Fraction fps);
176 
179 
181  void ScalePoints(double scale) override;
182 
184  bool Contains(int64_t frame_number) const;
186  bool ExactlyContains(int64_t frame_number) const override;
187 
189  int64_t GetLength() const;
190 
192  void RemoveBox(int64_t frame_number);
193 
195  BBox GetBox(int64_t frame_number);
197  BBox GetBox(int64_t frame_number) const
198  {
199  return const_cast<TrackedObjectBBox *>(this)->GetBox(frame_number);
200  }
201 
203  double ScaledStrokeWidth(int64_t frame_number, int image_width, int image_height) const;
204 
206  bool LoadBoxData(std::string inputFilePath);
207 
209  double FrameNToTime(int64_t frame_number, double time_scale) const;
210 
212  BBox InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target);
213 
215  void clear();
216 
218  std::string Json() const override;
219  Json::Value JsonValue() const override;
220  void SetJson(const std::string value) override;
221  void SetJsonValue(const Json::Value root) override;
222 
225  Json::Value PropertiesJSON(int64_t requested_frame) const override;
226 
227  // Generate JSON for a property
228  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;
229 
231  std::map<std::string, float> GetBoxValues(int64_t frame_number) const override;
232  };
233 } // namespace openshot
234 
235 #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:598
openshot::TrackedObjectBBox::stroke_alpha
Keyframe stroke_alpha
Stroke box opacity.
Definition: TrackedObjectBBox.h:156
openshot::ObjectMaskData::height
int height
Definition: TrackedObjectBBox.h:30
openshot::ObjectMaskData::width
int width
Definition: TrackedObjectBBox.h:29
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:137
openshot::TrackedObjectBBox::HasMask
bool HasMask(int64_t frame_number, int64_t max_frame_gap=0) const
Definition: TrackedObjectBBox.cpp:172
openshot::BBox::height
float height
bounding box height
Definition: TrackedObjectBBox.h:51
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:218
openshot::TrackedObjectBBox::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition: TrackedObjectBBox.cpp:481
openshot::TrackedObjectBBox::background
Color background
Background fill color.
Definition: TrackedObjectBBox.h:158
openshot::TrackedObjectBBox::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition: TrackedObjectBBox.cpp:535
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AnimatedCurve.h:24
openshot::BBox::JsonValue
Json::Value JsonValue() const
Generate Json::Value for this object.
Definition: TrackedObjectBBox.h:80
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:661
openshot::TrackedObjectBBox::FrameNToTime
double FrameNToTime(int64_t frame_number, double time_scale) const
Get the time of the given frame.
Definition: TrackedObjectBBox.cpp:396
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:49
openshot::TrackedObjectBBox::ScalePoints
void ScalePoints(double scale) override
Update the TimeScale member variable.
Definition: TrackedObjectBBox.cpp:403
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:197
openshot::ObjectMaskData::HasData
bool HasData() const
Definition: TrackedObjectBBox.h:33
openshot::TrackedObjectBBox::Json
std::string Json() const override
Get and Set JSON methods.
Definition: TrackedObjectBBox.cpp:474
openshot::TrackedObjectBBox::GetBaseFPS
Fraction GetBaseFPS()
Return the object's BaseFps.
Definition: TrackedObjectBBox.cpp:391
openshot::BBox::BBox
BBox()
Blank constructor.
Definition: TrackedObjectBBox.h:55
KeyFrame.h
Header file for the Keyframe class.
openshot::TrackedObjectBBox::MaskVec
std::map< double, ObjectMaskData > MaskVec
Index optional object masks by time of each frame.
Definition: TrackedObjectBBox.h:147
openshot::TrackedObjectBBox::scale_y
Keyframe scale_y
Y-direction scale Keyframe.
Definition: TrackedObjectBBox.h:151
openshot::Color
This class represents a color (used on the timeline and clips)
Definition: Color.h:27
openshot::ObjectMaskData
Definition: TrackedObjectBBox.h:27
openshot::BBox::angle
float angle
bounding box rotation angle [degrees]
Definition: TrackedObjectBBox.h:52
openshot::TrackedObjectBBox
This class contains the properties of a tracked object and functions to manipulate it.
Definition: TrackedObjectBBox.h:139
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::AddMask
void AddMask(int64_t frame_num, const ObjectMaskData &mask)
Definition: TrackedObjectBBox.cpp:163
openshot::TrackedObjectBBox::scale_x
Keyframe scale_x
X-direction scale Keyframe.
Definition: TrackedObjectBBox.h:150
openshot::InvalidJSON
Exception for invalid JSON.
Definition: Exceptions.h:223
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:261
openshot::TrackedObjectBBox::TrackedObjectBBox
TrackedObjectBBox()
Default Constructor.
Definition: TrackedObjectBBox.cpp:118
openshot::BBox::width
float width
bounding box width
Definition: TrackedObjectBBox.h:50
openshot::BBox::SetJsonValue
void SetJsonValue(const Json::Value root)
Load Json::Value into this object.
Definition: TrackedObjectBBox.h:110
openshot::TrackedObjectBBox::GetMask
ObjectMaskData GetMask(int64_t frame_number, int64_t max_frame_gap=0) const
Definition: TrackedObjectBBox.cpp:182
openshot::TrackedObjectBBox::InterpolateBoxes
BBox InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target)
Interpolate the bouding-boxes properties.
Definition: TrackedObjectBBox.cpp:351
openshot::TrackedObjectBBox::delta_y
Keyframe delta_y
Y-direction displacement Keyframe.
Definition: TrackedObjectBBox.h:149
openshot::TrackedObjectBBox::RemoveBox
void RemoveBox(int64_t frame_number)
Remove a bounding-box from the BoxVec map.
Definition: TrackedObjectBBox.cpp:246
openshot::TrackedObjectBBox::BoxVec
std::map< double, BBox > BoxVec
Index the bounding-box by time of each frame.
Definition: TrackedObjectBBox.h:146
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:698
openshot::BBox::SetJson
void SetJson(const std::string value)
Load JSON string into this object.
Definition: TrackedObjectBBox.h:93
openshot::TrackedObjectBBox::mask_color
Color mask_color
Object mask overlay color.
Definition: TrackedObjectBBox.h:159
openshot::TrackedObjectBBox::delta_x
Keyframe delta_x
X-direction displacement Keyframe.
Definition: TrackedObjectBBox.h:148
openshot::BBox
This struct holds the information of a bounding-box.
Definition: TrackedObjectBBox.h:46
openshot::TrackedObjectBBox::LoadBoxData
bool LoadBoxData(std::string inputFilePath)
Load the bounding-boxes information from the protobuf file.
Definition: TrackedObjectBBox.cpp:408
openshot::TrackedObjectBBox::clear
void clear()
Clear the BoxVec map.
Definition: TrackedObjectBBox.cpp:467
openshot::TrackedObjectBBox::SetBaseFPS
void SetBaseFPS(Fraction fps)
Update object's BaseFps.
Definition: TrackedObjectBBox.cpp:385
openshot::TrackedObjectBBox::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition: TrackedObjectBBox.cpp:517
openshot::TrackedObjectBBox::stroke
Color stroke
Border line color.
Definition: TrackedObjectBBox.h:157
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:232
openshot::BBox::Json
std::string Json() const
Generate JSON string of this object.
Definition: TrackedObjectBBox.h:74
openshot::TrackedObjectBBox::protobufDataPath
std::string protobufDataPath
Path to the protobuf file that holds the bounding box points across the frames.
Definition: TrackedObjectBBox.h:161
openshot::BBox::BBox
BBox(float _cx, float _cy, float _width, float _height, float _angle)
Definition: TrackedObjectBBox.h:63
Color.h
Header file for Color class.
openshot::TrackedObjectBBox::mask_alpha
Keyframe mask_alpha
Object mask overlay opacity.
Definition: TrackedObjectBBox.h:154
openshot::TrackedObjectBBox::ScaledStrokeWidth
double ScaledStrokeWidth(int64_t frame_number, int image_width, int image_height) const
Return stroke width adjusted for source-to-output raster scaling.
Definition: TrackedObjectBBox.cpp:309
openshot::BBox::cx
float cx
x-coordinate of the bounding box center
Definition: TrackedObjectBBox.h:48
Json.h
Header file for JSON class.
openshot::TrackedObjectBBox::stroke_width
Keyframe stroke_width
Thickness of border line.
Definition: TrackedObjectBBox.h:155
TrackedObjectBase.h
Header file for the TrackedObjectBase class.
openshot::TrackedObjectBBox::background_corner
Keyframe background_corner
Radius of rounded corners.
Definition: TrackedObjectBBox.h:153
openshot::ObjectMaskData::rle
std::vector< uint32_t > rle
Definition: TrackedObjectBBox.h:31
openshot::TrackedObjectBBox::HasMaskData
bool HasMaskData() const
Definition: TrackedObjectBBox.cpp:177
Exceptions.h
Header file for all Exception classes.
openshot::TrackedObjectBBox::background_alpha
Keyframe background_alpha
Background box opacity.
Definition: TrackedObjectBBox.h:152
openshot::TrackedObjectBBox::GetLength
int64_t GetLength() const
Get the size of BoxVec map.
Definition: TrackedObjectBBox.cpp:208