02_whole_body_from_image_default.cpp 4.3 KB
Newer Older
1
// -------------------------- OpenPose C++ API Tutorial - Example 2 - Whole body from image --------------------------
G
gineshidalgo99 已提交
2 3
// It reads an image, process it, and displays it with the pose, hand, and face 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_000000000241.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
        // Example: How to use the pose keypoints
        if (datumsPtr != nullptr && !datumsPtr->empty())
        {
56 57 58 59
            op::opLog("Body keypoints: " + datumsPtr->at(0)->poseKeypoints.toString(), op::Priority::High);
            op::opLog("Face keypoints: " + datumsPtr->at(0)->faceKeypoints.toString(), op::Priority::High);
            op::opLog("Left hand keypoints: " + datumsPtr->at(0)->handKeypoints[0].toString(), op::Priority::High);
            op::opLog("Right hand keypoints: " + datumsPtr->at(0)->handKeypoints[1].toString(), op::Priority::High);
G
gineshidalgo99 已提交
60 61
        }
        else
62
            op::opLog("Nullptr or empty datumsPtr found.", op::Priority::High);
G
gineshidalgo99 已提交
63 64 65 66
    }
    catch (const std::exception& e)
    {
        op::error(e.what(), __LINE__, __FUNCTION__, __FILE__);
G
gineshidalgo99 已提交
67 68 69
    }
}

G
gineshidalgo99 已提交
70
int tutorialApiCpp()
G
gineshidalgo99 已提交
71
{
72 73
    try
    {
74
        op::opLog("Starting OpenPose demo...", op::Priority::High);
G
gineshidalgo99 已提交
75
        const auto opTimer = op::getTimerInit();
76 77

        // Configuring OpenPose
78
        op::opLog("Configuring OpenPose...", op::Priority::High);
79 80 81 82 83 84 85
        op::Wrapper opWrapper{op::ThreadManagerMode::Asynchronous};
        // Add hand and face
        opWrapper.configure(op::WrapperStructFace{true});
        opWrapper.configure(op::WrapperStructHand{true});
        // Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
        if (FLAGS_disable_multi_thread)
            opWrapper.disableMultiThreading();
G
gineshidalgo99 已提交
86

87
        // Starting OpenPose
88
        op::opLog("Starting thread(s)...", op::Priority::High);
89
        opWrapper.start();
G
gineshidalgo99 已提交
90

91
        // Process and display image
92 93
        const cv::Mat cvImageToProcess = cv::imread(FLAGS_image_path);
        const op::Matrix imageToProcess = OP_CV2OPCONSTMAT(cvImageToProcess);
94 95 96 97
        auto datumProcessed = opWrapper.emplaceAndPop(imageToProcess);
        if (datumProcessed != nullptr)
        {
            printKeypoints(datumProcessed);
G
gineshidalgo99 已提交
98 99
            if (!FLAGS_no_display)
                display(datumProcessed);
100 101
        }
        else
102
            op::opLog("Image could not be processed.", op::Priority::High);
G
gineshidalgo99 已提交
103

G
gineshidalgo99 已提交
104 105 106 107
        // Measuring total time
        op::printTime(opTimer, "OpenPose demo successfully finished. Total time: ", " seconds.", op::Priority::High);

        // Return
108 109
        return 0;
    }
G
Gines Hidalgo 已提交
110
    catch (const std::exception&)
G
gineshidalgo99 已提交
111
    {
112
        return -1;
G
gineshidalgo99 已提交
113 114 115 116 117 118 119 120
    }
}

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

G
gineshidalgo99 已提交
121 122
    // Running tutorialApiCpp
    return tutorialApiCpp();
G
gineshidalgo99 已提交
123
}