handPoseVideo.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. float thresh = 0.01;
  22. cv::VideoCapture cap("asl.mp4");
  23. if (!cap.isOpened())
  24. {
  25. cerr << "Unable to connect to camera" << endl;
  26. return 1;
  27. }
  28. Mat frame, frameCopy;
  29. int frameWidth = cap.get(CAP_PROP_FRAME_WIDTH);
  30. int frameHeight = cap.get(CAP_PROP_FRAME_HEIGHT);
  31. float aspect_ratio = frameWidth/(float)frameHeight;
  32. int inHeight = 368;
  33. int inWidth = (int(aspect_ratio*inHeight) * 8) / 8;
  34. cout << "inWidth = " << inWidth << " ; inHeight = " << inHeight << endl;
  35. VideoWriter video("Output-Skeleton.avi",VideoWriter::fourcc('M','J','P','G'), 10, Size(frameWidth,frameHeight));
  36. Net net = readNetFromCaffe(protoFile, weightsFile);
  37. double t=0;
  38. while(1)
  39. {
  40. double t = (double) cv::getTickCount();
  41. cap >> frame;
  42. frameCopy = frame.clone();
  43. Mat inpBlob = blobFromImage(frame, 1.0 / 255, Size(inWidth, inHeight), Scalar(0, 0, 0), false, false);
  44. net.setInput(inpBlob);
  45. Mat output = net.forward();
  46. int H = output.size[2];
  47. int W = output.size[3];
  48. // find the position of the body parts
  49. vector<Point> points(nPoints);
  50. for (int n=0; n < nPoints; n++)
  51. {
  52. // Probability map of corresponding body's part.
  53. Mat probMap(H, W, CV_32F, output.ptr(0,n));
  54. resize(probMap, probMap, Size(frameWidth, frameHeight));
  55. Point maxLoc;
  56. double prob;
  57. minMaxLoc(probMap, 0, &prob, 0, &maxLoc);
  58. if (prob > thresh)
  59. {
  60. circle(frameCopy, cv::Point((int)maxLoc.x, (int)maxLoc.y), 8, Scalar(0,255,255), -1);
  61. 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);
  62. }
  63. points[n] = maxLoc;
  64. }
  65. int nPairs = sizeof(POSE_PAIRS)/sizeof(POSE_PAIRS[0]);
  66. for (int n = 0; n < nPairs; n++)
  67. {
  68. // lookup 2 connected body/hand parts
  69. Point2f partA = points[POSE_PAIRS[n][0]];
  70. Point2f partB = points[POSE_PAIRS[n][1]];
  71. if (partA.x<=0 || partA.y<=0 || partB.x<=0 || partB.y<=0)
  72. continue;
  73. line(frame, partA, partB, Scalar(0,255,255), 8);
  74. circle(frame, partA, 8, Scalar(0,0,255), -1);
  75. circle(frame, partB, 8, Scalar(0,0,255), -1);
  76. }
  77. t = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
  78. cout << "Time Taken for frame = " << t << endl;
  79. cv::putText(frame, cv::format("time taken = %.2f sec", t), cv::Point(50, 50), cv::FONT_HERSHEY_COMPLEX, .8, cv::Scalar(255, 50, 0), 2);
  80. // imshow("Output-Keypoints", frameCopy);
  81. imshow("Output-Skeleton", frame);
  82. video.write(frame);
  83. char key = waitKey(1);
  84. if (key==27)
  85. break;
  86. }
  87. // When everything done, release the video capture and write object
  88. cap.release();
  89. video.release();
  90. return 0;
  91. }