01_body_from_image_default.cpp 5.1 KB
Newer Older
1
// ----------------------------- OpenPose C++ API Tutorial - Example 1 - Body from image -----------------------------
G
gineshidalgo99 已提交
2 3
// It reads an image, process it, and displays it with the pose keypoints.

4 5 6
// Third-party dependencies
#include <opencv2/opencv.hpp>
// Command-line user interface
G
gineshidalgo99 已提交
7 8 9 10 11 12 13 14 15
#define OPENPOSE_FLAGS_DISABLE_POSE
#include <openpose/flags.hpp>
// OpenPose dependencies
#include <openpose/headers.hpp>

// Custom OpenPose flags
// Producer
DEFINE_string(image_path, "examples/media/COCO_val2014_000000000192.jpg",
    "Process an image. Read all standard formats (jpg, png, bmp, etc.).");
G
gineshidalgo99 已提交
16 17 18
// Display
DEFINE_bool(no_display,                 false,
    "Enable to disable the visual display.");
G
gineshidalgo99 已提交
19 20

// This worker will just read and return all the jpg files in a directory
G
gineshidalgo99 已提交
21
void display(const std::shared_ptr<std::vector<std::shared_ptr<op::Datum>>>& datumsPtr)
G
gineshidalgo99 已提交
22
{
G
gineshidalgo99 已提交
23 24 25 26 27 28 29 30
    try
    {
        // User's displaying/saving/other processing here
            // datum.cvOutputData: rendered frame with pose or heatmaps
            // datum.poseKeypoints: Array<float> with the estimated pose
        if (datumsPtr != nullptr && !datumsPtr->empty())
        {
            // Display image
31
            const cv::Mat cvMat = OP_OP2CVCONSTMAT(datumsPtr->at(0)->cvOutputData);
32 33 34 35 36 37 38
            if (!cvMat.empty())
            {
                cv::imshow(OPEN_POSE_NAME_AND_VERSION + " - Tutorial C++ API", cvMat);
                cv::waitKey(0);
            }
            else
                op::opLog("Empty cv::Mat as output.", op::Priority::High, __LINE__, __FUNCTION__, __FILE__);
G
gineshidalgo99 已提交
39 40
        }
        else
41
            op::opLog("Nullptr or empty datumsPtr found.", op::Priority::High);
G
gineshidalgo99 已提交
42 43
    }
    catch (const std::exception& e)
G
gineshidalgo99 已提交
44
    {
G
gineshidalgo99 已提交
45
        op::error(e.what(), __LINE__, __FUNCTION__, __FILE__);
G
gineshidalgo99 已提交
46 47 48
    }
}

G
gineshidalgo99 已提交
49
void printKeypoints(const std::shared_ptr<std::vector<std::shared_ptr<op::Datum>>>& datumsPtr)
G
gineshidalgo99 已提交
50
{
G
gineshidalgo99 已提交
51
    try
G
gineshidalgo99 已提交
52
    {
G
gineshidalgo99 已提交
53 54 55 56
        // Example: How to use the pose keypoints
        if (datumsPtr != nullptr && !datumsPtr->empty())
        {
            // Alternative 1
57
            op::opLog("Body keypoints: " + datumsPtr->at(0)->poseKeypoints.toString(), op::Priority::High);
G
gineshidalgo99 已提交
58

G
gineshidalgo99 已提交
59
            // // Alternative 2
G
Gines Hidalgo 已提交
60
            // op::opLog(datumsPtr->at(0)->poseKeypoints, op::Priority::High);
G
gineshidalgo99 已提交
61

G
gineshidalgo99 已提交
62
            // // Alternative 3
G
Gines Hidalgo 已提交
63
            // std::cout << datumsPtr->at(0)->poseKeypoints << std::endl;
G
gineshidalgo99 已提交
64

G
gineshidalgo99 已提交
65
            // // Alternative 4 - Accesing each element of the keypoints
66
            // op::opLog("\nKeypoints:", op::Priority::High);
G
Gines Hidalgo 已提交
67
            // const auto& poseKeypoints = datumsPtr->at(0)->poseKeypoints;
68
            // op::opLog("Person pose keypoints:", op::Priority::High);
G
gineshidalgo99 已提交
69 70
            // for (auto person = 0 ; person < poseKeypoints.getSize(0) ; person++)
            // {
71
            //     op::opLog("Person " + std::to_string(person) + " (x, y, score):", op::Priority::High);
G
gineshidalgo99 已提交
72 73 74 75 76
            //     for (auto bodyPart = 0 ; bodyPart < poseKeypoints.getSize(1) ; bodyPart++)
            //     {
            //         std::string valueToPrint;
            //         for (auto xyscore = 0 ; xyscore < poseKeypoints.getSize(2) ; xyscore++)
            //             valueToPrint += std::to_string(   poseKeypoints[{person, bodyPart, xyscore}]   ) + " ";
77
            //         op::opLog(valueToPrint, op::Priority::High);
G
gineshidalgo99 已提交
78 79
            //     }
            // }
80
            // op::opLog(" ", op::Priority::High);
G
gineshidalgo99 已提交
81 82
        }
        else
83
            op::opLog("Nullptr or empty datumsPtr found.", op::Priority::High);
G
gineshidalgo99 已提交
84 85 86 87
    }
    catch (const std::exception& e)
    {
        op::error(e.what(), __LINE__, __FUNCTION__, __FILE__);
G
gineshidalgo99 已提交
88 89 90
    }
}

G
gineshidalgo99 已提交
91
int tutorialApiCpp()
G
gineshidalgo99 已提交
92
{
93 94
    try
    {
95
        op::opLog("Starting OpenPose demo...", op::Priority::High);
G
gineshidalgo99 已提交
96
        const auto opTimer = op::getTimerInit();
97 98

        // Configuring OpenPose
99
        op::opLog("Configuring OpenPose...", op::Priority::High);
100 101 102 103
        op::Wrapper opWrapper{op::ThreadManagerMode::Asynchronous};
        // Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
        if (FLAGS_disable_multi_thread)
            opWrapper.disableMultiThreading();
G
gineshidalgo99 已提交
104

105
        // Starting OpenPose
106
        op::opLog("Starting thread(s)...", op::Priority::High);
107
        opWrapper.start();
G
gineshidalgo99 已提交
108

109
        // Process and display image
110 111
        const cv::Mat cvImageToProcess = cv::imread(FLAGS_image_path);
        const op::Matrix imageToProcess = OP_CV2OPCONSTMAT(cvImageToProcess);
112 113 114 115
        auto datumProcessed = opWrapper.emplaceAndPop(imageToProcess);
        if (datumProcessed != nullptr)
        {
            printKeypoints(datumProcessed);
G
gineshidalgo99 已提交
116 117
            if (!FLAGS_no_display)
                display(datumProcessed);
118 119
        }
        else
120
            op::opLog("Image could not be processed.", op::Priority::High);
G
gineshidalgo99 已提交
121

G
gineshidalgo99 已提交
122 123 124 125
        // Measuring total time
        op::printTime(opTimer, "OpenPose demo successfully finished. Total time: ", " seconds.", op::Priority::High);

        // Return
126 127
        return 0;
    }
G
Gines Hidalgo 已提交
128
    catch (const std::exception&)
G
gineshidalgo99 已提交
129
    {
130
        return -1;
G
gineshidalgo99 已提交
131 132 133 134 135 136 137 138
    }
}

int main(int argc, char *argv[])
{
    // Parsing command line flags
    gflags::ParseCommandLineFlags(&argc, &argv, true);

G
gineshidalgo99 已提交
139 140
    // Running tutorialApiCpp
    return tutorialApiCpp();
G
gineshidalgo99 已提交
141
}