Advertisement
Pug_coder

HandT2

May 26th, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <opencv2/dnn.hpp>
  2. #include <opencv2/imgproc.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7. using namespace cv;
  8. using namespace cv::dnn;
  9.  
  10.  
  11. const int POSE_PAIRS[20][2] =
  12.         {
  13.                 {0,1}, {1,2}, {2,3}, {3,4},         // thumb
  14.                 {0,5}, {5,6}, {6,7}, {7,8},         // index
  15.                 {0,9}, {9,10}, {10,11}, {11,12},    // middle
  16.                 {0,13}, {13,14}, {14,15}, {15,16},  // ring
  17.                 {0,17}, {17,18}, {18,19}, {19,20}   // small
  18.         };
  19.  
  20. string protoFile = "hand/pose_deploy.prototxt";
  21. string weightsFile = "hand/pose_iter_102000.caffemodel";
  22.  
  23. int nPoints = 22;
  24.  
  25. int main(int argc, char **argv)
  26. {
  27.  
  28.     cout << "USAGE : ./handPoseImage <imageFile> " << endl;
  29.  
  30.     string imageFile = "right-frontal.jpg";
  31.     // Take arguments from commmand line
  32.     if (argc == 2)
  33.     {
  34.         imageFile = argv[1];
  35.     }
  36.  
  37.     float thresh = 0.01;
  38.  
  39.     Mat frame = imread(imageFile);
  40.     Mat frameCopy = frame.clone();
  41.     int frameWidth = frame.cols;
  42.     int frameHeight = frame.rows;
  43.  
  44.     float aspect_ratio = frameWidth/(float)frameHeight;
  45.     int inHeight = 368;
  46.     int inWidth = (int(aspect_ratio*inHeight) * 8) / 8;
  47.  
  48.     cout << "inWidth = " << inWidth << " ; inHeight = " << inHeight << endl;
  49.  
  50.     double t = (double) cv::getTickCount();
  51.     Net net = readNetFromCaffe(protoFile, weightsFile);
  52.  
  53.     Mat inpBlob = blobFromImage(frame, 1.0 / 255, Size(inWidth, inHeight), Scalar(0, 0, 0), false, false);
  54.  
  55.     net.setInput(inpBlob);
  56.  
  57.     Mat output = net.forward();
  58.  
  59.     int H = output.size[2];
  60.     int W = output.size[3];
  61.  
  62.     // find the position of the body parts
  63.     vector<Point> points(nPoints);
  64.     for (int n=0; n < nPoints; n++)
  65.     {
  66.         // Probability map of corresponding body's part.
  67.         Mat probMap(H, W, CV_32F, output.ptr(0,n));
  68.         resize(probMap, probMap, Size(frameWidth, frameHeight));
  69.  
  70.         Point maxLoc;
  71.         double prob;
  72.         minMaxLoc(probMap, 0, &prob, 0, &maxLoc);
  73.         if (prob > thresh)
  74.         {
  75.             circle(frameCopy, cv::Point((int)maxLoc.x, (int)maxLoc.y), 8, Scalar(0,255,255), -1);
  76.             cv::putText(frameCopy, cv::format("%d", n), cv::Point((int)maxLoc.x, (int)maxLoc.y), cv::FONT_HERSHEY_COMPLEX, 1, cv::Scalar(0, 0, 255), 2);
  77.  
  78.         }
  79.         points[n] = maxLoc;
  80.     }
  81.  
  82.     int nPairs = sizeof(POSE_PAIRS)/sizeof(POSE_PAIRS[0]);
  83.  
  84.     for (int n = 0; n < nPairs; n++)
  85.     {
  86.         // lookup 2 connected body/hand parts
  87.         Point2f partA = points[POSE_PAIRS[n][0]];
  88.         Point2f partB = points[POSE_PAIRS[n][1]];
  89.  
  90.         if (partA.x<=0 || partA.y<=0 || partB.x<=0 || partB.y<=0)
  91.             continue;
  92.  
  93.         line(frame, partA, partB, Scalar(0,255,255), 8);
  94.         circle(frame, partA, 8, Scalar(0,0,255), -1);
  95.         circle(frame, partB, 8, Scalar(0,0,255), -1);
  96.     }
  97.  
  98.     t = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
  99.     cout << "Time Taken = " << t << endl;
  100.     imshow("Output-Keypoints", frameCopy);
  101.     imshow("Output-Skeleton", frame);
  102.     imwrite("Output-Skeleton.jpg", frame);
  103.  
  104.     waitKey();
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement