Skip to content

Commit

Permalink
Clip.cpp: Removed hard-coded Tracker effect check in AddEffect function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BrennoCaldato committed Jan 22, 2021
1 parent a7d2b6a commit 1746331
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 13 deletions.
21 changes: 11 additions & 10 deletions src/Clip.cpp
Expand Up @@ -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<openshot::TrackedObjectBBox> 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> trackedObjectBBox = std::static_pointer_cast<TrackedObjectBBox>(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);
}

}
}
Expand Down
1 change: 1 addition & 0 deletions src/EffectBase.cpp
Expand Up @@ -47,6 +47,7 @@ void EffectBase::InitEffectInfo()
info.has_audio = false;
info.name = "";
info.description = "";
info.has_tracked_object = false;
}

// Display file information
Expand Down
5 changes: 5 additions & 0 deletions src/EffectBase.h
Expand Up @@ -37,6 +37,7 @@
#include "ClipBase.h"
#include "Json.h"
#include "Frame.h"
#include "TrackedObjectBase.h"

namespace openshot
{
Expand All @@ -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
};

/**
Expand All @@ -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<int, std::shared_ptr<TrackedObjectBase> > trackedObjects;

/// Information about the current effect
EffectInfoStruct info;

Expand Down
2 changes: 1 addition & 1 deletion src/TrackedObjectBBox.h
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/TrackedObjectBase.h
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/effects/ObjectDetection.cpp
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/effects/Tracker.cpp
Expand Up @@ -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
Expand All @@ -60,6 +62,8 @@ Tracker::Tracker()
trackedData = std::make_shared<TrackedObjectBBox>(trackedDataObject);
ClipBase* parentClip = this->ParentClip();
trackedData->ParentClip(parentClip);
// Insert TrackedObject with index 0 to the trackedObjects map
trackedObjects.insert({0, trackedData});
}


Expand All @@ -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;
}
Expand Down Expand Up @@ -207,7 +212,9 @@ void Tracker::SetJsonValue(const Json::Value root) {
}
}

trackedData->SetJsonValue(root);
for (auto const& trackedObject : trackedObjects){
trackedObject.second->SetJsonValue(root);
}

return;
}
Expand All @@ -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);
Expand Down

0 comments on commit 1746331

Please sign in to comment.