Skip to content

Commit

Permalink
Key "objects" in Json hold a dict of tracked objects
Browse files Browse the repository at this point in the history
  • Loading branch information
BrennoCaldato committed May 8, 2021
1 parent 7a9d196 commit c6ce5b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
8 changes: 1 addition & 7 deletions src/TrackedObjectBBox.cpp
Expand Up @@ -382,25 +382,19 @@ void TrackedObjectBBox::SetJsonValue(const Json::Value root)
if (!root["BaseFPS"]["den"].isNull())
BaseFps.den = (int)root["BaseFPS"]["den"].asInt();
}

// Set the TimeScale by the given JSON object
if (!root["TimeScale"].isNull())
{
double scale = (double)root["TimeScale"].asDouble();
this->ScalePoints(scale);
}

// Set the protobuf data path by the given JSON object
if (!root["protobuf_data_path"].isNull())
protobufDataPath = root["protobuf_data_path"].asString();

// Set the id of the child clip
if (!root["child_clip_id"].isNull() && root["child_clip_id"].asString() != ""){
Clip* parentClip = (Clip *) ParentClip();

if(parentClip && (root["child_clip_id"].asString() != parentClip->Id())){
ChildClipId(root["child_clip_id"].asString());
}
ChildClipId(root["child_clip_id"].asString());
}

// Set the Keyframes by the given JSON object
Expand Down
40 changes: 24 additions & 16 deletions src/effects/ObjectDetection.cpp
Expand Up @@ -369,6 +369,9 @@ bool ObjectDetection::LoadObjDetectdData(std::string inputFilePath){
ClipBase* parentClip = this->ParentClip();
trackedObjPtr->ParentClip(parentClip);

// Create a temp ID. This ID is necessary to initialize the object_id Json list
// this Id will be replaced by the one created in the UI
trackedObjPtr->Id(std::to_string(objectId));
trackedObjects.insert({objectId, trackedObjPtr});
}

Expand Down Expand Up @@ -456,11 +459,13 @@ Json::Value ObjectDetection::JsonValue() const {
root["display_box_text"] = display_box_text.JsonValue();

// Add tracked object's IDs to root
root["objects_id"] = Json::Value(Json::arrayValue);
Json::Value objects;
for (auto const& trackedObject : trackedObjects){
Json::Value trackedObjectJSON = trackedObject.second->JsonValue();
root["objects_id"].append(trackedObject.second->Id());
// add object json
objects[trackedObject.second->Id()] = trackedObjectJSON;
}
root["objects"] = objects;

// Add the selected object Json to root
if(trackedObjects.count(selectedObjectIndex) != 0){
Expand All @@ -482,22 +487,19 @@ void ObjectDetection::SetJson(const std::string value) {
// Parse JSON string into JSON objects
try
{
std::cout<<"entrou no objectDetection SetJson \n"<<value<<"\n\n";
const Json::Value root = openshot::stringToJson(value);
// Set all values that match
SetJsonValue(root);
}
catch (const std::exception& e)
{
// Error parsing JSON (or missing keys)
std::cout<< "\n\n"<<e.what()<<"\n\n";
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
}

// Load Json::Value into this object
void ObjectDetection::SetJsonValue(const Json::Value root) {
std::cout<<"entrou no objectDetection SetJasonValue \n"<<root<<"\n\n";
// Set parent data
EffectBase::SetJsonValue(root);

Expand All @@ -506,7 +508,7 @@ void ObjectDetection::SetJsonValue(const Json::Value root) {
protobuf_data_path = root["protobuf_data_path"].asString();

if(!LoadObjDetectdData(protobuf_data_path)){
std::cout<<"Invalid protobuf data path";
throw InvalidFile("Invalid protobuf data path");
protobuf_data_path = "";
}
}
Expand Down Expand Up @@ -534,6 +536,15 @@ void ObjectDetection::SetJsonValue(const Json::Value root) {
}
}

if (!root["objects"].isNull()){
for (auto const& trackedObject : trackedObjects){
std::string obj_id = std::to_string(trackedObject.first);
if(!root["objects"][obj_id].isNull()){
trackedObject.second->SetJsonValue(root["objects"][obj_id]);
}
}
}

// Set the tracked object's ids
if (!root["objects_id"].isNull()){
for (auto const& trackedObject : trackedObjects){
Expand All @@ -542,13 +553,6 @@ void ObjectDetection::SetJsonValue(const Json::Value root) {
trackedObject.second->SetJsonValue(trackedObjectJSON);
}
}

// Set the selected object's properties
if(trackedObjects.count(selectedObjectIndex) != 0){
auto selectedObject = trackedObjects.at(selectedObjectIndex);
if (selectedObject)
selectedObject->SetJsonValue(root);
}
}

// Get all properties for a specific frame
Expand All @@ -557,12 +561,16 @@ std::string ObjectDetection::PropertiesJSON(int64_t requested_frame) const {
// Generate JSON properties list
Json::Value root;

// Add the selected object Json to root
Json::Value objects;
if(trackedObjects.count(selectedObjectIndex) != 0){
auto selectedObject = trackedObjects.at(selectedObjectIndex);
if (selectedObject)
root = selectedObject->PropertiesJSON(requested_frame);
if (selectedObject){
Json::Value trackedObjectJSON = selectedObject->PropertiesJSON(requested_frame);
// add object json
objects[selectedObject->Id()] = trackedObjectJSON;
}
}
root["objects"] = objects;

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

0 comments on commit c6ce5b0

Please sign in to comment.