resizeTest.cpp 4.3 KB
Newer Older
1 2 3
// ------------------------- OpenPose Resize Layer Testing -------------------------

#include <openpose/headers.hpp>
G
gineshidalgo99 已提交
4 5 6 7 8 9 10 11 12 13 14
#ifdef USE_CUDA
    #include <chrono> // `std::chrono::` functions and classes, e.g. std::chrono::milliseconds
    // GFlags: DEFINE_bool, _int32, _int64, _uint64, _double, _string
    #include <gflags/gflags.h>
    // Allow Google Flags in Ubuntu 14
    #ifndef GFLAGS_GFLAGS_H_
    namespace gflags = google;
    #endif
    #ifdef USE_CAFFE
        #include <caffe/net.hpp>
    #endif
15

G
gineshidalgo99 已提交
16 17 18 19 20 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 52 53 54 55 56 57 58
    DEFINE_string(image_path,               "examples/media/COCO_val2014_000000000192.jpg",     "Process the desired image.");

    cv::Mat gpuResize(cv::Mat& img, const cv::Size& newSize)
    {
        #ifdef USE_CUDA
            // Upload to Source to GPU
            float* cpuPtr = &img.at<float>(0);
            float* gpuPtr;
            cudaMallocHost((void **)&gpuPtr, img.size().width * img.size().height * sizeof(float));
            cudaMemcpy(gpuPtr, cpuPtr, img.size().width * img.size().height * sizeof(float),
                       cudaMemcpyHostToDevice);

            // Upload to Dest to GPU
            cv::Mat newImg = cv::Mat(newSize,CV_32FC1,cv::Scalar(0));
            float* newCpuPtr = &newImg.at<float>(0);
            float* newGpuPtr;
            cudaMallocHost((void **)&newGpuPtr, newSize.width * newSize.height * sizeof(float));
            cudaMemcpy(newGpuPtr, newCpuPtr, newSize.width * newSize.height * sizeof(float),
                       cudaMemcpyHostToDevice);

            std::vector<const float*> sourcePtrs;
            sourcePtrs.emplace_back(gpuPtr);
            std::array<int, 4> targetSize = {1,1,newImg.size().height,newImg.size().width};
            std::array<int, 4> sourceSize = {1,1,img.size().height,img.size().width};
            std::vector<std::array<int, 4>> sourceSizes;
            sourceSizes.emplace_back(sourceSize);
            op::resizeAndMergeGpu(newGpuPtr, sourcePtrs, targetSize, sourceSizes);
            cudaMemcpy(newCpuPtr, newGpuPtr, newImg.size().width * newImg.size().height * sizeof(float),
                       cudaMemcpyDeviceToHost);

            cudaFree(gpuPtr);
            cudaFree(newGpuPtr);
            return newImg;
        #else
            UNUSED(img);
            UNUSED(newSize);
            op::error("OpenPose must be compiled with the `USE_CAFFE` & `USE_CUDA` macro definitions in order to run"
                  " this functionality.", __LINE__, __FUNCTION__, __FILE__);
        #endif
    }

    cv::Mat cpuResize(cv::Mat& img, cv::Size newSize)
    {
59 60 61 62 63 64 65
        // Upload to Source to GPU
        float* cpuPtr = &img.at<float>(0);

        // Upload to Dest to GPU
        cv::Mat newImg = cv::Mat(newSize,CV_32FC1,cv::Scalar(0));

        std::vector<const float*> sourcePtrs;
G
gineshidalgo99 已提交
66
        sourcePtrs.emplace_back(cpuPtr);
67 68 69 70
        std::array<int, 4> targetSize = {1,1,newImg.size().height,newImg.size().width};
        std::array<int, 4> sourceSize = {1,1,img.size().height,img.size().width};
        std::vector<std::array<int, 4>> sourceSizes;
        sourceSizes.emplace_back(sourceSize);
G
gineshidalgo99 已提交
71
        op::resizeAndMergeCpu(&newImg.at<float>(0), sourcePtrs, targetSize, sourceSizes);
72 73

        return newImg;
G
gineshidalgo99 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    }

    int resizeTest()
    {
        // logging_level
        cv::Mat img = op::loadImage(FLAGS_image_path, CV_LOAD_IMAGE_GRAYSCALE);
        if(img.empty())
            op::error("Could not open or find the image: " + FLAGS_image_path, __LINE__, __FUNCTION__, __FILE__);
        img.convertTo(img, CV_32FC1);
        img = cpuResize(img, cv::Size(img.size().width/4,img.size().height/4));
        img*=0.005;

        cv::Mat gpuImg = gpuResize(img, cv::Size(img.size().width*8,img.size().height*8));
        cv::Mat cpuImg = cpuResize(img, cv::Size(img.size().width*8,img.size().height*8));
        cv::imshow("gpuImg", gpuImg);
        cv::imshow("cpuImg", cpuImg);

        op::log("Done");
        cv::waitKey(0);

        return 0;
    }
#endif
97 98 99

int main(int argc, char *argv[])
{
G
gineshidalgo99 已提交
100 101 102
    #ifdef USE_CUDA
        // Parsing command line flags
        gflags::ParseCommandLineFlags(&argc, &argv, true);
103

G
gineshidalgo99 已提交
104 105 106 107 108 109 110
        // Running handFromJsonTest
        return resizeTest();
    #else
        op::error("OpenPose must be compiled with the `USE_CAFFE` & `USE_CUDA` macro definitions in order to run"
              " this functionality.", __LINE__, __FUNCTION__, __FILE__);
        return 0;
    #endif
111
}