示例#1
0
        private static int TutorialApiCpp()
        {
            try
            {
                OpenPose.Log("Starting OpenPose demo...", Priority.High);
                using (var opTimer = OpenPose.GetTimerInit())
                {
                    // OpenPose wrapper
                    OpenPose.Log("Configuring OpenPose...", Priority.High);
                    using (var opWrapper = new Wrapper <Datum>())
                    {
                        ConfigureWrapper(opWrapper);

                        // Start, run, and stop processing - exec() blocks this thread until OpenPose wrapper has finished
                        OpenPose.Log("Starting thread(s)...", Priority.High);
                        opWrapper.Exec();
                    }

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

                // Return
                return(0);
            }
            catch (Exception)
            {
                return(-1);
            }
        }
示例#2
0
        private static int TutorialApiCpp()
        {
            try
            {
                OpenPose.Log("Starting OpenPose demo...", Priority.High);
                using (var opTimer = OpenPose.GetTimerInit())
                {
                    // Configuring OpenPose
                    OpenPose.Log("Configuring OpenPose...", Priority.High);
                    using (var opWrapper = new Wrapper <Datum>(ThreadManagerMode.AsynchronousOut))
                    {
                        ConfigureWrapper(opWrapper);

                        // Start, run, and stop processing - exec() blocks this thread until OpenPose wrapper has finished
                        OpenPose.Log("Starting thread(s)...", Priority.High);
                        opWrapper.Start();

                        // User processing
                        var userOutputClass = new UserOutputClass();
                        var userWantsToExit = false;
                        while (!userWantsToExit)
                        {
                            // Pop frame
                            if (opWrapper.WaitAndPop(out var datumProcessed))
                            {
                                if (!Flags.NoDisplay)
                                {
                                    userWantsToExit = userOutputClass.Display(datumProcessed);
                                }
                                userOutputClass.PrintKeyPoints(datumProcessed);
                                datumProcessed.Dispose();
                            }
                            // If OpenPose finished reading images
                            else if (!opWrapper.IsRunning)
                            {
                                break;
                            }
                            // Something else happened
                            else
                            {
                                OpenPose.Log("Processed datum could not be emplaced.", Priority.High);
                            }
                        }

                        OpenPose.Log("Stopping thread(s)", Priority.High);
                        opWrapper.Stop();

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

                // Return
                return(0);
            }
            catch (Exception)
            {
                return(-1);
            }
        }
示例#3
0
        private static int TutorialAddModule1()
        {
            try
            {
                OpenPose.Log("Starting OpenPose demo...", Priority.High);
                using (var opTimer = OpenPose.GetTimerInit())
                {
                    // Configuring OpenPose
                    OpenPose.Log("Configuring OpenPose...", Priority.High);
                    using (var opWrapperT = new Wrapper <CustomDatum>())
                    {
                        ConfigureWrapper(opWrapperT);

                        OpenPose.Log("Starting thread(s)...", Priority.High);
                        // Start, run & stop threads - it blocks this thread until all others have finished
                        opWrapperT.Exec();

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

                // Return successful message
                return(0);
            }
            catch (Exception)
            {
                return(-1);
            }
        }
示例#4
0
        private static int TutorialApiCpp()
        {
            try
            {
                OpenPose.Log("Starting OpenPose demo...", Priority.High);
                using (var opTimer = OpenPose.GetTimerInit())
                {
                    // Configuring OpenPose
                    OpenPose.Log("Configuring OpenPose...", Priority.High);
                    using (var opWrapper = new Wrapper <Datum>(ThreadManagerMode.AsynchronousIn))
                    {
                        ConfigureWrapper(opWrapper);

                        // Start, run, and stop processing - exec() blocks this thread until OpenPose wrapper has finished
                        OpenPose.Log("Starting thread(s)...", Priority.High);
                        opWrapper.Start();

                        // User processing
                        var userInputClass  = new UserInputClass(Flags.ImageDir);
                        var userWantsToExit = false;
                        while (!userWantsToExit && !userInputClass.IsFinished())
                        {
                            // Push frame
                            using (var datumToProcess = userInputClass.CreateDatum())
                            {
                                if (datumToProcess != null)
                                {
                                    var successfullyEmplaced = opWrapper.WaitAndEmplace(datumToProcess);
                                    if (!successfullyEmplaced)
                                    {
                                        OpenPose.Log("Processed datum could not be emplaced.", Priority.High);
                                    }
                                }
                            }
                        }

                        OpenPose.Log("Stopping thread(s)", Priority.High);
                        opWrapper.Stop();
                    }

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

                // Return
                return(0);
            }
            catch (Exception)
            {
                return(-1);
            }
        }
示例#5
0
        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);
            }
        }
示例#6
0
        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);
            }
        }