From c60dd400c127d5d612b2d25be0e14e7f4395fb9b Mon Sep 17 00:00:00 2001 From: Brenno Date: Thu, 4 Feb 2021 15:57:30 -0300 Subject: [PATCH] Updated JSON functions related to Tracked Objects --- src/TrackedObjectBBox.cpp | 2 +- src/TrackedObjectBBox.h | 2 +- src/TrackedObjectBase.h | 3 ++ src/effects/ObjectDetection.cpp | 56 +++++++++----------------------- src/effects/Tracker.cpp | 57 +++++++-------------------------- 5 files changed, 33 insertions(+), 87 deletions(-) diff --git a/src/TrackedObjectBBox.cpp b/src/TrackedObjectBBox.cpp index cbdc76289..6eb005e69 100644 --- a/src/TrackedObjectBBox.cpp +++ b/src/TrackedObjectBBox.cpp @@ -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 diff --git a/src/TrackedObjectBBox.h b/src/TrackedObjectBBox.h index 2cdd27a67..693cac8b6 100644 --- a/src/TrackedObjectBBox.h +++ b/src/TrackedObjectBBox.h @@ -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; diff --git a/src/TrackedObjectBase.h b/src/TrackedObjectBase.h index 49f84c942..6d1038ef3 100644 --- a/src/TrackedObjectBase.h +++ b/src/TrackedObjectBase.h @@ -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 diff --git a/src/effects/ObjectDetection.cpp b/src/effects/ObjectDetection.cpp index 6d0e199cf..6be796681 100644 --- a/src/effects/ObjectDetection.cpp +++ b/src/effects/ObjectDetection.cpp @@ -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()); @@ -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()); @@ -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 @@ -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"; @@ -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 @@ -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); diff --git a/src/effects/Tracker.cpp b/src/effects/Tracker.cpp index c46b23117..ebbdef471 100644 --- a/src/effects/Tracker.cpp +++ b/src/effects/Tracker.cpp @@ -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 @@ -274,9 +269,9 @@ 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"; @@ -284,26 +279,12 @@ void Tracker::SetJsonValue(const Json::Value root) { } } - // 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; @@ -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);