-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataProcessing.py
126 lines (104 loc) · 4.43 KB
/
dataProcessing.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
import math
import json
import getPositions
import numpy as np
def calculateDistances(rightPalmPosition, rightFingerTipPositions, leftPalmPosition, leftFingerTipPositions):
featureVector = []
if rightPalmPosition != [] and rightFingerTipPositions != []:
for n in range(0, len(rightFingerTipPositions)):
# Extract the distance in each direction
x = abs(rightFingerTipPositions[n][0] - rightPalmPosition[0])
y = abs(rightFingerTipPositions[n][2] - rightPalmPosition[2])
# Add the euclidian distance to the feature vector
fingerDistance = euclidianDistance(x, y)
featureVector.append(euclidianDistance(x,y))
if (leftPalmPosition != [] and leftFingerTipPositions != []):
for n in range(0, len(leftFingerTipPositions)):
# Extract the distance in each direction
x = abs(leftFingerTipPositions[n][0] - leftPalmPosition[0])
y = abs(leftFingerTipPositions[n][2] - leftPalmPosition[2])
# Add the euclidian distance to the feature vector
featureVector.append(euclidianDistance(x,y))
return featureVector
def euclidian3DDistance(x,y,z):
return math.sqrt(math.pow(x,2) + math.pow(y,2) + math.pow(z,2))
def euclidianDistance(x,y):
return math.sqrt(math.pow(x,2) + math.pow(y,2))
'''
Normalizes and rounds to five decimals
'''
def normalize(array):
normalizedData = []
for n in range(0,2):
minval = min(array[n*5:(n+1)*5])
maxval= max(array[n*5:(n+1)*5])
for a in range(n * 5, (n+1) * 5):
tmp = array[a]
#rounding to 5 decimals
normalizedData.append(round((tmp - minval) / (maxval - minval),5))
return normalizedData
'''
Parsing the txt files to extract the data.
'''
def readRawData(file):
labelData = np.loadtxt(file, delimiter = '\t', usecols = (0,), dtype = str, unpack = True)
rawLeftPalmData = np.loadtxt(file, delimiter = '\t', usecols = (1,), dtype = str)
rawRightPalmData = np.loadtxt(file, delimiter = '\t', usecols = (2,), dtype = str)
rawFingerData = np.loadtxt(file, delimiter = '\t', usecols = (3,), dtype = str)
leftPalmData = []
for item in rawLeftPalmData:
item = item.translate(None, '[],')
floats = [float(s) for s in item.split()]
leftPalmData.append(floats)
rightPalmData = []
for item in rawRightPalmData:
item = item.translate(None, '[],')
floats = [float(s) for s in item.split()]
rightPalmData.append(floats)
fingerData = []
for item in rawFingerData:
item = item.translate(None, '[],')
itemArray = [float(s) for s in item.split(" ")]
i = 1
temp3 = []
temp30 = []
for element in itemArray:
temp3.append(element)
if i % 3 == 0:
temp30.append(temp3)
temp3 = []
if i % 30 == 0:
fingerData.append(temp30)
break
i += 1
return (leftPalmData, rightPalmData, fingerData, labelData)
'''
Orders data to featureVector format: right palm, right fingers, left palm, left fingers
Normalizing
'''
def convertToFeatureVectors(leftPalmData, rightPalmData, fingerData):
featureVectorList = []
for i in range(0, len(leftPalmData)):
leftPalm = leftPalmData[i]
rightPalm = rightPalmData[i]
fingers = fingerData[i]
half = len(fingers) / 2
featureVector = calculateDistances(rightPalm, fingers[half:], leftPalm, fingers[:half])
featureVectorList.append(normalize(featureVector))
return(featureVectorList)
'''
Processes the raw data into feature files, which can later be used to train the models.
It expects featureFileName to be the path to a file where to save processed features,
based on the file dataFileName.
'''
def saveFeatureData(featureFileName, dataFileName):
(leftPalmData, rightPalmData, fingerData, labelData) = readRawData(dataFileName)
featureVectorList = convertToFeatureVectors(leftPalmData, rightPalmData, fingerData)
featureFile = open('training/' + featureFileName, 'a')
i = 0
for vector in featureVectorList:
featureFile.write("{}".format(labelData[i]))
for feature in vector:
featureFile.write(", {}".format(feature))
featureFile.write("\n")
i += 1