Example #1
0
        private static void ConfigureWrapper(Wrapper <Datum> opWrapper)
        {
            try
            {
                // Configuring OpenPose

                // logging_level
                OpenPose.CheckBool(0 <= Flags.LoggingLevel && Flags.LoggingLevel <= 255, "Wrong logging_level value.");
                ConfigureLog.PriorityThreshold = (Priority)Flags.LoggingLevel;
                Profiler.SetDefaultX((ulong)Flags.ProfileSpeed);

                // Applying user defined configuration - GFlags to program variables
                // outputSize
                var outputSize = OpenPose.FlagsToPoint(Flags.OutputResolution, "-1x-1");
                // netInputSize
                var netInputSize = OpenPose.FlagsToPoint(Flags.NetResolution, "-1x368");
                // faceNetInputSize
                var faceNetInputSize = OpenPose.FlagsToPoint(Flags.FaceNetResolution, "368x368 (multiples of 16)");
                // handNetInputSize
                var handNetInputSize = OpenPose.FlagsToPoint(Flags.HandNetResolution, "368x368 (multiples of 16)");
                // poseMode
                var poseMode = OpenPose.FlagsToPoseMode(Flags.Body);
                // poseModel
                var poseModel = OpenPose.FlagsToPoseModel(Flags.ModelPose);
                // JSON saving
                if (!string.IsNullOrEmpty(Flags.WriteKeyPoint))
                {
                    OpenPose.Log("Flag `write_keypoint` is deprecated and will eventually be removed. Please, use `write_json` instead.", Priority.Max);
                }
                // keypointScale
                var keypointScale = OpenPose.FlagsToScaleMode(Flags.KeyPointScale);
                // heatmaps to add
                var heatMapTypes = OpenPose.FlagsToHeatMaps(Flags.HeatmapsAddParts, Flags.HeatmapsAddBackground, Flags.HeatmapsAddPAFs);
                var heatMapScale = OpenPose.FlagsToHeatMapScaleMode(Flags.HeatmapsScale);
                // >1 camera view?
                var multipleView = (Flags.Enable3D || Flags.Views3D > 1);
                // Face and hand detectors
                var faceDetector = OpenPose.FlagsToDetector(Flags.FaceDetector);
                var handDetector = OpenPose.FlagsToDetector(Flags.HandDetector);
                // Enabling Google Logging
                const bool enableGoogleLogging = true;

                // Pose configuration (use WrapperStructPose{} for default and recommended configuration)
                var pose = new WrapperStructPose(poseMode,
                                                 netInputSize,
                                                 outputSize,
                                                 keypointScale,
                                                 Flags.NumGpu,
                                                 Flags.NumGpuStart,
                                                 Flags.ScaleNumber,
                                                 (float)Flags.ScaleGap,
                                                 OpenPose.FlagsToRenderMode(Flags.RenderPose, multipleView),
                                                 poseModel,
                                                 !Flags.DisableBlending,
                                                 (float)Flags.AlphaPose,
                                                 (float)Flags.AlphaHeatmap,
                                                 Flags.PartToShow,
                                                 Flags.ModelFolder,
                                                 heatMapTypes,
                                                 heatMapScale,
                                                 Flags.PartCandidates,
                                                 (float)Flags.RenderThreshold,
                                                 Flags.NumberPeopleMax,
                                                 Flags.MaximizePositives,
                                                 Flags.FpsMax,
                                                 Flags.PrototxtPath,
                                                 Flags.CaffeModelPath,
                                                 (float)Flags.UpsamplingRatio,
                                                 enableGoogleLogging);
                // Face configuration (use op::WrapperStructFace{} to disable it)
                var face = new WrapperStructFace(Flags.Face,
                                                 faceDetector,
                                                 faceNetInputSize,
                                                 OpenPose.FlagsToRenderMode(Flags.FaceRender, multipleView, Flags.RenderPose),
                                                 (float)Flags.FaceAlphaPose,
                                                 (float)Flags.FaceAlphaHeatmap,
                                                 (float)Flags.FaceRenderThreshold);
                // Hand configuration (use op::WrapperStructHand{} to disable it)
                var hand = new WrapperStructHand(Flags.Hand,
                                                 handDetector,
                                                 handNetInputSize,
                                                 Flags.HandScaleNumber,
                                                 (float)Flags.HandScaleRange,
                                                 OpenPose.FlagsToRenderMode(Flags.HandRender, multipleView, Flags.RenderPose),
                                                 (float)Flags.HandAlphaPose,
                                                 (float)Flags.HandAlphaHeatmap,
                                                 (float)Flags.HandRenderThreshold);
                // Extra functionality configuration (use op::WrapperStructExtra{} to disable it)
                var extra = new WrapperStructExtra(Flags.Enable3D,
                                                   Flags.MinViews3D,
                                                   Flags.Identification,
                                                   Flags.Tracking,
                                                   Flags.IkThreads);
                // Output (comment or use default argument to disable any output)
                var output = new WrapperStructOutput(Flags.CliVerbose,
                                                     Flags.WriteKeyPoint,
                                                     OpenPose.StringToDataFormat(Flags.WriteKeyPointFormat),
                                                     Flags.WriteJson,
                                                     Flags.WriteCocoJson,
                                                     Flags.WriteCocoJsonVariants,
                                                     Flags.WriteCocoJsonVariant,
                                                     Flags.WriteImages,
                                                     Flags.WriteImagesFormat,
                                                     Flags.WriteVideo,
                                                     Flags.WriteVideoFps,
                                                     Flags.WriteVideoWithAudio,
                                                     Flags.WriteHeatmaps,
                                                     Flags.WriteHeatmapsFormat,
                                                     Flags.WriteVideoAdam,
                                                     Flags.WriteBvh,
                                                     Flags.UdpHost,
                                                     Flags.UdpPort);

                opWrapper.Configure(pose);
                opWrapper.Configure(face);
                opWrapper.Configure(hand);
                opWrapper.Configure(extra);
                opWrapper.Configure(output);

                // No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
                // Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
                if (Flags.DisableMultiThread)
                {
                    opWrapper.DisableMultiThreading();
                }

                // Return successful message
                OpenPose.Log("Stopping OpenPose...", Priority.High);
            }
            catch (Exception e)
            {
                OpenPose.Error(e.Message, -1, nameof(ConfigureWrapper));
            }
        }
        private static void ConfigureWrapper(Wrapper <CustomDatum> opWrapper)
        {
            try
            {
                // Configuring OpenPose

                // logging_level
                OpenPose.CheckBool(0 <= Flags.LoggingLevel && Flags.LoggingLevel <= 255, "Wrong logging_level value.");
                ConfigureLog.PriorityThreshold = (Priority)Flags.LoggingLevel;
                Profiler.SetDefaultX((ulong)Flags.ProfileSpeed);

                // Applying user defined configuration - GFlags to program variables
                // producerType
                var tie            = OpenPose.FlagsToProducer(Flags.ImageDir, Flags.Video, Flags.IpCamera, Flags.Camera, Flags.FlirCamera, Flags.FlirCameraIndex);
                var producerType   = tie.Item1;
                var producerString = tie.Item2;
                // cameraSize
                var cameraSize = OpenPose.FlagsToPoint(Flags.CameraResolution, "-1x-1");
                // outputSize
                var outputSize = OpenPose.FlagsToPoint(Flags.OutputResolution, "-1x-1");
                // netInputSize
                var netInputSize = OpenPose.FlagsToPoint(Flags.NetResolution, "-1x368");
                // faceNetInputSize
                var faceNetInputSize = OpenPose.FlagsToPoint(Flags.FaceNetResolution, "368x368 (multiples of 16)");
                // handNetInputSize
                var handNetInputSize = OpenPose.FlagsToPoint(Flags.HandNetResolution, "368x368 (multiples of 16)");
                // poseMode
                var poseMode = OpenPose.FlagsToPoseMode(Flags.Body);
                // poseModel
                var poseModel = OpenPose.FlagsToPoseModel(Flags.ModelPose);
                // JSON saving
                if (!string.IsNullOrEmpty(Flags.WriteKeyPoint))
                {
                    OpenPose.Log("Flag `write_keypoint` is deprecated and will eventually be removed. Please, use `write_json` instead.");
                }
                // keypointScale
                var keyPointScale = OpenPose.FlagsToScaleMode(Flags.KeyPointScale);
                // heatmaps to add
                var heatMapTypes = OpenPose.FlagsToHeatMaps(Flags.HeatmapsAddParts, Flags.HeatmapsAddBackground, Flags.HeatmapsAddPAFs);
                var heatMapScale = OpenPose.FlagsToHeatMapScaleMode(Flags.HeatmapsScale);
                // >1 camera view?
                var multipleView = (Flags.Enable3D || Flags.Views3D > 1 || Flags.FlirCamera);
                // Face and hand detectors
                var faceDetector = OpenPose.FlagsToDetector(Flags.FaceDetector);
                var handDetector = OpenPose.FlagsToDetector(Flags.HandDetector);
                // Enabling Google Logging
                const bool enableGoogleLogging = true;

                // Configuring OpenPose
                OpenPose.Log("Configuring OpenPose...", Priority.High);
                {
                    // Pose configuration (use WrapperStructPose{} for default and recommended configuration)
                    var pose = new WrapperStructPose(poseMode,
                                                     netInputSize,
                                                     outputSize,
                                                     keyPointScale,
                                                     Flags.NumGpu,
                                                     Flags.NumGpuStart,
                                                     Flags.ScaleNumber,
                                                     (float)Flags.ScaleGap,
                                                     OpenPose.FlagsToRenderMode(Flags.RenderPose, multipleView),
                                                     poseModel,
                                                     !Flags.DisableBlending,
                                                     (float)Flags.AlphaPose,
                                                     (float)Flags.AlphaHeatmap,
                                                     Flags.PartToShow,
                                                     Flags.ModelFolder,
                                                     heatMapTypes,
                                                     heatMapScale,
                                                     Flags.PartCandidates,
                                                     (float)Flags.RenderThreshold,
                                                     Flags.NumberPeopleMax,
                                                     Flags.MaximizePositives,
                                                     Flags.FpsMax,
                                                     Flags.PrototxtPath,
                                                     Flags.CaffeModelPath,
                                                     (float)Flags.UpsamplingRatio,
                                                     enableGoogleLogging);
                    // Face configuration (use op::WrapperStructFace{} to disable it)
                    var face = new WrapperStructFace(Flags.Face,
                                                     faceDetector,
                                                     faceNetInputSize,
                                                     OpenPose.FlagsToRenderMode(Flags.FaceRender, multipleView, Flags.RenderPose),
                                                     (float)Flags.FaceAlphaPose,
                                                     (float)Flags.FaceAlphaHeatmap,
                                                     (float)Flags.FaceRenderThreshold);
                    // Hand configuration (use op::WrapperStructHand{} to disable it)
                    var hand = new WrapperStructHand(Flags.Hand,
                                                     handDetector,
                                                     handNetInputSize,
                                                     Flags.HandScaleNumber,
                                                     (float)Flags.HandScaleRange,
                                                     OpenPose.FlagsToRenderMode(Flags.HandRender, multipleView, Flags.RenderPose),
                                                     (float)Flags.HandAlphaPose,
                                                     (float)Flags.HandAlphaHeatmap,
                                                     (float)Flags.HandRenderThreshold);
                    // Extra functionality configuration (use op::WrapperStructExtra{} to disable it)
                    var extra = new WrapperStructExtra(Flags.Enable3D,
                                                       Flags.MinViews3D,
                                                       Flags.Identification,
                                                       Flags.Tracking,
                                                       Flags.IkThreads);
                    // Producer (use default to disable any input)
                    var input = new WrapperStructInput(producerType,
                                                       producerString,
                                                       Flags.FrameFirst,
                                                       Flags.FrameStep,
                                                       Flags.FrameLast,
                                                       Flags.ProcessRealTime,
                                                       Flags.FrameFlip,
                                                       Flags.FrameRotate,
                                                       Flags.FramesRepeat,
                                                       cameraSize,
                                                       Flags.CameraParameterPath,
                                                       Flags.FrameUndistort,
                                                       Flags.Views3D);
                    // Output (comment or use default argument to disable any output)
                    var output = new WrapperStructOutput(Flags.CliVerbose,
                                                         Flags.WriteKeyPoint,
                                                         OpenPose.StringToDataFormat(Flags.WriteKeyPointFormat),
                                                         Flags.WriteJson,
                                                         Flags.WriteCocoJson,
                                                         Flags.WriteCocoJsonVariants,
                                                         Flags.WriteCocoJsonVariant,
                                                         Flags.WriteImages,
                                                         Flags.WriteImagesFormat,
                                                         Flags.WriteVideo,
                                                         Flags.WriteVideoFps,
                                                         Flags.WriteVideoWithAudio,
                                                         Flags.WriteHeatmaps,
                                                         Flags.WriteHeatmapsFormat,
                                                         Flags.WriteVideoAdam,
                                                         Flags.WriteBvh,
                                                         Flags.UdpHost,
                                                         Flags.UdpPort);
                    // GUI (comment or use default argument to disable any visual output)
                    var gui = new WrapperStructGui(OpenPose.FlagsToDisplayMode(Flags.Display, Flags.Enable3D),
                                                   !Flags.NoGuiVerbose,
                                                   Flags.FullScreen);
                    opWrapper.Configure(pose);
                    opWrapper.Configure(face);
                    opWrapper.Configure(hand);
                    opWrapper.Configure(extra);
                    opWrapper.Configure(input);
                    opWrapper.Configure(output);
                    opWrapper.Configure(gui);

                    // Custom post-processing
                    var userPostProcessing  = new UserPostProcessing(/* Your class arguments here */);
                    var wUserPostProcessing = new StdSharedPtr <UserWorker <CustomDatum> >(new WUserPostProcessing(userPostProcessing));

                    // Add custom processing
                    const bool workerProcessingOnNewThread = false;
                    opWrapper.SetWorker(WorkerType.PostProcessing, wUserPostProcessing, workerProcessingOnNewThread);

                    // Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
                    if (Flags.DisableMultiThread)
                    {
                        opWrapper.DisableMultiThreading();
                    }
                }
            }
            catch (Exception e)
            {
                OpenPose.Error(e.Message, -1, nameof(ConfigureWrapper));
            }
        }
        private static int OpenPoseTutorialThread2()
        {
            try
            {
                OpenPose.Log("Starting OpenPose demo...", Priority.High);
                using (var opTimer = OpenPose.GetTimerInit())
                {
                    // ------------------------- INITIALIZATION -------------------------
                    // Step 1 - Set logging level
                    // - 0 will output all the logging messages
                    // - 255 will output nothing
                    OpenPose.CheckBool(0 <= Flags.LoggingLevel && Flags.LoggingLevel <= 255, "Wrong logging_level value.");
                    ConfigureLog.PriorityThreshold = (Priority)Flags.LoggingLevel;
                    // Step 2 - Setting thread workers && manage
                    using (var threadManager = new ThreadManager <UserDatum>())
                    {
                        // Step 3 - Initializing the worker classes
                        // Frames producer (e.g., video, webcam, ...)
                        using (var wUserInput = new StdSharedPtr <UserWorkerProducer <UserDatum> >(new WUserInput(Flags.ImageDir)))
                        {
                            // Processing
                            using (var wUserProcessing = new StdSharedPtr <UserWorker <UserDatum> >(new WUserPostProcessing()))
                            {
                                // GUI (Display)
                                using (var wUserOutput = new StdSharedPtr <UserWorkerConsumer <UserDatum> >(new WUserOutput()))
                                {
                                    // ------------------------- CONFIGURING THREADING -------------------------
                                    // In this simple multi-thread example, we will do the following:
                                    // 3 (virtual) queues: 0, 1, 2
                                    // 1 real queue: 1. The first and last queue ids (in this case 0 and 2) are not actual queues, but the
                                    // beginning and end of the processing sequence
                                    // 2 threads: 0, 1
                                    // wUserInput will generate frames (there is no real queue 0) and push them on queue 1
                                    // wGui will pop frames from queue 1 and process them (there is no real queue 2)
                                    var threadId = 0UL;
                                    var queueIn  = 0UL;
                                    var queueOut = 1UL;
                                    threadManager.Add(threadId++, wUserInput, queueIn++, queueOut++);       // Thread 0, queues 0 -> 1
                                    threadManager.Add(threadId++, wUserProcessing, queueIn++, queueOut++);  // Thread 1, queues 1 -> 2
                                    threadManager.Add(threadId++, wUserOutput, queueIn++, queueOut++);      // Thread 2, queues 2 -> 3

                                    // ------------------------- STARTING AND STOPPING THREADING -------------------------
                                    OpenPose.Log("Starting thread(s)...", Priority.High);
                                    // Two different ways of running the program on multithread environment
                                    // Option a) Using the main thread (this thread) for processing (it saves 1 thread, recommended)
                                    threadManager.Exec();
                                    // Option b) Giving to the user the control of this thread
                                    // // VERY IMPORTANT NOTE: if OpenCV is compiled with Qt support, this option will not work. Qt needs the main
                                    // // thread to plot visual results, so the final GUI (which uses OpenCV) would return an exception similar to:
                                    // // `QMetaMethod::invoke: Unable to invoke methods with return values in queued connections`
                                    // // Start threads
                                    // threadManager.start();
                                    // // Keep program alive while running threads. Here the user could perform any other desired function
                                    // while (threadManager.isRunning())
                                    //     std::this_thread::sleep_for(std::chrono::milliseconds{33});
                                    // // Stop and join threads
                                    // op::log("Stopping thread(s)", op::Priority::High);
                                    // threadManager.stop();
                                }
                            }
                        }
                    }

                    // Measuring total time
                    OpenPose.PrintTime(opTimer, "OpenPose demo successfully finished. Total time: ", " seconds.", Priority.High);
                }

                // Return
                return(0);
            }
            catch (Exception e)
            {
                OpenPose.Error(e.Message, -1, nameof(OpenPoseTutorialThread2));
                return(-1);
            }
        }
        private static int TutorialDeveloperThread1()
        {
            try
            {
                OpenPose.Log("Starting OpenPose demo...", Priority.High);
                using (var opTimer = OpenPose.GetTimerInit())
                {
                    // ------------------------- INITIALIZATION -------------------------
                    // Step 1 - Set logging level
                    // - 0 will output all the logging messages
                    // - 255 will output nothing
                    OpenPose.CheckBool(0 <= Flags.LoggingLevel && Flags.LoggingLevel <= 255, "Wrong logging_level value.");
                    ConfigureLog.PriorityThreshold = (Priority)Flags.LoggingLevel;
                    // Step 2 - Read GFlags (user defined configuration)
                    // cameraSize
                    var cameraSize = OpenPose.FlagsToPoint(Flags.CameraResolution, "-1x-1");
                    // outputSize
                    var outputSize = OpenPose.FlagsToPoint(Flags.OutputResolution, "-1x-1");
                    // producerType
                    var tie                    = OpenPose.FlagsToProducer(Flags.ImageDir, Flags.Video, Flags.IpCamera, Flags.Camera, Flags.FlirCamera, Flags.FlirCameraIndex);
                    var producerType           = tie.Item1;
                    var producerString         = tie.Item2;
                    var displayProducerFpsMode = Flags.ProcessRealTime ? ProducerFpsMode.OriginalFps : ProducerFpsMode.RetrievalFps;
                    using (var producerSharedPtr = OpenPose.CreateProducer(producerType,
                                                                           cameraSize,
                                                                           producerString,
                                                                           Flags.CameraParameterPath,
                                                                           Flags.FrameUndistort,
                                                                           Flags.Views3D))
                    {
                        producerSharedPtr.Get().SetProducerFpsMode(displayProducerFpsMode);
                        OpenPose.Log("", Priority.Low);
                        // Step 3 - Setting producer
                        //var videoSeekSharedPtr = std::make_shared<std::pair<std::atomic<bool>, std::atomic<int>>>();
                        //videoSeekSharedPtr->first = false;
                        //videoSeekSharedPtr->second = 0;
                        // Step 4 - Setting thread workers && manager
                        // Note:
                        // nativeDebugging may occur crash
                        using (var threadManager = new ThreadManager <Datum>())
                        {
                            // Step 5 - Initializing the worker classes
                            // Frames producer (e.g., video, webcam, ...)
                            using (var datumProducer = new StdSharedPtr <DatumProducer <Datum> >(new DatumProducer <Datum>(producerSharedPtr)))
                                using (var wDatumProducer = new StdSharedPtr <WDatumProducer <Datum> >(new WDatumProducer <Datum>(datumProducer)))
                                {
                                    // Specific WUserClass
                                    using (var wUserClass = new StdSharedPtr <UserWorker <Datum> >(new WUserClass()))
                                    {
                                        // GUI (Display)
                                        using (var gui = new StdSharedPtr <Gui>(new Gui(outputSize, Flags.FullScreen, threadManager.GetIsRunningSharedPtr())))
                                            using (var wGui = new StdSharedPtr <WGui <Datum> >(new WGui <Datum>(gui)))
                                            {
                                                // ------------------------- CONFIGURING THREADING -------------------------
                                                // In this simple multi-thread example, we will do the following:
                                                // 3 (virtual) queues: 0, 1, 2
                                                // 1 real queue: 1. The first and last queue ids (in this case 0 and 2) are not actual queues, but the
                                                // beginning and end of the processing sequence
                                                // 2 threads: 0, 1
                                                // wDatumProducer will generate frames (there is no real queue 0) and push them on queue 1
                                                // wGui will pop frames from queue 1 and process them (there is no real queue 2)
                                                var threadId = 0UL;
                                                var queueIn  = 0UL;
                                                var queueOut = 1UL;
                                                threadManager.Add(threadId++, wDatumProducer, queueIn++, queueOut++); // Thread 0, queues 0 -> 1
                                                threadManager.Add(threadId++, wUserClass, queueIn++, queueOut++);     // Thread 1, queues 1 -> 2
                                                threadManager.Add(threadId++, wGui, queueIn++, queueOut++);           // Thread 2, queues 2 -> 3

                                                // Equivalent single-thread version
                                                // const auto threadId = 0ull;
                                                // auto queueIn = 0ull;
                                                // auto queueOut = 1ull;
                                                // threadManager.add(threadId, wDatumProducer, queueIn++, queueOut++);     // Thread 0, queues 0 -> 1
                                                // threadManager.add(threadId, wUserClass, queueIn++, queueOut++);         // Thread 1, queues 1 -> 2
                                                // threadManager.add(threadId, wGui, queueIn++, queueOut++);               // Thread 2, queues 2 -> 3

                                                // Smart multi-thread version
                                                // Assume wUser is the slowest process, and that wDatumProducer + wGui is faster than wGui itself,
                                                // then, we can group the last 2 in the same thread and keep wGui in a different thread:
                                                // const auto threadId = 0ull;
                                                // auto queueIn = 0ull;
                                                // auto queueOut = 1ull;
                                                // threadManager.add(threadId, wDatumProducer, queueIn++, queueOut++);     // Thread 0, queues 0 -> 1
                                                // threadManager.add(threadId+1, wUserClass, queueIn++, queueOut++);       // Thread 1, queues 1 -> 2
                                                // threadManager.add(threadId, wGui, queueIn++, queueOut++);               // Thread 0, queues 2 -> 3

                                                // ------------------------- STARTING AND STOPPING THREADING -------------------------
                                                OpenPose.Log("Starting thread(s)...", Priority.High);
                                                // Two different ways of running the program on multithread environment
                                                // Option a) Using the main thread (this thread) for processing (it saves 1 thread, recommended)
                                                threadManager.Exec();
                                                // Option b) Giving to the user the control of this thread
                                                // // VERY IMPORTANT NOTE: if OpenCV is compiled with Qt support, this option will not work. Qt needs the main
                                                // // thread to plot visual results, so the final GUI (which uses OpenCV) would return an exception similar to:
                                                // // `QMetaMethod::invoke: Unable to invoke methods with return values in queued connections`
                                                // // Start threads
                                                // threadManager.start();
                                                // // Keep program alive while running threads. Here the user could perform any other desired function
                                                // while (threadManager.isRunning())
                                                //     std::this_thread::sleep_for(std::chrono::milliseconds{33});
                                                // // Stop and join threads
                                                // op::log("Stopping thread(s)", op::Priority::High);
                                                // threadManager.stop();
                                            }
                                    }
                                }
                        }
                    }

                    // Measuring total time
                    OpenPose.PrintTime(opTimer, "OpenPose demo successfully finished. Total time: ", " seconds.", Priority.High);
                }

                // Return
                return(0);
            }
            catch (Exception e)
            {
                OpenPose.Error(e.Message, -1, nameof(TutorialDeveloperThread1));
                return(-1);
            }
        }