From 17463313fc4cca0210422feae49dc0b2ad9f6f00 Mon Sep 17 00:00:00 2001 From: Brenno Date: Fri, 22 Jan 2021 19:00:44 -0300 Subject: [PATCH] Clip.cpp: Removed hard-coded Tracker effect check in AddEffect function Added "has_tracked_object" property on EffectBase so that the AddEffect function checks for this property instead of the Tracker effect name - this way it's possible to support other effects that have tracked objects but different names. --- src/Clip.cpp | 21 +++++++++++---------- src/EffectBase.cpp | 1 + src/EffectBase.h | 5 +++++ src/TrackedObjectBBox.h | 2 +- src/TrackedObjectBase.h | 3 +++ src/effects/ObjectDetection.cpp | 1 + src/effects/Tracker.cpp | 19 +++++++++++++++++-- 7 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/Clip.cpp b/src/Clip.cpp index 3288929ef..b1ea5c315 100644 --- a/src/Clip.cpp +++ b/src/Clip.cpp @@ -1170,24 +1170,25 @@ void Clip::AddEffect(EffectBase* effect) sort_effects(); // Add Tracker to Timeline - if (effect->info.class_name == "Tracker"){ + if (effect->info.has_tracked_object){ Timeline* parentTimeline = (Timeline *) ParentTimeline(); // Check if this clip has a parent timeline if (parentTimeline){ - // Downcast effect as Tracker - Tracker* tracker = (Tracker *) effect; - - // Get tracked data from the Tracker effect - std::shared_ptr trackedData = tracker->trackedData; + // Iterate through effect's vector of Tracked Objects + for (auto const& trackedObject : effect->trackedObjects){ + + // Cast the Tracked Object as TrackedObjectBBox + std::shared_ptr trackedObjectBBox = std::static_pointer_cast(trackedObject.second); - // Set tracked data parent clip to this - trackedData->ParentClip(this); + // Set the Tracked Object's parent clip to this + trackedObjectBBox->ParentClip(this); - // Add tracked data to the timeline - parentTimeline->AddTrackedObject(trackedData); + // Add the Tracked Object to the timeline + parentTimeline->AddTrackedObject(trackedObjectBBox); + } } } diff --git a/src/EffectBase.cpp b/src/EffectBase.cpp index 458381ced..aa2bc4e42 100644 --- a/src/EffectBase.cpp +++ b/src/EffectBase.cpp @@ -47,6 +47,7 @@ void EffectBase::InitEffectInfo() info.has_audio = false; info.name = ""; info.description = ""; + info.has_tracked_object = false; } // Display file information diff --git a/src/EffectBase.h b/src/EffectBase.h index 090637e7e..320e840cf 100644 --- a/src/EffectBase.h +++ b/src/EffectBase.h @@ -37,6 +37,7 @@ #include "ClipBase.h" #include "Json.h" #include "Frame.h" +#include "TrackedObjectBase.h" namespace openshot { @@ -54,6 +55,7 @@ namespace openshot std::string description; ///< The description of this effect and what it does bool has_video; ///< Determines if this effect manipulates the image of a frame bool has_audio; ///< Determines if this effect manipulates the audio of a frame + bool has_tracked_object; ///< Determines if this effect track objects through the clip }; /** @@ -73,6 +75,9 @@ namespace openshot public: + /// Map of Tracked Object's by their indices (used by Effects that track objects on clips) + std::map > trackedObjects; + /// Information about the current effect EffectInfoStruct info; diff --git a/src/TrackedObjectBBox.h b/src/TrackedObjectBBox.h index a990b4fda..4a5a34ada 100644 --- a/src/TrackedObjectBBox.h +++ b/src/TrackedObjectBBox.h @@ -221,7 +221,7 @@ namespace openshot /// 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; + 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; diff --git a/src/TrackedObjectBase.h b/src/TrackedObjectBase.h index 5278f4c0c..683b0b3f1 100644 --- a/src/TrackedObjectBase.h +++ b/src/TrackedObjectBase.h @@ -90,6 +90,9 @@ namespace openshot { virtual void SetJson(const std::string value) = 0; ///< Load JSON string into this object virtual void SetJsonValue(const Json::Value root) = 0; ///< 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) + virtual Json::Value PropertiesJSON(int64_t requested_frame) const = 0; }; } // Namespace openshot diff --git a/src/effects/ObjectDetection.cpp b/src/effects/ObjectDetection.cpp index 7c84e7163..929c9039a 100644 --- a/src/effects/ObjectDetection.cpp +++ b/src/effects/ObjectDetection.cpp @@ -64,6 +64,7 @@ void ObjectDetection::init_effect_details() info.description = "Detect objects through the video."; info.has_audio = false; info.has_video = true; + info.has_tracked_object = false; } // This method is required for all derived classes of EffectBase, and returns a diff --git a/src/effects/Tracker.cpp b/src/effects/Tracker.cpp index 651d9fca7..260909bf9 100644 --- a/src/effects/Tracker.cpp +++ b/src/effects/Tracker.cpp @@ -48,6 +48,8 @@ Tracker::Tracker(std::string clipTrackerDataPath) trackedData->LoadBoxData(clipTrackerDataPath); ClipBase* parentClip = this->ParentClip(); trackedData->ParentClip(parentClip); + // Insert TrackedObject with index 0 to the trackedObjects map + trackedObjects.insert({0, trackedData}); } // Default constructor @@ -60,6 +62,8 @@ Tracker::Tracker() trackedData = std::make_shared(trackedDataObject); ClipBase* parentClip = this->ParentClip(); trackedData->ParentClip(parentClip); + // Insert TrackedObject with index 0 to the trackedObjects map + trackedObjects.insert({0, trackedData}); } @@ -75,6 +79,7 @@ void Tracker::init_effect_details() info.description = "Track the selected bounding box through the video."; info.has_audio = false; info.has_video = true; + info.has_tracked_object = true; this->TimeScale = 1.0; } @@ -207,7 +212,9 @@ void Tracker::SetJsonValue(const Json::Value root) { } } - trackedData->SetJsonValue(root); + for (auto const& trackedObject : trackedObjects){ + trackedObject.second->SetJsonValue(root); + } return; } @@ -218,7 +225,15 @@ std::string Tracker::PropertiesJSON(int64_t requested_frame) const { // Generate JSON properties list Json::Value root; - root = trackedData->PropertiesJSON(requested_frame); + + // Add trackedObjects properties to JSON + for (auto const& trackedObject : trackedObjects){ + Json::Value trackedObjectJSON = trackedObject.second->PropertiesJSON(requested_frame); + // Save the trackedData properties on root + for (const auto& key : trackedObjectJSON.getMemberNames()){ + root[key] = trackedObjectJSON[key]; + } + } // Append effect's properties root["name"] = add_property_json("Tracker", 0.0, "string", "", NULL, -1, -1, true, requested_frame);