From 5eb9f60eeabb74353739b08690d9db48708e6210 Mon Sep 17 00:00:00 2001 From: Brenno Date: Fri, 22 Jan 2021 19:47:36 -0300 Subject: [PATCH] Added support to update the detected bounding-boxes keyframes Added support to show transform handlers for the bounding-boxes (Tracked Objects) detected by the Object Detection effect and update their Keyframes. --- src/Timeline.cpp | 3 ++ src/effects/ObjectDetection.cpp | 62 ++++++++++++++++++++++++++------- src/effects/Tracker.cpp | 35 +++++++++++++++---- 3 files changed, 81 insertions(+), 19 deletions(-) diff --git a/src/Timeline.cpp b/src/Timeline.cpp index 05b9bead..dd2c1702 100644 --- a/src/Timeline.cpp +++ b/src/Timeline.cpp @@ -310,11 +310,13 @@ std::string Timeline::GetTrackedObjectValues(std::string id) const { float y1 = firstBox.cy - (firstBox.height/2); float x2 = firstBox.cx + (firstBox.width/2); float y2 = firstBox.cy + (firstBox.height/2); + float r = firstBox.angle; trackedObjectJson["x1"] = x1; trackedObjectJson["y1"] = y1; trackedObjectJson["x2"] = x2; trackedObjectJson["y2"] = y2; + trackedObjectJson["r"] = r; } else { @@ -323,6 +325,7 @@ std::string Timeline::GetTrackedObjectValues(std::string id) const { trackedObjectJson["y1"] = 0; trackedObjectJson["x2"] = 0; trackedObjectJson["y2"] = 0; + trackedObjectJson["r"] = 0; } return trackedObjectJson.toStyledString(); diff --git a/src/effects/ObjectDetection.cpp b/src/effects/ObjectDetection.cpp index 8036e9fc..f95f559b 100644 --- a/src/effects/ObjectDetection.cpp +++ b/src/effects/ObjectDetection.cpp @@ -89,13 +89,30 @@ std::shared_ptr ObjectDetection::GetFrame(std::shared_ptr frame, i DetectionData detections = detectionsData[frame_number]; for(int i = 0; i bb_nrml = detections.boxes.at(i); - cv::Rect2d box((int)(bb_nrml.x*fw), - (int)(bb_nrml.y*fh), - (int)(bb_nrml.width*fw), - (int)(bb_nrml.height*fh)); - drawPred(detections.classIds.at(i), detections.confidences.at(i), - box, cv_image, detections.objectIds.at(i)); + + // Get the object id + int objectId = detections.objectIds.at(i); + + // Search for the object in the trackedObjects map + auto trackedObject_it = trackedObjects.find(objectId); + + // Cast the object as TrackedObjectBBox + std::shared_ptr trackedObject = std::static_pointer_cast(trackedObject_it->second); + + // Check if the tracked object has data for this frame + if (trackedObject->Contains(frame_number)){ + + // Get the bounding-box of given frame + BBox trackedBox = trackedObject->GetBox(frame_number); + cv::Rect2d box( + (int)( (trackedBox.cx-trackedBox.width/2)*fw), + (int)( (trackedBox.cy-trackedBox.height/2)*fh), + (int)( trackedBox.width*fw), + (int)( trackedBox.height*fh) + ); + drawPred(detections.classIds.at(i), detections.confidences.at(i), + box, cv_image, detections.objectIds.at(i)); + } } } @@ -259,8 +276,14 @@ Json::Value ObjectDetection::JsonValue() const { // Add trackedObjects IDs to JSON for (auto const& trackedObject : trackedObjects){ - // Save the trackedObject Id on root - root["box_id"+to_string(trackedObject.first)] = trackedObject.second->Id(); + Json::Value trackedObjectJSON = trackedObject.second->JsonValue(); + // Save the trackedObject JSON on root + root["box_id-"+to_string(trackedObject.first)] = trackedObjectJSON["box_id"]; + root["delta_x-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_x"]; + root["delta_y-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_y"]; + root["scale_x-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_x"]; + root["scale_y-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_y"]; + root["rotation-"+to_string(trackedObject.first)] = trackedObjectJSON["rotation"]; } // return JsonValue @@ -301,8 +324,14 @@ void ObjectDetection::SetJsonValue(const Json::Value root) { for (auto const& trackedObject : trackedObjects){ Json::Value trackedObjectJSON; - trackedObjectJSON["box_id"] = root["box_id"+to_string(trackedObject.first)]; - trackedObject.second->SetJsonValue(trackedObjectJSON); + trackedObjectJSON["box_id"] = root["box_id-"+to_string(trackedObject.first)]; + trackedObjectJSON["delta_x"] = root["delta_x-"+to_string(trackedObject.first)]; + trackedObjectJSON["delta_y"] = root["delta_y-"+to_string(trackedObject.first)]; + trackedObjectJSON["scale_x"] = root["scale_x-"+to_string(trackedObject.first)]; + trackedObjectJSON["scale_y"] = root["scale_y-"+to_string(trackedObject.first)]; + trackedObjectJSON["rotation"] = root["rotation-"+to_string(trackedObject.first)]; + if (!trackedObjectJSON.isNull()) + trackedObject.second->SetJsonValue(trackedObjectJSON); } } @@ -316,7 +345,16 @@ std::string ObjectDetection::PropertiesJSON(int64_t requested_frame) const { for (auto const& trackedObject : trackedObjects){ // Save the trackedObject Id on root Json::Value trackedObjectJSON = trackedObject.second->PropertiesJSON(requested_frame); - root["box_id"+to_string(trackedObject.first)] = trackedObjectJSON["box_id"]; + root["box_id-"+to_string(trackedObject.first)] = trackedObjectJSON["box_id"]; + root["x1-"+to_string(trackedObject.first)] = trackedObjectJSON["x1"]; + root["y1-"+to_string(trackedObject.first)] = trackedObjectJSON["y1"]; + root["x2-"+to_string(trackedObject.first)] = trackedObjectJSON["x2"]; + root["y2-"+to_string(trackedObject.first)] = trackedObjectJSON["y2"]; + root["delta_x-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_x"]; + root["delta_y-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_y"]; + root["scale_x-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_x"]; + root["scale_y-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_y"]; + root["rotation-"+to_string(trackedObject.first)] = trackedObjectJSON["rotation"]; } 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 260909bf..5f43cbeb 100644 --- a/src/effects/Tracker.cpp +++ b/src/effects/Tracker.cpp @@ -151,8 +151,15 @@ Json::Value Tracker::JsonValue() const { trackedDataJSON = trackedData->JsonValue(); // Save the trackedData properties on root - for (const auto& key : trackedDataJSON.getMemberNames()){ - root[key] = trackedDataJSON[key]; + for (auto const& trackedObject : trackedObjects){ + Json::Value trackedObjectJSON = trackedObject.second->JsonValue(); + // Save the trackedObject JSON on root + root["box_id-"+to_string(trackedObject.first)] = trackedObjectJSON["box_id"]; + root["delta_x-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_x"]; + root["delta_y-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_y"]; + root["scale_x-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_x"]; + root["scale_y-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_y"]; + root["rotation-"+to_string(trackedObject.first)] = trackedObjectJSON["rotation"]; } // return JsonValue @@ -213,7 +220,15 @@ void Tracker::SetJsonValue(const Json::Value root) { } for (auto const& trackedObject : trackedObjects){ - trackedObject.second->SetJsonValue(root); + Json::Value trackedObjectJSON; + trackedObjectJSON["box_id"] = root["box_id-"+to_string(trackedObject.first)]; + trackedObjectJSON["delta_x"] = root["delta_x-"+to_string(trackedObject.first)]; + trackedObjectJSON["delta_y"] = root["delta_y-"+to_string(trackedObject.first)]; + trackedObjectJSON["scale_x"] = root["scale_x-"+to_string(trackedObject.first)]; + trackedObjectJSON["scale_y"] = root["scale_y-"+to_string(trackedObject.first)]; + trackedObjectJSON["rotation"] = root["rotation-"+to_string(trackedObject.first)]; + if (!trackedObjectJSON.isNull()) + trackedObject.second->SetJsonValue(trackedObjectJSON); } return; @@ -229,10 +244,16 @@ std::string Tracker::PropertiesJSON(int64_t requested_frame) const { // 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]; - } + root["box_id-"+to_string(trackedObject.first)] = trackedObjectJSON["box_id"]; + root["x1-"+to_string(trackedObject.first)] = trackedObjectJSON["x1"]; + root["y1-"+to_string(trackedObject.first)] = trackedObjectJSON["y1"]; + root["x2-"+to_string(trackedObject.first)] = trackedObjectJSON["x2"]; + root["y2-"+to_string(trackedObject.first)] = trackedObjectJSON["y2"]; + root["delta_x-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_x"]; + root["delta_y-"+to_string(trackedObject.first)] = trackedObjectJSON["delta_y"]; + root["scale_x-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_x"]; + root["scale_y-"+to_string(trackedObject.first)] = trackedObjectJSON["scale_y"]; + root["rotation-"+to_string(trackedObject.first)] = trackedObjectJSON["rotation"]; } // Append effect's properties