OpenPoseVideo.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include <opencv2/opencv.hpp>
  2. #include <iostream>
  3. using namespace std;
  4. using namespace cv;
  5. using namespace cv::dnn;
  6. #define MPI
  7. #ifdef MPI
  8. const int POSE_PAIRS[14][2] =
  9. {
  10. {0,1}, {1,2}, {2,3},
  11. {3,4}, {1,5}, {5,6},
  12. {6,7}, {1,14}, {14,8}, {8,9},
  13. {9,10}, {14,11}, {11,12}, {12,13}
  14. };
  15. string protoFile = "pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt";
  16. string weightsFile = "pose/mpi/pose_iter_160000.caffemodel";
  17. int nPoints = 15;
  18. #endif
  19. #ifdef COCO
  20. const int POSE_PAIRS[17][2] =
  21. {
  22. {1,2}, {1,5}, {2,3},
  23. {3,4}, {5,6}, {6,7},
  24. {1,8}, {8,9}, {9,10},
  25. {1,11}, {11,12}, {12,13},
  26. {1,0}, {0,14},
  27. {14,16}, {0,15}, {15,17}
  28. };
  29. string protoFile = "pose/coco/pose_deploy_linevec.prototxt";
  30. string weightsFile = "pose/coco/pose_iter_440000.caffemodel";
  31. int nPoints = 18;
  32. #endif
  33. int main(int argc, char **argv)
  34. {
  35. cout << "USAGE : ./OpenPose <videoFile> " << endl;
  36. cout << "USAGE : ./OpenPose <videoFile> <device>" << endl;
  37. string device = "cpu";
  38. string videoFile = "sample_video.mp4";
  39. // Take arguments from commmand line
  40. if (argc == 2)
  41. {
  42. if((string)argv[1] == "gpu")
  43. device = "gpu";
  44. else
  45. videoFile = argv[1];
  46. }
  47. else if (argc == 3)
  48. {
  49. videoFile = argv[1];
  50. if((string)argv[2] == "gpu")
  51. device = "gpu";
  52. }
  53. int inWidth = 368;
  54. int inHeight = 368;
  55. float thresh = 0.01;
  56. cv::VideoCapture cap(videoFile);
  57. if (!cap.isOpened())
  58. {
  59. cerr << "Unable to connect to camera" << endl;
  60. return 1;
  61. }
  62. Mat frame, frameCopy;
  63. int frameWidth = cap.get(CAP_PROP_FRAME_WIDTH);
  64. int frameHeight = cap.get(CAP_PROP_FRAME_HEIGHT);
  65. VideoWriter video("Output-Skeleton.avi",VideoWriter::fourcc('M','J','P','G'), 10, Size(frameWidth,frameHeight));
  66. Net net = readNetFromCaffe(protoFile, weightsFile);
  67. if (device == "cpu")
  68. {
  69. cout << "Using CPU device" << endl;
  70. net.setPreferableBackend(DNN_TARGET_CPU);
  71. }
  72. else if (device == "gpu")
  73. {
  74. cout << "Using GPU device" << endl;
  75. net.setPreferableBackend(DNN_BACKEND_CUDA);
  76. net.setPreferableTarget(DNN_TARGET_CUDA);
  77. }
  78. double t=0;
  79. while( waitKey(1) < 0)
  80. {
  81. double t = (double) cv::getTickCount();
  82. cap >> frame;
  83. frameCopy = frame.clone();
  84. Mat inpBlob = blobFromImage(frame, 1.0 / 255, Size(inWidth, inHeight), Scalar(0, 0, 0), false, false);
  85. net.setInput(inpBlob);
  86. Mat output = net.forward();
  87. int H = output.size[2];
  88. int W = output.size[3];
  89. // find the position of the body parts
  90. vector<Point> points(nPoints);
  91. for (int n=0; n < nPoints; n++)
  92. {
  93. // Probability map of corresponding body's part.
  94. Mat probMap(H, W, CV_32F, output.ptr(0,n));
  95. Point2f p(-1,-1);
  96. Point maxLoc;
  97. double prob;
  98. minMaxLoc(probMap, 0, &prob, 0, &maxLoc);
  99. if (prob > thresh)
  100. {
  101. p = maxLoc;
  102. p.x *= (float)frameWidth / W ;
  103. p.y *= (float)frameHeight / H ;
  104. circle(frameCopy, cv::Point((int)p.x, (int)p.y), 8, Scalar(0,255,255), -1);
  105. cv::putText(frameCopy, cv::format("%d", n), cv::Point((int)p.x, (int)p.y), cv::FONT_HERSHEY_COMPLEX, 1.1, cv::Scalar(0, 0, 255), 2);
  106. }
  107. points[n] = p;
  108. }
  109. int nPairs = sizeof(POSE_PAIRS)/sizeof(POSE_PAIRS[0]);
  110. for (int n = 0; n < nPairs; n++)
  111. {
  112. // lookup 2 connected body/hand parts
  113. Point2f partA = points[POSE_PAIRS[n][0]];
  114. Point2f partB = points[POSE_PAIRS[n][1]];
  115. if (partA.x<=0 || partA.y<=0 || partB.x<=0 || partB.y<=0)
  116. continue;
  117. line(frame, partA, partB, Scalar(0,255,255), 8);
  118. circle(frame, partA, 8, Scalar(0,0,255), -1);
  119. circle(frame, partB, 8, Scalar(0,0,255), -1);
  120. }
  121. t = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
  122. cv::putText(frame, cv::format("time taken = %.3f sec", t), cv::Point(50, 50), cv::FONT_HERSHEY_COMPLEX, .8, cv::Scalar(255, 50, 0), 2);
  123. imshow("Output-Skeleton", frame);
  124. video.write(frame);
  125. }
  126. // When everything done, release the video capture and write object
  127. cap.release();
  128. video.release();
  129. return 0;
  130. }