-
Notifications
You must be signed in to change notification settings - Fork 261
/
TrackedObjectBBox.h
239 lines (201 loc) · 8.6 KB
/
TrackedObjectBBox.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
* @file
* @brief Header file for the TrackedObjectBBox class
* @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/>.
*/
#ifndef OPENSHOT_TRACKEDOBJECTBBOX_H
#define OPENSHOT_TRACKEDOBJECTBBOX_H
#include <iostream>
#include <iomanip>
#include <cmath>
#include <assert.h>
#include <vector>
#include "Exceptions.h"
#include "Fraction.h"
#include "Coordinate.h"
#include "Point.h"
#include "Json.h"
#include "KeyFrame.h"
#include "TrackedObjectBase.h"
#include "protobuf_messages/trackerdata.pb.h"
#include <google/protobuf/util/time_util.h>
using google::protobuf::util::TimeUtil;
namespace openshot
{
/**
* @brief This struct holds the information of a bounding-box: a rectangular shape that enclosures an object or a
* desired set of pixels in a digital image.
*
* The bounding-box structure holds four floating-point properties: the x and y coordinates of the rectangle's
* top left corner (x1, y1), the rectangle's width and the rectangle's height.
*/
struct BBox
{
float cx = -1; ///< x-coordinate of the bounding box center
float cy = -1; ///< y-coordinate of the bounding box center
float width = -1; ///< bounding box width
float height = -1; ///< bounding box height
float angle = -1; ///< bounding box rotation angle [degrees]
/// Blank constructor
BBox()
{
return;
}
/// Default constructor, which takes the bounding box top-left corner coordinates, width and height.
/// @param _cx X-coordinate of the bounding box center
/// @param _cy Y-coordinate of the bounding box center
/// @param _width Bounding box width
/// @param _height Bounding box height
/// @param _angle Bounding box rotation angle [degrees]
BBox(float _cx, float _cy, float _width, float _height, float _angle)
{
cx = _cx;
cy = _cy;
width = _width;
height = _height;
angle = _angle;
}
/// Generate JSON string of this object
std::string Json() const
{
return JsonValue().toStyledString();
}
/// Generate Json::Value for this object
Json::Value JsonValue() const
{
Json::Value root;
root["cx"] = cx;
root["cy"] = cy;
root["width"] = width;
root["height"] = height;
root["angle"] = angle;
return root;
}
/// Load JSON string into this object
void SetJson(const std::string value)
{
// 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)");
}
}
/// Load Json::Value into this object
void SetJsonValue(const Json::Value root)
{
// Set data from Json (if key is found)
if (!root["cx"].isNull())
cx = root["cx"].asDouble();
if (!root["cy"].isNull())
cy = root["cy"].asDouble();
if (!root["width"].isNull())
width = root["width"].asDouble();
if (!root["height"].isNull())
height = root["height"].asDouble();
if (!root["angle"].isNull())
angle = root["angle"].asDouble();
}
};
/**
* @brief This class holds the information of a bounding-box (mapped by time) over the frames that contain
* the object enclosured by it.
*
* The bounding-box displacement in X and Y directions and it's width and height variation over the frames
* are set as openshot::Keyframe objects
*
* The bounding-box information over the clip's frames are saved into a protobuf file and loaded into an
* object of this class.
*/
class TrackedObjectBBox : public TrackedObjectBase
{
private:
bool visible;
Fraction BaseFps;
double TimeScale;
public:
std::map<double, BBox> BoxVec; ///< Index the bounding-box by time of each frame
Keyframe delta_x; ///< X-direction displacement Keyframe
Keyframe delta_y; ///< Y-direction displacement Keyframe
Keyframe scale_x; ///< X-direction scale Keyframe
Keyframe scale_y; ///< Y-direction scale Keyframe
Keyframe rotation; ///< Rotation Keyframe
std::string protobufDataPath; ///< Path to the protobuf file that holds the bbox points across the frames
/// Default Constructor
TrackedObjectBBox();
/// Add a BBox to the BoxVec map
void AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle) override;
/// Update object's BaseFps
void SetBaseFPS(Fraction fps);
/// Return the object's BaseFps
Fraction GetBaseFPS();
/// Update the TimeScale member variable
void ScalePoints(double scale) override;
/// Check if there is a bounding-box in the given frame
bool Contains(int64_t frame_number) const;
/// Check if there is a bounding-box in the exact frame number
bool ExactlyContains(int64_t frame_number) const;
/// Get the size of BoxVec map
int64_t GetLength() const;
/// Remove a bounding-box from the BoxVec map
void RemoveBox(int64_t frame_number);
/// Return a bounding-box from BoxVec with it's properties adjusted by the Keyframes
BBox GetBox(int64_t frame_number) const
{
return const_cast<TrackedObjectBBox *>(this)->GetBox(frame_number);
}
BBox GetBox(int64_t frame_number);
/// Load the bounding-boxes information from the protobuf file
bool LoadBoxData(std::string inputFilePath);
/// Get the time of the given frame
double FrameNToTime(int64_t frame_number, double time_scale) const;
/// Interpolate the bouding-boxes properties
BBox InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target);
/// Clear the BoxVec map
void clear();
/// Get and Set JSON methods
std::string Json() const override; ///< Generate JSON string of this object
Json::Value JsonValue() const override; ///< Generate Json::Value for this object
void SetJson(const std::string value) override; ///< Load JSON string into this object
void SetJsonValue(const Json::Value root) override; ///< Load Json::Value into this object
/// Get all properties for a specific frame (perfect for a UI to display the current state
/// of all properties at any time)
Json::Value PropertiesJSON(int64_t requested_frame) const override;
// Generate JSON for a property
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;
/// Return the bounding box properties and it's keyframes indexed by their names
std::map<std::string, float> GetBoxValues(int64_t frame_number) const override;
/// Return properties of this object's parent clip
std::map<std::string, float> GetParentClipProperties(int64_t frame_number) const override;
};
} // namespace openshot
#endif