-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateData.py
66 lines (48 loc) · 1.98 KB
/
createData.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
# Create Data from images that collect by 'collectImages.py'
import os
import pickle
import mediapipe as mp
import cv2
# import matplotlib.pyplot as plt
mp_drawing = mp.solutions.drawing_utils
mp_drawing_styles = mp.solutions.drawing_styles
mp_hands = mp.solutions.hands
hands =mp_hands.Hands(
static_image_mode=True,
max_num_hands=2,
min_detection_confidence=0.5)
DATA_DIR = './images'
# data & labels will be stored to pickle file
data = []
labels = []
for dir_ in os.listdir(DATA_DIR):
# Extract only directories under DATA_DIR
directories = [directory for directory in dir_ if os.path.isdir(os.path.join(DATA_DIR, dir_))]
# Print the list of directories
for directory in directories:
# print('directory',directory)
for imgPath in os.listdir(os.path.join(DATA_DIR, directory)):
print('imgPath',imgPath)
# temporary list used to store the normalized coordinates of landmarks
dataAux = []
x_ = []
y_ = []
img = cv2.imread(os.path.join(DATA_DIR, dir_, imgPath))
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = hands.process(imgRGB)
if results.multi_hand_landmarks:
for hand_landmarks in results.multi_hand_landmarks:
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
x_.append(x)
y_.append(y)
for i in range(len(hand_landmarks.landmark)):
x = hand_landmarks.landmark[i].x
y = hand_landmarks.landmark[i].y
dataAux.append(x - min(x_))
dataAux.append(y - min(y_))
data.append(dataAux)
labels.append(dir_)
with open('images.pickle', 'wb') as f:
pickle.dump({'data': data, 'labels': labels}, f)