-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandsFromVideo.py
97 lines (81 loc) · 2.7 KB
/
handsFromVideo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import cv2
import numpy as np
import os
import sys
import time
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--cudaversion", type=int, choices=[8,9])
args = parser.parse_args()
dir_path = ""
model_path = ""
if args.cudaversion == 9:
dir_path = '/home/li/Work/openpose-cuda9.0/build/python/'
model_path = "/home/li/Work/openpose-cuda9.0/models/"
else:
dir_path = '/home/li/Work/openpose-cuda8.0/build/python/'
model_path = "/home/li/Work/openpose-cuda8.0/models/"
print(dir_path)
sys.path.append(dir_path)
from openpose import pyopenpose as op
cam = cv2.VideoCapture(0, cv2.CAP_V4L)
print(cam.isOpened())
cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1980)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 1280)
params = dict()
params["model_folder"] = model_path
params["hand"] = True
params["hand_detector"] = 2
params["body"] = 0
try:
# Starting OpenPose
opWrapper = op.WrapperPython()
opWrapper.configure(params)
opWrapper.start()
while(True):
start = time.time()
rec, imageToProcess = cam.read()
if not rec :
break
# Read image and face rectangle locations
#imageToProcess = cv2.imread(args[0].image_path)
print(imageToProcess.shape)
height = imageToProcess.shape[0]
width = imageToProcess.shape[1]
handRectangles = [
# Left/Right hands person 0
[
op.Rectangle(0., 0., height, height),
op.Rectangle(width/2., 0., height, height),
],
# Left/Right hands person 1
[
op.Rectangle(0., 0., height, height),
op.Rectangle(width/2, 0., height, height),
]
]
# Create new datum
datum = op.Datum()
datum.cvInputData = imageToProcess
datum.handRectangles = handRectangles
# Process and display image
opWrapper.emplaceAndPop([datum])
end = time.time()
print("run time : " , str(end - start))
cv2.namedWindow("OpenPose 1.4.0 - Tutorial Python API", cv2.WINDOW_NORMAL)
#print("Left hand keypoints: \n" + str(datum.handKeypoints[0]))
#print("Right hand keypoints: \n" + str(datum.handKeypoints[1]))
print("Left hand 0 keypoints: \n" + str(datum.handKeypoints[0][0][0]))
print(datum.handKeypoints[0].shape)
cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData)
realend = time.time()
print("cal and show run time : " , str(realend - start))
#cv2.imshow("hands_video", img)
if(cv2.waitKey(1)&0xff == ord('q')):
break
except Exception as e:
# print(e)
sys.exit(-1)
cv2.waitKey(1)
cam.release()
cv2.destroyAllWindows()