-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvoxels.py
251 lines (186 loc) · 6.34 KB
/
voxels.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""
Extract the VOXEL representation from the point cloud dataset
USAGE: change the parent_dir and extract_path to the correct variables.
- parent_dir is raw Path_to_training_or_test_data.
- extract_path is the Path_to_put_extracted_data samples.
EXAMPLE: SPECIFICATION
parent_dir = '/Users/sandeep/Research/Ti-mmWave/data/Temp_Samples/Train/'
sub_dirs=['boxing','jack','jump','squats','walk']
extract_path = '/Users/sandeep/Research/Ti-mmWave/data/extract/Train_Data_voxels_'
"""
parent_dir = 'Path_to_training_or_test_data'
sub_dirs=['boxing','jack','jump','squats','walk']
extract_path = 'Path_to_put_extracted_data'
import glob
import os
import numpy as np
import csv
import time
import time
def voxalize(x_points, y_points, z_points, x, y, z, velocity):
x_min = np.min(x)
x_max = np.max(x)
y_min = np.min(y)
y_max = np.max(y)
z_max = np.max(z)
z_min = np.min(z)
z_res = (z_max - z_min)/z_points
y_res = (y_max - y_min)/y_points
x_res = (x_max - x_min)/x_points
pixel = np.zeros([x_points,y_points,z_points])
x_current = x_min
y_current = y_min
z_current = z_min
x_prev = x_min
y_prev = y_min
z_prev = z_min
x_count = 0
y_count = 0
z_count = 0
start_time = time.time()
for i in range(y.shape[0]):
x_current = x_min
x_prev = x_min
x_count = 0
done=False
while x_current <= x_max and x_count < x_points and done==False:
y_prev = y_min
y_current = y_min
y_count = 0
while y_current <= y_max and y_count < y_points and done==False:
z_prev = z_min
z_current = z_min
z_count = 0
while z_current <= z_max and z_count < z_points and done==False:
if x[i] < x_current and y[i] < y_current and z[i] < z_current and x[i] >= x_prev and y[i] >= y_prev and z[i] >= z_prev:
pixel[x_count,y_count,z_count] = pixel[x_count,y_count,z_count] + 1
done = True
#velocity_voxel[x_count,y_count,z_count] = velocity_voxel[x_count,y_count,z_count] + velocity[i]
z_prev = z_current
z_current = z_current + z_res
z_count = z_count + 1
y_prev = y_current
y_current = y_current + y_res
y_count = y_count + 1
x_prev = x_current
x_current = x_current + x_res
x_count = x_count + 1
return pixel
def get_data(file_path):
with open(file_path) as f:
lines = f.readlines()
frame_num_count = -1
frame_num = []
x = []
y = []
z = []
velocity = []
intensity = []
wordlist = []
for x1 in lines:
for word in x1.split():
wordlist.append(word)
length1 = len(wordlist)
for i in range(0,length1):
if wordlist[i] == "point_id:" and wordlist[i+1] == "0":
frame_num_count += 1
if wordlist[i] == "point_id:":
frame_num.append(frame_num_count)
if wordlist[i] == "x:":
x.append(wordlist[i+1])
if wordlist[i] == "y:":
y.append(wordlist[i+1])
if wordlist[i] == "z:":
z.append(wordlist[i+1])
if wordlist[i] == "velocity:":
velocity.append(wordlist[i+1])
if wordlist[i] == "intensity:":
intensity.append(wordlist[i+1])
x = np.asarray(x)
y = np.asarray(y)
z = np.asarray(z)
frame_num = np.asarray(frame_num)
velocity = np.asarray(velocity)
intensity = np.asarray(intensity)
x = x.astype(np.float)
y = y.astype(np.float)
z = z.astype(np.float)
velocity = velocity.astype(np.float)
intensity = intensity.astype(np.float)
frame_num = frame_num.astype(np.int)
data = dict()
for i in range(len(frame_num)):
if int(frame_num[i]) in data:
data[frame_num[i]].append([x[i],y[i],z[i],velocity[i],intensity[i]])
else:
data[frame_num[i]]=[]
data[frame_num[i]].append([x[i],y[i],z[i],velocity[i],intensity[i]])
data_pro1 = dict()
# Merging of frames together with sliding of frames
together_frames = 1
sliding_frames = 1
#we have frames in data
frames_number = []
for i in data:
frames_number.append(i)
frames_number=np.array(frames_number)
total_frames = frames_number.max()
i = 0
j = 0
while together_frames+i < total_frames:
curr_j_data =[]
for k in range(together_frames):
curr_j_data = curr_j_data + data[i+k]
#print(len(curr_j_data))
data_pro1[j] = curr_j_data
j = j+1
i = i+sliding_frames
pixels = []
# Now for 2 second windows, we need to club together the frames and we will have some sliding windows
for i in data_pro1:
f = data_pro1[i]
f = np.array(f)
#y and z points in this cluster of frames
x_c = f[:,0]
y_c = f[:,1]
z_c = f[:,2]
vel_c=f[:,3]
pix = voxalize(10, 32, 32, x_c, y_c, z_c, vel_c)
#print(i, f.shape,pix.shape)
pixels.append(pix)
pixels = np.array(pixels)
frames_together = 60
sliding = 10
train_data=[]
i = 0
while i+frames_together<=pixels.shape[0]:
local_data=[]
for j in range(frames_together):
local_data.append(pixels[i+j])
train_data.append(local_data)
i = i + sliding
train_data = np.array(train_data)
del x,y,z, velocity, data, data_pro1, pixels
return train_data
# parse the data file
def parse_RF_files(parent_dir, sub_dirs, file_ext='*.txt'):
print(sub_dirs)
features =np.empty((0, 60, 10, 32, 32) )
labels = []
for sub_dir in sub_dirs:
files=sorted(glob.glob(os.path.join(parent_dir,sub_dir, file_ext)))
for fn in files:
print(fn)
print(sub_dir)
train_data = get_data(fn)
features=np.vstack([features,train_data])
for i in range(train_data.shape[0]):
labels.append(sub_dir)
print(features.shape,len(labels))
del train_data
return features, labels
for sub_dir in sub_dirs:
features, labels = parse_RF_files(parent_dir,[sub_dir])
Data_path = extract_path + sub_dir
np.savez(Data_path, features,labels)
del features,labels