OpenPose 1.0.0rc2 OpenPose: A Real-Time Multi-Person Key-Point Detection And Multi-Threading C++ Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
wrapper.hpp
Go to the documentation of this file.
1 #ifndef OPENPOSE_WRAPPER_WRAPPER_HPP
2 #define OPENPOSE_WRAPPER_WRAPPER_HPP
3
4 #include <openpose/core/common.hpp>
5 #include <openpose/thread/headers.hpp>
6 #include <openpose/wrapper/enumClasses.hpp>
7 #include <openpose/wrapper/wrapperStructExtra.hpp>
8 #include <openpose/wrapper/wrapperStructFace.hpp>
9 #include <openpose/wrapper/wrapperStructGui.hpp>
10 #include <openpose/wrapper/wrapperStructHand.hpp>
11 #include <openpose/wrapper/wrapperStructInput.hpp>
12 #include <openpose/wrapper/wrapperStructOutput.hpp>
13 #include <openpose/wrapper/wrapperStructPose.hpp>
14
15 namespace op
16 {
32 template<typename TDatums = std::vector<Datum>,
33 typename TDatumsSP = std::shared_ptr<TDatums>,
34 typename TWorker = std::shared_ptr<Worker<TDatumsSP>>>
35 class WrapperT
36 {
37 public:
45 explicit WrapperT(const ThreadManagerMode threadManagerMode = ThreadManagerMode::Synchronous);
46
51 virtual ~WrapperT();
52
58 void disableMultiThreading();
59
67 void setWorker(const WorkerType workerType, const TWorker& worker, const bool workerOnNewThread = true);
68
72 void configure(const WrapperStructPose& wrapperStructPose);
73
77 void configure(const WrapperStructFace& wrapperStructFace);
78
82 void configure(const WrapperStructHand& wrapperStructHand);
83
87 void configure(const WrapperStructExtra& wrapperStructExtra);
88
92 void configure(const WrapperStructInput& wrapperStructInput);
93
97 void configure(const WrapperStructOutput& wrapperStructOutput);
98
102 void configure(const WrapperStructGui& wrapperStructGui);
103
109 void exec();
110
120 void start();
121
126 void stop();
127
133 bool isRunning() const;
134
142 bool tryEmplace(TDatumsSP& tDatums);
143
152 bool waitAndEmplace(TDatumsSP& tDatums);
153
160 bool tryPush(const TDatumsSP& tDatums);
161
168 bool waitAndPush(const TDatumsSP& tDatums);
169
177 bool tryPop(TDatumsSP& tDatums);
178
187 bool waitAndPop(TDatumsSP& tDatums);
188
192 bool emplaceAndPop(TDatumsSP& tDatums);
193
197 TDatumsSP emplaceAndPop(const cv::Mat& cvMat);
198
199 private:
200 const ThreadManagerMode mThreadManagerMode;
201 ThreadManager<TDatumsSP> mThreadManager;
202 bool mMultiThreadEnabled;
203 // Configuration
204 WrapperStructPose mWrapperStructPose;
205 WrapperStructFace mWrapperStructFace;
206 WrapperStructHand mWrapperStructHand;
207 WrapperStructExtra mWrapperStructExtra;
208 WrapperStructInput mWrapperStructInput;
209 WrapperStructOutput mWrapperStructOutput;
210 WrapperStructGui mWrapperStructGui;
211 // User configurable workers
212 std::array<bool, int(WorkerType::Size)> mUserWsOnNewThread;
213 std::array<std::vector<TWorker>, int(WorkerType::Size)> mUserWs;
214
215 DELETE_COPY(WrapperT);
216 };
217
218 // Type
219 typedef WrapperT<DATUM_BASE_NO_PTR> Wrapper;
220 }
221
222
223
224
225
226 // Implementation
227 #include <openpose/wrapper/wrapperAuxiliary.hpp>
228 namespace op
229 {
230 template<typename TDatums, typename TDatumsSP, typename TWorker>
231 WrapperT<TDatums, TDatumsSP, TWorker>::WrapperT(const ThreadManagerMode threadManagerMode) :
232 mThreadManagerMode{threadManagerMode},
233 mThreadManager{threadManagerMode},
234 mMultiThreadEnabled{true}
235 {
236 }
237
238 template<typename TDatums, typename TDatumsSP, typename TWorker>
239 WrapperT<TDatums, TDatumsSP, TWorker>::~WrapperT()
240 {
241 try
242 {
243 stop();
244 // Reset mThreadManager
245 mThreadManager.reset();
246 // Reset user workers
247 for (auto& userW : mUserWs)
248 userW.clear();
249 }
250 catch (const std::exception& e)
251 {
252 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
253 }
254 }
255
256 template<typename TDatums, typename TDatumsSP, typename TWorker>
257 void WrapperT<TDatums, TDatumsSP, TWorker>::disableMultiThreading()
258 {
259 try
260 {
261 mMultiThreadEnabled = false;
262 }
263 catch (const std::exception& e)
264 {
265 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
266 }
267 }
268
269 template<typename TDatums, typename TDatumsSP, typename TWorker>
270 void WrapperT<TDatums, TDatumsSP, TWorker>::setWorker(
271 const WorkerType workerType, const TWorker& worker, const bool workerOnNewThread)
272 {
273 try
274 {
275 // Sanity check
276 if (worker == nullptr)
277 error("Your worker is a nullptr.", __LINE__, __FILE__, __FUNCTION__);
278 // Add worker
279 mUserWs[int(workerType)].clear();
280 mUserWs[int(workerType)].emplace_back(worker);
281 mUserWsOnNewThread[int(workerType)] = workerOnNewThread;
282 }
283 catch (const std::exception& e)
284 {
285 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
286 }
287 }
288
289 template<typename TDatums, typename TDatumsSP, typename TWorker>
290 void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructPose& wrapperStructPose)
291 {
292 try
293 {
294 mWrapperStructPose = wrapperStructPose;
295 }
296 catch (const std::exception& e)
297 {
298 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
299 }
300 }
301
302 template<typename TDatums, typename TDatumsSP, typename TWorker>
303 void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructFace& wrapperStructFace)
304 {
305 try
306 {
307 mWrapperStructFace = wrapperStructFace;
308 }
309 catch (const std::exception& e)
310 {
311 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
312 }
313 }
314
315 template<typename TDatums, typename TDatumsSP, typename TWorker>
316 void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructHand& wrapperStructHand)
317 {
318 try
319 {
320 mWrapperStructHand = wrapperStructHand;
321 }
322 catch (const std::exception& e)
323 {
324 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
325 }
326 }
327
328 template<typename TDatums, typename TDatumsSP, typename TWorker>
329 void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructExtra& wrapperStructExtra)
330 {
331 try
332 {
333 mWrapperStructExtra = wrapperStructExtra;
334 }
335 catch (const std::exception& e)
336 {
337 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
338 }
339 }
340
341 template<typename TDatums, typename TDatumsSP, typename TWorker>
342 void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructInput& wrapperStructInput)
343 {
344 try
345 {
346 mWrapperStructInput = wrapperStructInput;
347 }
348 catch (const std::exception& e)
349 {
350 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
351 }
352 }
353
354 template<typename TDatums, typename TDatumsSP, typename TWorker>
355 void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructOutput& wrapperStructOutput)
356 {
357 try
358 {
359 mWrapperStructOutput = wrapperStructOutput;
360 }
361 catch (const std::exception& e)
362 {
363 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
364 }
365 }
366
367 template<typename TDatums, typename TDatumsSP, typename TWorker>
368 void WrapperT<TDatums, TDatumsSP, TWorker>::configure(const WrapperStructGui& wrapperStructGui)
369 {
370 try
371 {
372 mWrapperStructGui = wrapperStructGui;
373 }
374 catch (const std::exception& e)
375 {
376 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
377 }
378 }
379
380 template<typename TDatums, typename TDatumsSP, typename TWorker>
381 void WrapperT<TDatums, TDatumsSP, TWorker>::exec()
382 {
383 try
384 {
385 configureThreadManager<TDatums, TDatumsSP, TWorker>(
386 mThreadManager, mMultiThreadEnabled, mThreadManagerMode, mWrapperStructPose, mWrapperStructFace,
387 mWrapperStructHand, mWrapperStructExtra, mWrapperStructInput, mWrapperStructOutput, mWrapperStructGui,
388 mUserWs, mUserWsOnNewThread);
389 log("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
390 mThreadManager.exec();
391 }
392 catch (const std::exception& e)
393 {
394 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
395 }
396 }
397
398 template<typename TDatums, typename TDatumsSP, typename TWorker>
399 void WrapperT<TDatums, TDatumsSP, TWorker>::start()
400 {
401 try
402 {
403 configureThreadManager<TDatums, TDatumsSP, TWorker>(
404 mThreadManager, mMultiThreadEnabled, mThreadManagerMode, mWrapperStructPose, mWrapperStructFace,
405 mWrapperStructHand, mWrapperStructExtra, mWrapperStructInput, mWrapperStructOutput, mWrapperStructGui,
406 mUserWs, mUserWsOnNewThread);
407 log("", Priority::Low, __LINE__, __FUNCTION__, __FILE__);
408 mThreadManager.start();
409 }
410 catch (const std::exception& e)
411 {
412 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
413 }
414 }
415
416 template<typename TDatums, typename TDatumsSP, typename TWorker>
417 void WrapperT<TDatums, TDatumsSP, TWorker>::stop()
418 {
419 try
420 {
421 mThreadManager.stop();
422 }
423 catch (const std::exception& e)
424 {
425 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
426 }
427 }
428
429 template<typename TDatums, typename TDatumsSP, typename TWorker>
430 bool WrapperT<TDatums, TDatumsSP, TWorker>::isRunning() const
431 {
432 try
433 {
434 return mThreadManager.isRunning();
435 }
436 catch (const std::exception& e)
437 {
438 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
439 return false;
440 }
441 }
442
443 template<typename TDatums, typename TDatumsSP, typename TWorker>
444 bool WrapperT<TDatums, TDatumsSP, TWorker>::tryEmplace(TDatumsSP& tDatums)
445 {
446 try
447 {
448 if (!mUserWs[int(WorkerType::Input)].empty())
449 error("Emplace cannot be called if an input worker was already selected.",
450 __LINE__, __FUNCTION__, __FILE__);
451 return mThreadManager.tryEmplace(tDatums);
452 }
453 catch (const std::exception& e)
454 {
455 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
456 return false;
457 }
458 }
459
460 template<typename TDatums, typename TDatumsSP, typename TWorker>
461 bool WrapperT<TDatums, TDatumsSP, TWorker>::waitAndEmplace(TDatumsSP& tDatums)
462 {
463 try
464 {
465 if (!mUserWs[int(WorkerType::Input)].empty())
466 error("Emplace cannot be called if an input worker was already selected.",
467 __LINE__, __FUNCTION__, __FILE__);
468 return mThreadManager.waitAndEmplace(tDatums);
469 }
470 catch (const std::exception& e)
471 {
472 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
473 return false;
474 }
475 }
476
477 template<typename TDatums, typename TDatumsSP, typename TWorker>
478 bool WrapperT<TDatums, TDatumsSP, TWorker>::tryPush(const TDatumsSP& tDatums)
479 {
480 try
481 {
482 if (!mUserWs[int(WorkerType::Input)].empty())
483 error("Push cannot be called if an input worker was already selected.",
484 __LINE__, __FUNCTION__, __FILE__);
485 return mThreadManager.tryPush(tDatums);
486 }
487 catch (const std::exception& e)
488 {
489 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
490 return false;
491 }
492 }
493
494 template<typename TDatums, typename TDatumsSP, typename TWorker>
495 bool WrapperT<TDatums, TDatumsSP, TWorker>::waitAndPush(const TDatumsSP& tDatums)
496 {
497 try
498 {
499 if (!mUserWs[int(WorkerType::Input)].empty())
500 error("Push cannot be called if an input worker was already selected.",
501 __LINE__, __FUNCTION__, __FILE__);
502 return mThreadManager.waitAndPush(tDatums);
503 }
504 catch (const std::exception& e)
505 {
506 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
507 return false;
508 }
509 }
510
511 template<typename TDatums, typename TDatumsSP, typename TWorker>
512 bool WrapperT<TDatums, TDatumsSP, TWorker>::tryPop(TDatumsSP& tDatums)
513 {
514 try
515 {
516 if (!mUserWs[int(WorkerType::Output)].empty())
517 error("Pop cannot be called if an output worker was already selected.",
518 __LINE__, __FUNCTION__, __FILE__);
519 return mThreadManager.tryPop(tDatums);
520 }
521 catch (const std::exception& e)
522 {
523 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
524 return false;
525 }
526 }
527
528 template<typename TDatums, typename TDatumsSP, typename TWorker>
529 bool WrapperT<TDatums, TDatumsSP, TWorker>::waitAndPop(TDatumsSP& tDatums)
530 {
531 try
532 {
533 if (!mUserWs[int(WorkerType::Output)].empty())
534 error("Pop cannot be called if an output worker was already selected.",
535 __LINE__, __FUNCTION__, __FILE__);
536 return mThreadManager.waitAndPop(tDatums);
537 }
538 catch (const std::exception& e)
539 {
540 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
541 return false;
542 }
543 }
544
545 template<typename TDatums, typename TDatumsSP, typename TWorker>
546 bool WrapperT<TDatums, TDatumsSP, TWorker>::emplaceAndPop(TDatumsSP& tDatums)
547 {
548 try
549 {
550 // Run waitAndEmplace + waitAndPop
551 if (waitAndEmplace(tDatums))
552 return waitAndPop(tDatums);
553 return false;
554 }
555 catch (const std::exception& e)
556 {
557 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
558 return false;
559 }
560 }
561
562 template<typename TDatums, typename TDatumsSP, typename TWorker>
563 TDatumsSP WrapperT<TDatums, TDatumsSP, TWorker>::emplaceAndPop(const cv::Mat& cvMat)
564 {
565 try
566 {
567 // Create new datum
568 auto datumsPtr = std::make_shared<TDatums>();
569 datumsPtr->emplace_back();
570 auto& datum = datumsPtr->at(0);
571 // Fill datum
572 datum.cvInputData = cvMat;
573 // Emplace and pop
574 emplaceAndPop(datumsPtr);
575 // Return result
576 return datumsPtr;
577 }
578 catch (const std::exception& e)
579 {
580 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
581 return TDatumsSP{};
582 }
583 }
584
585 extern template class WrapperT<DATUM_BASE_NO_PTR>;
586 }
587
588 #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:270
op::Wrapper
WrapperT< DATUM_BASE_NO_PTR > Wrapper
Definition: wrapper.hpp:219
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:478
op::WrapperT::start
void start()
Definition: wrapper.hpp:399
op::WrapperT::waitAndEmplace
bool waitAndEmplace(TDatumsSP &tDatums)
Definition: wrapper.hpp:461
common.hpp
op::WrapperStructFace
Definition: wrapperStructFace.hpp:15
op::WrapperT::configure
void configure(const WrapperStructPose &wrapperStructPose)
Definition: wrapper.hpp:290
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:417
wrapperStructGui.hpp
wrapperStructOutput.hpp
op::WrapperT::isRunning
bool isRunning() const
Definition: wrapper.hpp:430
op::WrapperT::waitAndPop
bool waitAndPop(TDatumsSP &tDatums)
Definition: wrapper.hpp:529
op::WrapperT::WrapperT
WrapperT(const ThreadManagerMode threadManagerMode=ThreadManagerMode::Synchronous)
Definition: wrapper.hpp:231
op::WrapperStructInput
Definition: wrapperStructInput.hpp:14
op::WrapperStructGui
Definition: wrapperStructGui.hpp:12
op::WorkerType
WorkerType
Definition: enumClasses.hpp:6
op::WrapperT
Definition: wrapper.hpp:35
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:381
op::WrapperStructExtra
Definition: wrapperStructExtra.hpp:13
op::WrapperStructHand
Definition: wrapperStructHand.hpp:15
op::WrapperT::tryEmplace
bool tryEmplace(TDatumsSP &tDatums)
Definition: wrapper.hpp:444
op::WrapperT::tryPop
bool tryPop(TDatumsSP &tDatums)
Definition: wrapper.hpp:512
op::WrapperT::~WrapperT
virtual ~WrapperT()
Definition: wrapper.hpp:239
wrapperStructInput.hpp
op::ThreadManagerMode
ThreadManagerMode
Definition: enumClasses.hpp:9
op::WrapperT::emplaceAndPop
bool emplaceAndPop(TDatumsSP &tDatums)
Definition: wrapper.hpp:546
enumClasses.hpp
op::WrapperT::disableMultiThreading
void disableMultiThreading()
Definition: wrapper.hpp:257
op::WrapperStructOutput
Definition: wrapperStructOutput.hpp:13
op::WrapperT::waitAndPush
bool waitAndPush(const TDatumsSP &tDatums)
Definition: wrapper.hpp:495