OpenPose  1.7.0
The first real-time multi-person system to jointly detect human body, hand, facial, and foot keypoints
wPeopleJsonSaver.hpp
Go to the documentation of this file.
1 #ifndef OPENPOSE_FILESTREAM_W_PEOPLE_JSON_SAVER_HPP
2 #define OPENPOSE_FILESTREAM_W_PEOPLE_JSON_SAVER_HPP
3 
7 
8 namespace op
9 {
10  template<typename TDatums>
11  class WPeopleJsonSaver : public WorkerConsumer<TDatums>
12  {
13  public:
14  explicit WPeopleJsonSaver(const std::shared_ptr<PeopleJsonSaver>& peopleJsonSaver);
15 
16  virtual ~WPeopleJsonSaver();
17 
19 
20  void workConsumer(const TDatums& tDatums);
21 
22  private:
23  const std::shared_ptr<PeopleJsonSaver> spPeopleJsonSaver;
24 
25  DELETE_COPY(WPeopleJsonSaver);
26  };
27 }
28 
29 
30 
31 
32 
33 // Implementation
35 namespace op
36 {
37  template<typename TDatums>
38  WPeopleJsonSaver<TDatums>::WPeopleJsonSaver(const std::shared_ptr<PeopleJsonSaver>& peopleJsonSaver) :
39  spPeopleJsonSaver{peopleJsonSaver}
40  {
41  }
42 
43  template<typename TDatums>
45  {
46  }
47 
48  template<typename TDatums>
50  {
51  }
52 
53  template<typename TDatums>
54  void WPeopleJsonSaver<TDatums>::workConsumer(const TDatums& tDatums)
55  {
56  try
57  {
58  if (checkNoNullNorEmpty(tDatums))
59  {
60  // Debugging log
61  opLogIfDebug("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
62  // Profiling speed
63  const auto profilerKey = Profiler::timerInit(__LINE__, __FUNCTION__, __FILE__);
64  // Save body/face/hand keypoints to JSON file
65  const auto& tDatumFirstPtr = (*tDatums)[0];
66  const auto baseFileName = (!tDatumFirstPtr->name.empty() ? tDatumFirstPtr->name
67  : std::to_string(tDatumFirstPtr->id)) + "_keypoints";
68  const bool humanReadable = false;
69  for (auto i = 0u ; i < tDatums->size() ; i++)
70  {
71  const auto& tDatumPtr = (*tDatums)[i];
72  // const auto fileName = baseFileName;
73  const auto fileName = baseFileName + (i != 0 ? "_" + std::to_string(i) : "");
74 
75  // Pose IDs from long long to float
76  Array<float> poseIds{tDatumPtr->poseIds};
77 
78  const std::vector<std::pair<Array<float>, std::string>> keypointVector{
79  // Pose IDs
80  std::make_pair(poseIds, "person_id"),
81  // 2D
82  std::make_pair(tDatumPtr->poseKeypoints, "pose_keypoints_2d"),
83  std::make_pair(tDatumPtr->faceKeypoints, "face_keypoints_2d"),
84  std::make_pair(tDatumPtr->handKeypoints[0], "hand_left_keypoints_2d"),
85  std::make_pair(tDatumPtr->handKeypoints[1], "hand_right_keypoints_2d"),
86  // 3D
87  std::make_pair(tDatumPtr->poseKeypoints3D, "pose_keypoints_3d"),
88  std::make_pair(tDatumPtr->faceKeypoints3D, "face_keypoints_3d"),
89  std::make_pair(tDatumPtr->handKeypoints3D[0], "hand_left_keypoints_3d"),
90  std::make_pair(tDatumPtr->handKeypoints3D[1], "hand_right_keypoints_3d")
91  };
92  // Save keypoints
93  spPeopleJsonSaver->save(
94  keypointVector, tDatumPtr->poseCandidates, fileName, humanReadable);
95  }
96  // Profiling speed
97  Profiler::timerEnd(profilerKey);
98  Profiler::printAveragedTimeMsOnIterationX(profilerKey, __LINE__, __FUNCTION__, __FILE__);
99  // Debugging log
100  opLogIfDebug("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
101  }
102  }
103  catch (const std::exception& e)
104  {
105  this->stop();
106  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
107  }
108  }
109 
111 }
112 
113 #endif // OPENPOSE_FILESTREAM_W_PEOPLE_JSON_SAVER_HPP
static void printAveragedTimeMsOnIterationX(const std::string &key, const int line, const std::string &function, const std::string &file, const unsigned long long x=DEFAULT_X)
static const std::string timerInit(const int line, const std::string &function, const std::string &file)
static void timerEnd(const std::string &key)
WPeopleJsonSaver(const std::shared_ptr< PeopleJsonSaver > &peopleJsonSaver)
void workConsumer(const TDatums &tDatums)
bool checkNoNullNorEmpty(const TPointerContainer &tPointerContainer)
COMPILE_TEMPLATE_DATUM(WPoseTriangulation)
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
void opLogIfDebug(const T &message, const Priority priority=Priority::Max, const int line=-1, const std::string &function="", const std::string &file="")
Definition: errorAndLog.hpp:97