-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.py
133 lines (119 loc) · 4.06 KB
/
Utils.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
import subprocess
MIRROR = "https://pypi.tuna.tsinghua.edu.cn/simple"
def drawLandmarks(image, results):
import mediapipe.python.solutions as sol
# Draw face connections
sol.drawing_utils.draw_landmarks(
image,
results.face_landmarks,
sol.holistic.FACEMESH_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=sol.drawing_styles.
get_default_face_mesh_tesselation_style())
sol.drawing_utils.draw_landmarks(
image,
results.face_landmarks,
sol.holistic.FACEMESH_CONTOURS,
landmark_drawing_spec=None,
connection_drawing_spec=sol.drawing_styles.
get_default_face_mesh_contours_style())
# Draw pose connections
sol.drawing_utils.draw_landmarks(
image, results.pose_landmarks, sol.holistic.POSE_CONNECTIONS,
sol.drawing_styles.get_default_pose_landmarks_style())
# Draw left hand connections
sol.drawing_utils.draw_landmarks(
image, results.left_hand_landmarks, sol.holistic.HAND_CONNECTIONS,
sol.drawing_styles.get_default_hand_landmarks_style(),
sol.drawing_styles.get_default_hand_connections_style())
# Draw right hand connections
sol.drawing_utils.draw_landmarks(
image, results.right_hand_landmarks, sol.holistic.HAND_CONNECTIONS,
sol.drawing_styles.get_default_hand_landmarks_style(),
sol.drawing_styles.get_default_hand_connections_style())
def extractLandmarks(x):
res = {}
if not x.pose_landmarks is None:
a = x.pose_landmarks.landmark
b = []
for i in range(len(a)):
b.append([a[i].x, a[i].y, a[i].z])
res['body'] = b
else:
res['body'] = None
if not x.left_hand_landmarks is None:
a = x.left_hand_landmarks.landmark
b = []
for i in range(len(a)):
b.append([a[i].x, a[i].y, a[i].z])
res['rightHand'] = b
else:
res['rightHand'] = None
if not x.right_hand_landmarks is None:
a = x.right_hand_landmarks.landmark
b = []
for i in range(len(a)):
b.append([a[i].x, a[i].y, a[i].z])
res['leftHand'] = b
else:
res['leftHand'] = None
if not x.face_landmarks is None:
a = x.face_landmarks.landmark
b = []
for i in range(len(a)):
b.append([a[i].x, a[i].y, a[i].z])
res['face'] = b
else:
res['face'] = None
return res
def generateNullLandmarks():
res = {}
res['body'] = None
res['leftHand'] = None
res['rightHand'] = None
res['face'] = None
return res
def testPackages(download=True):
flag1 = False
flag2 = False
flag3 = False
flag4 = False
try:
import mediapipe
except ModuleNotFoundError:
print("Missing package mediapipe")
flag1 = True
try:
import cv2
except ModuleNotFoundError:
print("Missing package opencv-python")
flag2 = True
try:
import pyautogui
except ModuleNotFoundError:
print("Missing package PyAutoGUI")
flag3 = True
try:
import numpy
except ModuleNotFoundError:
print("Missing package numpy")
flag4 = True
if download and (flag1 or flag2 or flag3):
try:
print("Downloading" + (" mediapipe==0.10.14" if flag1 else "") +
(" opencv-python==4.10.0.84" if flag2 else "") +
(" PyAutoGUI==0.9.54" if flag3 else "") +
(" numpy==1.26.4" if flag4 else ""))
res = subprocess.run(
['pip', 'install', '-i', MIRROR] +
(['mediapipe==0.10.14'] if flag1 else []) +
(['opencv-python==4.10.0.84'] if flag2 else []) +
(['PyAutoGUI==0.9.54'] if flag3 else []) +
(['numpy==1.26.4'] if flag4 else []))
except OSError as e:
print(f"Download Packages Error: {e}")
if res.returncode != 0:
print(f"Download Packages Error: {res}")
else:
print("Download Complete")
return not (flag1 or flag2 or flag3)