Having issues with opencv/dnn with caffe model

asked 2018-10-08 17:02:22 -0500

Prof. xavier gravatar image

I am trying to make use of openpose example in opencv using caffe model and opencv/dnn.hpp

tutorial I have been following - https://www.learnopencv.com/deep-lear...

we require 2 files for the network as said in the tutorial : 1 - prototxt - https://github.com/spmallick/learnope... 2 - caffemodel - http://posefs1.perception.cs.cmu.edu/...

I downloaded the files and made this node for pose estimation

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/dnn/dnn.hpp>
#include <sensor_msgs/image_encodings.h>
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
#include <iostream>


 using namespace std;
 using namespace cv;
 using namespace cv::dnn;

  static const std::string OPENCV_WINDOW = "Image window";


  #define COCO

  #ifdef COCO
  const int POSE_PAIRS[17][2] = 
  {   
            {1,2}, {1,5}, {2,3},
             {3,4}, {5,6}, {6,7},
             {1,8}, {8,9}, {9,10},
             {1,11}, {11,12}, {12,13},
             {1,0},{0,14},
             {14,16}, {0,15}, {15,17}
     };

static const std::string protoFile = "pose/coco/pose_deploy_linevec.prototxt";
static const std::string weightsFile = "pose/coco/pose_iter_440000.caffemodel"; 


        int nPoints = 18;
        #endif

         class ImageConverter
      {
                ros::NodeHandle nh_;
                image_transport::ImageTransport it_;
                image_transport::Subscriber image_sub_;

        public:
                        ImageConverter()
                                             : it_(nh_)
     {

            image_sub_ = it_.subscribe("/zed/rgb/image_raw_color", 1, &ImageConverter::imageCb, this);
      }


     ~ImageConverter()
     {
           cv::destroyWindow(OPENCV_WINDOW);
       }



void imageCb(const sensor_msgs::ImageConstPtr& msg)
{
     cv_bridge::CvImagePtr cv_ptr;
     try
     {
     cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);
     }  
     catch (cv_bridge::Exception& e)
     {
     ROS_ERROR("cv_bridge exception: %s", e.what());
     return;
     }


if (cv_ptr->image.rows > 60 && cv_ptr->image.cols > 60)

detect_people(cv_ptr->image); 

cv::waitKey(3);



 }





void detect_people(cv::Mat msg)
{
int inWidth = msg.cols;
int inHeight = msg.rows;
float thresh = 0.1;   

cv::Mat frame;
msg.copyTo(frame);
cv::Mat frameCopy = frame.clone();
int frameWidth = frame.cols;
int frameHeight = frame.rows;

cv::dnn::Net net = cv::dnn::readNetFromCaffe("pose_deploy_linevec.prototxt" ,"pose_iter_440000.caffemodel");

cv::Mat inpBlob = blobFromImage(frame, 1.0/255, cv::Size(inWidth, inHeight), cv::Scalar(0, 0, 0), false, false);  

net.setInput(inpBlob);

cv::Mat output = net.forward();

int H = output.size[2];
int W = output.size[3];

std::vector<cv::Point> points(nPoints);

for (int n=0; n < nPoints; n++)
{
    // Probability map of corresponding body's part.
    cv::Mat probMap(H, W, CV_32F, output.ptr(0,n));

    cv::Point2f p(-1,-1);
    cv::Point maxLoc;
    double prob;
    cv::minMaxLoc(probMap, 0, &prob, 0, &maxLoc);
    if (prob > thresh)
    {
        p = maxLoc;
        p.x *= (float)frameWidth / W ;
        p.y *= (float)frameHeight / H ;

        cv::circle(frameCopy, cv::Point((int)p.x, (int)p.y), 8, Scalar(0,255,255), -1);
        cv::putText(frameCopy, cv::format("%d", n), cv::Point((int)p.x, (int)p.y), cv::FONT_HERSHEY_COMPLEX, 1,      cv::Scalar(0, 0, 255), 2);

    }
    points[n] = p;
}



int nPairs = sizeof(POSE_PAIRS)/sizeof(POSE_PAIRS[0]);

for (int n = 0; n < nPairs; n++)
{
    // lookup 2 connected body/hand parts
    Point2f partA = points[POSE_PAIRS[n][0]];
    Point2f partB = points[POSE_PAIRS[n][1]];

    if (partA.x<=0 || partA.y<=0 || partB.x<=0 || partB.y<=0)
        continue;

    cv::line(frame, partA, partB, cv::Scalar(0,255,255), 8 ...
(more)
edit retag flag offensive close merge delete

Comments

I would say this is first and foremost an opencv issue, not so much a ROS one.

OpenCV has its own support forums which may be more suitable for your question.

gvdhoorn gravatar image gvdhoorn  ( 2018-10-09 02:52:03 -0500 )edit