-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data.py
104 lines (71 loc) · 2.46 KB
/
get_data.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
import cv2
import mediapipe as mp
import pandas as pd
import os
import numpy as np
def image_processed(file_path):
# reading the static image
hand_img = cv2.imread(file_path)
# Image processing
# 1. Convert BGR to RGB
img_rgb = cv2.cvtColor(hand_img, cv2.COLOR_BGR2RGB)
# 2. Flip the img in Y-axis
img_flip = cv2.flip(img_rgb, 1)
# accessing MediaPipe solutions
mp_hands = mp.solutions.hands
# Initialize Hands
hands = mp_hands.Hands(static_image_mode=True,
max_num_hands=1, min_detection_confidence=0.7)
# Results
output = hands.process(img_flip)
hands.close()
try:
data = output.multi_hand_landmarks[0]
print(data)
data = str(data)
data = data.strip().split('\n')
garbage = ['landmark {', ' visibility: 0.0', ' presence: 0.0', '}']
without_garbage = []
for i in data:
if i not in garbage:
without_garbage.append(i)
clean = []
for i in without_garbage:
i = i.strip()
clean.append(i[2:])
for i in range(0, len(clean)):
clean[i] = float(clean[i])
return(clean)
except:
return(np.zeros([1,63], dtype=int)[0])
def make_csv():
mypath = 'DATASET'
file_name = open('dataset.csv', 'a')
for each_folder in os.listdir(mypath):
if '_' in each_folder:
pass
else:
for each_number in os.listdir(mypath + '/' + each_folder):
if '_' in each_number:
pass
else:
label = each_folder
file_loc = mypath + '/' + each_folder + '/' + each_number
data = image_processed(file_loc)
try:
for id,i in enumerate(data):
if id == 0:
print(i)
file_name.write(str(i))
file_name.write(',')
file_name.write(label)
file_name.write('\n')
except:
file_name.write('0')
file_name.write(',')
file_name.write('None')
file_name.write('\n')
file_name.close()
print('Data Created !!!')
if __name__ == "__main__":
make_csv()