OpenPose  1.7.0
The first real-time multi-person system to jointly detect human body, hand, facial, and foot keypoints
wrapper.hpp
Go to the documentation of this file.
1 #ifndef OPENPOSE_WRAPPER_WRAPPER_HPP
2 #define OPENPOSE_WRAPPER_WRAPPER_HPP
3 
14 
15 namespace op
16 {
32  template<typename TDatum = BASE_DATUM,
33  typename TDatums = std::vector<std::shared_ptr<TDatum>>,
34  typename TDatumsSP = std::shared_ptr<TDatums>,
35  typename TWorker = std::shared_ptr<Worker<TDatumsSP>>>
36  class WrapperT
37  {
38  public:
46  explicit WrapperT(const ThreadManagerMode threadManagerMode = ThreadManagerMode::Synchronous);
47 
52  virtual ~WrapperT();
53 
59  void disableMultiThreading();
60 
68  void setWorker(const WorkerType workerType, const TWorker& worker, const bool workerOnNewThread = true);
69 
73  void configure(const WrapperStructPose& wrapperStructPose);
74 
78  void configure(const WrapperStructFace& wrapperStructFace);
79 
83  void configure(const WrapperStructHand& wrapperStructHand);
84 
88  void configure(const WrapperStructExtra& wrapperStructExtra);
89 
93  void configure(const WrapperStructInput& wrapperStructInput);
94 
98  void configure(const WrapperStructOutput& wrapperStructOutput);
99 
103  void configure(const WrapperStructGui& wrapperStructGui);
104 
110  void exec();
111 
121  void start();
122 
127  void stop();
128 
134  bool isRunning() const;
135 
145  void setDefaultMaxSizeQueues(const long long defaultMaxSizeQueues = -1);
146 
154  bool tryEmplace(TDatumsSP& tDatums);
155 
164  bool waitAndEmplace(TDatumsSP& tDatums);
165 
171  bool waitAndEmplace(Matrix& matrix);
172 
179  bool tryPush(const TDatumsSP& tDatums);
180 
187  bool waitAndPush(const TDatumsSP& tDatums);
188 
194  bool waitAndPush(const Matrix& matrix);
195 
203  bool tryPop(TDatumsSP& tDatums);
204 
213  bool waitAndPop(TDatumsSP& tDatums);
214 
220  bool emplaceAndPop(TDatumsSP& tDatums);
221 
227  TDatumsSP emplaceAndPop(const Matrix& matrix);
228 
229  private:
230  const ThreadManagerMode mThreadManagerMode;
231  ThreadManager<TDatumsSP> mThreadManager;
232  bool mMultiThreadEnabled;
233  // Configuration
234  WrapperStructPose mWrapperStructPose;
235  WrapperStructFace mWrapperStructFace;
236  WrapperStructHand mWrapperStructHand;
237  WrapperStructExtra mWrapperStructExtra;
238  WrapperStructInput mWrapperStructInput;
239  WrapperStructOutput mWrapperStructOutput;
240  WrapperStructGui mWrapperStructGui;
241  // User configurable workers
242  std::array<bool, int(WorkerType::Size)> mUserWsOnNewThread;
243  std::array<std::vector<TWorker>, int(WorkerType::Size)> mUserWs;
244 
245  DELETE_COPY(WrapperT);
246  };
247 
248  // Type
250 }
251 
252 
253 
254 
255 
256 // Implementation
258 namespace op
259 {
260  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
262  mThreadManagerMode{threadManagerMode},
263  mThreadManager{threadManagerMode},
264  mMultiThreadEnabled{true}
265  {
266  }
267 
268  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
270  {
271  try
272  {
273  stop();
274  // Reset mThreadManager
275  mThreadManager.reset();
276  // Reset user workers
277  for (auto& userW : mUserWs)
278  userW.clear();
279  }
280  catch (const std::exception& e)
281  {
282  errorDestructor(e.what(), __LINE__, __FUNCTION__, __FILE__);
283  }
284  }
285 
286  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
288  {
289  try
290  {
291  mMultiThreadEnabled = false;
292  }
293  catch (const std::exception& e)
294  {
295  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
296  }
297  }
298 
299  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
301  const WorkerType workerType, const TWorker& worker, const bool workerOnNewThread)
302  {
303  try
304  {
305  // Sanity check
306  if (worker == nullptr)
307  error("Your worker is a nullptr.", __LINE__, __FILE__, __FUNCTION__);
308  // Add worker
309  mUserWs[int(workerType)].clear();
310  mUserWs[int(workerType)].emplace_back(worker);
311  mUserWsOnNewThread[int(workerType)] = workerOnNewThread;
312  }
313  catch (const std::exception& e)
314  {
315  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
316  }
317  }
318 
319  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
321  {
322  try
323  {
324  mWrapperStructPose = wrapperStructPose;
325  }
326  catch (const std::exception& e)
327  {
328  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
329  }
330  }
331 
332  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
334  {
335  try
336  {
337  mWrapperStructFace = wrapperStructFace;
338  }
339  catch (const std::exception& e)
340  {
341  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
342  }
343  }
344 
345  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
347  {
348  try
349  {
350  mWrapperStructHand = wrapperStructHand;
351  }
352  catch (const std::exception& e)
353  {
354  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
355  }
356  }
357 
358  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
360  {
361  try
362  {
363  mWrapperStructExtra = wrapperStructExtra;
364  }
365  catch (const std::exception& e)
366  {
367  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
368  }
369  }
370 
371  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
373  {
374  try
375  {
376  mWrapperStructInput = wrapperStructInput;
377  }
378  catch (const std::exception& e)
379  {
380  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
381  }
382  }
383 
384  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
386  {
387  try
388  {
389  mWrapperStructOutput = wrapperStructOutput;
390  }
391  catch (const std::exception& e)
392  {
393  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
394  }
395  }
396 
397  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
399  {
400  try
401  {
402  mWrapperStructGui = wrapperStructGui;
403  }
404  catch (const std::exception& e)
405  {
406  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
407  }
408  }
409 
410  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
412  {
413  try
414  {
415  configureThreadManager<TDatum, TDatums, TDatumsSP, TWorker>(
416  mThreadManager, mMultiThreadEnabled, mThreadManagerMode, mWrapperStructPose, mWrapperStructFace,
417  mWrapperStructHand, mWrapperStructExtra, mWrapperStructInput, mWrapperStructOutput, mWrapperStructGui,
418  mUserWs, mUserWsOnNewThread);
419  opLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
420  mThreadManager.exec();
421  }
422  catch (const std::exception& e)
423  {
424  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
425  }
426  }
427 
428  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
430  {
431  try
432  {
433  configureThreadManager<TDatum, TDatums, TDatumsSP, TWorker>(
434  mThreadManager, mMultiThreadEnabled, mThreadManagerMode, mWrapperStructPose, mWrapperStructFace,
435  mWrapperStructHand, mWrapperStructExtra, mWrapperStructInput, mWrapperStructOutput, mWrapperStructGui,
436  mUserWs, mUserWsOnNewThread);
437  opLog("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
438  mThreadManager.start();
439  }
440  catch (const std::exception& e)
441  {
442  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
443  }
444  }
445 
446  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
448  {
449  try
450  {
451  mThreadManager.stop();
452  }
453  catch (const std::exception& e)
454  {
455  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
456  }
457  }
458 
459  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
461  {
462  try
463  {
464  return mThreadManager.isRunning();
465  }
466  catch (const std::exception& e)
467  {
468  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
469  return false;
470  }
471  }
472 
473  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
475  {
476  try
477  {
478  mThreadManager.setDefaultMaxSizeQueues(defaultMaxSizeQueues);
479  }
480  catch (const std::exception& e)
481  {
482  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
483  }
484  }
485 
486  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
488  {
489  try
490  {
491  if (!mUserWs[int(WorkerType::Input)].empty())
492  error("Emplace cannot be called if an input worker was already selected.",
493  __LINE__, __FUNCTION__, __FILE__);
494  // tryEmplace for 1 camera
495  if (tDatums->size() < 2)
496  {
497  return mThreadManager.tryEmplace(tDatums);
498  }
499  // tryEmplace for multiview
500  else
501  {
502  bool successfulEmplace = true;
503  for (auto datumIndex = 0u; datumIndex < tDatums->size(); ++datumIndex)
504  {
505  auto tDatumsSingle = std::make_shared<TDatums>(TDatums({ tDatums->at(datumIndex) }));
506  if (!tryEmplace(tDatumsSingle))
507  {
508  successfulEmplace = false;
509  break;
510  }
511  }
512  return successfulEmplace;
513  }
514  }
515  catch (const std::exception& e)
516  {
517  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
518  return false;
519  }
520  }
521 
522  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
524  {
525  try
526  {
527  if (!mUserWs[int(WorkerType::Input)].empty())
528  error("Emplace cannot be called if an input worker was already selected.",
529  __LINE__, __FUNCTION__, __FILE__);
530  // waitAndEmplace for 1 camera
531  if (tDatums->size() < 2)
532  {
533  return mThreadManager.waitAndEmplace(tDatums);
534  }
535  // waitAndEmplace for multiview
536  else
537  {
538  bool successfulEmplace = true;
539  for (auto datumIndex = 0u ; datumIndex < tDatums->size() ; ++datumIndex)
540  {
541  auto tDatumsSingle = std::make_shared<TDatums>(TDatums({tDatums->at(datumIndex)}));
542  if (!waitAndEmplace(tDatumsSingle))
543  {
544  successfulEmplace = false;
545  opLog("Waiting to emplace for multi-camera failed.",
546  Priority::High, __LINE__, __FUNCTION__, __FILE__);
547  break;
548  }
549  }
550  return successfulEmplace;
551  }
552  }
553  catch (const std::exception& e)
554  {
555  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
556  return false;
557  }
558  }
559 
560  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
562  {
563  try
564  {
565  // Create new datum
566  auto datumsPtr = std::make_shared<std::vector<std::shared_ptr<TDatum>>>();
567  datumsPtr->emplace_back();
568  auto& tDatumPtr = datumsPtr->at(0);
569  tDatumPtr = std::make_shared<TDatum>();
570  // Fill datum
571  std::swap(tDatumPtr->cvInputData, matrix);
572  // Return result
573  return waitAndEmplace(datumsPtr);
574  }
575  catch (const std::exception& e)
576  {
577  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
578  return false;
579  }
580  }
581 
582  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
584  {
585  try
586  {
587  if (!mUserWs[int(WorkerType::Input)].empty())
588  error("Push cannot be called if an input worker was already selected.",
589  __LINE__, __FUNCTION__, __FILE__);
590  return mThreadManager.tryPush(tDatums);
591  }
592  catch (const std::exception& e)
593  {
594  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
595  return false;
596  }
597  }
598 
599  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
601  {
602  try
603  {
604  if (!mUserWs[int(WorkerType::Input)].empty())
605  error("Push cannot be called if an input worker was already selected.",
606  __LINE__, __FUNCTION__, __FILE__);
607  return mThreadManager.waitAndPush(tDatums);
608  }
609  catch (const std::exception& e)
610  {
611  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
612  return false;
613  }
614  }
615 
616  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
618  {
619  try
620  {
621  // Create new datum
622  auto datumsPtr = std::make_shared<std::vector<std::shared_ptr<TDatum>>>();
623  datumsPtr->emplace_back();
624  auto& tDatumPtr = datumsPtr->at(0);
625  tDatumPtr = std::make_shared<TDatum>();
626  // Fill datum
627  tDatumPtr->cvInputData = matrix.clone();
628  // Return result
629  return waitAndEmplace(datumsPtr);
630  }
631  catch (const std::exception& e)
632  {
633  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
634  return false;
635  }
636  }
637 
638  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
640  {
641  try
642  {
643  if (!mUserWs[int(WorkerType::Output)].empty())
644  error("Pop cannot be called if an output worker was already selected.",
645  __LINE__, __FUNCTION__, __FILE__);
646  return mThreadManager.tryPop(tDatums);
647  }
648  catch (const std::exception& e)
649  {
650  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
651  return false;
652  }
653  }
654 
655  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
657  {
658  try
659  {
660  if (!mUserWs[int(WorkerType::Output)].empty())
661  error("Pop cannot be called if an output worker was already selected.",
662  __LINE__, __FUNCTION__, __FILE__);
663  return mThreadManager.waitAndPop(tDatums);
664  }
665  catch (const std::exception& e)
666  {
667  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
668  return false;
669  }
670  }
671 
672  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
674  {
675  try
676  {
677  // Run waitAndEmplace + waitAndPop
678  if (waitAndEmplace(tDatums))
679  return waitAndPop(tDatums);
680  return false;
681  }
682  catch (const std::exception& e)
683  {
684  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
685  return false;
686  }
687  }
688 
689  template<typename TDatum, typename TDatums, typename TDatumsSP, typename TWorker>
691  {
692  try
693  {
694  // Create new datum
695  auto datumsPtr = std::make_shared<std::vector<std::shared_ptr<TDatum>>>();
696  datumsPtr->emplace_back();
697  auto& tDatumPtr = datumsPtr->at(0);
698  tDatumPtr = std::make_shared<TDatum>();
699  // Fill datum
700  tDatumPtr->cvInputData = matrix;
701  // Emplace and pop
702  emplaceAndPop(datumsPtr);
703  // Return result
704  return datumsPtr;
705  }
706  catch (const std::exception& e)
707  {
708  error(e.what(), __LINE__, __FUNCTION__, __FILE__);
709  return TDatumsSP{};
710  }
711  }
712 
713  extern template class WrapperT<BASE_DATUM>;
714 }
715 
716 #endif // OPENPOSE_WRAPPER_WRAPPER_HPP
Matrix clone() const
bool isRunning() const
Definition: wrapper.hpp:460
void stop()
Definition: wrapper.hpp:447
void setWorker(const WorkerType workerType, const TWorker &worker, const bool workerOnNewThread=true)
Definition: wrapper.hpp:300
bool waitAndPush(const TDatumsSP &tDatums)
Definition: wrapper.hpp:600
bool emplaceAndPop(TDatumsSP &tDatums)
Definition: wrapper.hpp:673
bool waitAndPop(TDatumsSP &tDatums)
Definition: wrapper.hpp:656
bool waitAndEmplace(TDatumsSP &tDatums)
Definition: wrapper.hpp:523
void exec()
Definition: wrapper.hpp:411
bool tryPush(const TDatumsSP &tDatums)
Definition: wrapper.hpp:583
bool tryPop(TDatumsSP &tDatums)
Definition: wrapper.hpp:639
virtual ~WrapperT()
Definition: wrapper.hpp:269
void disableMultiThreading()
Definition: wrapper.hpp:287
void configure(const WrapperStructPose &wrapperStructPose)
Definition: wrapper.hpp:320
bool tryEmplace(TDatumsSP &tDatums)
Definition: wrapper.hpp:487
void start()
Definition: wrapper.hpp:429
WrapperT(const ThreadManagerMode threadManagerMode=ThreadManagerMode::Synchronous)
Definition: wrapper.hpp:261
void setDefaultMaxSizeQueues(const long long defaultMaxSizeQueues=-1)
Definition: wrapper.hpp:474
#define BASE_DATUM
Definition: datum.hpp:403
ThreadManagerMode
Definition: enumClasses.hpp:10
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
WrapperT< BASE_DATUM > Wrapper
Definition: wrapper.hpp:249
OP_API void errorDestructor(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
WorkerType
Definition: enumClasses.hpp:24
OP_API void opLog(const std::string &message, const Priority priority=Priority::Max, const int line=-1, const std::string &function="", const std::string &file="")