From 9113153be845f1bfbb2274971dba65ec00f5823d Mon Sep 17 00:00:00 2001 From: Brenno Date: Thu, 12 Nov 2020 21:35:47 -0300 Subject: [PATCH] Added properties to Tracker effects (still needs some fine-tuning) --- src/KeyFrameBBox.cpp | 34 ++++++++++++++--------- src/KeyFrameBBox.h | 26 +++++++++--------- src/effects/Tracker.cpp | 59 +++++++++++++++++++++++++++++----------- src/effects/Tracker.h | 10 +++++-- tests/KeyFrame_Tests.cpp | 14 +++++----- 5 files changed, 92 insertions(+), 51 deletions(-) diff --git a/src/KeyFrameBBox.cpp b/src/KeyFrameBBox.cpp index ddb5c3fc..9c7c4ea0 100644 --- a/src/KeyFrameBBox.cpp +++ b/src/KeyFrameBBox.cpp @@ -114,12 +114,19 @@ namespace { } } - +/* KeyFrameBBox::KeyFrameBBox(): delta_x(0.0), delta_y(0.0), scale_x(0.0), scale_y(0.0), rotation(0.0) { this->TimeScale = 1.0; return; } +*/ + +KeyFrameBBox::KeyFrameBBox(){ + this->TimeScale = 1.0; + return; +} +/* void KeyFrameBBox::AddDisplacement(int64_t frame_num, double _delta_x, double _delta_y){ if (!this->Contains((int64_t) frame_num)) return; @@ -156,7 +163,7 @@ void KeyFrameBBox::AddRotation(int64_t _frame_num, double rot){ rotation.AddPoint(time, rot, openshot::InterpolationType::LINEAR); } - +*/ void KeyFrameBBox::AddBox(int64_t _frame_num , float _cx, float _cy, float _width, float _height){ if (_frame_num < 0) @@ -202,12 +209,12 @@ void KeyFrameBBox::RemovePoint(int64_t frame_number){ if (it != BoxVec.end()){ BoxVec.erase(time); - RemoveDelta(time); - RemoveScale(time); + //RemoveDelta(time); + //RemoveScale(time); } return; } - +/* void KeyFrameBBox::RemoveDelta(int64_t frame_number) { double time = this->FrameNToTime(frame_number, 1.0); @@ -276,6 +283,7 @@ void KeyFrameBBox::RemoveScale(int64_t frame_number) { return; } +*/ BBox KeyFrameBBox::GetValue(int64_t frame_number){ double time = this->FrameNToTime(frame_number, this->TimeScale); @@ -288,11 +296,11 @@ BBox KeyFrameBBox::GetValue(int64_t frame_number){ if ((it->first == time) || (it == BoxVec.begin())){ BBox res = it->second; - res.cx += this->delta_x.GetValue(it->first); + /*res.cx += this->delta_x.GetValue(it->first); res.cy += this->delta_y.GetValue(it->first); res.height += this->scale_y.GetValue(it->first); res.width += this->scale_x.GetValue(it->first); - + */ return res; } @@ -302,12 +310,12 @@ BBox KeyFrameBBox::GetValue(int64_t frame_number){ BBox res = InterpolateBoxes(prev(it, 1)->first, it->first, first_ref, second_ref, time); /*later add rotation transform to these points*/ - + /* res.cx += this->delta_x.GetValue(time); res.cy += this->delta_y.GetValue(time); res.height += this->scale_y.GetValue(time); res.width += this->scale_x.GetValue(time); - + */ return res; @@ -388,13 +396,13 @@ Json::Value KeyFrameBBox::JsonValue() { root["boxes"].append(elem); } - +/* root["delta_x"] = delta_x.JsonValue(); root["delta_y"] = delta_y.JsonValue(); root["scale_x"] = scale_x.JsonValue(); root["scale_y"] = scale_y.JsonValue(); root["rotation"] = rotation.JsonValue(); - +*/ // return JsonValue return root; } @@ -428,13 +436,13 @@ void KeyFrameBBox::clear(){ void KeyFrameBBox::SetJsonValue(const Json::Value root) { // Clear existing points BoxVec.clear(); - +/* delta_x.SetJsonValue(root["delta_x"]); delta_y.SetJsonValue(root["delta_y"]); scale_x.SetJsonValue(root["scale_x"]); scale_y.SetJsonValue(root["scale_y"]); rotation.SetJsonValue(root["rotation"]); - +*/ if (!root["BaseFPS"].isNull() && root["BaseFPS"].isObject()) { if (!root["BaseFPS"]["num"].isNull()) diff --git a/src/KeyFrameBBox.h b/src/KeyFrameBBox.h index a80c7d7d..80349286 100644 --- a/src/KeyFrameBBox.h +++ b/src/KeyFrameBBox.h @@ -138,18 +138,18 @@ namespace openshot { double TimeScale; std::map BoxVec; public: - Keyframe delta_x; - Keyframe delta_y; - Keyframe scale_x; - Keyframe scale_y; - Keyframe rotation; + //Keyframe delta_x; + //Keyframe delta_y; + //Keyframe scale_x; + //Keyframe scale_y; + //Keyframe rotation; KeyFrameBBox(); - void AddDisplacement(int64_t _frame_num, double _delta_x, double _delta_y); - void AddScale(int64_t _frame_num, double _delta_x, double _delta_y); + //void AddDisplacement(int64_t _frame_num, double _delta_x, double _delta_y); + //void AddScale(int64_t _frame_num, double _delta_x, double _delta_y); void AddBox(int64_t _frame_num, float _cx, float _cy, float _width, float _height); - void AddRotation(int64_t _frame_num, double rot); + //void AddRotation(int64_t _frame_num, double rot); void SetBaseFPS(Fraction fps); Fraction GetBaseFPS(); @@ -162,19 +162,19 @@ namespace openshot { /// Remove a points by frame_number void RemovePoint(int64_t frame_number); - void RemoveDelta(int64_t frame_number); - void RemoveScale(int64_t frame_number); - void RemoveRotation(int64_t frame_number); + //void RemoveDelta(int64_t frame_number); + //void RemoveScale(int64_t frame_number); + //void RemoveRotation(int64_t frame_number); BBox GetValue(int64_t frame_number); /// Print collection of points - void PrintParams(); + //void PrintParams(); double FrameNToTime(int64_t frame_number, double time_scale); BBox InterpolateBoxes(double t1, double t2, BBox left, BBox right, double target); - /// Get and Set JSON methods + /// Get and Set JSON methods std::string Json(); ///< Generate JSON string of this object Json::Value JsonValue(); ///< Generate Json::Value for this object void SetJson(const std::string value); ///< Load JSON string into this object diff --git a/src/effects/Tracker.cpp b/src/effects/Tracker.cpp index 42ed3ea2..138ff3ac 100644 --- a/src/effects/Tracker.cpp +++ b/src/effects/Tracker.cpp @@ -33,29 +33,23 @@ using namespace openshot; /// Blank constructor, useful when using Json to load the effect properties -Tracker::Tracker(std::string clipTrackerDataPath) +Tracker::Tracker(std::string clipTrackerDataPath): delta_x(0.0), delta_y(0.0), scale_x(0.0), scale_y(0.0), rotation(0.0) { // Init effect properties init_effect_details(); -/* - this->trackedData.SetBaseFPS(fps); - this->trackedData.SetOpFPS(fps); -*/ // Tries to load the tracker data from protobuf LoadTrackedData(clipTrackerDataPath); } // Default constructor -Tracker::Tracker() +Tracker::Tracker(): delta_x(0.0), delta_y(0.0), scale_x(0.0), scale_y(0.0), rotation(0.0) { // Init effect properties init_effect_details(); -/* - this->trackedData.SetBaseFPS(fps); - this->trackedData.SetOpFPS(fps); -*/ + } + // Init effect settings void Tracker::init_effect_details() { @@ -89,15 +83,21 @@ std::shared_ptr Tracker::GetFrame(std::shared_ptr frame, int64_t f float fw = frame_image.size().width; float fh = frame_image.size().height; + double scale_x = this->scale_x.GetValue(frame_number); + double scale_y = this->scale_y.GetValue(frame_number); + double delta_x = this->delta_x.GetValue(frame_number); + double delta_y = this->delta_y.GetValue(frame_number); + + // Draw box on image //EffectFrameData fd = trackedDataById[frame_number]; BBox fd = this->trackedData.GetValue(frame_number); - - cv::Rect2d box((int)(fd.cx*fw), - (int)(fd.cy*fh), - (int)(fd.width*fw), - (int)(fd.height*fh)); + + cv::Rect2d box((int)( (fd.cx + delta_x) * fw ), + (int)( (fd.cy + delta_y) * fh ), + (int)( (fd.width + scale_x) * fw), + (int)( (fd.height + scale_y) * fh) ); cv::rectangle(frame_image, box, cv::Scalar( 255, 0, 0 ), 2, 1 ); } } @@ -147,7 +147,7 @@ bool Tracker::LoadTrackedData(std::string inputFilePath){ //trackedDataById[id] = EffectFrameData(id, rotation, x1, y1, x2, y2); if ((x1 >= 0.0) && (y1 >= 0.0) && (x2 >= 0.0) && (y2 >= 0.0)){ trackedData.AddBox(id, x1, y1, (x2-x1), (y2-y1)); - trackedData.AddRotation(id, rotation); + //trackedData.AddRotation(id, rotation); } } @@ -184,6 +184,13 @@ Json::Value Tracker::JsonValue() const { root["BaseFPS"]["num"] = BaseFPS.num; root["BaseFPS"]["den"] = BaseFPS.den; root["TimeScale"] = this->TimeScale; + + root["delta_x"] = delta_x.JsonValue(); + root["delta_y"] = delta_y.JsonValue(); + root["scale_x"] = scale_x.JsonValue(); + root["scale_y"] = scale_y.JsonValue(); + root["rotation"] = rotation.JsonValue(); + // return JsonValue return root; } @@ -234,6 +241,19 @@ void Tracker::SetJsonValue(const Json::Value root) { protobuf_data_path = ""; } } + + if (!root["delta_x"].isNull()) + delta_x.SetJsonValue(root["delta_x"]); + if (!root["delta_y"].isNull()) + delta_y.SetJsonValue(root["delta_y"]); + if (!root["scale_x"].isNull()) + scale_x.SetJsonValue(root["scale_x"]); + if (!root["scale_y"].isNull()) + scale_y.SetJsonValue(root["scale_y"]); + if (!root["rotation"].isNull()) + rotation.SetJsonValue(root["rotation"]); + + } // Get all properties for a specific frame @@ -248,6 +268,13 @@ std::string Tracker::PropertiesJSON(int64_t requested_frame) const { root["end"] = add_property_json("End", End(), "float", "", NULL, 0, 1000 * 60 * 30, false, requested_frame); root["duration"] = add_property_json("Duration", Duration(), "float", "", NULL, 0, 1000 * 60 * 30, true, requested_frame); + // Keyframes + root["delta_x"] = add_property_json("Displacement X-axis", delta_x.GetValue(requested_frame), "float", "", &delta_x, -1.0, 1.0, false, requested_frame); + root["delta_y"] = add_property_json("Displacement Y-axis", delta_y.GetValue(requested_frame), "float", "", &delta_y, -1.0, 1.0, false, requested_frame); + root["scale_x"] = add_property_json("Scale (Width)", scale_x.GetValue(requested_frame), "float", "", &scale_x, -1.0, 1.0, false, requested_frame); + root["scale_y"] = add_property_json("Scale (Height)", scale_y.GetValue(requested_frame), "float", "", &scale_y, -1.0, 1.0, false, requested_frame); + root["rotation"] = add_property_json("Rotation", rotation.GetValue(requested_frame), "float", "", &rotation, 0, 360, false, requested_frame); + // Return formatted string return root.toStyledString(); } diff --git a/src/effects/Tracker.h b/src/effects/Tracker.h index 391e8934..34408a7a 100644 --- a/src/effects/Tracker.h +++ b/src/effects/Tracker.h @@ -93,9 +93,15 @@ namespace openshot std::string protobuf_data_path; Fraction BaseFPS; double TimeScale; - public: - std::map trackedDataById; // Save object tracking box data + public: + Keyframe delta_x; + Keyframe delta_y; + Keyframe scale_x; + Keyframe scale_y; + Keyframe rotation; + + std::map trackedDataById; // Save object tracking box data KeyFrameBBox trackedData; diff --git a/tests/KeyFrame_Tests.cpp b/tests/KeyFrame_Tests.cpp index dcdab721..56192b1d 100644 --- a/tests/KeyFrame_Tests.cpp +++ b/tests/KeyFrame_Tests.cpp @@ -510,7 +510,7 @@ TEST(KeyFrameBBox_addBox_test) { CHECK_EQUAL(true, kfb.Contains(1)); CHECK_EQUAL(1, kfb.GetLength()); - kfb.PrintParams(); + //kfb.PrintParams(); kfb.RemovePoint(1); @@ -524,15 +524,15 @@ TEST(KeyFrameBBox_GetVal_test) { kfb.AddBox(1, 10.0, 10.0, 100.0, 100.0); - kfb.AddDisplacement(1, 20.0, 20.0); - kfb.AddScale(1, 30, 30); + //kfb.AddDisplacement(1, 20.0, 20.0); + //kfb.AddScale(1, 30, 30); BBox val = kfb.GetValue(1); - CHECK_EQUAL(30.0, val.cx); - CHECK_EQUAL(30.0, val.cy); - CHECK_EQUAL(130.0,val.width); - CHECK_EQUAL(130.0, val.height); + CHECK_EQUAL(10.0, val.cx); + CHECK_EQUAL(10.0, val.cy); + CHECK_EQUAL(100.0,val.width); + CHECK_EQUAL(100.0, val.height); }