-
Notifications
You must be signed in to change notification settings - Fork 2
/
QTGui.py
358 lines (277 loc) · 13.2 KB
/
QTGui.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import sys
import argparse
from PySide2 import QtCore, QtWidgets, QtGui,QtOpenGL
import pyqtgraph as pg
import pyqtgraph.opengl as pgl
import numpy as np
from lib.worker import Worker
import time
from lib import frameParser, classifier, storage
import serial
from lib.util import *
import joblib
from lib.POI import POITracker, Predictor
from datetime import datetime
import construct
import gtrack.gtrack as gtrack
commandport = "COM5"
dataport = "COM6"
max_targets = 15
classes = ["adult", "bicyclist","child"] # , "random"]
shortcuts = ['a','s','d']
class SerialReceiver(QtCore.QObject):
frameReceived = QtCore.Signal(frameParser.ParsedFrame)
def __init__(self):
QtCore.QObject.__init__(self)
self.serial = serial.Serial(dataport, 921600)
self.buffer = []
def run(self):
while(self.serial.in_waiting > 0):
byte = self.serial.read(self.serial.in_waiting)
bytes_read = len(byte)
# Store raw data
rawfile.write(byte)
# print(byte)
self.buffer += byte
# Check if we received full packet
for i in range(len(self.buffer)):
if frameParser.matchArrays([0x02, 0x01, 0x04, 0x03, 0x06, 0x05, 0x08, 0x07], self.buffer[i:i+8]):
framelen = int.from_bytes(self.buffer[i+12+8:i+15+8], 'little')
if len(self.buffer) < i+framelen+1:
return
#print(f"packet, at i={i} packet len: {framelen} buffer size:{len(self.buffer)}", flush = True)
#print(self.buffer)
#print(self.buffer[i:i+framelen+1])
frame = frameParser.parseFrame(bytes(self.buffer[i:]))
if frame is not None:
#print("frame received")
self.frameReceived.emit(frame)
else:
print("parse failure")
self.buffer = self.buffer[i+framelen:]
break
class Playback(QtCore.QObject):
frameReceived = QtCore.Signal(frameParser.ParsedFrame)
def __init__(self, file, recluster):
QtCore.QObject.__init__(self)
self.file = file
self.recluster = recluster
if recluster:
self.gtrack = gtrack.gtrack()
def reclusterFrame(self, data):
#print(data)
pointCloudLocation = frameParser.getPacket(data, frameParser.Message.POINT_CLOUD)
sideInfo = frameParser.getPacket(data, frameParser.Message.POINT_CLOUD_SIDE_INFO)
pointCloud = [{**pointCloudLocation[i], **sideInfo[i]} for i in range(len(pointCloudLocation))]
npdata = np.array([np.array([x['range'], x['azimuth'], x['doppler'], x['snr']]) for x in pointCloud])
self.gtrack.step(npdata)
#gtrack expects: [range, angle, doppler, SNR]
print(self.gtrack.getData().numtracks)
gtrackData = self.gtrack.getData()
#Create a nice frame object
parsedFrame = frameParser.ParsedFrame(data['header']['frameNumber'])
for t in range(gtrackData.numtracks):
target = self.gtrack.getTrack(t)
#print(target.uid)
tid = target.uid
cluster = parsedFrame.newCluster(tid)
cluster.addInfo({'posx':target.S[0],
'posy':target.S[1],
'sx':target.dim[0],
'sy':target.dim[1],
'tid':target.tid})
for targetIndex in range(len(pointCloud)):
if gtrackData.indices[targetIndex] == tid:
cluster.addPoint(pointCloud[targetIndex])
unique, counts = np.unique([gtrackData.indices[i] for i in range(len(pointCloud))], return_counts=True)
print(dict(zip(unique, counts)))
#Add points without a cluster
for targetIndex in range(len(pointCloud)):
if gtrackData.indices[targetIndex] >= 250:
parsedFrame.addPoint(pointCloud[targetIndex])
return parsedFrame
def getFrame(self):
try:
parsed = construct.RawCopy(frameParser.frameParser).parse_stream(self.file)
print(f"Getframe {parsed['offset2']} {self.file.tell()}",flush=True)
self.file.seek(parsed['offset2'])
#print(parsed)
#print("=================")
if self.recluster:
frame = self.reclusterFrame(parsed['value'])
else:
frame = frameParser.parseFrame(parsed['data'])
self.frameReceived.emit(frame)
except Exception as e:
print(e)
self.file.seek(10,1) #probably a file error, skip a bit
class MyWidget(QtWidgets.QWidget):
def __init__(self, outputfile, playback , inputfile, recluster):
super().__init__()
self.playback = playback
self.threadpool = QtCore.QThreadPool()
self.mouseLocation = None
#Create basic layout
self.button = QtWidgets.QPushButton("Start sensor")
self.text = QtWidgets.QLabel("Hello World")
self.text.setAlignment(QtCore.Qt.AlignCenter)
self.layout = QtWidgets.QVBoxLayout()
self.layout.addWidget(self.text)
self.layout.addWidget(self.button)
self.setLayout(self.layout)
self.button.clicked.connect(self.magicExecutor)
#Set up graphs
self.graphscontainer = QtWidgets.QHBoxLayout()
#POI container
self.POIs = np.random.rand(10, 2) * 30 - 15
self.plotwindow = pg.PlotWidget()
self.plot2 = self.plotwindow.plot(self.POIs, pen=None, symbol='o')
self.plotwindow.setRange(xRange=[-10,10], yRange=[0,25])
self.textitems = [pg.TextItem(text="") for i in range(max_targets)]
for x in self.textitems:
self.plotwindow.addItem(x)
self.plotwindow.scene().sigMouseMoved.connect(self.mouseMoved)
self.graphscontainer.addWidget(self.plotwindow)
self.plotwindow.sizeHint = QtCore.QSize(100,100)
self.glview = pgl.GLViewWidget()
grid = pgl.GLGridItem()
self.pointcloud = pgl.GLScatterPlotItem()
self.POI3D = pgl.GLScatterPlotItem(color=[1.0, 0, 0, 0.2], size=50)
# Draw the area we are viewing.
background = pgl.GLLinePlotItem(pos=np.array([[-5, 0, 0], [5, 0, 0], [10, 25, 0], [-10, 25, 0], [-5, 0, 0]]),
color=(1, 1, 1, 1), width=2, antialias=True, mode='line_strip')
mesh = pgl.MeshData.sphere(3,5)
self.clusterMesh = [pgl.GLMeshItem(meshdata=mesh, edgeColor=[.2,.9,.4,.1] ,drawEdges=True, drawFaces=False) for i in range(20)]
[self.glview.addItem(mesh) for mesh in self.clusterMesh]
self.glview.addItem(grid)
self.glview.addItem(background)
self.glview.addItem(self.pointcloud)
#self.glview.addItem(self.POI3D)
self.graphscontainer.addWidget(self.glview)
self.glview.setSizePolicy(self.plotwindow.sizePolicy())
self.layout.addLayout(self.graphscontainer)
#POI tracker
self.POITracker = POITracker(outputfile)
#neural net runner
#self.predictor = Predictor(20, max_targets)
#self.predictor.newPrediction.connect(self.showPrediction)
#start serial reader
try:
if (not self.playback):
self.serialReceiver = SerialReceiver()
self.serialReceiver.frameReceived.connect(self.POITracker.processFrame)
self.serialReceiver.frameReceived.connect(self.visualizeFrame) #Start timer for serial reading
self.readtimer = QtCore.QTimer(self)
self.readtimer.timeout.connect(self.serialReceiver.run)
self.readtimer.start(5)
else:
self.playback = Playback(inputfile, recluster)
self.playback.frameReceived.connect(self.POITracker.processFrame)
self.playback.frameReceived.connect(self.visualizeFrame)
self.readtimer = QtCore.QTimer(self)
self.readtimer.timeout.connect(self.playback.getFrame)
self.readtimer.start(100)
print(" start playback")
except Exception as msg:
print("serial not started!", msg)
def keyPressEvent(self, event):
#print("test", event.key())
mpoint = np.array([self.mouseLocation.x(), self.mouseLocation.y()])
distances = np.sqrt(np.sum((mpoint - self.POITracker.getLocations()) ** 2, axis=1))
closest = np.argmin(distances)
if(event.text() == " "):
self.POITracker.breakuppoi(self.POITracker.activePOIs[closest])
try:
selected_class = shortcuts.index(event.text())
if (distances[closest] < 1.0):
self.text.setText(f"mouse over point {self.POITracker.activePOIs[closest].uid} = {selected_class}")
self.POITracker.activePOIs[closest].class_id = selected_class
except ValueError as v:
pass
def mouseMoved(self, event):
if self.POIs.shape[0] > 0:
pos = event
localpoint = self.plotwindow.getViewBox().mapSceneToView(pos)
self.mouseLocation = localpoint
def showPrediction(self, results):
ti = self.textitems[results['tid']]
if(results['samples'] > 2):
pred = results['result']
cid = np.argmax(pred)
ti.setText(f"{results['tid']}: {int(pred[cid]*100):3}% {classes[cid]} set to: {self.POITracker.poiFromTid(results['tid']).class_id}")
ti.setPos(results['x'], results['y'])
else:
ti.setText("")
@QtCore.Slot()
def visualizeFrame(self, frame):
colormap = [[1.0,0.0,0.0,0.8],[1.0,1.0,0.0,.8],[0.0,1.0,0.0,.8],[0.0,1.0,1.0,.8],[1.0,0.0,1.0,.8]]
# Targets
# print(frame.clusters)
self.POIs = self.POITracker.getLocations()
pois = self.POITracker.getPOIs()
if args.clustering:
for i in range(20):
if(len(frame.clusters) > i):
cluster = frame.clusters[i]
self.clusterMesh[i].resetTransform()
self.clusterMesh[i].setColor(colormap[cluster.info['tid'] % (len(colormap) - 1)])
self.clusterMesh[i].scale(cluster.info['sx'],cluster.info['sy'],1)
self.clusterMesh[i].translate(cluster.info['posx'],cluster.info['posy'],0) # , color = np.array([colormap[i['tid'] % (len(colormap)-1)] for i in packet['data']])).
else:
self.clusterMesh[i].resetTransform()
self.plot2.setData(self.POIs)
for i in range(len(self.textitems)):
if len(pois) > i:
pos = pois[i].getPos()
self.textitems[i].setPos(pos[0], pos[1])
self.textitems[i].setText(f"{pois[i].class_id}, h={pois[i].getHeight(90):.2f}")
else:
self.textitems[i].setText("")
# Point cloud
points = [np.array([0.0,0.0,0.0])]
colors = [[0.0,0.0,0.0,0.0]]
for cluster in frame.clusters:
for point in cluster.points:
y, x = pol2cart(point['range'], point['azimuth'])
_, vel = pol2cart(point['range'], point['elevation'])
colors.append(colormap[cluster.info['tid'] % (len(colormap) - 1)])
points.append(np.array([x, y, vel],dtype=np.float32))
# unclustered points
#print(f"{len(frame.points)} unclustered points")
for point in frame.points:
y, x = pol2cart(point['range'], point['azimuth'])
_, vel = pol2cart(point['range'], point['elevation'])
colors.append([1.0, 1.0, 1.0, point['snr']/30.0])
points.append(np.array([x, y, vel],dtype=np.float32))
# [print(c.points, flush=True) for c in frame.clusters]
points = np.array(points,dtype=np.float32)
self.pointcloud.setData(pos=points, color=np.array(colors, dtype=np.float32))
def magicExecutor(self):
self.threadpool.start(Worker(lambda: startSensor(commandport)))
def parse_arguments(args):
parser = argparse.ArgumentParser(description='readout radar sensor')
parser.add_argument('--file', help = "filename to store/read")
parser.add_argument('--playback', help = "play back file" )
parser.add_argument('--clustering', help = "enable reclustering of pointclouds", action='store_true')
#parser.add_argument('--no_raw', action='store_', help = "dont store raw file")
return parser.parse_args()
args = None
if __name__ == "__main__":
app = QtWidgets.QApplication([])
args = parse_arguments(sys.argv)
print(args)
outputfile = None
inputfile = None
if(args.file is not None):
outputfile = open(f"{args.file}-{datetime.now().day}.msgpack", "ab")
rawfile = open(f"raw/{datetime.now().strftime('%y-%m-%d_%H%M')}.bin", "ab")
if(args.playback is not None):
inputfile = open(args.playback, 'rb')
inputfile.seek(0,2)
print(inputfile.tell())
inputfile.seek(0,0)
#inputfile.seek(355000,0)
widget = MyWidget(outputfile, args.playback, inputfile, args.clustering)
widget.resize(800, 600)
widget.show()
sys.exit(app.exec_())