Skip to content

Commit 5075986

Browse files
committed
Init add files
1 parent d112726 commit 5075986

37 files changed

+9382
-0
lines changed

.DS_Store

12 KB
Binary file not shown.

CMakeLists.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 2.8.12)
2+
3+
PROJECT(openPose)
4+
5+
find_package( OpenCV REQUIRED )
6+
7+
include_directories( ${OpenCV_INCLUDE_DIRS})
8+
9+
MACRO(add_example name)
10+
ADD_EXECUTABLE(${name} ${name}.cpp)
11+
TARGET_LINK_LIBRARIES(${name} ${OpenCV_LIBS})
12+
ENDMACRO()
13+
14+
add_example(OpenPoseImage)
15+
add_example(OpenPoseVideo)

OpenPoseImage.cpp

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
#define MPI
11+
12+
#ifdef MPI
13+
const int POSE_PAIRS[14][2] =
14+
{
15+
{0,1}, {1,2}, {2,3},
16+
{3,4}, {1,5}, {5,6},
17+
{6,7}, {1,14}, {14,8}, {8,9},
18+
{9,10}, {14,11}, {11,12}, {12,13}
19+
};
20+
21+
string protoFile = "pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt";
22+
string weightsFile = "pose/mpi/pose_iter_160000.caffemodel";
23+
24+
int nPoints = 15;
25+
#endif
26+
27+
#ifdef COCO
28+
const int POSE_PAIRS[17][2] =
29+
{
30+
{1,2}, {1,5}, {2,3},
31+
{3,4}, {5,6}, {6,7},
32+
{1,8}, {8,9}, {9,10},
33+
{1,11}, {11,12}, {12,13},
34+
{1,0}, {0,14},
35+
{14,16}, {0,15}, {15,17}
36+
};
37+
38+
string protoFile = "pose/coco/pose_deploy_linevec.prototxt";
39+
string weightsFile = "pose/coco/pose_iter_440000.caffemodel";
40+
41+
int nPoints = 18;
42+
#endif
43+
44+
int main(int argc, char **argv)
45+
{
46+
47+
cout << "USAGE : ./openpose <imageFile> " << endl;
48+
49+
string imageFile = "single.jpeg";
50+
// Take arguments from commmand line
51+
if (argc == 2)
52+
{
53+
imageFile = argv[1];
54+
}
55+
56+
int inWidth = 368;
57+
int inHeight = 368;
58+
float thresh = 0.1;
59+
60+
Mat frame = imread(imageFile);
61+
Mat frameCopy = frame.clone();
62+
int frameWidth = frame.cols;
63+
int frameHeight = frame.rows;
64+
65+
double t = (double) cv::getTickCount();
66+
Net net = readNetFromCaffe(protoFile, weightsFile);
67+
68+
Mat inpBlob = blobFromImage(frame, 1.0 / 255, Size(inWidth, inHeight), Scalar(0, 0, 0), false, false);
69+
70+
net.setInput(inpBlob);
71+
72+
Mat output = net.forward();
73+
74+
int H = output.size[2];
75+
int W = output.size[3];
76+
77+
// find the position of the body parts
78+
vector<Point> points(nPoints);
79+
for (int n=0; n < nPoints; n++)
80+
{
81+
// Probability map of corresponding body's part.
82+
Mat probMap(H, W, CV_32F, output.ptr(0,n));
83+
84+
Point2f p(-1,-1);
85+
Point maxLoc;
86+
double prob;
87+
minMaxLoc(probMap, 0, &prob, 0, &maxLoc);
88+
if (prob > thresh)
89+
{
90+
p = maxLoc;
91+
p.x *= (float)frameWidth / W ;
92+
p.y *= (float)frameHeight / H ;
93+
94+
circle(frameCopy, cv::Point((int)p.x, (int)p.y), 8, Scalar(0,255,255), -1);
95+
cv::putText(frameCopy, cv::format("%d", n), cv::Point((int)p.x, (int)p.y), cv::FONT_HERSHEY_COMPLEX, 1, cv::Scalar(0, 0, 255), 2);
96+
97+
}
98+
points[n] = p;
99+
}
100+
101+
int nPairs = sizeof(POSE_PAIRS)/sizeof(POSE_PAIRS[0]);
102+
103+
for (int n = 0; n < nPairs; n++)
104+
{
105+
// lookup 2 connected body/hand parts
106+
Point2f partA = points[POSE_PAIRS[n][0]];
107+
Point2f partB = points[POSE_PAIRS[n][1]];
108+
109+
if (partA.x<=0 || partA.y<=0 || partB.x<=0 || partB.y<=0)
110+
continue;
111+
112+
line(frame, partA, partB, Scalar(0,255,255), 8);
113+
circle(frame, partA, 8, Scalar(0,0,255), -1);
114+
circle(frame, partB, 8, Scalar(0,0,255), -1);
115+
}
116+
117+
t = ((double)cv::getTickCount() - t)/cv::getTickFrequency();
118+
cout << "Time Taken = " << t << endl;
119+
imshow("Output-Keypoints", frameCopy);
120+
imshow("Output-Skeleton", frame);
121+
imwrite("Output-Skeleton.jpg", frame);
122+
123+
waitKey();
124+
125+
return 0;
126+
}

OpenPoseImage.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import cv2
2+
import time
3+
import numpy as np
4+
5+
MODE = "COCO"
6+
7+
if MODE is "COCO":
8+
protoFile = "pose/coco/pose_deploy_linevec.prototxt"
9+
weightsFile = "pose/coco/pose_iter_440000.caffemodel"
10+
nPoints = 18
11+
POSE_PAIRS = [ [1,0],[1,2],[1,5],[2,3],[3,4],[5,6],[6,7],[1,8],[8,9],[9,10],[1,11],[11,12],[12,13],[0,14],[0,15],[14,16],[15,17]]
12+
13+
elif MODE is "MPI" :
14+
protoFile = "pose/mpi/pose_deploy_linevec_faster_4_stages.prototxt"
15+
weightsFile = "pose/mpi/pose_iter_160000.caffemodel"
16+
nPoints = 15
17+
POSE_PAIRS = [[0,1], [1,2], [2,3], [3,4], [1,5], [5,6], [6,7], [1,14], [14,8], [8,9], [9,10], [14,11], [11,12], [12,13] ]
18+
19+
20+
frame = cv2.imread("single.jpeg")
21+
frameCopy = np.copy(frame)
22+
frameWidth = frame.shape[1]
23+
frameHeight = frame.shape[0]
24+
threshold = 0.1
25+
26+
net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
27+
28+
t = time.time()
29+
# input image dimensions for the network
30+
inWidth = 368
31+
inHeight = 368
32+
inpBlob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
33+
(0, 0, 0), swapRB=False, crop=False)
34+
35+
net.setInput(inpBlob)
36+
37+
output = net.forward()
38+
print("time taken by network : {:.3f}".format(time.time() - t))
39+
40+
H = output.shape[2]
41+
W = output.shape[3]
42+
43+
# Empty list to store the detected keypoints
44+
points = []
45+
46+
for i in range(nPoints):
47+
# confidence map of corresponding body's part.
48+
probMap = output[0, i, :, :]
49+
50+
# Find global maxima of the probMap.
51+
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
52+
53+
# Scale the point to fit on the original image
54+
x = (frameWidth * point[0]) / W
55+
y = (frameHeight * point[1]) / H
56+
57+
if prob > threshold :
58+
cv2.circle(frameCopy, (int(x), int(y)), 8, (0, 255, 255), thickness=-1, lineType=cv2.FILLED)
59+
cv2.putText(frameCopy, "{}".format(i), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, lineType=cv2.LINE_AA)
60+
61+
# Add the point to the list if the probability is greater than the threshold
62+
points.append((int(x), int(y)))
63+
else :
64+
points.append(None)
65+
66+
# Draw Skeleton
67+
for pair in POSE_PAIRS:
68+
partA = pair[0]
69+
partB = pair[1]
70+
71+
if points[partA] and points[partB]:
72+
cv2.line(frame, points[partA], points[partB], (0, 255, 255), 2)
73+
cv2.circle(frame, points[partA], 8, (0, 0, 255), thickness=-1, lineType=cv2.FILLED)
74+
75+
76+
cv2.imshow('Output-Keypoints', frameCopy)
77+
cv2.imshow('Output-Skeleton', frame)
78+
79+
80+
cv2.imwrite('Output-Keypoints.jpg', frameCopy)
81+
cv2.imwrite('Output-Skeleton.jpg', frame)
82+
83+
print("Total time taken : {:.3f}".format(time.time() - t))
84+
85+
cv2.waitKey(0)
86+

OpenPoseVideo.cpp

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

0 commit comments

Comments
 (0)