ConsDrive / scripts /analysis /traffic_light_stoplines.py
ZenoNing's picture
Upload ConsDrive v2 source (part 2)
314ecfb verified
Raw History Blame Contribute Delete
2.27 kB
"""Dump traffic-light stop lines per town so recorder logs can be checked for red-light waits offline.
Usage: python traffic_light_stoplines.py --port 2000 --out stoplines.json [--towns Town12 Town13 ...]
Writes {town: [{"loc": [x, y], "stops": [[x, y, yaw_deg, road_id, lane_id], ...]}, ...]}; "loc" is the
traffic-light actor location in metres, which matches the recorder's "Create ...: traffic.traffic_light".
Stop signs go to {town + "/stop_signs": [[x, y, extent_x, extent_y], ...]} (trigger-volume centre and
half extents in metres).
"""
import argparse
import json
import xml.etree.ElementTree as ET
import carla
ROUTES = "leaderboard/data/bench2drive220.xml"
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--port", type=int, default=2000)
ap.add_argument("--out", required=True)
ap.add_argument("--towns", nargs="*")
args = ap.parse_args()
towns = args.towns or sorted({r.get("town") for r in ET.parse(ROUTES).getroot().iter("route")})
client = carla.Client("localhost", args.port, worker_threads=4)
client.set_timeout(600.0)
try:
out = json.load(open(args.out))
except FileNotFoundError:
out = {}
for town in towns:
if town in out and f"{town}/stop_signs" in out:
continue
world = client.load_world(town)
lights = []
for tl in world.get_actors().filter("traffic.traffic_light"):
loc = tl.get_location()
stops = [[w.transform.location.x, w.transform.location.y, w.transform.rotation.yaw, w.road_id, w.lane_id]
for w in tl.get_stop_waypoints()]
lights.append({"loc": [loc.x, loc.y], "stops": stops})
signs = []
for sign in world.get_actors().filter("traffic.stop"):
centre = sign.get_transform().transform(sign.trigger_volume.location)
signs.append([centre.x, centre.y, sign.trigger_volume.extent.x, sign.trigger_volume.extent.y])
out[town] = lights
out[f"{town}/stop_signs"] = signs
json.dump(out, open(args.out, "w"))
print(f"{town}: {len(lights)} traffic lights, {sum(len(l['stops']) for l in lights)} stop lines, "
f"{len(signs)} stop signs", flush=True)
if __name__ == "__main__":
main()