gui.cpp 8.1 KB
Newer Older
G
gineshidalgo99 已提交
1 2 3
#include <chrono>
#include <thread>
#include <opencv2/highgui/highgui.hpp> // cv::waitKey
G
Gines Hidalgo 已提交
4 5 6
#include <openpose/filestream/fileStream.hpp>
#include <openpose/utilities/check.hpp>
#include <openpose/gui/gui.hpp>
G
gineshidalgo99 已提交
7 8 9

namespace op
{
G
gineshidalgo99 已提交
10
    const std::string OPEN_POSE_TEXT{"OpenPose 1.2.0"};
11

G
gineshidalgo99 已提交
12 13 14 15 16 17 18 19 20
    inline void showGuiHelp()
    {
        try
        {
            const auto helpCvMat = loadImage("./doc/GUI_help/GUI_help.png");

            if (!helpCvMat.empty())
            {
                const auto fullScreen = false;
21
                FrameDisplayer frameDisplayer{OPEN_POSE_TEXT + " - GUI Help", Point<int>{helpCvMat.cols, helpCvMat.rows}, fullScreen};
G
gineshidalgo99 已提交
22 23 24 25 26 27 28 29 30
                frameDisplayer.displayFrame(helpCvMat, 33);
            }
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

31 32
    void handleWaitKey(bool& guiPaused, FrameDisplayer& frameDisplayer, std::vector<std::shared_ptr<PoseExtractor>>& poseExtractors,
                       std::vector<std::shared_ptr<Renderer>>& renderers, std::shared_ptr<std::atomic<bool>>& isRunningSharedPtr,
G
gineshidalgo99 已提交
33 34 35 36 37 38 39 40
                       std::shared_ptr<std::pair<std::atomic<bool>, std::atomic<int>>>& spVideoSeek)
    {
        try
        {
            const int key = cv::waitKey(1);
            if (key != -1)
            {
                // Some OpenCV versions has a problem and key must be casted to char
G
Gines Hidalgo 已提交
41
                const auto castedKey = (char)std::tolower((char)key);
G
gineshidalgo99 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
                // ------------------------- General Commands ------------------------- //
                // Exit program
                if (castedKey==27)
                {
                    if (isRunningSharedPtr != nullptr)
                    {
                        *isRunningSharedPtr = false;
                        guiPaused = false;
                    }
                }
                // Help
                else if (castedKey=='h')
                    showGuiHelp();
                // Switch full screen - normal screen
                else if (castedKey=='f')
57
                    frameDisplayer.switchGuiDisplayMode();
G
gineshidalgo99 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
                // ------------------------- Producer-Related ------------------------- //
                // Pause
                else if (castedKey==' ')
                    guiPaused = !guiPaused;
                // Fake pause
                else if (castedKey=='m')
                {
                    if (spVideoSeek != nullptr)
                        spVideoSeek->first = !spVideoSeek->first;
                }
                // Seeking in video
                else if (castedKey=='l' || castedKey=='k')
                {
                    if (spVideoSeek != nullptr)
                    {
                        // Normal case, +-30 frames
                        if (!spVideoSeek->first)
                            spVideoSeek->second += 30 * (castedKey=='l' ? -2 : 1);
                        // Frame by frame (if forced paused)
                        else
                            spVideoSeek->second += (castedKey=='l' ? -1 : 1);
                    }
                }
                // Enable/disable blending
                else if (castedKey=='b')
                {
84 85
                    for (auto& renderer : renderers)
                        renderer->setBlendOriginalFrame(!renderer->getBlendOriginalFrame());
G
gineshidalgo99 已提交
86 87 88 89
                }
                // ------------------------- OpenPose-Related ------------------------- //
                // Modifying thresholds
                else if (castedKey=='-' || castedKey=='=')
90
                    for (auto& poseExtractor : poseExtractors)
G
gineshidalgo99 已提交
91 92
                        poseExtractor->increase(PoseProperty::NMSThreshold, 0.005f * (castedKey=='-' ? -1 : 1));
                else if (castedKey=='_' || castedKey=='+')
93
                    for (auto& poseExtractor : poseExtractors)
G
gineshidalgo99 已提交
94 95
                        poseExtractor->increase(PoseProperty::ConnectMinSubsetScore, 0.005f * (castedKey=='_' ? -1 : 1));
                else if (castedKey=='[' || castedKey==']')
96
                    for (auto& poseExtractor : poseExtractors)
G
gineshidalgo99 已提交
97 98
                        poseExtractor->increase(PoseProperty::ConnectInterThreshold, 0.005f * (castedKey=='[' ? -1 : 1));
                else if (castedKey=='{' || castedKey=='}')
99
                    for (auto& poseExtractor : poseExtractors)
100
                        poseExtractor->increase(PoseProperty::ConnectInterMinAboveThreshold, (castedKey=='{' ? -0.1f : 0.1f));
G
gineshidalgo99 已提交
101
                else if (castedKey==';' || castedKey=='\'')
102
                    for (auto& poseExtractor : poseExtractors)
G
gineshidalgo99 已提交
103 104 105 106
                        poseExtractor->increase(PoseProperty::ConnectMinSubsetCnt, (castedKey==';' ? -1 : 1));
                // ------------------------- Miscellaneous ------------------------- //
                // Show googly eyes
                else if (castedKey=='g')
107 108
                    for (auto& renderer : renderers)
                        renderer->setShowGooglyEyes(!renderer->getShowGooglyEyes());
G
gineshidalgo99 已提交
109 110 111 112
                // ------------------------- OpenPose-Related ------------------------- //
                else if (castedKey==',' || castedKey=='.')
                {
                    const auto increment = (castedKey=='.' ? 1 : -1);
113 114
                    for (auto& renderer : renderers)
                        renderer->increaseElementToRender(increment);
G
gineshidalgo99 已提交
115 116 117 118 119 120
                }
                else
                {
                    const std::string key2part = "0123456789qwertyuiopasd";
                    const auto newElementToRender = key2part.find(castedKey);
                    if (newElementToRender != std::string::npos)
121 122
                        for (auto& renderer : renderers)
                            renderer->setElementToRender((int)newElementToRender);
G
gineshidalgo99 已提交
123 124 125 126 127 128 129 130 131
                }
            }
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

132 133
    void handleUserInput(FrameDisplayer& frameDisplayer, std::vector<std::shared_ptr<PoseExtractor>>& poseExtractors,
                         std::vector<std::shared_ptr<Renderer>>& renderers, std::shared_ptr<std::atomic<bool>>& isRunningSharedPtr,
G
gineshidalgo99 已提交
134 135 136 137 138 139
                         std::shared_ptr<std::pair<std::atomic<bool>, std::atomic<int>>>& spVideoSeek)
    {
        try
        {
            // The handleUserInput must be always performed, even if no tDatum is detected
            bool guiPaused = false;
140
            handleWaitKey(guiPaused, frameDisplayer, poseExtractors, renderers, isRunningSharedPtr, spVideoSeek);
G
gineshidalgo99 已提交
141 142 143
            while (guiPaused)
            {
                std::this_thread::sleep_for(std::chrono::milliseconds{1});
144
                handleWaitKey(guiPaused, frameDisplayer, poseExtractors, renderers, isRunningSharedPtr, spVideoSeek);
G
gineshidalgo99 已提交
145 146 147 148 149 150 151 152
            }
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }

153
    Gui::Gui(const Point<int>& outputSize, const bool fullScreen, const std::shared_ptr<std::atomic<bool>>& isRunningSharedPtr,
G
gineshidalgo99 已提交
154
             const std::shared_ptr<std::pair<std::atomic<bool>, std::atomic<int>>>& videoSeekSharedPtr,
155 156
             const std::vector<std::shared_ptr<PoseExtractor>>& poseExtractors, const std::vector<std::shared_ptr<Renderer>>& renderers) :
        mFrameDisplayer{OPEN_POSE_TEXT, outputSize, fullScreen},
G
gineshidalgo99 已提交
157
        mPoseExtractors{poseExtractors},
158
        mRenderers{renderers},
G
gineshidalgo99 已提交
159 160 161 162 163
        spIsRunning{isRunningSharedPtr},
        spVideoSeek{videoSeekSharedPtr}
    {
    }

164 165 166 167 168
    void Gui::initializationOnThread()
    {
        mFrameDisplayer.initializationOnThread();
    }

G
gineshidalgo99 已提交
169 170 171 172 173 174 175 176 177 178 179 180
    void Gui::update(const cv::Mat& cvOutputData)
    {
        try
        {
            // Check tDatum integrity
            const bool returnedIsValidFrame = ((spIsRunning == nullptr || *spIsRunning) && !cvOutputData.empty());

            // Display
            if (returnedIsValidFrame)
                mFrameDisplayer.displayFrame(cvOutputData, -1);

            // Handle user input
181
            handleUserInput(mFrameDisplayer, mPoseExtractors, mRenderers, spIsRunning, spVideoSeek);
G
gineshidalgo99 已提交
182 183 184 185 186 187 188
        }
        catch (const std::exception& e)
        {
            error(e.what(), __LINE__, __FUNCTION__, __FILE__);
        }
    }
}