AI Watch A1
Multi-person 3D skeleton detection using Intel RealSense and OpenPose with Kafka support.
FacadeSingleton.cpp
Go to the documentation of this file.
1//
2// FacadeSingleton.cpp
3// AI Watch A1
4//
5// Created by Denny Caruso on 20/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 "FacadeSingleton.hpp"
12
13
14
17
18
19
21 std::ifstream inputFile(CONF_FILE_PATH);
22 std::string singleLineConfigurationFile;
23
24 int * fileArgc = new (std::nothrow) int;
25 * fileArgc = (int) totalFileParameters;
26 char *** fileArgv = new (std::nothrow) char **;
27 * fileArgv = new (std::nothrow) char *[totalFileParameters];
28 unsigned char rowCounter = 0;
29
30 if (!fileArgc || !fileArgv || ! * fileArgv) CV_Error(NEW_ALLOC_ERROR, NEW_ALLOC_SCOPE);
31 // Read usage's configuration parameters from file
32 while (inputFile >> singleLineConfigurationFile) {
33 (* fileArgv)[rowCounter] = new (std::nothrow) char [(unsigned int) singleLineConfigurationFile.size()];
34 if (!(* fileArgv)[rowCounter]) CV_Error(NEW_ALLOC_ERROR, NEW_ALLOC_SCOPE);
35 strcpy((* fileArgv)[rowCounter], singleLineConfigurationFile.c_str());
36 rowCounter++;
37 }
38
40}
41
43 delete getCameraManager();
44 delete getOutputManager();
45 delete getOpenCV_Manager();
46 delete getUsageManager();
48 delete getImageManager();
49 delete getKafkaManager();
50 delete sharedInstance;
51}
52
54 this->cameraManager = cameraManager;
55}
56
58 this->outputManager = outputManager;
59}
60
62 this->openCV_Manager = openCV_Manager;
63}
64
66 this->usageManager = usageManager;
67}
68
70 this->coordinateMappingManager = coordinateMappingManager;
71}
72
74 this->imageManager = imageManager;
75}
76
78 this->kafkaManager = kafkaManager;
79}
80
81
82
84 std::lock_guard <std::mutex> lock(singletonMutex);
86 return sharedInstance;
87}
88
90 std::lock_guard <std::mutex> lock(singletonMutex);
91 if (sharedInstance == nullptr) return nullptr;
92 return sharedInstance;
93}
94
96 return this->cameraManager;
97}
98
100 return this->outputManager;
101}
102
104 return this->openCV_Manager;
105}
106
108 return this->usageManager;
109}
110
112 return this->coordinateMappingManager;
113}
114
116 return this->imageManager;
117}
118
120 return this->kafkaManager;
121}
122
123void FacadeSingleton::startEnvironment (rs2::pipeline & pipelineStream, struct rs2_intrinsics & color_intrin, float * scale, unsigned short int resX, unsigned short int resY, const char * destinationKafkaTopic, Room room) {
128
130 FacadeSingleton::setKafkaManager(new KafkaManager(destinationKafkaTopic));
131 FacadeSingleton::getCameraManager()->startEnvironment(pipelineStream, color_intrin, scale, resX, resY, FIRST_BOOT);
133}
134
135void FacadeSingleton::getVideoFrames (unsigned int user_nFrame, rs2::pipeline & pipelineStream, float scale, const unsigned short int framesToSkip) {
136 getOpenCV_Manager()->getVideoFramesCV(user_nFrame, pipelineStream, scale, framesToSkip);
137}
138
139void FacadeSingleton::getVideoBodyKeyPoints (int * argc, char *** argv) {
140 SystemCommand * openPoseCommand = new OpenPoseCommand();
141 openPoseCommand->executeCommand(argc, argv);
142 delete openPoseCommand;
143}
144
145void FacadeSingleton::showSkeletons (unsigned int user_nFrame, const float skeletonThreshold) {
146 getOpenCV_Manager()->showSkeletonsCV(user_nFrame, skeletonThreshold);
147}
148
149void FacadeSingleton::sendData (unsigned int user_nFrame) {
150 char ** argv = * FacadeSingleton::getUsageManager()->get_argv(), * outputFolder = argv[outputFolderOffset];
151 unsigned int frameID = FacadeSingleton::getCameraManager()->getFrameID(), currentImageID;
153
154 for (unsigned int nFrame = 0; nFrame < user_nFrame; nFrame++) {
155 Json::Value currentJSON;
156 std::stringstream outputJsonFilePath;
157 currentImageID = frameID - user_nFrame + nFrame;
158 outputJsonFilePath << outputFolder << "movement/frame" << currentImageID << "_skeletonsPoints3D.json";
159 if (myOutputManagerJSON->loadJSON(outputJsonFilePath.str(), currentJSON)) {
160 std::string key = std::to_string(currentImageID);
161 FacadeSingleton::getKafkaManager()->sendData(key.c_str(), currentJSON);
162 }
163 }
164}
165
167 SystemCommand * cleanCommand = new CleanCommand();
168 cleanCommand->executeCommand(getUsageManager()->get_argc(), getUsageManager()->get_argv());
169 delete cleanCommand;
170}
The CleanCommand class is a class that implements a command to clean the build folder by deleting old...
The CoordinateMappingManager class is a class that is responsible for converting coordinates values f...
FacadeSingleton class is used as a single access point to a simplified interface.
RealSenseManager * cameraManager
A pointer to RealSenseManager object. A RealSenseManager object is responsible for handling the camer...
CoordinateMappingManager * getCoordinateMappingManager(void)
Get the CoordinateMappingManager pointer.
void showSkeletons(unsigned int user_nFrame, const float skeletonThreshold)
Retrieve OpenPose's output, convert it to RealSense coordinate's space, show results and save them.
void setOutputManager(OutputManager *outputManager)
Set the OutputManager pointer.
void setUsageManager(UsageManager *usageManager)
Set the UsageManager pointer.
CoordinateMappingManager * coordinateMappingManager
A pointer to CoordinateMappingManager object. A CoordinateMappingManager object is responsible for tr...
void startEnvironment(rs2::pipeline &pipelineStream, struct rs2_intrinsics &color_intrin, float *scale, unsigned short int resX, unsigned short int resY, const char *destinationKafkaTopic, Room room)
This method is useful to invoke at the boot of the program where we want to initialize the different ...
void setImageManager(ImageManager *imageManager)
Set the ImageManager pointer.
OpenCV_Manager * getOpenCV_Manager(void)
Get the OpenCV_Manager pointer.
ImageManager * getImageManager(void)
Get the ImageManager pointer.
KafkaManager * kafkaManager
A pointer to KafkaManager object. A KafkaManager object is responsible for sending previously produce...
ImageManager * imageManager
A pointer to CoordinateMappingManager object. A CoordinateMappingManager object is responsible for br...
static std::mutex singletonMutex
Mutex for thread-safe access.
void setCoordinateMappingManager(CoordinateMappingManager *coordinateMappingManager)
Set the CoordinateMappingManager pointer.
OutputManager * outputManager
A pointer to OutputManager object. An OutputManager object is responsible for handling the final outp...
void setOpenCV_Manager(OpenCV_Manager *openCV_Manager)
Set the OpenCV_Manager pointer.
void setKafkaManager(KafkaManager *kafkaManager)
Set the KafkaManager pointer.
UsageManager * usageManager
A pointer to UsageManager object. An UsageManager object is responsible for checking if the parameter...
void getVideoBodyKeyPoints(int *argc, char ***argv)
Start and execute OpenPose submodule.
static FacadeSingleton * sharedInstance
The field for storing the singleton instance.
~FacadeSingleton(void)
Destroy the Facade Singleton object.
OpenCV_Manager * openCV_Manager
A pointer to OpenCV_Manager object. An OpenCV_Manager object is responsible for converting camera fra...
OutputManager * getOutputManager(void)
Get the OutputManager pointer.
void setCameraManager(RealSenseManager *cameraManager)
Set the RealSenseManager pointer.
UsageManager * getUsageManager(void)
Get the UsageManager pointer.
void getVideoFrames(unsigned int user_nFrame, rs2::pipeline &pipelineStream, float scale, const unsigned short int framesToSkip)
Get user_nFrame video frames from the pipeline by applying a specific scaling factor.
RealSenseManager * getCameraManager(void)
Get the RealSenseManager pointer.
void cleanBuildFolder(void)
This method cleans the build folder, in order to prepare the environment for the next loop iteration....
FacadeSingleton(const int expected_argc=0, const char *expectedUsageMessage=nullptr)
Construct a new Facade Singleton object.
void sendData(unsigned int user_nFrame)
Send saved results by getVideoBodyKeyPoints(...) via Kafka using the KafkaManager.
static FacadeSingleton * getInstance(void)
Get the unique class instance. This methods should be called in the following scenario: when we just ...
KafkaManager * getKafkaManager(void)
Get the KafkaManager pointer.
ImageManager class is a general-purpose class useful for operations on images such as loading,...
Kafka class is a class that is responsible to send all generated output data, via Apache Kafka techno...
void sendData(const char *key, Json::Value root)
Send data contained in root via Kafka with ID equal to key.
OpenCV_Manager class is used for general-purpose tasks on the captured frames.
void getVideoFramesCV(unsigned int user_nFrame, rs2::pipeline &pipelineStream, float scale, const unsigned short int framesToSkip)
Get user_nFrame video frames from the pipeline by applying a specific scaling factor.
void showSkeletonsCV(unsigned int user_nFrame, const float skeletonThreshold)
Retrieve OpenPose's output, convert it to RealSense coordinate's space, show results and save them.
The OpenPoseCommand class is a class that implements a command to start and run OpenPose.
OutputManagerJSON class is a class that abstracts final JSON output-producing operations.
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.
OutputManager class is a class that abstracts final output-producing operations. An output format inh...
RealSenseD435Manager class is a class that abstracts the behavior of an Intel RealSense D435 Camera,...
RealSenseManager class is a class that abstracts the behavior of an Intel RealSense Camera,...
virtual void startEnvironment(rs2::pipeline &pipelineStream, struct rs2_intrinsics &color_intrin, float *scale, unsigned short int resX, unsigned short int resY, bool firstBoot)=0
This method is useful to invoke at the boot of the program where we want to initialize the different ...
unsigned int getFrameID(void)
Get the current frame ID value.
Room class represents the Room abstraction. A Room object has different specifications,...
Definition: Room.hpp:64
SystemCommand class is a class that represents a command given by the program to the system.
void executeCommand(int *argc, char ***argv)
This method is responsible for executing commands on the system based on the filled command string....
The UnityCoordinateMappingManager class is a class that is responsible for converting coordinates val...
UsageManager class is a utility class that helps with configuration parameters.
char *** get_argv(void)
Get the argv's pointer.
static UsageManager * getInstance(void)
Get the unique class instance. This methods should be called in the following scenario: when we just ...
static const char * CONF_FILE_PATH
Definition: constants.hpp:59
static const bool FIRST_BOOT
Definition: constants.hpp:71
static const short int totalFileParameters
Definition: constants.hpp:66
static const short int outputFolderOffset
Definition: constants.hpp:65
static const short int NEW_ALLOC_ERROR
Definition: constants.hpp:47
static const char * NEW_ALLOC_SCOPE
Definition: constants.hpp:48
const char * expectedUsageMessage
Definition: main.cpp:214
const int expected_argc
Definition: main.cpp:213