Download scripts/analysis/recorder_traffic_lights.py from ZenoNing/ConsDrive: direct link, hf CLI and curl.
- Browser
- Download file 3.06 kB
-
https://huggingface.co/ZenoNing/ConsDrive/resolve/main/scripts/analysis/recorder_traffic_lights.py
- Command line
-
hf download hf://ZenoNing/ConsDrive/scripts/analysis/recorder_traffic_lights.py
-
curl -L -o recorder_traffic_lights.py https://huggingface.co/ZenoNing/ConsDrive/resolve/main/scripts/analysis/recorder_traffic_lights.py
3.06 kB
| """Extract traffic-light locations and state changes from CARLA recorder logs. | |
| Usage: python recorder_traffic_lights.py --port 2000 --out-dir DIR [--workers 4] log1.log [log2.log ...] | |
| For each log writes DIR/<log stem>_tl.npz with | |
| tl_ids: (L,) recorder actor ids of traffic lights | |
| tl_locs: (L, 2) x, y in metres | |
| changes: (C, 3) t, tl_id, state (state: 0 red, 1 yellow, 2 green, 3 off, 4 unknown; first row per light | |
| is its state in the first recorded frame) | |
| """ | |
| import argparse | |
| import re | |
| from multiprocessing import Pool | |
| from pathlib import Path | |
| import carla | |
| import numpy as np | |
| FRAME_RE = re.compile(r"^Frame \d+ at ([\d.]+) seconds") | |
| CREATE_RE = re.compile(r"^ Create (\d+): traffic\.traffic_light \(\d+\) at \(([-+\d.e]+), ([-+\d.e]+), ([-+\d.e]+)\)") | |
| STATE_RE = re.compile(r"^ Id: (\d+) state: (\d+) frozen") | |
| PORT = None | |
| def extract(text): | |
| locs, last, changes = {}, {}, [] | |
| t = None | |
| for line in text.splitlines(): | |
| if line.startswith("Frame "): | |
| m = FRAME_RE.match(line) | |
| if m: | |
| t = float(m.group(1)) | |
| continue | |
| if line.startswith(" Id: ") and " state: " in line: | |
| m = STATE_RE.match(line) | |
| if m and t is not None: | |
| tl, state = int(m.group(1)), int(m.group(2)) | |
| if last.get(tl) != state: | |
| last[tl] = state | |
| changes.append((t, tl, state)) | |
| continue | |
| if line.startswith(" Create ") and "traffic_light" in line: | |
| m = CREATE_RE.match(line) | |
| if m: | |
| locs[int(m.group(1))] = (float(m.group(2)) / 100, float(m.group(3)) / 100) | |
| ids = np.array(sorted(locs), dtype=np.int64) | |
| return { | |
| "tl_ids": ids, | |
| "tl_locs": np.array([locs[i] for i in ids], dtype=np.float64).reshape(-1, 2), | |
| "changes": np.array(changes, dtype=np.float64).reshape(-1, 3), | |
| } | |
| def work(job): | |
| log, out = job | |
| client = carla.Client("localhost", PORT, worker_threads=2) | |
| client.set_timeout(600.0) | |
| data = extract(client.show_recorder_file_info(str(log), True)) | |
| np.savez_compressed(out, **data) | |
| return f"{log.stem}: {len(data['tl_ids'])} lights, {len(data['changes'])} state changes" | |
| def init(port): | |
| global PORT | |
| PORT = port | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--port", type=int, default=2000) | |
| ap.add_argument("--out-dir", required=True) | |
| ap.add_argument("--workers", type=int, default=4) | |
| ap.add_argument("logs", nargs="+") | |
| args = ap.parse_args() | |
| out_dir = Path(args.out_dir) | |
| out_dir.mkdir(parents=True, exist_ok=True) | |
| jobs = [] | |
| for log in args.logs: | |
| log = Path(log).resolve() | |
| out = out_dir / f"{log.stem}_tl.npz" | |
| if not out.exists(): | |
| jobs.append((log, out)) | |
| with Pool(args.workers, initializer=init, initargs=(args.port,)) as pool: | |
| for msg in pool.imap_unordered(work, jobs): | |
| print(msg, flush=True) | |
| if __name__ == "__main__": | |
| main() | |