-
Notifications
You must be signed in to change notification settings - Fork 2
/
extract_landMark.py
99 lines (65 loc) · 2.59 KB
/
extract_landMark.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
#!/usr/bin/env python
# coding: utf-8
# In[3]:
import numpy as np
import mediapipe as mp
import cv2 as cv
import time
import os
import sys
# In[4]:
def usage():
print("""This script is to extract landmark from video and save it as numpy array for traning step
all you need is to collect al data and then call this script.
you also need to create folder npy_data
exp:
$python extract_landMark.py
""")
# In[5]:
def extract_keypoints(results):
pose = np.array([[res.x, res.y, res.z, res.visibility] for res in results.pose_landmarks.landmark]).flatten() if results.pose_landmarks else np.zeros(33*4)
lh = np.array([[res.x, res.y, res.z] for res in results.left_hand_landmarks.landmark]).flatten() if results.left_hand_landmarks else np.zeros(21*3)
rh = np.array([[res.x, res.y, res.z] for res in results.right_hand_landmarks.landmark]).flatten() if results.right_hand_landmarks else np.zeros(21*3)
return np.concatenate([pose,lh, rh])
def extract_nmpy_array(action_name):
try:
folders=os.path.join(DATA_PATH,'data',action_name)
except FileNotFoundError:
usage()
print("You need to collect data first.")
sys.exit()
try:
os.mkdir(os.path.join(DATA_PATH,'npy_data',action_name))
except FileNotFoundError:
print('No such file or directory: npy_data')
usage()
sys.exit()
for folder in os.listdir(folders):
try:
os.mkdir(os.path.join(DATA_PATH,'npy_data',action_name,folder))
except:
pass
folder_path=os.path.join(DATA_PATH,'data',action_name,folder)
for imag in os.listdir(folder_path):
image=os.path.join(DATA_PATH,'data',action_name,folder,imag)
image=cv.imread(image)
RGB=cv.cvtColor(image,cv.COLOR_BGR2RGB)
results=holistic.process(RGB)
keyPoint=extract_keypoints(results)
npy_path=os.path.join(DATA_PATH,'npy_data',action_name,folder,imag[:-3]+'npy')
np.save(npy_path,keyPoint)
# In[6]:
mp_holistic = mp.solutions.holistic # Holistic model
mp_drawing = mp.solutions.drawing_utils # Drawing utilities
holistic=mp.solutions.holistic.Holistic(min_detection_confidence=0.5, min_tracking_confidence=0.5)
# In[7]:
DATA_PATH=os.getcwd()
if __name__=="__main__":
if not len(sys.argv[1:]):
for i in os.listdir(os.path.join(DATA_PATH,'data')):
print("extract data from folder: {}...".format(i))
extract_nmpy_array(i)
else:
usage()
# In[5]:
# In[ ]: