Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/HandPose/handPoseVideo.cpp
3118 views
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
const int POSE_PAIRS[20][2] =
11
{
12
{0,1}, {1,2}, {2,3}, {3,4}, // thumb
13
{0,5}, {5,6}, {6,7}, {7,8}, // index
14
{0,9}, {9,10}, {10,11}, {11,12}, // middle
15
{0,13}, {13,14}, {14,15}, {15,16}, // ring
16
{0,17}, {17,18}, {18,19}, {19,20} // small
17
};
18
19
string protoFile = "hand/pose_deploy.prototxt";
20
string weightsFile = "hand/pose_iter_102000.caffemodel";
21
22
int nPoints = 22;
23
24
int main(int argc, char **argv)
25
{
26
float thresh = 0.01;
27
28
cv::VideoCapture cap("asl.mp4");
29
30
if (!cap.isOpened())
31
{
32
cerr << "Unable to connect to camera" << endl;
33
return 1;
34
}
35
36
Mat frame, frameCopy;
37
int frameWidth = cap.get(CAP_PROP_FRAME_WIDTH);
38
int frameHeight = cap.get(CAP_PROP_FRAME_HEIGHT);
39
float aspect_ratio = frameWidth/(float)frameHeight;
40
int inHeight = 368;
41
int inWidth = (int(aspect_ratio*inHeight) * 8) / 8;
42
43
cout << "inWidth = " << inWidth << " ; inHeight = " << inHeight << endl;
44
45
VideoWriter video("Output-Skeleton.avi",VideoWriter::fourcc('M','J','P','G'), 10, Size(frameWidth,frameHeight));
46
47
Net net = readNetFromCaffe(protoFile, weightsFile);
48
49
double t=0;
50
while(1)
51
{
52
double t = (double) cv::getTickCount();
53
54
cap >> frame;
55
frameCopy = frame.clone();
56
Mat inpBlob = blobFromImage(frame, 1.0 / 255, Size(inWidth, inHeight), Scalar(0, 0, 0), false, false);
57
58
net.setInput(inpBlob);
59
60
Mat output = net.forward();
61
62
int H = output.size[2];
63
int W = output.size[3];
64
65
// find the position of the body parts
66
vector<Point> points(nPoints);
67
for (int n=0; n < nPoints; n++)
68
{
69
// Probability map of corresponding body's part.
70
Mat probMap(H, W, CV_32F, output.ptr(0,n));
71
resize(probMap, probMap, Size(frameWidth, frameHeight));
72
73
Point maxLoc;
74
double prob;
75
minMaxLoc(probMap, 0, &prob, 0, &maxLoc);
76
if (prob > thresh)
77
{
78
circle(frameCopy, cv::Point((int)maxLoc.x, (int)maxLoc.y), 8, Scalar(0,255,255), -1);
79
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);
80
81
}
82
points[n] = maxLoc;
83
}
84
85
int nPairs = sizeof(POSE_PAIRS)/sizeof(POSE_PAIRS[0]);
86
87
for (int n = 0; n < nPairs; n++)
88
{
89
// lookup 2 connected body/hand parts
90
Point2f partA = points[POSE_PAIRS[n][0]];
91
Point2f partB = points[POSE_PAIRS[n][1]];
92
93
if (partA.x<=0 || partA.y<=0 || partB.x<=0 || partB.y<=0)
94
continue;
95
96
line(frame, partA, partB, Scalar(0,255,255), 8);
97
circle(frame, partA, 8, Scalar(0,0,255), -1);
98
circle(frame, partB, 8, Scalar(0,0,255), -1);
99
}
100
101
t = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
102
cout << "Time Taken for frame = " << t << endl;
103
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);
104
// imshow("Output-Keypoints", frameCopy);
105
imshow("Output-Skeleton", frame);
106
video.write(frame);
107
char key = waitKey(1);
108
if (key==27)
109
break;
110
}
111
// When everything done, release the video capture and write object
112
cap.release();
113
video.release();
114
115
return 0;
116
}
117
118