|
1 | 1 | from typing import Optional, Tuple |
| 2 | +from collections import deque |
2 | 3 | import numpy as np |
3 | 4 | import time |
4 | 5 |
|
@@ -59,6 +60,11 @@ def __init__(self): |
59 | 60 | self._roi_half = 10 # radius in depth pixels (=> (2h+1)^2 window) |
60 | 61 | self._hud_on = True # toggle with 'g' |
61 | 62 |
|
| 63 | + self._avg_len = 10 |
| 64 | + self._roi_mean_hist = deque(maxlen=self._avg_len) |
| 65 | + self._center_hist = deque(maxlen=self._avg_len) |
| 66 | + self._hist_cursor_xy = None |
| 67 | + |
62 | 68 | self.out_annotations = self.createOutput( |
63 | 69 | possibleDatatypes=[ |
64 | 70 | dai.Node.DatatypeHierarchy(dai.DatatypeEnum.ImgAnnotations, True) |
@@ -135,17 +141,56 @@ def _draw_depth_hud(self, hud: dai.ImgAnnotation, preview_w: int, preview_h: int |
135 | 141 | roi = self._last_depth[y0:y1, x0:x1] |
136 | 142 |
|
137 | 143 | # stats (ignore zeros if depth) |
| 144 | + if self._hist_cursor_xy != (cx, cy): |
| 145 | + self._roi_mean_hist.clear() |
| 146 | + self._center_hist.clear() |
| 147 | + self._hist_cursor_xy = (cx, cy) |
| 148 | + |
138 | 149 | if self._depth_is_mm: |
139 | 150 | valid = roi > 0 |
140 | | - center_val = float(self._last_depth[cy, cx]) / 1000.0 # mm → m |
141 | | - mean_val = ( |
142 | | - float(roi[valid].mean() / 1000.0) if np.any(valid) else float("nan") |
| 151 | + |
| 152 | + # center (mm) |
| 153 | + center_mm = float(self._last_depth[cy, cx]) |
| 154 | + if center_mm > 0: |
| 155 | + self._center_hist.append(center_mm) |
| 156 | + |
| 157 | + # roi mean (mm) |
| 158 | + if np.any(valid): |
| 159 | + roi_mean_mm = float(roi[valid].mean()) |
| 160 | + self._roi_mean_hist.append(roi_mean_mm) |
| 161 | + |
| 162 | + # aggregated (m) |
| 163 | + if len(self._center_hist) > 0: |
| 164 | + center_val = float(np.mean(self._center_hist)) / 1000.0 |
| 165 | + else: |
| 166 | + center_val = float("nan") |
| 167 | + |
| 168 | + if len(self._roi_mean_hist) > 0: |
| 169 | + mean_val = float(np.mean(self._roi_mean_hist)) / 1000.0 |
| 170 | + else: |
| 171 | + mean_val = float("nan") |
| 172 | + |
| 173 | + val_text = ( |
| 174 | + f"Depth: {center_val:.2f} m, " |
| 175 | + f"ROI mean: {mean_val:.2f} m " |
| 176 | + f"(avg {len(self._roi_mean_hist)}/{self._avg_len})" |
143 | 177 | ) |
144 | | - val_text = f"Depth: {center_val:.2f} m, ROI mean: {mean_val:.2f} m" |
145 | 178 | else: |
146 | | - center_val = float(self._last_depth[cy, cx]) |
147 | | - mean_val = float(roi.mean()) |
148 | | - val_text = f"Disp: {center_val:.1f}, ROI mean: {mean_val:.1f}" |
| 179 | + # disparity path (no zero filtering) |
| 180 | + center_disp = float(self._last_depth[cy, cx]) |
| 181 | + roi_mean_disp = float(roi.mean()) |
| 182 | + |
| 183 | + self._center_hist.append(center_disp) |
| 184 | + self._roi_mean_hist.append(roi_mean_disp) |
| 185 | + |
| 186 | + center_val = float(np.mean(self._center_hist)) |
| 187 | + mean_val = float(np.mean(self._roi_mean_hist)) |
| 188 | + |
| 189 | + val_text = ( |
| 190 | + f"Disp: {center_val:.1f}, " |
| 191 | + f"ROI mean: {mean_val:.1f} " |
| 192 | + f"(avg {len(self._roi_mean_hist)}/{self._avg_len})" |
| 193 | + ) |
149 | 194 |
|
150 | 195 | # normalized positions relative to preview (assume 1:1 mapping depth→preview) |
151 | 196 | u = (cx + 0.5) / W |
|
0 commit comments