OpenPose  1.7.0
The first real-time multi-person system to jointly detect human body, hand, facial, and foot keypoints
datumProducer.hpp
Go to the documentation of this file.
1 #ifndef OPENPOSE_PRODUCER_DATUM_PRODUCER_HPP
2 #define OPENPOSE_PRODUCER_DATUM_PRODUCER_HPP
3 
4 #include <atomic>
5 #include <limits> // std::numeric_limits
10 
11 namespace op
12 {
13  template<typename TDatum>
15  {
16  public:
17  explicit DatumProducer(
18  const std::shared_ptr<Producer>& producerSharedPtr,
19  const unsigned long long frameFirst = 0, const unsigned long long frameStep = 1,
20  const unsigned long long frameLast = std::numeric_limits<unsigned long long>::max(),
21  const std::shared_ptr<std::pair<std::atomic<bool>, std::atomic<int>>>& videoSeekSharedPtr = nullptr);
22 
23  virtual ~DatumProducer();
24 
25  std::pair<bool, std::shared_ptr<std::vector<std::shared_ptr<TDatum>>>> checkIfRunningAndGetDatum();
26 
27  private:
28  const unsigned long long mNumberFramesToProcess;
29  std::shared_ptr<Producer> spProducer;
30  unsigned long long mGlobalCounter;
31  unsigned long long mFrameStep;
32  unsigned int mNumberConsecutiveEmptyFrames;
33  std::shared_ptr<std::pair<std::atomic<bool>, std::atomic<int>>> spVideoSeek;
34 
35  void checkIfTooManyConsecutiveEmptyFrames(
36  unsigned int& numberConsecutiveEmptyFrames, const bool emptyFrame) const;
37 
38  DELETE_COPY(DatumProducer);
39  };
40 }
41 
42 
43 
44 
45 
46 // Implementation
49 namespace op
50 {
51  // Auxiliary functions for DatumProducer in order to 1) Reduce compiling time and 2) Remove OpenCV deps.
53  const std::shared_ptr<Producer>& producerSharedPtr, const unsigned long long frameFirst,
54  const unsigned long long frameStep, const unsigned long long frameLast);
56  unsigned int& numberConsecutiveEmptyFrames, const bool emptyFrame);
58  const std::shared_ptr<Producer>& producerSharedPtr, const unsigned long long numberFramesToProcess,
59  const unsigned long long globalCounter);
61  const std::shared_ptr<Producer>& producerSharedPtr,
62  const std::shared_ptr<std::pair<std::atomic<bool>, std::atomic<int>>>& videoSeekSharedPtr);
64  const std::shared_ptr<Producer>& producerSharedPtr);
66 
67  template<typename TDatum>
69  const std::shared_ptr<Producer>& producerSharedPtr,
70  const unsigned long long frameFirst, const unsigned long long frameStep,
71  const unsigned long long frameLast,
72  const std::shared_ptr<std::pair<std::atomic<bool>, std::atomic<int>>>& videoSeekSharedPtr) :
73  mNumberFramesToProcess{(frameLast != std::numeric_limits<unsigned long long>::max()
74  ? frameLast - frameFirst : frameLast)},
75  spProducer{producerSharedPtr},
76  mGlobalCounter{0ll},
77  mFrameStep{frameStep},
78  mNumberConsecutiveEmptyFrames{0u},
79  spVideoSeek{videoSeekSharedPtr}
80  {
81  try
82  {
83  datumProducerConstructor(producerSharedPtr, frameFirst, frameStep, frameLast);
84  }
85  catch (const std::exception& e)
86  {
87  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
88  }
89  }
90 
91  template<typename TDatum>
93  {
94  }
95 
96  template<typename TDatum>
97  std::pair<bool, std::shared_ptr<std::vector<std::shared_ptr<TDatum>>>> DatumProducer<TDatum>::checkIfRunningAndGetDatum()
98  {
99  try
100  {
101  // If producer released -> it sends an empty Matrix + a datumProducerRunning signal
103  spProducer, mNumberFramesToProcess, mGlobalCounter);
104  // If device is open
105  auto datums = std::make_shared<std::vector<std::shared_ptr<TDatum>>>();
106  if (datumProducerRunning)
107  {
108  // Fast forward/backward - Seek to specific frame index desired
110  // Get Matrix vector
111  std::string nextFrameName = spProducer->getNextFrameName();
112  const unsigned long long nextFrameNumber = datumProducerConstructorRunningAndGetNextFrameNumber(
113  spProducer);
114  const std::vector<Matrix> matrices = spProducer->getFrames();
115  // Check frames are not empty
116  checkIfTooManyConsecutiveEmptyFrames(
117  mNumberConsecutiveEmptyFrames, matrices.empty() || matrices[0].empty());
118  if (!matrices.empty())
119  {
120  // Get camera parameters
121  const std::vector<Matrix> cameraMatrices = spProducer->getCameraMatrices();
122  const std::vector<Matrix> cameraExtrinsics = spProducer->getCameraExtrinsics();
123  const std::vector<Matrix> cameraIntrinsics = spProducer->getCameraIntrinsics();
124  // Resize datum
125  datums->resize(matrices.size());
126  // Datum cannot be assigned before resize()
127  auto& datumPtr = (*datums)[0];
128  datumPtr = std::make_shared<TDatum>();
129  // Filling first element
130  std::swap(datumPtr->name, nextFrameName);
131  datumPtr->frameNumber = nextFrameNumber;
132  datumPtr->cvInputData = matrices[0];
134  if (!cameraMatrices.empty())
135  {
136  datumPtr->cameraMatrix = cameraMatrices[0];
137  datumPtr->cameraExtrinsics = cameraExtrinsics[0];
138  datumPtr->cameraIntrinsics = cameraIntrinsics[0];
139  }
140  // Initially, cvOutputData = cvInputData. No performance hit (both cv::Mat share raw memory)
141  datumPtr->cvOutputData = datumPtr->cvInputData;
142  // Resize if it's stereo-system
143  if (datums->size() > 1)
144  {
145  // Stereo-system: Assign all Matrices
146  for (auto i = 1u ; i < datums->size() ; i++)
147  {
148  auto& datumIPtr = (*datums)[i];
149  datumIPtr = std::make_shared<TDatum>();
150  datumIPtr->name = datumPtr->name;
151  datumIPtr->frameNumber = datumPtr->frameNumber;
152  datumIPtr->cvInputData = matrices[i];
154  datumIPtr->cvOutputData = datumIPtr->cvInputData;
155  if (cameraMatrices.size() > i)
156  {
157  datumIPtr->cameraMatrix = cameraMatrices[i];
158  datumIPtr->cameraExtrinsics = cameraExtrinsics[i];
159  datumIPtr->cameraIntrinsics = cameraIntrinsics[i];
160  }
161  }
162  }
163  // Check producer is running
164  if ((*datums)[0]->cvInputData.empty())
165  datums = nullptr;
166  // Increase counter if successful image
167  if (datums != nullptr)
168  mGlobalCounter += mFrameStep;
169  }
170  }
171  // Return result
172  return std::make_pair(datumProducerRunning, datums);
173  }
174  catch (const std::exception& e)
175  {
176  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
177  return std::make_pair(false, std::make_shared<std::vector<std::shared_ptr<TDatum>>>());
178  }
179  }
180 
181  template<typename TDatum>
183  unsigned int& numberConsecutiveEmptyFrames, const bool emptyFrame) const
184  {
186  numberConsecutiveEmptyFrames, emptyFrame);
187  }
188 
189  extern template class DatumProducer<BASE_DATUM>;
190 }
191 
192 
193 #endif // OPENPOSE_PRODUCER_DATUM_PRODUCER_HPP
std::pair< bool, std::shared_ptr< std::vector< std::shared_ptr< TDatum > > > > checkIfRunningAndGetDatum()
DatumProducer(const std::shared_ptr< Producer > &producerSharedPtr, const unsigned long long frameFirst=0, const unsigned long long frameStep=1, const unsigned long long frameLast=std::numeric_limits< unsigned long long >::max(), const std::shared_ptr< std::pair< std::atomic< bool >, std::atomic< int >>> &videoSeekSharedPtr=nullptr)
virtual ~DatumProducer()
#define OP_API
Definition: macros.hpp:18
OP_API void datumProducerConstructorRunningAndGetDatumApplyPlayerControls(const std::shared_ptr< Producer > &producerSharedPtr, const std::shared_ptr< std::pair< std::atomic< bool >, std::atomic< int >>> &videoSeekSharedPtr)
OP_API void datumProducerConstructorRunningAndGetDatumFrameIntegrity(Matrix &matrix)
OP_API void datumProducerConstructorTooManyConsecutiveEmptyFrames(unsigned int &numberConsecutiveEmptyFrames, const bool emptyFrame)
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
OP_API bool datumProducerConstructorRunningAndGetDatumIsDatumProducerRunning(const std::shared_ptr< Producer > &producerSharedPtr, const unsigned long long numberFramesToProcess, const unsigned long long globalCounter)
OP_API unsigned long long datumProducerConstructorRunningAndGetNextFrameNumber(const std::shared_ptr< Producer > &producerSharedPtr)
OP_API void datumProducerConstructor(const std::shared_ptr< Producer > &producerSharedPtr, const unsigned long long frameFirst, const unsigned long long frameStep, const unsigned long long frameLast)