Skip to content

Commit a9e34a9

Browse files
authored
Merge pull request #950 from OpenShot/invalid-tracker-region
Improved Tracker & Object Detector Effects (Faster Drawing, Corner Radius, New Style)
2 parents 284904d + 5a0a6a6 commit a9e34a9

File tree

12 files changed

+398
-679
lines changed

12 files changed

+398
-679
lines changed

src/Clip.cpp

Lines changed: 143 additions & 224 deletions
Large diffs are not rendered by default.

src/Clip.h

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ namespace openshot {
131131
void apply_background(std::shared_ptr<openshot::Frame> frame, std::shared_ptr<openshot::Frame> background_frame);
132132

133133
/// Apply effects to the source frame (if any)
134-
void apply_effects(std::shared_ptr<openshot::Frame> frame, std::shared_ptr<openshot::Frame> background_frame, TimelineInfoStruct* options, bool before_keyframes);
134+
void apply_effects(std::shared_ptr<openshot::Frame> frame, int64_t timeline_frame_number, TimelineInfoStruct* options, bool before_keyframes);
135135

136136
/// Apply keyframes to an openshot::Frame and use an existing background frame (if any)
137-
void apply_keyframes(std::shared_ptr<Frame> frame, std::shared_ptr<Frame> background_frame);
137+
void apply_keyframes(std::shared_ptr<Frame> frame, QSize timeline_size);
138138

139139
/// Apply waveform image to an openshot::Frame and use an existing background frame (if any)
140-
void apply_waveform(std::shared_ptr<Frame> frame, std::shared_ptr<Frame> background_frame);
140+
void apply_waveform(std::shared_ptr<Frame> frame, QSize timeline_size);
141141

142142
/// Adjust frame number for Clip position and start (which can result in a different number)
143143
int64_t adjust_timeline_framenumber(int64_t clip_frame_number);
@@ -154,15 +154,14 @@ namespace openshot {
154154
/// Adjust the audio and image of a time mapped frame
155155
void apply_timemapping(std::shared_ptr<openshot::Frame> frame);
156156

157-
/// Compare 2 floating point numbers
158-
bool isEqual(double a, double b);
157+
/// Compare 2 floating point numbers and return true if they are extremely close
158+
bool isNear(double a, double b);
159159

160160
/// Sort effects by order
161161
void sort_effects();
162162

163-
/// Reverse an audio buffer
164-
void reverse_buffer(juce::AudioBuffer<float>* buffer);
165-
163+
/// Scale a source size to a target size (given a specific scale-type)
164+
QSize scale_size(QSize source_size, ScaleType source_scale, int target_width, int target_height);
166165

167166
public:
168167
openshot::GravityType gravity; ///< The gravity of a clip determines where it snaps to its parent
@@ -224,6 +223,12 @@ namespace openshot {
224223
/// Close the internal reader
225224
void Close() override;
226225

226+
/// Return the associated ParentClip (if any)
227+
openshot::Clip* GetParentClip();
228+
229+
/// Return the associated Parent Tracked Object (if any)
230+
std::shared_ptr<openshot::TrackedObjectBase> GetParentTrackedObject();
231+
227232
/// Return the list of effects on the timeline
228233
std::list<openshot::EffectBase*> Effects() { return effects; };
229234

src/KeyFrame.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -374,18 +374,25 @@ void Keyframe::SetJsonValue(const Json::Value root) {
374374
Points.clear();
375375
Points.shrink_to_fit();
376376

377-
if (!root["Points"].isNull())
378-
// loop through points
379-
for (const auto existing_point : root["Points"]) {
380-
// Create Point
381-
Point p;
382-
383-
// Load Json into Point
384-
p.SetJsonValue(existing_point);
385-
386-
// Add Point to Keyframe
387-
AddPoint(p);
388-
}
377+
if (root.isObject() && !root["Points"].isNull()) {
378+
// loop through points in JSON Object
379+
for (const auto existing_point : root["Points"]) {
380+
// Create Point
381+
Point p;
382+
383+
// Load Json into Point
384+
p.SetJsonValue(existing_point);
385+
386+
// Add Point to Keyframe
387+
AddPoint(p);
388+
}
389+
} else if (root.isNumeric()) {
390+
// Create Point from Numeric value
391+
Point p(root.asFloat());
392+
393+
// Add Point to Keyframe
394+
AddPoint(p);
395+
}
389396
}
390397

391398
// Get the change in Y value (from the previous Y value)

src/TrackedObjectBBox.cpp

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,19 @@ using namespace openshot;
2626

2727
// Default Constructor, delegating
2828
TrackedObjectBBox::TrackedObjectBBox()
29-
: TrackedObjectBBox::TrackedObjectBBox(0, 0, 255, 0) {}
29+
: TrackedObjectBBox::TrackedObjectBBox(0, 0, 255, 255) {}
3030

3131
// Constructor that takes RGBA values for stroke, and sets the bounding-box
3232
// displacement as 0 and the scales as 1 for the first frame
3333
TrackedObjectBBox::TrackedObjectBBox(int Red, int Green, int Blue, int Alfa)
3434
: delta_x(0.0), delta_y(0.0),
3535
scale_x(1.0), scale_y(1.0), rotation(0.0),
36-
background_alpha(1.0), background_corner(0),
37-
stroke_width(2) , stroke_alpha(0.0),
36+
background_alpha(0.0), background_corner(12),
37+
stroke_width(2) , stroke_alpha(0.7),
3838
stroke(Red, Green, Blue, Alfa),
39-
background(0, 0, 255, 0)
39+
background(0, 0, 255, Alfa)
4040
{
4141
this->TimeScale = 1.0;
42-
return;
4342
}
4443

4544
// Add a BBox to the BoxVec map
@@ -315,7 +314,6 @@ Json::Value TrackedObjectBBox::JsonValue() const
315314
root["BaseFPS"]["num"] = BaseFps.num;
316315
root["BaseFPS"]["den"] = BaseFps.den;
317316
root["TimeScale"] = TimeScale;
318-
root["child_clip_id"] = ChildClipId();
319317

320318
// Keyframe's properties
321319
root["delta_x"] = delta_x.JsonValue();
@@ -380,11 +378,6 @@ void TrackedObjectBBox::SetJsonValue(const Json::Value root)
380378
if (!root["protobuf_data_path"].isNull())
381379
protobufDataPath = root["protobuf_data_path"].asString();
382380

383-
// Set the id of the child clip
384-
if (!root["child_clip_id"].isNull() && root["child_clip_id"].asString() != Id()){
385-
ChildClipId(root["child_clip_id"].asString());
386-
}
387-
388381
// Set the Keyframes by the given JSON object
389382
if (!root["delta_x"].isNull())
390383
delta_x.SetJsonValue(root["delta_x"]);
@@ -426,26 +419,23 @@ Json::Value TrackedObjectBBox::PropertiesJSON(int64_t requested_frame) const
426419
// Add the ID of this object to the JSON object
427420
root["box_id"] = add_property_json("Box ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
428421

429-
// Add the ID of this object's child clip to the JSON object
430-
root["child_clip_id"] = add_property_json("Child Clip ID", 0.0, "string", ChildClipId(), NULL, -1, -1, false, requested_frame);
431-
432422
// Add the data of given frame bounding-box to the JSON object
433-
root["x1"] = add_property_json("X1", box.cx-(box.width/2), "float", "", NULL, 0.0, 1.0, false, requested_frame);
434-
root["y1"] = add_property_json("Y1", box.cy-(box.height/2), "float", "", NULL, 0.0, 1.0, false, requested_frame);
435-
root["x2"] = add_property_json("X2", box.cx+(box.width/2), "float", "", NULL, 0.0, 1.0, false, requested_frame);
436-
root["y2"] = add_property_json("Y2", box.cy+(box.height/2), "float", "", NULL, 0.0, 1.0, false, requested_frame);
423+
root["x1"] = add_property_json("X1", box.cx-(box.width/2), "float", "", NULL, 0.0, 1.0, true, requested_frame);
424+
root["y1"] = add_property_json("Y1", box.cy-(box.height/2), "float", "", NULL, 0.0, 1.0, true, requested_frame);
425+
root["x2"] = add_property_json("X2", box.cx+(box.width/2), "float", "", NULL, 0.0, 1.0, true, requested_frame);
426+
root["y2"] = add_property_json("Y2", box.cy+(box.height/2), "float", "", NULL, 0.0, 1.0, true, requested_frame);
437427

438428
// Add the bounding-box Keyframes to the JSON object
439429
root["delta_x"] = add_property_json("Displacement X-axis", delta_x.GetValue(requested_frame), "float", "", &delta_x, -1.0, 1.0, false, requested_frame);
440430
root["delta_y"] = add_property_json("Displacement Y-axis", delta_y.GetValue(requested_frame), "float", "", &delta_y, -1.0, 1.0, false, requested_frame);
441431
root["scale_x"] = add_property_json("Scale (Width)", scale_x.GetValue(requested_frame), "float", "", &scale_x, 0.0, 1.0, false, requested_frame);
442432
root["scale_y"] = add_property_json("Scale (Height)", scale_y.GetValue(requested_frame), "float", "", &scale_y, 0.0, 1.0, false, requested_frame);
443433
root["rotation"] = add_property_json("Rotation", rotation.GetValue(requested_frame), "float", "", &rotation, 0, 360, false, requested_frame);
444-
root["visible"] = add_property_json("Visible", visible.GetValue(requested_frame), "int", "", &visible, 0, 1, false, requested_frame);
434+
root["visible"] = add_property_json("Visible", visible.GetValue(requested_frame), "int", "", &visible, 0, 1, true, requested_frame);
445435

446-
root["draw_box"] = add_property_json("Draw Box", draw_box.GetValue(requested_frame), "int", "", &draw_box, -1, 1.0, false, requested_frame);
447-
root["draw_box"]["choices"].append(add_property_choice_json("Off", 0, draw_box.GetValue(requested_frame)));
448-
root["draw_box"]["choices"].append(add_property_choice_json("On", 1, draw_box.GetValue(requested_frame)));
436+
root["draw_box"] = add_property_json("Draw Box", draw_box.GetValue(requested_frame), "int", "", &draw_box, 0, 1, false, requested_frame);
437+
root["draw_box"]["choices"].append(add_property_choice_json("Yes", true, draw_box.GetValue(requested_frame)));
438+
root["draw_box"]["choices"].append(add_property_choice_json("No", false, draw_box.GetValue(requested_frame)));
449439

450440
root["stroke"] = add_property_json("Border", 0.0, "color", "", NULL, 0, 255, false, requested_frame);
451441
root["stroke"]["red"] = add_property_json("Red", stroke.red.GetValue(requested_frame), "float", "", &stroke.red, 0, 255, false, requested_frame);
@@ -455,7 +445,7 @@ Json::Value TrackedObjectBBox::PropertiesJSON(int64_t requested_frame) const
455445
root["stroke_alpha"] = add_property_json("Stroke alpha", stroke_alpha.GetValue(requested_frame), "float", "", &stroke_alpha, 0.0, 1.0, false, requested_frame);
456446

457447
root["background_alpha"] = add_property_json("Background Alpha", background_alpha.GetValue(requested_frame), "float", "", &background_alpha, 0.0, 1.0, false, requested_frame);
458-
root["background_corner"] = add_property_json("Background Corner Radius", background_corner.GetValue(requested_frame), "int", "", &background_corner, 0.0, 60.0, false, requested_frame);
448+
root["background_corner"] = add_property_json("Background Corner Radius", background_corner.GetValue(requested_frame), "int", "", &background_corner, 0.0, 150.0, false, requested_frame);
459449

460450
root["background"] = add_property_json("Background", 0.0, "color", "", NULL, 0, 255, false, requested_frame);
461451
root["background"]["red"] = add_property_json("Red", background.red.GetValue(requested_frame), "float", "", &background.red, 0, 255, false, requested_frame);
@@ -530,36 +520,3 @@ std::map<std::string, float> TrackedObjectBBox::GetBoxValues(int64_t frame_numbe
530520

531521
return boxValues;
532522
}
533-
534-
// Return a map that contains the properties of this object's parent clip
535-
std::map<std::string, float> TrackedObjectBBox::GetParentClipProperties(int64_t frame_number) const {
536-
537-
// Get the parent clip of this object as a Clip pointer
538-
Clip* parentClip = (Clip *) ParentClip();
539-
540-
// Calculate parentClip's frame number
541-
long parentClip_start_position = round( parentClip->Position() * parentClip->info.fps.ToDouble() ) + 1;
542-
long parentClip_start_frame = ( parentClip->Start() * parentClip->info.fps.ToDouble() ) + 1;
543-
float parentClip_frame_number = round(frame_number - parentClip_start_position) + parentClip_start_frame;
544-
545-
// Get parentClip's Keyframes
546-
float parentClip_location_x = parentClip->location_x.GetValue(parentClip_frame_number);
547-
float parentClip_location_y = parentClip->location_y.GetValue(parentClip_frame_number);
548-
float parentClip_scale_x = parentClip->scale_x.GetValue(parentClip_frame_number);
549-
float parentClip_scale_y = parentClip->scale_y.GetValue(parentClip_frame_number);
550-
float parentClip_rotation = parentClip->rotation.GetValue(parentClip_frame_number);
551-
552-
// Initialize the parent clip properties map
553-
std::map<std::string, float> parentClipProperties;
554-
555-
// Set the map properties
556-
parentClipProperties["frame_number"] = parentClip_frame_number;
557-
parentClipProperties["timeline_frame_number"] = frame_number;
558-
parentClipProperties["location_x"] = parentClip_location_x;
559-
parentClipProperties["location_y"] = parentClip_location_y;
560-
parentClipProperties["scale_x"] = parentClip_scale_x;
561-
parentClipProperties["scale_y"] = parentClip_scale_y;
562-
parentClipProperties["rotation"] = parentClip_rotation;
563-
564-
return parentClipProperties;
565-
}

src/TrackedObjectBBox.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,6 @@ namespace openshot
211211

212212
/// Return a map that contains the bounding box properties and it's keyframes indexed by their names
213213
std::map<std::string, float> GetBoxValues(int64_t frame_number) const override;
214-
/// Return a map that contains the properties of this object's parent clip
215-
std::map<std::string, float> GetParentClipProperties(int64_t frame_number) const override;
216-
217214
};
218215
} // namespace openshot
219216

src/TrackedObjectBase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace openshot
2323

2424
// Constructor
2525
TrackedObjectBase::TrackedObjectBase(std::string _id)
26-
: visible(1.0), draw_box(1), id(_id), childClipId("") {}
26+
: visible(1.0), draw_box(1), id(_id) {}
2727

2828
Json::Value TrackedObjectBase::add_property_choice_json(
2929
std::string name, int value, int selected_value) const

src/TrackedObjectBase.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ namespace openshot {
3535
class TrackedObjectBase {
3636
protected:
3737
std::string id;
38-
std::string childClipId;
39-
4038
ClipBase* parentClip;
4139

4240
public:
4341

42+
/// Keyframe to track if a box is visible in the current frame (read-only)
4443
Keyframe visible;
44+
45+
/// Keyframe to determine if a specific box is drawn (or hidden)
4546
Keyframe draw_box;
4647

4748
/// Default constructor
@@ -60,9 +61,6 @@ namespace openshot {
6061
/// Get and set the parentClip of this object
6162
ClipBase* ParentClip() const { return parentClip; }
6263
void ParentClip(ClipBase* clip) { parentClip = clip; }
63-
/// Get and set the Id of the childClip of this object
64-
std::string ChildClipId() const { return childClipId; };
65-
void ChildClipId(std::string _childClipId) { childClipId = _childClipId; };
6664

6765
/// Check if there is data for the exact frame number
6866
virtual bool ExactlyContains(int64_t frame_number) const { return {}; };
@@ -71,8 +69,6 @@ namespace openshot {
7169
virtual void ScalePoints(double scale) { return; };
7270
/// Return the main properties of a TrackedObjectBBox instance - such as position, size and rotation
7371
virtual std::map<std::string, float> GetBoxValues(int64_t frame_number) const { std::map<std::string, float> ret; return ret; };
74-
/// Return the main properties of the tracked object's parent clip - such as position, size and rotation
75-
virtual std::map<std::string, float> GetParentClipProperties(int64_t frame_number) const { std::map<std::string, float> ret; return ret; }
7672
/// Add a bounding box to the tracked object's BoxVec map
7773
virtual void AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height, float _angle) { return; };
7874

0 commit comments

Comments
 (0)