Skip to content

Commit

Permalink
Updated JSON functions related to Tracked Objects
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennoCaldato committed Feb 4, 2021
1 parent d24c2e4 commit c60dd40
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/TrackedObjectBBox.cpp
Expand Up @@ -354,7 +354,7 @@ void TrackedObjectBBox::SetJsonValue(const Json::Value root)
{

// Set the Id by the given JSON object
if (!root["box_id"].isNull())
if (!root["box_id"].isNull() && root["box_id"].asString() != "None")
Id(root["box_id"].asString());

// Set the BaseFps by the given JSON object
Expand Down
2 changes: 1 addition & 1 deletion src/TrackedObjectBBox.h
Expand Up @@ -187,7 +187,7 @@ namespace openshot
/// 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;
bool ExactlyContains(int64_t frame_number) const override;

/// Get the size of BoxVec map
int64_t GetLength() const;
Expand Down
3 changes: 3 additions & 0 deletions src/TrackedObjectBase.h
Expand Up @@ -82,6 +82,9 @@ namespace openshot {
std::string ChildClipId() const { return childClipId; };
void ChildClipId(std::string _childClipId) { childClipId = _childClipId; };

/// Check if there is data for the exact frame number
virtual bool ExactlyContains(int64_t frame_number) const { return {}; };

/// Scale an object's property
virtual void ScalePoints(double scale) { return; };
/// Return the main properties of a TrackedObjectBBox instance - such as position, size and rotation
Expand Down
56 changes: 16 additions & 40 deletions src/effects/ObjectDetection.cpp
Expand Up @@ -275,7 +275,7 @@ std::string ObjectDetection::GetVisibleObjects(int64_t frame_number) const{
for (const auto& trackedObject : trackedObjects){
// Get the tracked object JSON properties for this frame
Json::Value trackedObjectJSON = trackedObject.second->PropertiesJSON(frame_number);
if (trackedObjectJSON["visible"]["value"].asBool()){
if (trackedObjectJSON["visible"]["value"].asBool() && trackedObject.second->ExactlyContains(frame_number)){
// Save the object's index and ID if it's visible in this frame
root["visible_objects_index"].append(trackedObject.first);
root["visible_objects_id"].append(trackedObject.second->Id());
Expand All @@ -300,8 +300,9 @@ Json::Value ObjectDetection::JsonValue() const {
root["type"] = info.class_name;
root["protobuf_data_path"] = protobuf_data_path;
root["selected_object_index"] = selectedObjectIndex;

// Add tracked object's IDs to root
root["objects_id"] = Json::Value(Json::arrayValue);

for (auto const& trackedObject : trackedObjects){
Json::Value trackedObjectJSON = trackedObject.second->JsonValue();
root["objects_id"].append(trackedObject.second->Id());
Expand All @@ -311,12 +312,8 @@ Json::Value ObjectDetection::JsonValue() const {
auto selectedObject = trackedObjects.at(selectedObjectIndex);
if (selectedObject){
Json::Value selectedObjectJSON = selectedObject->JsonValue();
root["delta_x"] = selectedObjectJSON["delta_x"];
root["delta_y"] = selectedObjectJSON["delta_y"];
root["scale_x"] = selectedObjectJSON["scale_x"];
root["scale_y"] = selectedObjectJSON["scale_y"];
root["rotation"] = selectedObjectJSON["rotation"];
root["visible"] = selectedObjectJSON["visible"];
for (auto const& key : selectedObjectJSON.getMemberNames())
root[key] = selectedObjectJSON[key];
}

// return JsonValue
Expand Down Expand Up @@ -346,8 +343,8 @@ void ObjectDetection::SetJsonValue(const Json::Value root) {
// Set parent data
EffectBase::SetJsonValue(root);
// Set data from Json (if key is found)
if (!root["protobuf_data_path"].isNull()){
protobuf_data_path = (root["protobuf_data_path"].asString());
if (!root["protobuf_data_path"].isNull() && protobuf_data_path.size() <= 1){
protobuf_data_path = root["protobuf_data_path"].asString();

if(!LoadObjDetectdData(protobuf_data_path)){
std::cout<<"Invalid protobuf data path";
Expand All @@ -359,28 +356,19 @@ void ObjectDetection::SetJsonValue(const Json::Value root) {
if (!root["selected_object_index"].isNull())
selectedObjectIndex = root["selected_object_index"].asInt();

// Set the object's ids
// Set the tracked object's ids
if (!root["objects_id"].isNull()){
for (auto const& trackedObject : trackedObjects){
Json::Value trackedObjectJSON;
trackedObjectJSON["box_id"] = root["objects_id"][trackedObject.first].asString();
trackedObject.second->SetJsonValue(trackedObjectJSON);
Json::Value trackedObjectJSON;
trackedObjectJSON["box_id"] = root["objects_id"][trackedObject.first].asString();
trackedObject.second->SetJsonValue(trackedObjectJSON);
}
}

// Set the selected object's properties
Json::Value selectedObjectJSON;
selectedObjectJSON["delta_x"] = root["delta_x"];
selectedObjectJSON["delta_y"] = root["delta_y"];
selectedObjectJSON["scale_x"] = root["scale_x"];
selectedObjectJSON["scale_y"] = root["scale_y"];
selectedObjectJSON["rotation"] = root["rotation"];
selectedObjectJSON["visible"] = root["visible"];
if (!selectedObjectJSON.isNull()){
auto selectedObject = trackedObjects.at(selectedObjectIndex);
if (selectedObject)
selectedObject->SetJsonValue(selectedObjectJSON);
}
auto selectedObject = trackedObjects.at(selectedObjectIndex);
if (selectedObject)
selectedObject->SetJsonValue(root);
}

// Get all properties for a specific frame
Expand All @@ -391,20 +379,8 @@ std::string ObjectDetection::PropertiesJSON(int64_t requested_frame) const {

// Add the selected object Json to root
auto selectedObject = trackedObjects.at(selectedObjectIndex);
if (selectedObject){
Json::Value selectedObjectJSON = selectedObject->PropertiesJSON(requested_frame);
root["box_id"] = selectedObjectJSON["box_id"];
root["visible"] = selectedObjectJSON["visible"];
root["x1"] = selectedObjectJSON["x1"];
root["y1"] = selectedObjectJSON["y1"];
root["x2"] = selectedObjectJSON["x2"];
root["y2"] = selectedObjectJSON["y2"];
root["delta_x"] = selectedObjectJSON["delta_x"];
root["delta_y"] = selectedObjectJSON["delta_y"];
root["scale_x"] = selectedObjectJSON["scale_x"];
root["scale_y"] = selectedObjectJSON["scale_y"];
root["rotation"] = selectedObjectJSON["rotation"];
}
if (selectedObject)
root = selectedObject->PropertiesJSON(requested_frame);

root["selected_object_index"] = add_property_json("Selected Object", selectedObjectIndex, "int", "", NULL, 0, 200, false, requested_frame);
root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
Expand Down
57 changes: 12 additions & 45 deletions src/effects/Tracker.cpp
Expand Up @@ -218,13 +218,8 @@ Json::Value Tracker::JsonValue() const {
Json::Value trackedObjectJSON = trackedObject.second->JsonValue();
root["objects_id"].append(trackedObject.second->Id());
// Save the trackedObject JSON on root
root["delta_x"] = trackedObjectJSON["delta_x"];
root["delta_y"] = trackedObjectJSON["delta_y"];
root["scale_x"] = trackedObjectJSON["scale_x"];
root["scale_y"] = trackedObjectJSON["scale_y"];
root["rotation"] = trackedObjectJSON["rotation"];
root["visible"] = trackedObjectJSON["visible"];
root["child_clip_id"] = trackedObjectJSON["child_clip_id"];
for (auto const& key : trackedObjectJSON.getMemberNames())
root[key] = trackedObjectJSON[key];
}

// return JsonValue
Expand Down Expand Up @@ -274,36 +269,22 @@ void Tracker::SetJsonValue(const Json::Value root) {
TimeScale = (double) root["TimeScale"].asDouble();

// Set data from Json (if key is found)
if (!root["protobuf_data_path"].isNull())
if (!root["protobuf_data_path"].isNull() && protobuf_data_path.size() <= 1)
{
protobuf_data_path = (root["protobuf_data_path"].asString());
protobuf_data_path = root["protobuf_data_path"].asString();
if(!trackedData->LoadBoxData(protobuf_data_path))
{
std::cout<<"Invalid protobuf data path";
protobuf_data_path = "";
}
}

// Set the object's ids
if (!root["objects_id"].isNull()){
for (auto const& trackedObject : trackedObjects){
Json::Value trackedObjectJSON;
trackedObjectJSON["box_id"] = root["objects_id"][trackedObject.first].asString();
trackedObject.second->SetJsonValue(trackedObjectJSON);
}
}

// Set the tracked object's properties
for (auto const& trackedObject : trackedObjects){
Json::Value trackedObjectJSON;
trackedObjectJSON["delta_x"] = root["delta_x"];
trackedObjectJSON["delta_y"] = root["delta_y"];
trackedObjectJSON["scale_x"] = root["scale_x"];
trackedObjectJSON["scale_y"] = root["scale_y"];
trackedObjectJSON["rotation"] = root["rotation"];
trackedObjectJSON["visible"] = root["visible"];
trackedObjectJSON["child_clip_id"] = root["child_clip_id"];
if (!trackedObjectJSON.isNull())
trackedObject.second->SetJsonValue(trackedObjectJSON);
Json::Value trackedObjectJSON = root;
if (!root["objects_id"].isNull())
trackedObjectJSON["box_id"] = root["objects_id"][trackedObject.first].asString();
trackedObject.second->SetJsonValue(trackedObjectJSON);
}

return;
Expand All @@ -316,23 +297,9 @@ std::string Tracker::PropertiesJSON(int64_t requested_frame) const {
// Generate JSON properties list
Json::Value root;

// Add trackedObjects IDs to JSON
for (auto const& trackedObject : trackedObjects){
// Save the trackedObject Id on root
Json::Value trackedObjectJSON = trackedObject.second->PropertiesJSON(requested_frame);
root["box_id"] = trackedObjectJSON["box_id"];
root["visible"] = trackedObjectJSON["visible"];
root["x1"] = trackedObjectJSON["x1"];
root["y1"] = trackedObjectJSON["y1"];
root["x2"] = trackedObjectJSON["x2"];
root["y2"] = trackedObjectJSON["y2"];
root["delta_x"] = trackedObjectJSON["delta_x"];
root["delta_y"] = trackedObjectJSON["delta_y"];
root["scale_x"] = trackedObjectJSON["scale_x"];
root["scale_y"] = trackedObjectJSON["scale_y"];
root["rotation"] = trackedObjectJSON["rotation"];
root["child_clip_id"] = trackedObjectJSON["child_clip_id"];
}
// Add trackedObject properties to JSON
for (auto const& trackedObject : trackedObjects)
root = trackedObject.second->PropertiesJSON(requested_frame);

// Append effect's properties
root["id"] = add_property_json("ID", 0.0, "string", Id(), NULL, -1, -1, true, requested_frame);
Expand Down

0 comments on commit c60dd40

Please sign in to comment.