-
Notifications
You must be signed in to change notification settings - Fork 1
/
skeleton.py
140 lines (115 loc) · 4.47 KB
/
skeleton.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import sys
import cv2
import math
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.append(dir_path + '/python/openpose/Release');
os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/x64/Release;' + dir_path + '/bin;'
import pyopenpose as op
class hand:
def __init__(self):
params = dict()
params["model_folder"] = "./models/"
#params["face"] = True
params["hand"] = True
params['render_pose']=1
#params['model_pose']='COCO'
#self.fourcc = cv2.VideoWriter_fourcc(*'XVID')
#self.out = cv2.VideoWriter('output.avi',self.fourcc, 20.0, (640,360))
self.leftX = []
self.leftY = []
self.left_centerX = 0
self.left_centerY = 0
self.right_centerX = 0
self.right_centerY = 0
self.distanceX = 0
self.distanceY = 0
self.opWrapper = op.WrapperPython()
self.opWrapper.configure(params)
self.opWrapper.start()
def detection(self, img):
img = cv2.resize(img, (640, 360), fx=1.0, fy=1.0, interpolation=cv2.INTER_CUBIC)
datum = op.Datum()
datum.cvInputData = img
self.opWrapper.emplaceAndPop([datum])
#body = datum.poseKeypoints
#face = datum.faceKeypoints
left = datum.handKeypoints[0]
right = datum.handKeypoints[1]
if left.shape == () or right.shape == () or sum(left[0])[0] == 0. or sum(right[0])[0] == 0.:
img = cv2.flip(img, 1)
cv2.imshow("Pose and Hand", img)
cv2.waitKey(1)
return False
self.leftX = []
self.leftY = []
for l in left[0]:
self.leftX.append(l[0])
self.leftY.append(l[1])
self.points=[]
self.rightX = []
self.rightY = []
for r in right[0]:
self.rightX.append(r[0])
self.rightY.append(r[1])
self.points.append(r[0])
self.points.append(r[1])
self.left_centerX, self.left_centerY = self.get_center(self.leftX, self.leftY)
self.right_centerX, self.right_centerY = self.get_center(self.rightX, self.rightY)
self.distanceX = abs(self.left_centerX - self.right_centerX)
self.distanceY = abs(self.left_centerY - self.right_centerY)
img = datum.cvOutputData
self.draw_area(img)
# write the flipped frame
#self.out.write(img)
img = cv2.flip(img, 1)
cv2.imshow("Pose and Hand", img)
cv2.waitKey(1)
return True
def draw_area(self, image):
# sound area
# -------------------------------
# midi : 100 < distanceX < 300
# this : area_sizeX = 200 + distance(200)
# -------------------------------
# midi : -150 < distanceY < 150
# this : area_sizeY = 300
# -------------------------------
distance = 200
area_sizeX = 200
area_sizeY = 300
rightX = self.right_centerX
rightY = self.right_centerY
area_startX = int(rightX - (area_sizeX / 2)) + distance
area_startY = int(rightY - (area_sizeY / 2))
area_endX = int(rightX + (area_sizeX / 2)) + distance
area_endY = int(rightY + (area_sizeY / 2))
y = int((area_endY - area_startY) / 7)
for row in range(1, 7):
addY = y * row
cv2.line(image, (area_startX, area_startY + addY), (area_endX, area_startY + addY), (0, 0, 255), 2)
x = int((area_endX - area_startX) / 3)
for col in range(1, 3):
addX = x * col
cv2.line(image, (area_startX + addX, area_startY), (area_startX + addX, area_endY), (0, 0, 255), 2)
cv2.rectangle(image, (area_startX, area_startY), (area_endX, area_endY), (0, 0, 255), 2)
def get_point(self):
return self.leftX, self.leftY, self.rightX, self.rightY
def get_center(self, X, Y):
x = 0
y = 0
cnt = 0
for i in range(len(X)):
if X[i] == 0.: continue
cnt += 1
x += X[i]
y += Y[i]
x /= cnt
y /= cnt
return x, y
def get_left_center(self):
return self.left_centerX, self.left_centerY
def get_right_center(self):
return self.right_centerX, self.right_centerY
def get_distance(self):
return self.distanceX, self.distanceY