OpenPose  1.7.0
The first real-time multi-person system to jointly detect human body, hand, facial, and foot keypoints
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>
17  class WQueueAssembler : public Worker<std::shared_ptr<TDatums>>
18  {
19  public:
20  explicit WQueueAssembler();
21 
22  virtual ~WQueueAssembler();
23 
25 
26  void work(std::shared_ptr<TDatums>& tDatums);
27 
28  private:
29  std::shared_ptr<TDatums> mNextTDatums;
30 
31  DELETE_COPY(WQueueAssembler);
32  };
33 }
34 
35 
36 
37 
38 
39 // Implementation
40 namespace op
41 {
42  template<typename TDatums>
44  {
45  }
46 
47  template<typename TDatums>
49  {
50  }
51 
52  template<typename TDatums>
54  {
55  }
56 
57  template<typename TDatums>
58  void WQueueAssembler<TDatums>::work(std::shared_ptr<TDatums>& tDatums)
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 tDatumPtr = (*tDatums)[0];
74  // Single view --> Return
75  if (tDatumPtr->subIdMax == 0)
76  return;
77  // Multiple view --> Merge views into different TDatums (1st frame)
78  if (mNextTDatums == nullptr)
79  mNextTDatums = std::make_shared<TDatums>();
80  // Multiple view --> Merge views into different TDatums
81  mNextTDatums->emplace_back(tDatumPtr);
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  opLogIfDebug("", 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 
109  extern template class WQueueAssembler<BASE_DATUMS>;
110 }
111 
112 #endif // OPENPOSE_THREAD_W_QUEUE_ASSEMBLER_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)
void work(std::shared_ptr< TDatums > &tDatums)
bool checkNoNullNorEmpty(const TPointerContainer &tPointerContainer)
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