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 
22  virtual ~WQueueAssembler();
23 
25 
26  void work(TDatums& tDatums);
27 
28  private:
29  TDatums mNextTDatums;
30 
31  DELETE_COPY(WQueueAssembler);
32  };
33 }
34 
35 
36 
37 
38 
39 // Implementation
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  }
56 
57  template<typename TDatums, typename TDatumsNoPtr>
59  {
60  try
61  {
62  // Profiling speed
63  const auto profilerKey = Profiler::timerInit(__LINE__, __FUNCTION__, __FILE__);
64  // Input TDatums -> enqueue it
65  if (checkNoNullNorEmpty(tDatums))
66  {
67  // Sanity check
68  if (tDatums->size() > 1)
69  error("This function assumes that WQueueSplitter (inside WDatumProducer)"
70  " was applied in the first place, i.e., that there is only 1 element"
71  " per TDatums (size = " + std::to_string(tDatums->size()) + ").",
72  __LINE__, __FUNCTION__, __FILE__);
73  auto tDatum = (*tDatums)[0];
74  // Single view --> Return
75  if (tDatum.subIdMax == 0)
76  return;
77  // Multiple view --> Merge views into different TDatums (1st frame)
78  if (mNextTDatums == nullptr)
79  mNextTDatums = std::make_shared<TDatumsNoPtr>();
80  // Multiple view --> Merge views into different TDatums
81  mNextTDatums->emplace_back(tDatum);
82  // Last view - Return frame
83  if (mNextTDatums->back().subId == mNextTDatums->back().subIdMax)
84  {
85  tDatums = mNextTDatums;
86  mNextTDatums = nullptr;
87  // Profiling speed
88  Profiler::timerEnd(profilerKey);
89  Profiler::printAveragedTimeMsOnIterationX(profilerKey, __LINE__, __FUNCTION__, __FILE__);
90  // Debugging log
91  dLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
92  }
93  // Non-last view - Return nothing
94  else
95  tDatums = nullptr;
96  }
97  // Sleep if no new tDatums to either pop or push
98  else
99  std::this_thread::sleep_for(std::chrono::milliseconds{1});
100  }
101  catch (const std::exception& e)
102  {
103  this->stop();
104  tDatums = nullptr;
105  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
106  }
107  }
108 
110 }
111 
112 #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="")
virtual ~WQueueAssembler()
Definition: wQueueAssembler.hpp:48
void work(TDatums &tDatums)
Definition: wQueueAssembler.hpp:58
WQueueAssembler()
Definition: wQueueAssembler.hpp:43
void initializationOnThread()
Definition: wQueueAssembler.hpp:53
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)