OpenPose  1.0.0rc2
OpenPose: A Real-Time Multi-Person Key-Point Detection And Multi-Threading C++ Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
wQueueAssembler.hpp
Go to the documentation of this file.
1 #ifndef OPENPOSE_THREAD_W_QUEUE_ASSEMBLER_HPP
2 #define OPENPOSE_THREAD_W_QUEUE_ASSEMBLER_HPP
3 
4 #include <queue> // std::queue
8 
9 namespace op
10 {
11  // Note: The goal of WQueueAssembler and WQueueSplitter (integrated in wDatumProducer) is to reduce the latency
12  // of OpenPose. E.g., if 4 cameras in stereo mode, without this, OpenPose would have to process all 4 cameras
13  // with the same GPU. In this way, this work is parallelized over GPUs (1 view for each).
14  // Pros: Latency highly recuded, same speed
15  // Cons: Requires these extra 2 classes and proper threads for them
16  template<typename TDatums, typename TDatumsNoPtr>
17  class WQueueAssembler : public Worker<TDatums>
18  {
19  public:
20  explicit WQueueAssembler();
21 
23 
24  void work(TDatums& tDatums);
25 
26  private:
27  TDatums mNextTDatums;
28 
29  DELETE_COPY(WQueueAssembler);
30  };
31 }
32 
33 
34 
35 
36 
37 // Implementation
38 #include <chrono>
39 #include <thread>
40 namespace op
41 {
42  template<typename TDatums, typename TDatumsNoPtr>
44  {
45  }
46 
47  template<typename TDatums, typename TDatumsNoPtr>
49  {
50  }
51 
52  template<typename TDatums, typename TDatumsNoPtr>
54  {
55  try
56  {
57  // Profiling speed
58  const auto profilerKey = Profiler::timerInit(__LINE__, __FUNCTION__, __FILE__);
59  // Input TDatums -> enqueue it
60  if (checkNoNullNorEmpty(tDatums))
61  {
62  // Security check
63  if (tDatums->size() > 1)
64  error("This function assumes that WQueueSplitter (inside WDatumProducer)"
65  " was applied in the first place, i.e., that there is only 1 element"
66  " per TDatums (size = " + std::to_string(tDatums->size()) + ").",
67  __LINE__, __FUNCTION__, __FILE__);
68  auto tDatum = (*tDatums)[0];
69  // Single view --> Return
70  if (tDatum.subIdMax == 0)
71  return;
72  // Multiple view --> Merge views into different TDatums (1st frame)
73  if (mNextTDatums == nullptr)
74  mNextTDatums = std::make_shared<TDatumsNoPtr>();
75  // Multiple view --> Merge views into different TDatums
76  mNextTDatums->emplace_back(tDatum);
77  // Last view - Return frame
78  if (mNextTDatums->back().subId == mNextTDatums->back().subIdMax)
79  {
80  tDatums = mNextTDatums;
81  mNextTDatums = nullptr;
82  // Profiling speed
83  Profiler::timerEnd(profilerKey);
84  Profiler::printAveragedTimeMsOnIterationX(profilerKey, __LINE__, __FUNCTION__, __FILE__);
85  // Debugging log
86  dLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
87  }
88  // Non-last view - Return nothing
89  else
90  tDatums = nullptr;
91  }
92  // Sleep if no new tDatums to either pop or push
93  else
94  std::this_thread::sleep_for(std::chrono::milliseconds{1});
95  }
96  catch (const std::exception& e)
97  {
98  this->stop();
99  tDatums = nullptr;
100  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
101  }
102  }
103 
105 }
106 
107 #endif // OPENPOSE_THREAD_W_QUEUE_ASSEMBLER_HPP
Definition: worker.hpp:9
Definition: wQueueAssembler.hpp:17
static const std::string timerInit(const int line, const std::string &function, const std::string &file)
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
void work(TDatums &tDatums)
Definition: wQueueAssembler.hpp:53
WQueueAssembler()
Definition: wQueueAssembler.hpp:43
void initializationOnThread()
Definition: wQueueAssembler.hpp:48
void dLog(const T &message, const Priority priority=Priority::Max, const int line=-1, const std::string &function="", const std::string &file="")
Definition: errorAndLog.hpp:53
bool checkNoNullNorEmpty(const TPointerContainer &tPointerContainer)
Definition: pointerContainer.hpp:7
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 void timerEnd(const std::string &key)