-
Notifications
You must be signed in to change notification settings - Fork 2
/
predict.py
321 lines (264 loc) · 11.4 KB
/
predict.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import sys
import io
from construct import *
import pyqtgraph as pg
import numpy as np
import serial
import h5py
from datetime import datetime
import time
import joblib
import sklearn
import classifier
# commandport = "/dev/ttyACM0"
# dataport = "/dev/ttyACM1"
commandport = "COM10"
dataport = "COM11"
datasetName = "pointclouds"
#Label for new samples
label = sys.argv[2]
#Packet definition
rangeFFTSize = 256
dopplerFFTSize = 256
antennas = 8
colormap = [[1.0,0.0,0.0,0.5],[1.0,1.0,0.0,.5],[0.0,1.0,0.0,.5],[0.0,1.0,1.0,.5],[1.0,0.0,1.0,.5]]
labels = []
datasamples = []
currentsample = []
packetsinsample = 0
model = joblib.load('model.joblib')
from enum import Enum
class Message(Enum):
MMWDEMO_OUTPUT_MSG_DETECTED_POINTS = 1
MMWDEMO_OUTPUT_MSG_RANGE_PROFILE = 2
MMWDEMO_OUTPUT_MSG_NOISE_PROFILE = 3
MMWDEMO_OUTPUT_MSG_AZIMUT_STATIC_HEAT_MAP = 4
MMWDEMO_OUTPUT_MSG_RANGE_DOPPLER_HEAT_MAP = 5
MMWDEMO_OUTPUT_MSG_POINT_CLOUD = 6
MMWDEMO_OUTPUT_MSG_TARGET_LIST = 7
MMWDEMO_OUTPUT_MSG_TARGET_INDEX = 8
MMWDEMO_OUTPUT_MSG_STATS = 9
MMWDEMO_OUTPUT_MSG_HEATMAP = 10
MMWDEMO_OUTPUT_MSG_MAX = 11
frame = Aligned(4,
Struct(
#Find sync bytes by looping over untill we find the magic word
"sync" / RepeatUntil(lambda x, lst, ctx : lst[-8:] == [0x02, 0x01, 0x04, 0x03, 0x06, 0x05, 0x08, 0x07], Byte),
"header" / Struct(
"version" / Int32ul,
'platform' / Int32ul,
'timestamp' / Int32ul,
'totalPacketLen' / Int32ul,
'frameNumber' / Int32ul,
'subframeNumber' / Int32ul,
'chirpProcessingMargin' / Int32ul,
'frameProcessingMargin' / Int32ul,
'trackingProcessingTime' / Int32ul,
'uartSendingTime' / Int32ul,
'numTLVs' / Int16ul,
'checksum' / Int16ul,
),
"packets" / Struct(
"type" / Int32ul,
"len" / Int32ul,
"data" / Switch(this.type,
{
Message.MMWDEMO_OUTPUT_MSG_POINT_CLOUD.value:
"objects" / Struct(
"range" / Float32l,
"angle" / Float32l,
"doppler" / Float32l,
"snr" / Float32l,
)[lambda ctx: int((ctx.len - 8) / 16)],
Message.MMWDEMO_OUTPUT_MSG_TARGET_LIST.value:
"targets" / Struct(
"tid" / Int32ul,
"posx" / Float32l,
"posy" / Float32l,
"velX" / Float32l,
"velY" / Float32l,
"accX" / Float32l,
"accY"/ Float32l,
"ec" / Float32l[9],
"g" / Float32l
)[lambda ctx: int((ctx.len-8) / (17*4))],
Message.MMWDEMO_OUTPUT_MSG_TARGET_INDEX.value:
"indices" / Int8ul[this.len - 8],
Message.MMWDEMO_OUTPUT_MSG_NOISE_PROFILE.value:
Array(rangeFFTSize, Int16ul),
Message.MMWDEMO_OUTPUT_MSG_AZIMUT_STATIC_HEAT_MAP.value:
Array(rangeFFTSize * antennas, Struct("Img" / Int16sl, "Re" / Int16sl)),
Message.MMWDEMO_OUTPUT_MSG_RANGE_DOPPLER_HEAT_MAP.value:
Array(rangeFFTSize * dopplerFFTSize, Int16ul),
Message.MMWDEMO_OUTPUT_MSG_STATS.value:
Struct(
"interFrameProcessingTime" / Int32ul,
"transmitOutputTime" / Int32ul,
"interFrameProcessingMargin" / Int32ul,
"interChirpProcessingMargin" / Int32ul,
"activeFrameCPULoad" / Int32ul,
"interFrameCPULoad" / Int32ul
),
Message.MMWDEMO_OUTPUT_MSG_HEATMAP.value:
Float32l[lambda ctx: int(ctx.len / 4)],
}
, default=Array(this.len, Byte))
)[this.header.numTLVs]
)
)
def cart2pol(x, y):
rho = np.sqrt(x**2 + y**2)
phi = np.arctan2(y, x)
return(rho, phi)
def pol2cart(rho, phi):
x = rho * np.cos(phi)
y = rho * np.sin(phi)
return(x, y)
def parseData(stream, stopEvent):
dataSerial = serial.Serial(stream, 921600)
while True:
buffer += dataSerial.read(1)
outputfile.write(bytes(buffer[-1:]))
if matchArrays([0x02, 0x01, 0x04, 0x03, 0x06, 0x05, 0x08, 0x07], buffer[-8:]):
try:
data = frame.parse(bytes(buffer))
handleData(data)
except StreamError:
print("streamerror, streamsize: {}".format(buffer))
buffer = buffer[-8:]
def startSensor():
with serial.Serial(commandport,115200, parity=serial.PARITY_NONE) as controlSerial:
with open("customchirp.cfg", 'r') as configfile:
for line in configfile:
print(">> " + line, flush = True)
controlSerial.write(line.encode('ascii'))
print("<< " + controlSerial.readline().decode('ascii'), flush = True) #echo
print("<< " + controlSerial.readline().decode('ascii'), flush = True) #"done"
controlSerial.read(11) #prompt
time.sleep(0.01)
print("sensor started")
def addSample(data):
"""
Add a sample of shape:
4xn: (range, azimuth, doppler, snr) * numpoints
"""
out = np.zeros([1,50, 4])
for i, d in enumerate(data):
if(i < 50):
out[0,i] = [d['range'], d['angle'], d['doppler'], d['snr']]
writeDataset(samples, out)
#writeElement(labels, label)
writeElement(timestamps, datetime.timestamp(datetime.now()))
def matchArrays(a, b):
if len(a) != len(b):
return False
for i in range(0, len(a)):
if a[i] != b[i]:
return False
return True
def captureThreadMain(port):
print("Start listening on COM11",flush = True)
with serial.Serial(port, 921600) as dataSerial:
buffer = []
while(True):
byte = dataSerial.read(1)
#print(byte)
buffer += byte
#Check if we received full packet
if matchArrays([0x02, 0x01, 0x04, 0x03, 0x06, 0x05, 0x08, 0x07], buffer[-8:]):
#print(f"packet, size:{len(buffer)}", flush = True)
parseThreadMain(bytes(buffer))
buffer = buffer[-8:]
previous = {"header":{"frameNumber": -1}}
def predict_targets(parsed):
global previous
#tracking lags one frame behind the pointcloud
print(previous['header']['frameNumber'], parsed['header']['frameNumber'], flush=True)
if previous['header']['frameNumber'] == parsed['header']['frameNumber'] - 1:
if Message.MMWDEMO_OUTPUT_MSG_TARGET_LIST.value in parsed and Message.MMWDEMO_OUTPUT_MSG_POINT_CLOUD.value in previous:
for target in parsed[Message.MMWDEMO_OUTPUT_MSG_TARGET_LIST.value]:
tid = target['tid']
#print(tid, parsed[Message.MMWDEMO_OUTPUT_MSG_POINT_CLOUD.value])
points = [[d['range'], d['angle'], d['doppler'], d['snr']] for i, d in enumerate(previous[Message.MMWDEMO_OUTPUT_MSG_POINT_CLOUD.value]) if parsed[Message.MMWDEMO_OUTPUT_MSG_TARGET_INDEX.value][i] == tid]
if(len(points) > 2):
points = np.array([points])
features = classifier.get_featurevector(points)
pred = model.predict_proba(features)
predid = np.argmax(pred)
print(tid,predid, pred[0,predid], points.shape[1])
previous = parsed
def parseThreadMain(rawData):
if(rawData == None):
return
try:
data = frame.parse(rawData)
parsed = {"header": data['header']}
for packet in data['packets']:
parsed[packet['type']] = packet['data']
if packet['type'] == Message.MMWDEMO_OUTPUT_MSG_TARGET_LIST.value :
POIs = np.array([[x['posy'],x['posx'] * -1] for x in packet['data']])
scatter2.setData(pos=POIs, color = np.array([colormap[i['tid'] % (len(colormap)-1)] for i in packet['data']]))
# if packet['type'] == Message.MMWDEMO_OUTPUT_MSG_HEATMAP.value:
# print("heatmap, len = {}".format(packet['len']/4), flush=True)
# heatmap.setImage(np.flip(np.reshape(packet['data'], (64,128)), 1))
if packet['type'] == Message.MMWDEMO_OUTPUT_MSG_POINT_CLOUD.value:
#print(f"pointcloud, points: {(packet['len'] - 8) / 16}", flush="True")
x, y = pol2cart([x["range"] for x in packet['data']], [x['angle'] for x in packet['data']])
vel = [x["doppler"] for x in packet['data']]
colors = [(x['snr']/10,x['snr']/10,x['snr']/10) for x in packet['data']] #Brightness of the point is SNR
#pointcloud.setData([x["range"] for x in packet['data']], [x['angle'] for x in packet['data']])
scatterplot.setData(pos=np.column_stack((x,y * -1,vel)),color=np.array(colors))
addSample(packet['data'])
# global currentsample, packetsinsample, datasamples
# currentsample += packet['data']
# packetsinsample += 1
# if packetsinsample == 10 and len(currentsample) > 30:
# addSample(currentsample)
# currentsample = []
# packetsinsample = 0
# print('new sample')
# elif packetsinsample > 10:
# #Too few points, probably nothing interesting to see
# currentsample = []
# packetsinsample = 0
predict_targets(parsed)
except StreamError:
print("bad packet")
def writeDataset(dataset, data):
dataset.resize(dataset.shape[0] + data.shape[0], axis=0)
dataset[-data.shape[0]:] = data
def writeElement(dataset, data):
dataset.resize(dataset.shape[0]+1,axis=0)
dataset[-1] = data
import pyqtgraph.multiprocess as mp
proc = mp.QtProcess(processRequests=False)
rpg = proc._import('pyqtgraph')
gl = proc._import('pyqtgraph.opengl')
view = gl.GLViewWidget()
view.show()
grid = gl.GLGridItem()
scatterplot = gl.GLScatterPlotItem()
scatter2 = gl.GLScatterPlotItem(color = [1.0,0,0,0.2] , size = 50)
#Draw the area we are viewing.
background = gl.GLLinePlotItem(pos=np.array([[0,-10,0],[0,10,0], [25,10,0], [25,-10,0],[0,-10,0]]),color=(1,1,1,1), width=2, antialias=True, mode='line_strip')
view.addItem(grid)
view.addItem(background)
view.addItem(scatterplot)
view.addItem(scatter2)
#Open a file to store data
f = h5py.File(sys.argv[1]+datetime.now().strftime("%Y%m%d%H%M%S") + ".hdf5", 'w')
try:
samples = f['/'+datasetName+'/samples']
labels = f['/'+datasetName+'/labels']
timestamps = f['/'+datasetName+'/timestamps']
except KeyError as e:
samples = f.create_dataset('/'+datasetName+'/samples',(0, 50, 4), maxshape = (None, 50, 4))
labels = f.create_dataset('/'+datasetName+'/labels',(0,), maxshape = (None,),chunks=True)
timestamps = f.create_dataset('/'+datasetName+'/timestamps',(0,), maxshape = (None,),chunks=True)
#Send the startup commands to the sensor
startSensor()
#Start processing threads
#captureThread.start()
#writeThread.start()
#parseThread.start()
captureThreadMain(dataport)