-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathholistic.py
157 lines (133 loc) · 6.06 KB
/
holistic.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# Created by Alex Perira
# Import Libraries
import cv2 as cv
import numpy as np
import mediapipe as mp
class HolisticDetector:
def __init__(self, capture) -> None:
"""
Constructor for the HolisticDetector class.
@param
"""
# Localizes the capture
self.cap = capture
# Initializes the MediaPipe Holistic Solution
self.mp_holistic = mp.solutions.holistic
self.holistic = self.mp_holistic.Holistic(static_image_mode = False,
model_complexity = 1,
smooth_landmarks = True,
enable_segmentation = False,
smooth_segmentation = True,
refine_face_landmarks = False,
min_detection_confidence = 0.5,
min_tracking_confidence = 0.5)
# Initializes the MediaPipe Drawing Solutions
self.mp_drawing_styles = mp.solutions.drawing_styles
self.mp_drawing = mp.solutions.drawing_utils
def readCapture(self):
"""
Reads the VideoCapture capture.
@return videoStream
"""
# Reads the capture
success, stream = self.cap.read()
return stream
def showFrame(self, stream):
"""
@param
"""
# Shows the frame
cv.imshow("MediaPipe Holistic", stream)
def mpDetection(self, stream):
"""
Runs
@param
"""
#
stream = cv.cvtColor(stream, cv.COLOR_BGR2RGB)
stream.flags.writeable = False
#
self.results = self.holistic.process(stream)
stream.flags.writeable = True
#
stream = cv.cvtColor(stream, cv.COLOR_RGB2BGR)
return stream
def drawLandmarks(self, stream):
"""
@param
"""
# Draws the landmarks onto stream
self.mp_drawing.draw_landmarks(stream,
self.results.face_landmarks, self.mp_holistic.FACEMESH_TESSELATION,
self.mp_drawing.DrawingSpec(color = (80, 110, 10) , thickness = 1, circle_radius = 1),
self.mp_drawing_styles.get_default_face_mesh_tesselation_style()
)
# Draw pose connections
self.mp_drawing.draw_landmarks(stream,
self.results.pose_landmarks, self.mp_holistic.POSE_CONNECTIONS,
self.mp_drawing.DrawingSpec(color =(80, 22, 10) , thickness = 2, circle_radius = 4),
self.mp_drawing.DrawingSpec(color =(80, 44, 121), thickness = 2, circle_radius = 2)
)
# Draw left hand connections
self.mp_drawing.draw_landmarks(stream,
self.results.left_hand_landmarks, self.mp_holistic.HAND_CONNECTIONS,
self.mp_drawing_styles.get_default_hand_landmarks_style(),
self.mp_drawing_styles.get_default_hand_connections_style()
)
# Draw right hand connections
self.mp_drawing.draw_landmarks(stream,
self.results.right_hand_landmarks, self.mp_holistic.HAND_CONNECTIONS,
self.mp_drawing_styles.get_default_hand_landmarks_style(),
self.mp_drawing_styles.get_default_hand_connections_style()
)
def extractKeypoints(self):
"""
@param
"""
# Creates blank arrays
lh = []
rh = []
pose = []
face = []
# Checks is the left_hand_landmarks array is
if (self.results.left_hand_landmarks is not None):
lh = np.array([[result.x, result.y, result.z] for result in self.results.left_hand_landmarks.landmark]).flatten()
else:
lh = np.zeros(21 * 3)
# Checks is the right_hand_landmarks array is blank
if (self.results.right_hand_landmarks is not None):
rh = np.array([[result.x, result.y, result.z] for result in self.results.right_hand_landmarks.landmark]).flatten()
else:
rh = np.zeros(21 * 3)
# Checks is the pose_landmarks array is blank
if (self.results.pose_landmarks is not None):
pose = np.array([[result.x, result.y, result.z, result.visibility] for result in self.results.pose_landmarks.landmark]).flatten()
else:
pose = np.zeros(33 * 4)
# Checks is the pose_landmarks array is blank
if (self.results.face_landmarks is not None):
face = np.array([[result.x, result.y, result.z] for result in self.results.face_landmarks.landmark]).flatten()
else:
face = np.zeros(486 * 3)
return np.concatenate([pose, face, lh, rh])
def collectFrames(self, stream, action, video, frame):
if (frame == 0):
cv.putText(stream, 'Beginning Collection', (120,200),
cv.FONT_HERSHEY_SIMPLEX, 1, (0,255, 0), 4, cv.LINE_AA)
cv.putText(stream, 'Collecting frames for {} Video Number {}'.format(action, video), (15, 12),
cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv.LINE_AA)
# Displays stream
self.showFrame(stream)
# Waits
cv.waitKey(int(2.5 * 1000))
else:
cv.putText(stream, 'Collecting frames for {} Video Number {}'.format(action, video), (15, 12),
cv.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1, cv.LINE_AA)
# Displays stream
self.showFrame(stream)
def exportData(self, path):
"""
@param
"""
keypoints = self.extractKeypoints()
np.save(path, keypoints)