OpenPoseVideo.cpp 4.5 KB

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