handPoseImage.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include <opencv2/dnn.hpp>
  2. #include <opencv2/imgproc.hpp>
  3. #include <opencv2/highgui.hpp>
  4. #include <iostream>
  5. using namespace std;
  6. using namespace cv;
  7. using namespace cv::dnn;
  8. const int POSE_PAIRS[20][2] =
  9. {
  10. {0,1}, {1,2}, {2,3}, {3,4}, // thumb
  11. {0,5}, {5,6}, {6,7}, {7,8}, // index
  12. {0,9}, {9,10}, {10,11}, {11,12}, // middle
  13. {0,13}, {13,14}, {14,15}, {15,16}, // ring
  14. {0,17}, {17,18}, {18,19}, {19,20} // small
  15. };
  16. string protoFile = "hand/pose_deploy.prototxt";
  17. string weightsFile = "hand/pose_iter_102000.caffemodel";
  18. int nPoints = 22;
  19. int main(int argc, char **argv)
  20. {
  21. cout << "USAGE : ./handPoseImage <imageFile> " << endl;
  22. string imageFile = "right-frontal.jpg";
  23. // Take arguments from commmand line
  24. if (argc == 2)
  25. {
  26. imageFile = argv[1];
  27. }
  28. float thresh = 0.01;
  29. Mat frame = imread(imageFile);
  30. Mat frameCopy = frame.clone();
  31. int frameWidth = frame.cols;
  32. int frameHeight = frame.rows;
  33. float aspect_ratio = frameWidth/(float)frameHeight;
  34. int inHeight = 368;
  35. int inWidth = (int(aspect_ratio*inHeight) * 8) / 8;
  36. cout << "inWidth = " << inWidth << " ; inHeight = " << inHeight << endl;
  37. double t = (double) cv::getTickCount();
  38. Net net = readNetFromCaffe(protoFile, weightsFile);
  39. Mat inpBlob = blobFromImage(frame, 1.0 / 255, Size(inWidth, inHeight), Scalar(0, 0, 0), false, false);
  40. net.setInput(inpBlob);
  41. Mat output = net.forward();
  42. int H = output.size[2];
  43. int W = output.size[3];
  44. // find the position of the body parts
  45. vector<Point> points(nPoints);
  46. for (int n=0; n < nPoints; n++)
  47. {
  48. // Probability map of corresponding body's part.
  49. Mat probMap(H, W, CV_32F, output.ptr(0,n));
  50. resize(probMap, probMap, Size(frameWidth, frameHeight));
  51. Point maxLoc;
  52. double prob;
  53. minMaxLoc(probMap, 0, &prob, 0, &maxLoc);
  54. if (prob > thresh)
  55. {
  56. circle(frameCopy, cv::Point((int)maxLoc.x, (int)maxLoc.y), 8, Scalar(0,255,255), -1);
  57. 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);
  58. }
  59. points[n] = maxLoc;
  60. }
  61. int nPairs = sizeof(POSE_PAIRS)/sizeof(POSE_PAIRS[0]);
  62. for (int n = 0; n < nPairs; n++)
  63. {
  64. // lookup 2 connected body/hand parts
  65. Point2f partA = points[POSE_PAIRS[n][0]];
  66. Point2f partB = points[POSE_PAIRS[n][1]];
  67. if (partA.x<=0 || partA.y<=0 || partB.x<=0 || partB.y<=0)
  68. continue;
  69. line(frame, partA, partB, Scalar(0,255,255), 8);
  70. circle(frame, partA, 8, Scalar(0,0,255), -1);
  71. circle(frame, partB, 8, Scalar(0,0,255), -1);
  72. }
  73. t = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
  74. cout << "Time Taken = " << t << endl;
  75. imshow("Output-Keypoints", frameCopy);
  76. imshow("Output-Skeleton", frame);
  77. imwrite("Output-Skeleton.jpg", frame);
  78. waitKey();
  79. return 0;
  80. }