Skip to content

Commit

Permalink
Added support to update the detected bounding-boxes keyframes
Browse files Browse the repository at this point in the history
Added support to show transform handlers for the bounding-boxes (Tracked Objects) detected by the Object Detection effect and update their Keyframes.
  • Loading branch information
BrennoCaldato committed Jan 22, 2021
1 parent 32a217e commit 5eb9f60
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 19 deletions.
3 changes: 3 additions & 0 deletions src/Timeline.cpp
Expand Up @@ -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 {
Expand All @@ -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();
Expand Down
62 changes: 50 additions & 12 deletions src/effects/ObjectDetection.cpp
Expand Up @@ -89,13 +89,30 @@ std::shared_ptr<Frame> ObjectDetection::GetFrame(std::shared_ptr<Frame> frame, i

DetectionData detections = detectionsData[frame_number];
for(int i = 0; i<detections.boxes.size(); i++){
cv::Rect_<float> 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<TrackedObjectBBox> trackedObject = std::static_pointer_cast<TrackedObjectBBox>(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));
}
}
}

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

Expand All @@ -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);
Expand Down
35 changes: 28 additions & 7 deletions src/effects/Tracker.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 5eb9f60

Please sign in to comment.