openpose_python.cpp 18.5 KB
Newer Older
R
Raaj 已提交
1 2 3 4
#ifndef OPENPOSE_PYTHON_HPP
#define OPENPOSE_PYTHON_HPP
#define BOOST_DATE_TIME_NO_LIB

R
Raaj 已提交
5 6 7 8 9 10 11 12 13
#include <openpose/flags.hpp>
#include <openpose/headers.hpp>
#include <openpose/wrapper/headers.hpp>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <opencv2/core/core.hpp>
#include <stdexcept>
R
Raaj 已提交
14 15 16 17 18 19 20

#ifdef _WIN32
    #define OP_EXPORT __declspec(dllexport)
#else
    #define OP_EXPORT
#endif

R
Raaj 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
namespace op{

namespace py = pybind11;

void parse_gflags(const std::vector<std::string>& argv)
{
    std::vector<char*> argv_vec;
    for(auto& arg : argv) argv_vec.emplace_back((char*)arg.c_str());
    char** cast = &argv_vec[0];
    int size = argv_vec.size();
    gflags::ParseCommandLineFlags(&size, &cast, true);
}

void init_int(py::dict d)
{
    std::vector<std::string> argv;
    argv.emplace_back("openpose.py");
    for (auto item : d){
        argv.emplace_back("--" + std::string(py::str(item.first)));
        argv.emplace_back(py::str(item.second));
    }
    parse_gflags(argv);
}

void init_argv(std::vector<std::string> argv)
{
    argv.insert(argv.begin(), "openpose.py");
    parse_gflags(argv);
}

class WrapperPython{
R
Raaj 已提交
52
public:
R
Raaj 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    std::unique_ptr<op::Wrapper> opWrapper;

    WrapperPython(int mode = 0)
    {
        op::log("Starting OpenPose Python Wrapper...", op::Priority::High);

        // Construct opWrapper
        opWrapper = std::unique_ptr<op::Wrapper>(new op::Wrapper(static_cast<op::ThreadManagerMode>(mode)));
    }

    void configure(py::dict params = py::dict())
    {
        if(params.size()) init_int(params);

        // logging_level
        op::check(0 <= FLAGS_logging_level && FLAGS_logging_level <= 255, "Wrong logging_level value.",
                  __LINE__, __FUNCTION__, __FILE__);
G
gineshidalgo99 已提交
70
        op::ConfigureLog::setPriorityThreshold((op::Priority)FLAGS_logging_level);
R
Raaj 已提交
71 72 73
        op::Profiler::setDefaultX(FLAGS_profile_speed);

        // Applying user defined configuration - GFlags to program variables
G
gineshidalgo99 已提交
74 75 76 77
        // outputSize
        const auto outputSize = op::flagsToPoint(FLAGS_output_resolution, "-1x-1");
        // netInputSize
        const auto netInputSize = op::flagsToPoint(FLAGS_net_resolution, "-1x368");
R
Raaj 已提交
78 79 80 81
        // faceNetInputSize
        const auto faceNetInputSize = op::flagsToPoint(FLAGS_face_net_resolution, "368x368 (multiples of 16)");
        // handNetInputSize
        const auto handNetInputSize = op::flagsToPoint(FLAGS_hand_net_resolution, "368x368 (multiples of 16)");
82 83
        // poseMode
        const auto poseMode = op::flagsToPoseMode(FLAGS_body);
G
gineshidalgo99 已提交
84
        // poseModel
R
Raaj 已提交
85 86 87 88 89
        const auto poseModel = op::flagsToPoseModel(FLAGS_model_pose);
        // JSON saving
        if (!FLAGS_write_keypoint.empty())
            op::log("Flag `write_keypoint` is deprecated and will eventually be removed."
                    " Please, use `write_json` instead.", op::Priority::Max);
G
gineshidalgo99 已提交
90 91
        // keypointScaleMode
        const auto keypointScaleMode = op::flagsToScaleMode(FLAGS_keypoint_scale);
R
Raaj 已提交
92 93 94
        // heatmaps to add
        const auto heatMapTypes = op::flagsToHeatMaps(FLAGS_heatmaps_add_parts, FLAGS_heatmaps_add_bkg,
                                                      FLAGS_heatmaps_add_PAFs);
G
gineshidalgo99 已提交
95
        const auto heatMapScaleMode = op::flagsToHeatMapScaleMode(FLAGS_heatmaps_scale);
R
Raaj 已提交
96 97
        // >1 camera view?
        const auto multipleView = (FLAGS_3d || FLAGS_3d_views > 1);
G
gineshidalgo99 已提交
98 99 100
        // Face and hand detectors
        const auto faceDetector = op::flagsToDetector(FLAGS_face_detector);
        const auto handDetector = op::flagsToDetector(FLAGS_hand_detector);
R
Raaj 已提交
101 102 103 104 105
        // Enabling Google Logging
        const bool enableGoogleLogging = true;

        // Pose configuration (use WrapperStructPose{} for default and recommended configuration)
        const op::WrapperStructPose wrapperStructPose{
106
            poseMode, netInputSize, outputSize, keypointScaleMode, FLAGS_num_gpu, FLAGS_num_gpu_start,
R
Raaj 已提交
107 108
            FLAGS_scale_number, (float)FLAGS_scale_gap, op::flagsToRenderMode(FLAGS_render_pose, multipleView),
            poseModel, !FLAGS_disable_blending, (float)FLAGS_alpha_pose, (float)FLAGS_alpha_heatmap,
G
gineshidalgo99 已提交
109
            FLAGS_part_to_show, FLAGS_model_folder, heatMapTypes, heatMapScaleMode, FLAGS_part_candidates,
R
Raaj 已提交
110
            (float)FLAGS_render_threshold, FLAGS_number_people_max, FLAGS_maximize_positives, FLAGS_fps_max,
111
            FLAGS_prototxt_path, FLAGS_caffemodel_path, (float)FLAGS_upsampling_ratio, enableGoogleLogging};
R
Raaj 已提交
112 113 114
        opWrapper->configure(wrapperStructPose);
        // Face configuration (use op::WrapperStructFace{} to disable it)
        const op::WrapperStructFace wrapperStructFace{
G
gineshidalgo99 已提交
115 116
            FLAGS_face, faceDetector, faceNetInputSize,
            op::flagsToRenderMode(FLAGS_face_render, multipleView, FLAGS_render_pose),
R
Raaj 已提交
117 118 119 120
            (float)FLAGS_face_alpha_pose, (float)FLAGS_face_alpha_heatmap, (float)FLAGS_face_render_threshold};
        opWrapper->configure(wrapperStructFace);
        // Hand configuration (use op::WrapperStructHand{} to disable it)
        const op::WrapperStructHand wrapperStructHand{
G
gineshidalgo99 已提交
121
            FLAGS_hand, handDetector, handNetInputSize, FLAGS_hand_scale_number, (float)FLAGS_hand_scale_range,
R
Raaj 已提交
122 123 124 125 126 127 128 129 130 131
            op::flagsToRenderMode(FLAGS_hand_render, multipleView, FLAGS_render_pose), (float)FLAGS_hand_alpha_pose,
            (float)FLAGS_hand_alpha_heatmap, (float)FLAGS_hand_render_threshold};
        opWrapper->configure(wrapperStructHand);
        // Extra functionality configuration (use op::WrapperStructExtra{} to disable it)
        const op::WrapperStructExtra wrapperStructExtra{
            FLAGS_3d, FLAGS_3d_min_views, FLAGS_identification, FLAGS_tracking, FLAGS_ik_threads};
        opWrapper->configure(wrapperStructExtra);
        // Output (comment or use default argument to disable any output)
        const op::WrapperStructOutput wrapperStructOutput{
            FLAGS_cli_verbose, FLAGS_write_keypoint, op::stringToDataFormat(FLAGS_write_keypoint_format),
132
            FLAGS_write_json, FLAGS_write_coco_json, FLAGS_write_coco_json_variants, FLAGS_write_coco_json_variant,
R
Raaj 已提交
133
            FLAGS_write_images, FLAGS_write_images_format, FLAGS_write_video, FLAGS_write_video_fps,
G
gineshidalgo99 已提交
134 135
            FLAGS_write_video_with_audio, FLAGS_write_heatmaps, FLAGS_write_heatmaps_format, FLAGS_write_video_3d,
            FLAGS_write_video_adam, FLAGS_write_bvh, FLAGS_udp_host, FLAGS_udp_port};
R
Raaj 已提交
136 137 138 139 140
        opWrapper->configure(wrapperStructOutput);
        // No GUI. Equivalent to: opWrapper.configure(op::WrapperStructGui{});
        // Set to single-thread (for sequential processing and/or debugging and/or reducing latency)
        if (FLAGS_disable_multi_thread)
            opWrapper->disableMultiThreading();
G
gineshidalgo99 已提交
141 142
    }

R
Raaj 已提交
143 144
    void start(){
        opWrapper->start();
G
gineshidalgo99 已提交
145 146
    }

R
Raaj 已提交
147 148
    void stop(){
        opWrapper->stop();
G
gineshidalgo99 已提交
149 150
    }

R
Raaj 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    void exec(){
        const auto cameraSize = op::flagsToPoint(FLAGS_camera_resolution, "-1x-1");
        op::ProducerType producerType;
        std::string producerString;
        std::tie(producerType, producerString) = op::flagsToProducer(
            FLAGS_image_dir, FLAGS_video, FLAGS_ip_camera, FLAGS_camera, FLAGS_flir_camera, FLAGS_flir_camera_index);
        // Producer (use default to disable any input)
        const op::WrapperStructInput wrapperStructInput{
            producerType, producerString, FLAGS_frame_first, FLAGS_frame_step, FLAGS_frame_last,
            FLAGS_process_real_time, FLAGS_frame_flip, FLAGS_frame_rotate, FLAGS_frames_repeat,
            cameraSize, FLAGS_camera_parameter_path, FLAGS_frame_undistort, FLAGS_3d_views};
        opWrapper->configure(wrapperStructInput);
        // GUI (comment or use default argument to disable any visual output)
        const op::WrapperStructGui wrapperStructGui{
            op::flagsToDisplayMode(FLAGS_display, FLAGS_3d), !FLAGS_no_gui_verbose, FLAGS_fullscreen};
        opWrapper->configure(wrapperStructGui);
        opWrapper->exec();
    }
G
gineshidalgo99 已提交
169

R
Raaj 已提交
170 171 172 173
    void emplaceAndPop(std::vector<std::shared_ptr<op::Datum>>& l)
    {
        auto datumsPtr = std::make_shared<std::vector<std::shared_ptr<op::Datum>>>(l);
        opWrapper->emplaceAndPop(datumsPtr);
G
gineshidalgo99 已提交
174
    }
R
Raaj 已提交
175 176 177 178 179 180 181 182 183 184 185 186

    void waitAndEmplace(std::vector<std::shared_ptr<op::Datum>>& l)
    {
        auto datumsPtr = std::make_shared<std::vector<std::shared_ptr<op::Datum>>>(l);
        opWrapper->waitAndEmplace(datumsPtr);
    }

    bool waitAndPop(std::vector<std::shared_ptr<op::Datum>>& l)
    {
        auto datumsPtr = std::make_shared<std::vector<std::shared_ptr<op::Datum>>>(l);
        return opWrapper->waitAndPop(datumsPtr);
    }
R
Raaj 已提交
187 188
};

R
Raaj 已提交
189 190 191 192 193
std::vector<std::string> getImagesFromDirectory(const std::string& directoryPath)
{
    return op::getFilesOnDirectory(directoryPath, op::Extensions::Images);
}

194
PYBIND11_MODULE(pyopenpose, m) {
R
Raaj 已提交
195 196 197 198

    // Functions for Init Params
    m.def("init_int", &init_int, "Init Function");
    m.def("init_argv", &init_argv, "Init Function");
R
Raaj 已提交
199 200
    m.def("get_gpu_number", &op::getGpuNumber, "Get Total GPU");
    m.def("get_images_on_directory", &op::getImagesFromDirectory, "Get Images On Directory");
R
Raaj 已提交
201 202 203 204 205 206 207 208 209 210

    // OpenposePython
    py::class_<WrapperPython>(m, "WrapperPython")
        .def(py::init<>())
        .def(py::init<int>())
        .def("configure", &WrapperPython::configure)
        .def("start", &WrapperPython::start)
        .def("stop", &WrapperPython::stop)
        .def("execute", &WrapperPython::exec)
        .def("emplaceAndPop", &WrapperPython::emplaceAndPop)
R
Raaj 已提交
211 212
        .def("waitAndEmplace", &WrapperPython::waitAndEmplace)
        .def("waitAndPop", &WrapperPython::waitAndPop)
R
Raaj 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
        ;

    // Datum Object
    py::class_<op::Datum, std::shared_ptr<op::Datum>>(m, "Datum")
        .def(py::init<>())
        .def_readwrite("id", &op::Datum::id)
        .def_readwrite("subId", &op::Datum::subId)
        .def_readwrite("subIdMax", &op::Datum::subIdMax)
        .def_readwrite("name", &op::Datum::name)
        .def_readwrite("frameNumber", &op::Datum::frameNumber)
        .def_readwrite("cvInputData", &op::Datum::cvInputData)
        .def_readwrite("inputNetData", &op::Datum::inputNetData)
        .def_readwrite("outputData", &op::Datum::outputData)
        .def_readwrite("cvOutputData", &op::Datum::cvOutputData)
        .def_readwrite("cvOutputData3D", &op::Datum::cvOutputData3D)
        .def_readwrite("poseKeypoints", &op::Datum::poseKeypoints)
        .def_readwrite("poseIds", &op::Datum::poseIds)
        .def_readwrite("poseScores", &op::Datum::poseScores)
        .def_readwrite("poseHeatMaps", &op::Datum::poseHeatMaps)
        .def_readwrite("poseCandidates", &op::Datum::poseCandidates)
        .def_readwrite("faceRectangles", &op::Datum::faceRectangles)
        .def_readwrite("faceKeypoints", &op::Datum::faceKeypoints)
        .def_readwrite("faceHeatMaps", &op::Datum::faceHeatMaps)
        .def_readwrite("handRectangles", &op::Datum::handRectangles)
        .def_readwrite("handKeypoints", &op::Datum::handKeypoints)
        .def_readwrite("handHeatMaps", &op::Datum::handHeatMaps)
        .def_readwrite("poseKeypoints3D", &op::Datum::poseKeypoints3D)
        .def_readwrite("faceKeypoints3D", &op::Datum::faceKeypoints3D)
        .def_readwrite("handKeypoints3D", &op::Datum::handKeypoints3D)
        .def_readwrite("cameraMatrix", &op::Datum::cameraMatrix)
        .def_readwrite("cameraExtrinsics", &op::Datum::cameraExtrinsics)
        .def_readwrite("cameraIntrinsics", &op::Datum::cameraIntrinsics)
R
Raaj 已提交
245
        .def_readwrite("poseNetOutput", &op::Datum::poseNetOutput)
R
Raaj 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
        .def_readwrite("scaleInputToNetInputs", &op::Datum::scaleInputToNetInputs)
        .def_readwrite("netInputSizes", &op::Datum::netInputSizes)
        .def_readwrite("scaleInputToOutput", &op::Datum::scaleInputToOutput)
        .def_readwrite("netOutputSize", &op::Datum::netOutputSize)
        .def_readwrite("scaleNetToOutput", &op::Datum::scaleNetToOutput)
        .def_readwrite("elementRendered", &op::Datum::elementRendered)
        ;

    // Rectangle
    py::class_<op::Rectangle<float>>(m, "Rectangle")
        .def("__repr__", [](op::Rectangle<float> &a) { return a.toString(); })
        .def(py::init<>())
        .def(py::init<float, float, float, float>())
        .def_readwrite("x", &op::Rectangle<float>::x)
        .def_readwrite("y", &op::Rectangle<float>::y)
        .def_readwrite("width", &op::Rectangle<float>::width)
        .def_readwrite("height", &op::Rectangle<float>::height)
        ;

    // Point
    py::class_<op::Point<int>>(m, "Point")
        .def("__repr__", [](op::Point<int> &a) { return a.toString(); })
        .def(py::init<>())
        .def(py::init<int, int>())
        .def_readwrite("x", &op::Point<int>::x)
        .def_readwrite("y", &op::Point<int>::y)
        ;

    #ifdef VERSION_INFO
        m.attr("__version__") = VERSION_INFO;
    #else
        m.attr("__version__") = "dev";
    #endif
}
R
Raaj 已提交
280

R
Raaj 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
}

// Numpy - op::Array<float> interop
namespace pybind11 { namespace detail {

template <> struct type_caster<op::Array<float>> {
    public:

        PYBIND11_TYPE_CASTER(op::Array<float>, _("numpy.ndarray"));

        // Cast numpy to op::Array<float>
        bool load(handle src, bool imp)
        {
            // array b(src, true);
            array b = reinterpret_borrow<array>(src);
            buffer_info info = b.request();

            if (info.format != format_descriptor<float>::format())
                throw std::runtime_error("op::Array only supports float32 now");

            //std::vector<int> a(info.shape);
            std::vector<int> shape(std::begin(info.shape), std::end(info.shape));

            // No copy
            value = op::Array<float>(shape, (float*)info.ptr);
            // Copy
            //value = op::Array<float>(shape);
            //memcpy(value.getPtr(), info.ptr, value.getVolume()*sizeof(float));

            return true;
G
gineshidalgo99 已提交
311
        }
R
Raaj 已提交
312 313 314 315 316 317 318 319 320 321 322 323 324

        // Cast op::Array<float> to numpy
        static handle cast(const op::Array<float> &m, return_value_policy, handle defval)
        {
            std::string format = format_descriptor<float>::format();
            return array(buffer_info(
                m.getPseudoConstPtr(),/* Pointer to buffer */
                sizeof(float),        /* Size of one scalar */
                format,               /* Python struct-style format descriptor */
                m.getSize().size(),   /* Number of dimensions */
                m.getSize(),          /* Buffer dimensions */
                m.getStride()         /* Strides (in bytes) for each index */
                )).release();
G
gineshidalgo99 已提交
325
        }
R
Raaj 已提交
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

    };
}} // namespace pybind11::detail

// Numpy - cv::Mat interop
namespace pybind11 { namespace detail {

template <> struct type_caster<cv::Mat> {
    public:

        PYBIND11_TYPE_CASTER(cv::Mat, _("numpy.ndarray"));

        // Cast numpy to cv::Mat
        bool load(handle src, bool)
        {
            /* Try a default converting into a Python */
            //array b(src, true);
            array b = reinterpret_borrow<array>(src);
            buffer_info info = b.request();

            int ndims = info.ndim;

            decltype(CV_32F) dtype;
            size_t elemsize;
            if (info.format == format_descriptor<float>::format()) {
                if (ndims == 3) {
                    dtype = CV_32FC3;
                } else {
                    dtype = CV_32FC1;
G
gineshidalgo99 已提交
355
                }
R
Raaj 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
                elemsize = sizeof(float);
            } else if (info.format == format_descriptor<double>::format()) {
                if (ndims == 3) {
                    dtype = CV_64FC3;
                } else {
                    dtype = CV_64FC1;
                }
                elemsize = sizeof(double);
            } else if (info.format == format_descriptor<unsigned char>::format()) {
                if (ndims == 3) {
                    dtype = CV_8UC3;
                } else {
                    dtype = CV_8UC1;
                }
                elemsize = sizeof(unsigned char);
            } else {
                throw std::logic_error("Unsupported type");
                return false;
G
gineshidalgo99 已提交
374 375
            }

R
Raaj 已提交
376
            std::vector<int> shape = {(int)info.shape[0], (int)info.shape[1]};
G
gineshidalgo99 已提交
377

R
Raaj 已提交
378 379
            value = cv::Mat(cv::Size(shape[1], shape[0]), dtype, info.ptr, cv::Mat::AUTO_STEP);
            return true;
G
gineshidalgo99 已提交
380
        }
R
Raaj 已提交
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

        // Cast cv::Mat to numpy
        static handle cast(const cv::Mat &m, return_value_policy, handle defval)
        {
            std::string format = format_descriptor<unsigned char>::format();
            size_t elemsize = sizeof(unsigned char);
            int dim;
            switch(m.type()) {
                case CV_8U:
                    format = format_descriptor<unsigned char>::format();
                    elemsize = sizeof(unsigned char);
                    dim = 2;
                    break;
                case CV_8UC3:
                    format = format_descriptor<unsigned char>::format();
                    elemsize = sizeof(unsigned char);
                    dim = 3;
                    break;
                case CV_32F:
                    format = format_descriptor<float>::format();
                    elemsize = sizeof(float);
                    dim = 2;
                    break;
                case CV_64F:
                    format = format_descriptor<double>::format();
                    elemsize = sizeof(double);
                    dim = 2;
                    break;
                default:
                    throw std::logic_error("Unsupported type");
            }

            std::vector<size_t> bufferdim;
            std::vector<size_t> strides;
            if (dim == 2) {
                bufferdim = {(size_t) m.rows, (size_t) m.cols};
                strides = {elemsize * (size_t) m.cols, elemsize};
            } else if (dim == 3) {
                bufferdim = {(size_t) m.rows, (size_t) m.cols, (size_t) 3};
                strides = {(size_t) elemsize * m.cols * 3, (size_t) elemsize * 3, (size_t) elemsize};
            }
            return array(buffer_info(
                m.data,         /* Pointer to buffer */
                elemsize,       /* Size of one scalar */
                format,         /* Python struct-style format descriptor */
                dim,            /* Number of dimensions */
                bufferdim,      /* Buffer dimensions */
                strides         /* Strides (in bytes) for each index */
                )).release();
G
gineshidalgo99 已提交
430
        }
R
Raaj 已提交
431

R
Raaj 已提交
432 433
    };
}} // namespace pybind11::detail
R
Raaj 已提交
434 435

#endif