1 #ifndef OPENPOSE_WRAPPER_WRAPPER_HPP
2 #define OPENPOSE_WRAPPER_WRAPPER_HPP
33 typename TDatums = std::vector<std::shared_ptr<TDatum>>,
34 typename TDatumsSP = std::shared_ptr<TDatums>,
35 typename TWorker = std::shared_ptr<Worker<TDatumsSP>>>
68 void setWorker(
const WorkerType workerType,
const TWorker& worker,
const bool workerOnNewThread =
true);
179 bool tryPush(
const TDatumsSP& tDatums);
203 bool tryPop(TDatumsSP& tDatums);
232 bool mMultiThreadEnabled;
260 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
262 mThreadManagerMode{threadManagerMode},
263 mThreadManager{threadManagerMode},
264 mMultiThreadEnabled{true}
268 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
275 mThreadManager.reset();
277 for (
auto& userW : mUserWs)
280 catch (
const std::exception& e)
286 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
291 mMultiThreadEnabled =
false;
293 catch (
const std::exception& e)
295 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
299 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
301 const WorkerType workerType,
const TWorker& worker,
const bool workerOnNewThread)
306 if (worker ==
nullptr)
307 error(
"Your worker is a nullptr.", __LINE__, __FILE__, __FUNCTION__);
309 mUserWs[int(workerType)].clear();
310 mUserWs[int(workerType)].emplace_back(worker);
311 mUserWsOnNewThread[int(workerType)] = workerOnNewThread;
313 catch (
const std::exception& e)
315 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
319 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
324 mWrapperStructPose = wrapperStructPose;
326 catch (
const std::exception& e)
328 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
332 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
337 mWrapperStructFace = wrapperStructFace;
339 catch (
const std::exception& e)
341 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
345 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
350 mWrapperStructHand = wrapperStructHand;
352 catch (
const std::exception& e)
354 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
358 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
363 mWrapperStructExtra = wrapperStructExtra;
365 catch (
const std::exception& e)
367 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
371 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
376 mWrapperStructInput = wrapperStructInput;
378 catch (
const std::exception& e)
380 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
384 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
389 mWrapperStructOutput = wrapperStructOutput;
391 catch (
const std::exception& e)
393 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
397 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
402 mWrapperStructGui = wrapperStructGui;
404 catch (
const std::exception& e)
406 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
410 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
415 configureThreadManager<TDatum, TDatums, TDatumsSP, TWorker>(
416 mThreadManager, mMultiThreadEnabled, mThreadManagerMode, mWrapperStructPose, mWrapperStructFace,
417 mWrapperStructHand, mWrapperStructExtra, mWrapperStructInput, mWrapperStructOutput, mWrapperStructGui,
418 mUserWs, mUserWsOnNewThread);
420 mThreadManager.exec();
422 catch (
const std::exception& e)
424 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
428 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
433 configureThreadManager<TDatum, TDatums, TDatumsSP, TWorker>(
434 mThreadManager, mMultiThreadEnabled, mThreadManagerMode, mWrapperStructPose, mWrapperStructFace,
435 mWrapperStructHand, mWrapperStructExtra, mWrapperStructInput, mWrapperStructOutput, mWrapperStructGui,
436 mUserWs, mUserWsOnNewThread);
438 mThreadManager.start();
440 catch (
const std::exception& e)
442 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
446 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
451 mThreadManager.stop();
453 catch (
const std::exception& e)
455 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
459 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
464 return mThreadManager.isRunning();
466 catch (
const std::exception& e)
468 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
473 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
478 mThreadManager.setDefaultMaxSizeQueues(defaultMaxSizeQueues);
480 catch (
const std::exception& e)
482 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
486 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
492 error(
"Emplace cannot be called if an input worker was already selected.",
493 __LINE__, __FUNCTION__, __FILE__);
495 if (tDatums->size() < 2)
497 return mThreadManager.tryEmplace(tDatums);
502 bool successfulEmplace =
true;
503 for (
auto datumIndex = 0u; datumIndex < tDatums->size(); ++datumIndex)
505 auto tDatumsSingle = std::make_shared<TDatums>(TDatums({ tDatums->at(datumIndex) }));
506 if (!tryEmplace(tDatumsSingle))
508 successfulEmplace =
false;
512 return successfulEmplace;
515 catch (
const std::exception& e)
517 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
522 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
528 error(
"Emplace cannot be called if an input worker was already selected.",
529 __LINE__, __FUNCTION__, __FILE__);
531 if (tDatums->size() < 2)
533 return mThreadManager.waitAndEmplace(tDatums);
538 bool successfulEmplace =
true;
539 for (
auto datumIndex = 0u ; datumIndex < tDatums->size() ; ++datumIndex)
541 auto tDatumsSingle = std::make_shared<TDatums>(TDatums({tDatums->at(datumIndex)}));
542 if (!waitAndEmplace(tDatumsSingle))
544 successfulEmplace =
false;
545 opLog(
"Waiting to emplace for multi-camera failed.",
550 return successfulEmplace;
553 catch (
const std::exception& e)
555 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
560 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
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>();
571 std::swap(tDatumPtr->cvInputData, matrix);
573 return waitAndEmplace(datumsPtr);
575 catch (
const std::exception& e)
577 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
582 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
588 error(
"Push cannot be called if an input worker was already selected.",
589 __LINE__, __FUNCTION__, __FILE__);
590 return mThreadManager.tryPush(tDatums);
592 catch (
const std::exception& e)
594 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
599 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
605 error(
"Push cannot be called if an input worker was already selected.",
606 __LINE__, __FUNCTION__, __FILE__);
607 return mThreadManager.waitAndPush(tDatums);
609 catch (
const std::exception& e)
611 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
616 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
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>();
627 tDatumPtr->cvInputData = matrix.
clone();
629 return waitAndEmplace(datumsPtr);
631 catch (
const std::exception& e)
633 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
638 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
644 error(
"Pop cannot be called if an output worker was already selected.",
645 __LINE__, __FUNCTION__, __FILE__);
646 return mThreadManager.tryPop(tDatums);
648 catch (
const std::exception& e)
650 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
655 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
661 error(
"Pop cannot be called if an output worker was already selected.",
662 __LINE__, __FUNCTION__, __FILE__);
663 return mThreadManager.waitAndPop(tDatums);
665 catch (
const std::exception& e)
667 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
672 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
678 if (waitAndEmplace(tDatums))
679 return waitAndPop(tDatums);
682 catch (
const std::exception& e)
684 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
689 template<
typename TDatum,
typename TDatums,
typename TDatumsSP,
typename TWorker>
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>();
700 tDatumPtr->cvInputData = matrix;
702 emplaceAndPop(datumsPtr);
706 catch (
const std::exception& e)
708 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
void setWorker(const WorkerType workerType, const TWorker &worker, const bool workerOnNewThread=true)
bool waitAndPush(const TDatumsSP &tDatums)
bool emplaceAndPop(TDatumsSP &tDatums)
bool waitAndPop(TDatumsSP &tDatums)
bool waitAndEmplace(TDatumsSP &tDatums)
bool tryPush(const TDatumsSP &tDatums)
bool tryPop(TDatumsSP &tDatums)
void disableMultiThreading()
void configure(const WrapperStructPose &wrapperStructPose)
bool tryEmplace(TDatumsSP &tDatums)
WrapperT(const ThreadManagerMode threadManagerMode=ThreadManagerMode::Synchronous)
void setDefaultMaxSizeQueues(const long long defaultMaxSizeQueues=-1)
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
WrapperT< BASE_DATUM > Wrapper
OP_API void errorDestructor(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
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="")