Skip to content

Commit

Permalink
Merge pull request #674 from OpenShot/fix-saving
Browse files Browse the repository at this point in the history
Changed JSON communication for detected objects
  • Loading branch information
BrennoCaldato committed May 19, 2021
2 parents f51af2e + e2b51da commit 461a030
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 53 deletions.
4 changes: 2 additions & 2 deletions src/CVObjectDetection.cpp
Expand Up @@ -85,8 +85,8 @@ void CVObjectDetection::detectObjectsClip(openshot::Clip &video, size_t _start,
size_t frame_number;
if(!process_interval || end <= 1 || end-start == 0){
// Get total number of frames in video
start = (int)(video.Start() * video.Reader()->info.fps.ToFloat()) + 1;
end = (int)(video.End() * video.Reader()->info.fps.ToFloat()) + 1;
start = (int)(video.Start() * video.Reader()->info.fps.ToFloat());
end = (int)(video.End() * video.Reader()->info.fps.ToFloat());
}

for (frame_number = start; frame_number <= end; frame_number++)
Expand Down
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
50 changes: 24 additions & 26 deletions src/effects/ObjectDetection.cpp
Expand Up @@ -371,6 +371,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 @@ -458,21 +461,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 the selected object Json to root
if(trackedObjects.count(selectedObjectIndex) != 0){
auto selectedObject = trackedObjects.at(selectedObjectIndex);
if (selectedObject){
Json::Value selectedObjectJSON = selectedObject->JsonValue();
for (auto const& key : selectedObjectJSON.getMemberNames())
root[key] = selectedObjectJSON[key];
}
// add object json
objects[trackedObject.second->Id()] = trackedObjectJSON;
}
root["objects"] = objects;

// return JsonValue
return root;
Expand All @@ -484,22 +479,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 @@ -508,7 +500,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 @@ -536,6 +528,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 @@ -544,13 +545,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 @@ -559,12 +553,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
50 changes: 32 additions & 18 deletions src/effects/Tracker.cpp
Expand Up @@ -60,6 +60,7 @@ Tracker::Tracker(std::string clipTrackerDataPath)
trackedData->LoadBoxData(clipTrackerDataPath);
ClipBase* parentClip = this->ParentClip();
trackedData->ParentClip(parentClip);
trackedData->Id(std::to_string(0));
// Insert TrackedObject with index 0 to the trackedObjects map
trackedObjects.insert({0, trackedData});
}
Expand All @@ -74,6 +75,7 @@ Tracker::Tracker()
trackedData = std::make_shared<TrackedObjectBBox>(trackedDataObject);
ClipBase* parentClip = this->ParentClip();
trackedData->ParentClip(parentClip);
trackedData->Id(std::to_string(0));
// Insert TrackedObject with index 0 to the trackedObjects map
trackedObjects.insert({0, trackedData});
}
Expand Down Expand Up @@ -265,17 +267,15 @@ Json::Value Tracker::JsonValue() const {
root["BaseFPS"]["num"] = BaseFPS.num;
root["BaseFPS"]["den"] = BaseFPS.den;
root["TimeScale"] = this->TimeScale;
root["objects_id"] = Json::Value(Json::arrayValue);

// Add trackedObjects IDs to JSON
for (auto const& trackedObject : trackedObjects){
// Get the trackedObject JSON
Json::Value trackedObjectJSON = trackedObject.second->JsonValue();
root["objects_id"].append(trackedObject.second->Id());
// Save the trackedObject JSON on root
for (auto const& key : trackedObjectJSON.getMemberNames())
root[key] = trackedObjectJSON[key];
}
Json::Value objects;
for (auto const& trackedObject : trackedObjects){
Json::Value trackedObjectJSON = trackedObject.second->JsonValue();
// add object json
objects[trackedObject.second->Id()] = trackedObjectJSON;
}
root["objects"] = objects;

// return JsonValue
return root;
Expand Down Expand Up @@ -334,27 +334,41 @@ void Tracker::SetJsonValue(const Json::Value root) {
}
}

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

return;
}


// Get all properties for a specific frame
std::string Tracker::PropertiesJSON(int64_t requested_frame) const {

// Generate JSON properties list
Json::Value root;

// Add trackedObject properties to JSON
for (auto const& trackedObject : trackedObjects)
root = trackedObject.second->PropertiesJSON(requested_frame);
Json::Value objects;
for (auto const& trackedObject : trackedObjects){
Json::Value trackedObjectJSON = trackedObject.second->PropertiesJSON(requested_frame);
// add object json
objects[trackedObject.second->Id()] = trackedObjectJSON;
}
root["objects"] = objects;

// 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 461a030

Please sign in to comment.