1 # To use Inference Engine backend, specify location of plugins:
2 # export LD_LIBRARY_PATH=/opt/intel/deeplearning_deploymenttoolkit/deployment_tools/external/mklml_lnx/lib:$LD_LIBRARY_PATH
7 parser = argparse.ArgumentParser(
8 description='This script is used to demonstrate OpenPose human pose estimation network '
9 'from https://github.com/CMU-Perceptual-Computing-Lab/openpose project using OpenCV. '
10 'The sample and model are simplified and could be used for a single person on the frame.')
11 parser.add_argument('--input', help='Path to image or video. Skip to capture frames from camera')
12 parser.add_argument('--proto', help='Path to .prototxt')
13 parser.add_argument('--model', help='Path to .caffemodel')
14 parser.add_argument('--dataset', help='Specify what kind of model was trained. '
15 'It could be (COCO, MPI) depends on dataset.')
16 parser.add_argument('--thr', default=0.1, type=float, help='Threshold value for pose parts heat map')
17 parser.add_argument('--width', default=368, type=int, help='Resize input to specific width.')
18 parser.add_argument('--height', default=368, type=int, help='Resize input to specific height.')
20 args = parser.parse_args()
22 if args.dataset == 'COCO':
23 BODY_PARTS = { "Nose": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
24 "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
25 "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "REye": 14,
26 "LEye": 15, "REar": 16, "LEar": 17, "Background": 18 }
28 POSE_PAIRS = [ ["Neck", "RShoulder"], ["Neck", "LShoulder"], ["RShoulder", "RElbow"],
29 ["RElbow", "RWrist"], ["LShoulder", "LElbow"], ["LElbow", "LWrist"],
30 ["Neck", "RHip"], ["RHip", "RKnee"], ["RKnee", "RAnkle"], ["Neck", "LHip"],
31 ["LHip", "LKnee"], ["LKnee", "LAnkle"], ["Neck", "Nose"], ["Nose", "REye"],
32 ["REye", "REar"], ["Nose", "LEye"], ["LEye", "LEar"] ]
34 assert(args.dataset == 'MPI')
35 BODY_PARTS = { "Head": 0, "Neck": 1, "RShoulder": 2, "RElbow": 3, "RWrist": 4,
36 "LShoulder": 5, "LElbow": 6, "LWrist": 7, "RHip": 8, "RKnee": 9,
37 "RAnkle": 10, "LHip": 11, "LKnee": 12, "LAnkle": 13, "Chest": 14,
40 POSE_PAIRS = [ ["Head", "Neck"], ["Neck", "RShoulder"], ["RShoulder", "RElbow"],
41 ["RElbow", "RWrist"], ["Neck", "LShoulder"], ["LShoulder", "LElbow"],
42 ["LElbow", "LWrist"], ["Neck", "Chest"], ["Chest", "RHip"], ["RHip", "RKnee"],
43 ["RKnee", "RAnkle"], ["Chest", "LHip"], ["LHip", "LKnee"], ["LKnee", "LAnkle"] ]
46 inHeight = args.height
48 net = cv.dnn.readNetFromCaffe(args.proto, args.model)
50 cap = cv.VideoCapture(args.input if args.input else 0)
52 while cv.waitKey(1) < 0:
53 hasFrame, frame = cap.read()
58 frameWidth = frame.shape[1]
59 frameHeight = frame.shape[0]
60 inp = cv.dnn.blobFromImage(frame, 1.0 / 255, (inWidth, inHeight),
61 (0, 0, 0), swapRB=False, crop=False)
65 assert(len(BODY_PARTS) == out.shape[1])
68 for i in range(len(BODY_PARTS)):
69 # Slice heatmap of corresponging body's part.
70 heatMap = out[0, i, :, :]
72 # Originally, we try to find all the local maximums. To simplify a sample
73 # we just find a global one. However only a single pose at the same time
74 # could be detected this way.
75 _, conf, _, point = cv.minMaxLoc(heatMap)
76 x = (frameWidth * point[0]) / out.shape[3]
77 y = (frameHeight * point[1]) / out.shape[2]
79 # Add a point if it's confidence is higher than threshold.
80 points.append((int(x), int(y)) if conf > args.thr else None)
82 for pair in POSE_PAIRS:
85 assert(partFrom in BODY_PARTS)
86 assert(partTo in BODY_PARTS)
88 idFrom = BODY_PARTS[partFrom]
89 idTo = BODY_PARTS[partTo]
91 if points[idFrom] and points[idTo]:
92 cv.line(frame, points[idFrom], points[idTo], (0, 255, 0), 3)
93 cv.ellipse(frame, points[idFrom], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
94 cv.ellipse(frame, points[idTo], (3, 3), 0, 0, 360, (0, 0, 255), cv.FILLED)
96 t, _ = net.getPerfProfile()
97 freq = cv.getTickFrequency() / 1000
98 cv.putText(frame, '%.2fms' % (t / freq), (10, 20), cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0))
100 cv.imshow('OpenPose using OpenCV', frame)