All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
wrapper.hpp
Go to the documentation of this file.
#ifndef OPENPOSE_WRAPPER_WRAPPER_HPP
#define OPENPOSE_WRAPPER_WRAPPER_HPP
#include <openpose/core/common.hpp>
#include <openpose/thread/headers.hpp>
#include <openpose/wrapper/enumClasses.hpp>
#include <openpose/wrapper/wrapperStructExtra.hpp>
#include <openpose/wrapper/wrapperStructFace.hpp>
#include <openpose/wrapper/wrapperStructHand.hpp>
#include <openpose/wrapper/wrapperStructInput.hpp>
#include <openpose/wrapper/wrapperStructOutput.hpp>
#include <openpose/wrapper/wrapperStructPose.hpp>
namespace op
{
template<typename TDatums = std::vector<Datum>,
typename TDatumsSP = std::shared_ptr<TDatums>,
typename TWorker = std::shared_ptr<Worker<TDatumsSP>>>
class WrapperT
{
public:
explicit WrapperT(const ThreadManagerMode threadManagerMode = ThreadManagerMode::Synchronous);
virtual ~WrapperT();
void disableMultiThreading();
void setWorker(const WorkerType workerType, const TWorker& worker, const bool workerOnNewThread = true);
void configure(const WrapperStructPose& wrapperStructPose);
void configure(const WrapperStructFace& wrapperStructFace);
void configure(const WrapperStructHand& wrapperStructHand);
void configure(const WrapperStructExtra& wrapperStructExtra);
void configure(const WrapperStructInput& wrapperStructInput);
void configure(const WrapperStructOutput& wrapperStructOutput);
void exec();
void start();
void stop();
bool isRunning() const;
bool tryEmplace(TDatumsSP& tDatums);
bool waitAndEmplace(TDatumsSP& tDatums);
bool tryPush(const TDatumsSP& tDatums);
bool waitAndPush(const TDatumsSP& tDatums);
bool tryPop(TDatumsSP& tDatums);
bool waitAndPop(TDatumsSP& tDatums);
bool emplaceAndPop(TDatumsSP& tDatums);
TDatumsSP emplaceAndPop(const cv::Mat& cvMat);
private:
const ThreadManagerMode mThreadManagerMode;
ThreadManager<TDatumsSP> mThreadManager;
bool mMultiThreadEnabled;
// Configuration
WrapperStructPose mWrapperStructPose;
WrapperStructFace mWrapperStructFace;
WrapperStructHand mWrapperStructHand;
WrapperStructExtra mWrapperStructExtra;
WrapperStructInput mWrapperStructInput;
WrapperStructOutput mWrapperStructOutput;
// User configurable workers
std::array<bool, int(WorkerType::Size)> mUserWsOnNewThread;
std::array<std::vector<TWorker>, int(WorkerType::Size)> mUserWs;
DELETE_COPY(WrapperT);
};
// Type
typedef WrapperT<DATUM_BASE_NO_PTR> Wrapper;
}
// Implementation
#include <openpose/wrapper/wrapperAuxiliary.hpp>
namespace op
{
template<typename TDatums, typename TDatumsSP, typename TWorker>
WrapperT<TDatums, TDatumsSP, TWorker>::WrapperT(const ThreadManagerMode threadManagerMode) :
mThreadManagerMode{threadManagerMode},
mThreadManager{threadManagerMode},
mMultiThreadEnabled{true}
{
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
WrapperT<TDatums, TDatumsSP, TWorker>::~WrapperT()
{
try
{
stop();
// Reset mThreadManager
mThreadManager.reset();
// Reset user workers
for (auto& userW : mUserWs)
userW.clear();
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
void WrapperT<TDatums, TDatumsSP, TWorker>::disableMultiThreading()
{
try
{
mMultiThreadEnabled = false;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
void WrapperT<TDatums, TDatumsSP, TWorker>::setWorker(
const WorkerType workerType, const TWorker& worker, const bool workerOnNewThread)
{
try
{
// Sanity check
if (worker == nullptr)
error("Your worker is a nullptr.", __LINE__, __FILE__, __FUNCTION__);
// Add worker
mUserWs[int(workerType)].clear();
mUserWs[int(workerType)].emplace_back(worker);
mUserWsOnNewThread[int(workerType)] = workerOnNewThread;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructPose& wrapperStructPose)
{
try
{
mWrapperStructPose = wrapperStructPose;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructFace& wrapperStructFace)
{
try
{
mWrapperStructFace = wrapperStructFace;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructHand& wrapperStructHand)
{
try
{
mWrapperStructHand = wrapperStructHand;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructExtra& wrapperStructExtra)
{
try
{
mWrapperStructExtra = wrapperStructExtra;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructInput& wrapperStructInput)
{
try
{
mWrapperStructInput = wrapperStructInput;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructOutput& wrapperStructOutput)
{
try
{
mWrapperStructOutput = wrapperStructOutput;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
void WrapperT<TDatums, TDatumsSP, TWorker>::exec()
{
try
{
configureThreadManager<TDatums, TDatumsSP, TWorker>(
mThreadManager, mMultiThreadEnabled, mThreadManagerMode, mWrapperStructPose, mWrapperStructFace,
mWrapperStructHand, mWrapperStructExtra, mWrapperStructInput, mWrapperStructOutput,
mUserWs, mUserWsOnNewThread);
log("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
mThreadManager.exec();
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
void WrapperT<TDatums, TDatumsSP, TWorker>::start()
{
try
{
configureThreadManager<TDatums, TDatumsSP, TWorker>(
mThreadManager, mMultiThreadEnabled, mThreadManagerMode, mWrapperStructPose, mWrapperStructFace,
mWrapperStructHand, mWrapperStructExtra, mWrapperStructInput, mWrapperStructOutput,
mUserWs, mUserWsOnNewThread);
log("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
mThreadManager.start();
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
void WrapperT<TDatums, TDatumsSP, TWorker>::stop()
{
try
{
mThreadManager.stop();
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
bool WrapperT<TDatums, TDatumsSP, TWorker>::isRunning() const
{
try
{
return mThreadManager.isRunning();
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
return false;
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
bool WrapperT<TDatums, TDatumsSP, TWorker>::tryEmplace(TDatumsSP& tDatums)
{
try
{
if (!mUserWs[int(WorkerType::Input)].empty())
error("Emplace cannot be called if an input worker was already selected.",
__LINE__, __FUNCTION__, __FILE__);
return mThreadManager.tryEmplace(tDatums);
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
return false;
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
bool WrapperT<TDatums, TDatumsSP, TWorker>::waitAndEmplace(TDatumsSP& tDatums)
{
try
{
if (!mUserWs[int(WorkerType::Input)].empty())
error("Emplace cannot be called if an input worker was already selected.",
__LINE__, __FUNCTION__, __FILE__);
return mThreadManager.waitAndEmplace(tDatums);
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
return false;
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
bool WrapperT<TDatums, TDatumsSP, TWorker>::tryPush(const TDatumsSP& tDatums)
{
try
{
if (!mUserWs[int(WorkerType::Input)].empty())
error("Push cannot be called if an input worker was already selected.",
__LINE__, __FUNCTION__, __FILE__);
return mThreadManager.tryPush(tDatums);
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
return false;
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
bool WrapperT<TDatums, TDatumsSP, TWorker>::waitAndPush(const TDatumsSP& tDatums)
{
try
{
if (!mUserWs[int(WorkerType::Input)].empty())
error("Push cannot be called if an input worker was already selected.",
__LINE__, __FUNCTION__, __FILE__);
return mThreadManager.waitAndPush(tDatums);
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
return false;
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
bool WrapperT<TDatums, TDatumsSP, TWorker>::tryPop(TDatumsSP& tDatums)
{
try
{
if (!mUserWs[int(WorkerType::Output)].empty())
error("Pop cannot be called if an output worker was already selected.",
__LINE__, __FUNCTION__, __FILE__);
return mThreadManager.tryPop(tDatums);
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
return false;
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
bool WrapperT<TDatums, TDatumsSP, TWorker>::waitAndPop(TDatumsSP& tDatums)
{
try
{
if (!mUserWs[int(WorkerType::Output)].empty())
error("Pop cannot be called if an output worker was already selected.",
__LINE__, __FUNCTION__, __FILE__);
return mThreadManager.waitAndPop(tDatums);
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
return false;
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
bool WrapperT<TDatums, TDatumsSP, TWorker>::emplaceAndPop(TDatumsSP& tDatums)
{
try
{
// Run waitAndEmplace + waitAndPop
if (waitAndEmplace(tDatums))
return waitAndPop(tDatums);
return false;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
return false;
}
}
template<typename TDatums, typename TDatumsSP, typename TWorker>
TDatumsSP WrapperT<TDatums, TDatumsSP, TWorker>::emplaceAndPop(const cv::Mat& cvMat)
{
try
{
// Create new datum
auto datumsPtr = std::make_shared<TDatums>();
datumsPtr->emplace_back();
auto& datum = datumsPtr->at(0);
// Fill datum
datum.cvInputData = cvMat;
// Emplace and pop
emplaceAndPop(datumsPtr);
// Return result
return datumsPtr;
}
catch (const std::exception& e)
{
error(e.what(), __LINE__, __FUNCTION__, __FILE__);
return TDatumsSP{};
}
}
extern template class WrapperT<DATUM_BASE_NO_PTR>;
}
#endif // OPENPOSE_WRAPPER_WRAPPER_HPP
headers.hpp
op::Priority::Low
op::WrapperT::setWorker
void setWorker(const WorkerType workerType, const TWorker &worker, const bool workerOnNewThread=true)
Definition: wrapper.hpp:263
op::Wrapper
WrapperT< DATUM_BASE_NO_PTR > Wrapper
Definition: wrapper.hpp:212
op::WrapperStructPose
Definition: wrapperStructPose.hpp:17
op::WorkerType::Size
op::ThreadManagerMode::Synchronous
wrapperAuxiliary.hpp
wrapperStructFace.hpp
wrapperStructExtra.hpp
op::WrapperT::tryPush
bool tryPush(const TDatumsSP &tDatums)
Definition: wrapper.hpp:458
op::WrapperT::start
void start()
Definition: wrapper.hpp:379
op::WrapperT::waitAndEmplace
bool waitAndEmplace(TDatumsSP &tDatums)
Definition: wrapper.hpp:441
common.hpp
op::WrapperStructFace
Definition: wrapperStructFace.hpp:15
op::WrapperT::configure
void configure(const WrapperStructPose &wrapperStructPose)
Definition: wrapper.hpp:283
op::error
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
op::WorkerType::Input
op::WorkerType::Output
op::WrapperT::stop
void stop()
Definition: wrapper.hpp:397
wrapperStructOutput.hpp
op::WrapperT::isRunning
bool isRunning() const
Definition: wrapper.hpp:410
op::WrapperT::waitAndPop
bool waitAndPop(TDatumsSP &tDatums)
Definition: wrapper.hpp:509
op::WrapperT::WrapperT
WrapperT(const ThreadManagerMode threadManagerMode=ThreadManagerMode::Synchronous)
Definition: wrapper.hpp:224
op::WrapperStructInput
Definition: wrapperStructInput.hpp:14
op::WorkerType
WorkerType
Definition: enumClasses.hpp:6
op::WrapperT
Definition: wrapper.hpp:34
wrapperStructPose.hpp
wrapperStructHand.hpp
op::ThreadManager< TDatumsSP >
op::log
OP_API void log(const std::string &message, const Priority priority=Priority::Max, const int line=-1, const std::string &function="", const std::string &file="")
op::WrapperT::exec
void exec()
Definition: wrapper.hpp:361
op::WrapperStructExtra
Definition: wrapperStructExtra.hpp:13
op::WrapperStructHand
Definition: wrapperStructHand.hpp:15
op::WrapperT::tryEmplace
bool tryEmplace(TDatumsSP &tDatums)
Definition: wrapper.hpp:424
op::WrapperT::tryPop
bool tryPop(TDatumsSP &tDatums)
Definition: wrapper.hpp:492
op::WrapperT::~WrapperT
virtual ~WrapperT()
Definition: wrapper.hpp:232
wrapperStructInput.hpp
op::ThreadManagerMode
ThreadManagerMode
Definition: enumClasses.hpp:9
op::WrapperT::emplaceAndPop
bool emplaceAndPop(TDatumsSP &tDatums)
Definition: wrapper.hpp:526
enumClasses.hpp
op::WrapperT::disableMultiThreading
void disableMultiThreading()
Definition: wrapper.hpp:250
op::WrapperStructOutput
Definition: wrapperStructOutput.hpp:14
op::WrapperT::waitAndPush
bool waitAndPush(const TDatumsSP &tDatums)
Definition: wrapper.hpp:475