Download dataloader/detection_labels.py from ZenoNing/ConsDrive: direct link, hf CLI and curl.
- Browser
- Download file 5.08 kB
-
https://huggingface.co/ZenoNing/ConsDrive/resolve/main/dataloader/detection_labels.py
- Command line
-
hf download hf://ZenoNing/ConsDrive/dataloader/detection_labels.py
-
curl -L -o detection_labels.py https://huggingface.co/ZenoNing/ConsDrive/resolve/main/dataloader/detection_labels.py
5.08 kB
| """Layout and per-frame extraction of the stage-2 detection / traffic-control labels. | |
| scripts/build_detection_labels.py writes, per route, <data_root>/detection_labels/<...>/labels.npz: | |
| objects [F, K, OBJ_DIM] float32 ground-truth boxes in that frame's ego frame (x forward, y right, | |
| CARLA convention) that lie roughly inside the front camera view, | |
| nearest first, zero padded | |
| count [F] int16 | |
| ego [F, EGO_DIM] float32 ego speed, world pose and traffic-control state | |
| Classes follow dataloader.object_bank (car / walker / static), so the detector produces exactly the | |
| objects the stage-3 bank was trained on. OBJ_BANK_VISIBLE is the bank's own visibility test (field of | |
| view, range, BEV occlusion by other boxes); OBJ_LIDAR_POINTS is SimLingo's lidar hit count. | |
| """ | |
| import numpy as np | |
| from dataloader.object_bank import ( | |
| CLASS_CAR, CLASS_STATIC, CLASS_WALKER, DEFAULT_BANK, ego_pose_from_matrix, normalize_angle, visible_mask, | |
| ) | |
| (OBJ_X, OBJ_Y, OBJ_Z, OBJ_YAW, OBJ_SPEED, OBJ_HALF_L, OBJ_HALF_W, OBJ_HALF_H, OBJ_CLASS, OBJ_ID, | |
| OBJ_LIDAR_POINTS, OBJ_BANK_VISIBLE) = range(12) | |
| OBJ_DIM = 12 | |
| (EGO_SPEED, EGO_X, EGO_Y, EGO_YAW, EGO_TL_STATE, EGO_TL_DIST, EGO_LIGHT_HAZARD, EGO_STOP_HAZARD, | |
| EGO_STOP_CLOSE, EGO_STOP_DIST, EGO_JUNCTION, EGO_TARGET_SPEED) = range(12) | |
| EGO_DIM = 12 | |
| # EGO_TL_STATE of the traffic light affecting the ego (-1 none); EGO_STOP_DIST of the next stop | |
| # stencil in the ego lane (-1 none) | |
| TL_STATES = {"Red": 0, "Yellow": 1, "Green": 2, "Off": 3, "Unknown": 3} | |
| MAX_OBJECTS = 64 | |
| LABEL_RANGE = 65.0 # metres from the camera; training/eval pick their own range below this | |
| LABEL_HALF_FOV_DEG = 62.0 # a little wider than the 55 degree half FOV so boundary objects are kept | |
| CLASS_NAMES = {"car": CLASS_CAR, "walker": CLASS_WALKER, "static_car": CLASS_STATIC, "static": CLASS_STATIC, | |
| "static_trafficwarning": CLASS_STATIC} | |
| def frame_labels(boxes, measurement): | |
| """One SimLingo frame (boxes/*.json.gz, measurements/*.json.gz) -> (objects [n, OBJ_DIM], ego [EGO_DIM]).""" | |
| ego = np.zeros(EGO_DIM, dtype=np.float32) | |
| ego_x, ego_y, ego_yaw = ego_pose_from_matrix(measurement["ego_matrix"]) | |
| ego[EGO_SPEED] = float(measurement.get("speed", 0.0) or 0.0) | |
| ego[EGO_X], ego[EGO_Y], ego[EGO_YAW] = ego_x, ego_y, ego_yaw | |
| ego[EGO_TL_STATE], ego[EGO_TL_DIST], ego[EGO_STOP_DIST] = -1.0, -1.0, -1.0 | |
| ego[EGO_LIGHT_HAZARD] = float(bool(measurement.get("light_hazard"))) | |
| ego[EGO_STOP_HAZARD] = float(bool(measurement.get("stop_sign_hazard"))) | |
| ego[EGO_STOP_CLOSE] = float(bool(measurement.get("stop_sign_close"))) | |
| ego[EGO_JUNCTION] = float(bool(measurement.get("junction"))) | |
| ego[EGO_TARGET_SPEED] = float(measurement.get("target_speed", 0.0) or 0.0) | |
| rows = [] | |
| for box in boxes: | |
| cls_name = box.get("class") | |
| if cls_name == "traffic_light" and box.get("affects_ego"): | |
| dist = float(box.get("distance", -1.0)) | |
| if ego[EGO_TL_DIST] < 0 or dist < ego[EGO_TL_DIST]: | |
| ego[EGO_TL_STATE] = TL_STATES.get(str(box.get("state")), 3) | |
| ego[EGO_TL_DIST] = dist | |
| continue | |
| if cls_name == "landmark" and "STOP" in str(box.get("name", "")).upper(): | |
| x, y = box["position"][:2] | |
| if x > 0 and abs(y) < 4.0 and (ego[EGO_STOP_DIST] < 0 or x < ego[EGO_STOP_DIST]): | |
| ego[EGO_STOP_DIST] = x | |
| continue | |
| cls = CLASS_NAMES.get(cls_name) | |
| if cls is None or "position" not in box or "extent" not in box: | |
| continue | |
| pos = np.asarray(box["position"], dtype=np.float64) | |
| if pos.shape[0] < 3 or not np.isfinite(pos).all(): | |
| continue | |
| extent = box["extent"] | |
| speed = float(box.get("speed", 0.0) or 0.0) if cls != CLASS_STATIC else 0.0 | |
| rows.append([pos[0], pos[1], pos[2], normalize_angle(float(box.get("yaw", 0.0))), max(speed, 0.0), | |
| max(float(extent[0]), 0.1), max(float(extent[1]), 0.1), | |
| max(float(extent[2]), 0.05) if len(extent) > 2 else 0.75, cls, | |
| float(box["id"]) if box.get("id") is not None else -1.0, | |
| float(box.get("num_points", 0) or 0), 0.0]) | |
| if not rows: | |
| return np.zeros((0, OBJ_DIM), dtype=np.float32), ego | |
| rows = np.asarray(rows, dtype=np.float64) | |
| # every box is a potential occluder; only the ones roughly in view are kept as labels | |
| rows[:, OBJ_BANK_VISIBLE] = visible_mask( | |
| rows[:, [OBJ_X, OBJ_Y]], rows[:, OBJ_YAW], rows[:, OBJ_HALF_L], rows[:, OBJ_HALF_W], rows[:, OBJ_HALF_H], | |
| DEFAULT_BANK["max_range"], DEFAULT_BANK["fov_deg"], DEFAULT_BANK["camera_x"], DEFAULT_BANK["camera_z"]) | |
| dx = rows[:, OBJ_X] - DEFAULT_BANK["camera_x"] | |
| dist = np.hypot(dx, rows[:, OBJ_Y]) | |
| angle = np.degrees(np.arctan2(rows[:, OBJ_Y], dx)) | |
| keep = (dx > 0.5) & (np.abs(angle) < LABEL_HALF_FOV_DEG) & (dist < LABEL_RANGE) | |
| rows = rows[keep][np.argsort(dist[keep])][:MAX_OBJECTS] | |
| return rows.astype(np.float32), ego | |