AI Watch A1
Multi-person 3D skeleton detection using Intel RealSense and OpenPose with Kafka support.
OutputManagerJSON.cpp
Go to the documentation of this file.
1//
2// OutputManagerJSON.cpp
3// AI Watch A1
4//
5// Created by Denny Caruso on 21/07/22.
6//
7
8// License: Apache 2.0. See LICENSE file in root directory.
9// Copyright(c) 2022. All Rights Reserved.
10
11#include "OutputManager.hpp"
12
13
14
15Json::Value OutputManagerJSON::makeOutputString (std::vector <Point3D *> skeletonPoints3D, std::vector <bool> bodyKeyPointsMap, unsigned int frameID, unsigned int personID) {
16 Json::Value root, arraySkeletonPoints3D(Json::arrayValue);
17 Json::StyledStreamWriter writer;
18
19 // For each OpenPose's skeleton body joint point
20 for (unsigned char i = 0; i < skeletonPoints3D.size(); i++) {
21 if (i >= openPoseBodyKeyPointsNumber) continue;
22 Json::Value singlePoint3D_JSON;
23 // Build JSON node for i-th body joint point
24 singlePoint3D_JSON["pointID"] = Json::Value((unsigned int) i);
25 singlePoint3D_JSON["confidence"] = Json::Value(((BodyKeyPoint *) skeletonPoints3D.at(i)->getDecorated())->getConfidence());
26 singlePoint3D_JSON["x"] = Json::Value((double) skeletonPoints3D.at(i)->getZ());
27 singlePoint3D_JSON["y"] = Json::Value((double) skeletonPoints3D.at(i)->getY());
28 singlePoint3D_JSON["z"] = Json::Value((double) skeletonPoints3D.at(i)->getX());
29 singlePoint3D_JSON["x_rotation"] = Json::Value(0.0);
30 singlePoint3D_JSON["y_rotation"] = Json::Value(0.0);
31 singlePoint3D_JSON["z_rotation"] = Json::Value(0.0);
32 singlePoint3D_JSON["w_rotation"] = Json::Value(1.0);
33 arraySkeletonPoints3D.append(singlePoint3D_JSON);
34 }
35
36 root["personID"] = Json::Value((unsigned int) personID);
37 root["skeleton"] = arraySkeletonPoints3D;
38 OutputManager::setStringOutputData(root.toStyledString());
39 return root;
40}
41
42bool OutputManagerJSON::loadJSON (std::string filePathJSON, Json::Value & currentJSON) {
43 Json::Reader readerJSON;
44 std::ifstream streamJSON(filePathJSON.c_str(), std::ifstream::binary);
45 return readerJSON.parse(streamJSON, currentJSON, false);
46}
47
48void OutputManagerJSON::saveJSON (std::string filePath) {
49 Json::StyledStreamWriter writer;
50 std::ofstream outputFile(filePath);
52 outputFile.close();
53}
54
55Json::Value OutputManagerJSON::getValueAt (std::string key, Json::Value currentJSON) {
56 return currentJSON[key];
57}
58
59Json::Value OutputManagerJSON::getValueAt (unsigned int i, Json::Value currentJSON) {
60 return currentJSON[i];
61}
62
63Json::Value OutputManagerJSON::getValueAt (std::string key, unsigned int i, Json::Value currentJSON) {
64 return (currentJSON[i])[key];
65}
66
67void OutputManagerJSON::createJSON (Json::Value & people, cv::Mat & colorImage, cv::Mat & distanceImage, cv::Mat & skeletonOnlyImage, unsigned int nFrame, const char * outputFolder, const float skeletonThreshold) {
68 Json::Value root, peopleArray(Json::arrayValue);
69 std::stringstream outputJsonFilePath;
70 root["ID_Frame"] = nFrame;
71 root["thingId"] = Json::Value(std::string("digitaltwin:Laboratorio_Corridoio:1"));
72
73
74 // For each OpenPose's detected skeleton
75 for (Json::Value::ArrayIndex i = 0; i < people.size(); i++) {
76 Json::Value singlePerson = getValueAt("pose_keypoints_2d", i, people);
77 Skeleton singlePersonSkeleton = Skeleton(colorImage, distanceImage, skeletonOnlyImage, singlePerson);
78 singlePersonSkeleton.generateSkeleton();
79 // Discard Skeleton with low consistency value
80 if (singlePersonSkeleton.getConsistency() > skeletonThreshold) {
81 peopleArray.append(makeOutputString(* singlePersonSkeleton.getSkeletonPoints3D(), singlePersonSkeleton.getBodyKeyPointsMap(), nFrame, (unsigned int) i));
82 }
83 }
84
85 root["People"] = peopleArray;
86 OutputManager::setStringOutputData(root.toStyledString());
87 outputJsonFilePath << outputFolder << "movement/frame" << nFrame << "_" << JSON_FILE_PATH;
88 saveJSON(std::string(outputJsonFilePath.str()));
89 outputJsonFilePath.str(std::string());
90 outputJsonFilePath.clear();
91}
BodyKeyPoint class is used for body keypoint creations and manipulations.
void createJSON(Json::Value &people, cv::Mat &colorImage, cv::Mat &distanceImage, cv::Mat &skeletonOnlyImage, unsigned int nFrame, const char *outputFolder, const float skeletonThreshold)
This methods take a reference to a JSON node that represents all the people's informations within the...
Json::Value makeOutputString(std::vector< Point3D * > skeletonPoints3D, std::vector< bool > bodyKeyPointsMap, unsigned int frameID, unsigned int personID) override
This method is specific for saving the output in JSON format.
void saveJSON(std::string filePath)
Saves content present in the "stringOutputData" in a JSON file. The file path where to save the file ...
Json::Value getValueAt(std::string key, Json::Value currentJSON)
Utility method to get the value at a given key in a given JSON node.
bool loadJSON(std::string filePathJSON, Json::Value &currentJSON)
Loads a JSON file and returns it as a reference. The JSON file path on disk is given.
void setStringOutputData(std::string stringOutputData)
Set the output file's content.
std::string getStringOutputData(void)
Get the output file's content.
Skeleton class is used for skeleton creations and manipulations.
Definition: Skeleton.hpp:37
float getConsistency(void)
Get the Consistency float value.
Definition: Skeleton.cpp:222
std::vector< Point3D * > * getSkeletonPoints3D(void)
Get the Skeleton Points3D vector's pointer.
Definition: Skeleton.cpp:218
std::vector< bool > getBodyKeyPointsMap(void)
Get the BodyKeyPoints Map vector.
Definition: Skeleton.cpp:69
void generateSkeleton(void)
A high-level method that abstracts all the internal structure and operations of the Skeleton class....
Definition: Skeleton.cpp:203
static const char * JSON_FILE_PATH
Definition: constants.hpp:58
static const short int openPoseBodyKeyPointsNumber
Definition: constants.hpp:68