examples/a121/algo/parking/parking.py#
1# Copyright (c) Acconeer AB, 2023-2026
2# All rights reserved
3
4from __future__ import annotations
5
6import numpy as np
7
8# Added here to force pyqtgraph to choose PySide
9import PySide6 # noqa: F401
10from PySide6 import QtCore
11
12import pyqtgraph as pg
13
14import acconeer.exptool as et
15from acconeer.exptool import a121
16from acconeer.exptool.a121.algo._utils import get_distances_m
17from acconeer.exptool.a121.algo.parking import (
18 ObstructionProcessor,
19 RefApp,
20 RefAppConfig,
21 RefAppResult,
22)
23from acconeer.exptool.a121.algo.parking._processors import MAX_AMPLITUDE
24from acconeer.exptool.a121.algo.parking._ref_app import get_sensor_configs
25
26
27SENSOR_ID = 1
28
29
30def main():
31 args = a121.ExampleArgumentParser().parse_args()
32 et.utils.config_logging(args)
33
34 client = a121.Client.open(**a121.get_client_args(args))
35 ref_app_config = RefAppConfig(
36 range_start_m=0.1,
37 range_end_m=0.4,
38 hwaas=24,
39 profile=a121.Profile.PROFILE_1,
40 update_rate=5,
41 queue_length_n=3,
42 amplitude_threshold=8.0,
43 weighted_distance_threshold_m=0.1,
44 obstruction_detection=True,
45 obstruction_start_m=0.03,
46 obstruction_end_m=0.05,
47 obstruction_distance_threshold=0.06,
48 )
49
50 ref_app = RefApp(client=client, sensor_id=SENSOR_ID, ref_app_config=ref_app_config)
51 ref_app.calibrate_ref_app()
52 ref_app.start()
53
54 pg_updater = PGUpdater(
55 ref_app_config,
56 ref_app.metadata,
57 SENSOR_ID,
58 ref_app.session_config,
59 )
60
61 pg_process = et.PGProcess(pg_updater)
62 pg_process.start()
63
64 interrupt_handler = et.utils.ExampleInterruptHandler()
65
66 while not interrupt_handler.got_signal:
67 ref_app_result = ref_app.get_next()
68 if ref_app_result.car_detected:
69 print("Car detected")
70 else:
71 print("No car detected")
72 if ref_app_config.obstruction_detection:
73 if ref_app_result.obstruction_detected:
74 print("Obstruction detected")
75 else:
76 print("No obstruction detected")
77 try:
78 pg_process.put_data(ref_app_result)
79 except et.PGProccessDiedException:
80 break
81
82 ref_app.stop()
83
84 print("Disconnecting...")
85 client.close()
86
87
88class PGUpdater:
89 def __init__(
90 self,
91 ref_app_config: RefAppConfig,
92 metadata: a121.Metadata,
93 sensor_id: int,
94 session_config: a121.SessionConfig,
95 ):
96 self.metadata = metadata
97 self.ref_app_config = ref_app_config
98 sensor_configs = get_sensor_configs(ref_app_config)
99 display_config = sensor_configs["base_config"]
100 self.sensor_id = sensor_id
101 self.session_config = session_config
102 self.sensor_config = display_config
103 self.distances = get_distances_m(display_config, metadata)
104
105 if self.ref_app_config.obstruction_detection:
106 obstruction_config = sensor_configs["obstruction_config"]
107 self.obs_distances = get_distances_m(obstruction_config, metadata)
108 self.obs_x_thres, self.obs_y_thres = ObstructionProcessor.get_thresholds(
109 ref_app_config.obstruction_distance_threshold, self.obs_distances
110 )
111
112 self.setup_is_done = False
113
114 def setup(self, win):
115 win.setWindowTitle("Acconeer parking detection example")
116 # Define pens and font.
117 blue_color = et.utils.color_cycler(0)
118 orange_color = et.utils.color_cycler(1)
119 brush = et.utils.pg_brush_cycler(0)
120
121 self.blue = {
122 "pen": pg.mkPen(blue_color, width=2),
123 "symbol": "o",
124 "symbolSize": 1,
125 "symbolBrush": brush,
126 "symbolPen": "k",
127 }
128
129 self.orange = {
130 "pen": pg.mkPen(orange_color, width=2),
131 "symbol": "o",
132 "symbolSize": 1,
133 "symbolBrush": brush,
134 "symbolPen": "k",
135 }
136 self.blue_transparent_pen = pg.mkPen(f"{blue_color}50", width=2)
137 self.orange_transparent_pen = pg.mkPen(f"{orange_color}50", width=2)
138
139 brush_dot = et.utils.pg_brush_cycler(1)
140
141 # Signature plot.
142 self.sig_plot = win.addPlot(row=0, col=0, colspan=2)
143 self.sig_plot.setTitle("Sampled Signatures")
144 self.sig_plot.setMenuEnabled(False)
145 self.sig_plot.showGrid(x=True, y=True)
146 self.sig_plot.addLegend()
147 self.sig_plot.setLabel("left", "Normalized energy")
148 self.sig_plot.setLabel("bottom", "Distance (m)")
149 self.sig_plot.addItem(pg.PlotDataItem())
150 self.sig_plot_x_range = (
151 min(self.distances),
152 max(self.distances) + self.ref_app_config.weighted_distance_threshold_m,
153 )
154
155 self.sig_plot.setXRange(self.sig_plot_x_range[0], self.sig_plot_x_range[1])
156 self.sig_plot.setYRange(0, 100)
157 symbol_kw_main = {
158 "symbol": "o",
159 "symbolSize": 7,
160 "symbolBrush": brush,
161 "symbolPen": None,
162 "pen": None,
163 }
164 self.sig_plot_curve = self.sig_plot.plot(**symbol_kw_main)
165 energy_threshold_line = pg.InfiniteLine(
166 angle=0, pen=pg.mkPen("k", width=1.5, style=QtCore.Qt.PenStyle.DashLine)
167 )
168 energy_threshold_line.setVisible(True)
169 energy_threshold_line.setPos(self.ref_app_config.amplitude_threshold)
170 self.sig_plot.addItem(energy_threshold_line)
171
172 self.sig_plot_cluster_start = pg.InfiniteLine(angle=90, pen=pg.mkPen("k", width=1.5))
173 self.sig_plot_cluster_start.setVisible(True)
174 self.sig_plot.addItem(self.sig_plot_cluster_start)
175
176 self.sig_plot_cluster_end = pg.InfiniteLine(angle=90, pen=pg.mkPen("k", width=1.5))
177 self.sig_plot_cluster_end.setVisible(True)
178 self.sig_plot.addItem(self.sig_plot_cluster_end)
179
180 self.sig_plot_smooth_max = et.utils.SmoothMax(self.session_config.update_rate)
181
182 parking_car_html = (
183 '<div style="text-align: center">'
184 '<span style="color: #FFFFFF;font-size:15pt;">'
185 "{}</span></div>".format("Parked car detected!")
186 )
187 self.parking_car_text_item = pg.TextItem(
188 html=parking_car_html,
189 fill=orange_color,
190 anchor=(0.5, 0),
191 )
192 parking_no_car_html = (
193 '<div style="text-align: center">'
194 '<span style="color: #FFFFFF;font-size:15pt;">'
195 "{}</span></div>".format("No car detected.")
196 )
197 self.parking_no_car_text_item = pg.TextItem(
198 html=parking_no_car_html,
199 fill=blue_color,
200 anchor=(0.5, 0),
201 )
202
203 self.sig_plot.addItem(self.parking_car_text_item)
204 self.sig_plot.addItem(self.parking_no_car_text_item)
205 self.parking_car_text_item.hide()
206 self.parking_no_car_text_item.hide()
207
208 self.cluster_width = self.ref_app_config.weighted_distance_threshold_m
209
210 # Obstruction plot.
211 if self.ref_app_config.obstruction_detection:
212 self.obstruction_plot = win.addPlot(row=1, col=1)
213 self.obstruction_plot.setTitle("Obstruction Detection Signatures")
214 self.obstruction_plot.setMenuEnabled(False)
215 self.obstruction_plot.showGrid(x=True, y=True)
216 self.obstruction_plot.addLegend()
217 self.obstruction_plot.setLabel("left", "Average energy")
218 self.obstruction_plot.setLabel("bottom", "Distance (m)")
219 self.obstruction_plot.addItem(pg.PlotDataItem())
220 self.obstruction_plot.setXRange(min(self.obs_distances), max(self.obs_distances))
221 self.obstruction_plot.setYRange(0, MAX_AMPLITUDE) # Set to standard
222 self.obstruction_plot_curve = self.obstruction_plot.plot(**self.orange)
223
224 symbol_obstruction_dot = {
225 "symbol": "o",
226 "symbolSize": 7,
227 "symbolBrush": brush_dot,
228 "symbolPen": None,
229 "pen": None,
230 }
231 self.obstruction_plot_point = self.obstruction_plot.plot(**symbol_obstruction_dot)
232
233 symbol_kw_main = {
234 "symbol": "o",
235 "symbolSize": 7,
236 "symbolBrush": brush,
237 "symbolPen": None,
238 "pen": None,
239 }
240 self.obstruction_plot_center = self.obstruction_plot.plot(**symbol_kw_main)
241
242 self.obstruction_center_rect = pg.QtWidgets.QGraphicsRectItem(0, 0, 0.01, 0.01)
243 self.obstruction_center_rect.setPen(self.orange_transparent_pen)
244 self.obstruction_plot.addItem(self.obstruction_center_rect)
245
246 obstruction_html = (
247 '<div style="text-align: center">'
248 '<span style="color: #FFFFFF;font-size:15pt;">'
249 "{}</span></div>".format("Obstruction detected!")
250 )
251 self.obstruction_text_item = pg.TextItem(
252 html=obstruction_html,
253 fill=orange_color,
254 anchor=(0.5, 0),
255 )
256 no_obstruction_html = (
257 '<div style="text-align: center">'
258 '<span style="color: #FFFFFF;font-size:15pt;">'
259 "{}</span></div>".format("No obstruction detected.")
260 )
261 self.no_obstruction_text_item = pg.TextItem(
262 html=no_obstruction_html,
263 fill=blue_color,
264 anchor=(0.5, 0),
265 )
266
267 obs_text_x_pos = (
268 min(self.obs_distances) + (max(self.obs_distances) - min(self.obs_distances)) * 0.5
269 )
270 obs_text_y_pos = MAX_AMPLITUDE * 0.9
271
272 self.obstruction_text_item.setPos(obs_text_x_pos, obs_text_y_pos)
273 self.no_obstruction_text_item.setPos(obs_text_x_pos, obs_text_y_pos)
274
275 self.obstruction_plot.addItem(self.obstruction_text_item)
276 self.obstruction_plot.addItem(self.no_obstruction_text_item)
277 self.obstruction_text_item.hide()
278 self.no_obstruction_text_item.hide()
279
280 # Parking info plot.
281 self.parking_plot = win.addPlot(row=1, col=0)
282 self.parking_plot.setTitle("Noise adjusted amplitude")
283 self.parking_plot.setMenuEnabled(False)
284 self.parking_plot.showGrid(x=True, y=True)
285 self.parking_plot.setLabel("left", "Normalized energy")
286 self.parking_plot.setLabel("bottom", "Distance (m)")
287 self.parking_plot.addItem(pg.PlotDataItem())
288 self.parking_plot.setXRange(min(self.distances), max(self.distances))
289 self.parking_plot.setYRange(0, 100) # Set to standard
290 self.parking_plot_curve = self.parking_plot.plot(**self.blue)
291 self.parking_smooth_max = et.utils.SmoothMax(self.session_config.update_rate)
292
293 def update_obstruction_text(self) -> None:
294 self.obstruction_text_timeout -= 1
295 if self.obstruction_text_timeout < 0:
296 self.obstruction_text_timeout = 0
297 self.obstruction_text_item.hide()
298
299 def show_obstruction_text(self) -> None:
300 self.obstruction_text_timeout = 5
301 self.obstruction_text_item.show()
302
303 def update(self, ref_app_result: RefAppResult) -> None:
304 signatures = ref_app_result.extra_result.signature_history
305 parking_data = ref_app_result.extra_result.parking_data
306
307 signature_x = [elm[0] for elm in signatures]
308 signature_y = [elm[1] for elm in signatures]
309
310 cluster_start = ref_app_result.extra_result.closest_object_dist
311 cluster_end = cluster_start + self.cluster_width
312 self.sig_plot_curve.setData(x=signature_x, y=signature_y)
313 self.sig_plot_cluster_start.setPos(cluster_start)
314 self.sig_plot_cluster_end.setPos(cluster_end)
315 self.sig_plot_cluster_start.setVisible(False)
316 self.sig_plot_cluster_end.setVisible(False)
317
318 sig_max = self.sig_plot_smooth_max.update(max(signature_y))
319 self.sig_plot.setYRange(0, sig_max)
320
321 sig_text_x_pos = (
322 self.sig_plot_x_range[0] + (self.sig_plot_x_range[1] - self.sig_plot_x_range[0]) * 0.5
323 )
324 sig_text_y_pos = sig_max * 0.9
325
326 self.parking_car_text_item.setPos(sig_text_x_pos, sig_text_y_pos)
327 self.parking_no_car_text_item.setPos(sig_text_x_pos, sig_text_y_pos)
328
329 if ref_app_result.car_detected:
330 self.parking_no_car_text_item.hide()
331 self.parking_car_text_item.show()
332 self.sig_plot_cluster_start.setVisible(True)
333 self.sig_plot_cluster_end.setVisible(True)
334 else:
335 self.parking_car_text_item.hide()
336 self.parking_no_car_text_item.show()
337
338 if self.ref_app_config.obstruction_detection:
339 obstruction_data = ref_app_result.extra_result.obstruction_data
340 self.obstruction_plot_curve.setData(self.obs_distances, obstruction_data)
341 point_x, point_y = ref_app_result.extra_result.obstruction_signature
342 center_x, center_y = ref_app_result.extra_result.obstruction_center
343 rect_x = center_x - self.obs_x_thres
344 rect_y = center_y - self.obs_y_thres
345 rect_w = 2 * self.obs_x_thres
346 rect_h = 2 * self.obs_y_thres
347
348 self.obstruction_center_rect.setRect(rect_x, rect_y, rect_w, rect_h)
349
350 self.obstruction_plot_point.setData(x=[point_x], y=[point_y])
351 self.obstruction_plot_center.setData(x=[center_x], y=[center_y])
352
353 if ref_app_result.obstruction_detected:
354 self.no_obstruction_text_item.hide()
355 self.obstruction_text_item.show()
356 else:
357 self.obstruction_text_item.hide()
358 self.no_obstruction_text_item.show()
359
360 park_max = np.amax(parking_data)
361 park_max = self.parking_smooth_max.update(park_max)
362 self.parking_plot.setYRange(0, park_max)
363 self.parking_plot_curve.setData(self.distances, parking_data)
364
365
366if __name__ == "__main__":
367 main()
View this example on GitHub: acconeer/acconeer-python-exploration