### Install Cityscapes Scripts Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Install the base package or with GUI tools. Optionally, enable Cython-accelerated computation. ```bash pip install cityscapesScripts ``` ```bash pip install "cityscapesScripts[gui]" ``` ```bash CYTHONIZE_EVAL= python setup.py build_ext --inplace ``` -------------------------------- ### Install cityscapesscripts with GUI support Source: https://github.com/mcordts/cityscapesscripts/blob/master/README.md Install the cityscapesscripts package along with its graphical tools (viewer and label tool), which depend on Qt5. Ensure Qt5 is installed in your environment. ```bash python -m pip install cityscapesscripts[gui] ``` -------------------------------- ### Install cityscapesscripts with pip Source: https://github.com/mcordts/cityscapesscripts/blob/master/README.md Install the core cityscapesscripts package using pip. This is the primary method for obtaining the library. ```bash python -m pip install cityscapesscripts ``` -------------------------------- ### Get Core Image Filename Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Obtain the common prefix for a Cityscapes filename, useful for matching across different modalities. Requires 'csHelpers' import. ```python from cityscapesscripts.helpers.csHelpers import getCoreImageFileName fname = 'frankfurt_000000_000294_gtFine_polygons.json' # Get the shared prefix across modalities core = getCoreImageFileName(fname) print(core) # frankfurt_000000_000294 ``` -------------------------------- ### Get 3D Box Parameters in V Coordinate System Source: https://github.com/mcordts/cityscapesscripts/blob/master/docs/Box3DImageTransform.ipynb Retrieves the size, center, and rotation of a 3D box in the V coordinate system. Requires `box3d_annotation` and `CRS_V` to be defined. ```python # Similar to the box vertices, you can retrieve box parameters center, size and rotation in any coordinate system size_V, center_V, rotation_V = box3d_annotation.get_parameters(coordinate_system=CRS_V) # size_C, center_C, rotation_C = box3d_annotation.get_parameters(coordinate_system=CRS_C) # size_S, center_S, rotation_S = box3d_annotation.get_parameters(coordinate_system=CRS_S) print("Size: ", size_V) print("Center: ", center_V) print("Rotation:", rotation_V) ``` -------------------------------- ### Get 3D Vertices in Vehicle Coordinate System Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Retrieve the 3D vertices of a bounding box in the vehicle coordinate system using a pre-initialized 'Box3dImageTransform'. ```python from cityscapesscripts.helpers.box3dImageTransform import Box3dImageTransform, CRS_V from cityscapesscripts.helpers.annotation import Annotation, CsObjectType # Assuming 'transformer' is already initialized as in the previous example # transformer = Box3dImageTransform(camera=camera) # transformer.initialize_box_from_annotation(box_obj, coordinate_system=CRS_V) # 3D vertices in vehicle coordinate system vertices_v = transformer.get_vertices(coordinate_system=CRS_V) print(vertices_v.keys()) # dict_keys(['BLB', 'BRB', 'FRB', 'FLB', 'BLT', 'BRT', 'FRT', 'FLT']) ``` -------------------------------- ### Get Face Visibility for 3D Box Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Determine the visibility of each face of a 3D bounding box, useful for occlusion-aware rendering. Requires a 'Box3dImageTransform' object. ```python from cityscapesscripts.helpers.box3dImageTransform import Box3dImageTransform, CRS_V from cityscapesscripts.helpers.annotation import Annotation, CsObjectType # Assuming 'transformer' is already initialized # transformer = Box3dImageTransform(camera=camera) # transformer.initialize_box_from_annotation(box_obj, coordinate_system=CRS_V) # Face visibility (useful for occlusion-aware rendering) visibilities = transformer.get_all_side_visibilities() labels = ['front', 'back', 'top', 'bottom', 'left', 'right'] for name, vis in zip(labels, visibilities): print(f" {name}: {vis}") ``` -------------------------------- ### Get 3D Box Vertices in 2D Image Coordinates Source: https://github.com/mcordts/cityscapesscripts/blob/master/docs/Box3DImageTransform.ipynb Projects the 3D box vertices into 2D image coordinates (u, v). The `box3d_annotation` object must be initialized. ```python # Get the vertices of the 3D box in the image coordinates box_vertices_I = box3d_annotation.get_vertices_2d() # Print the vertices of the box. # loc is encoded with a 3-char code # 0: B/F: Back or Front # 1: L/R: Left or Right # 2: B/T: Bottom or Top # BLT -> Back left top of the object print("\n {:>8} {:>8}".format("u[px]", "v[px]")) for loc, coord in box_vertices_I.items(): print("{}: {:8.2f} {:8.2f}".format(loc, coord[0], coord[1])) ``` -------------------------------- ### Get 3D box vertices in different coordinate systems Source: https://github.com/mcordts/cityscapesscripts/blob/master/docs/Box3DImageTransform.ipynb Retrieves the 3D vertices of the bounding box in the vehicle (CRS_V), camera (CRS_C), and sensor (CRS_S) coordinate systems using the Box3dImageTransform object. ```python # Get the vertices of the 3D box in the requested coordinate frame box_vertices_V = box3d_annotation.get_vertices(coordinate_system=CRS_V) box_vertices_C = box3d_annotation.get_vertices(coordinate_system=CRS_C) box_vertices_S = box3d_annotation.get_vertices(coordinate_system=CRS_S) ``` -------------------------------- ### Get Amodal 2D Bounding Box Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Calculate the amodal 2D bounding box from a 3D bounding box using a 'Box3dImageTransform' object. The result is in [xmin, ymin, xmax, ymax] format. ```python from cityscapesscripts.helpers.box3dImageTransform import Box3dImageTransform, CRS_V from cityscapesscripts.helpers.annotation import Annotation, CsObjectType # Assuming 'transformer' is already initialized # transformer = Box3dImageTransform(camera=camera) # transformer.initialize_box_from_annotation(box_obj, coordinate_system=CRS_V) # Amodal 2D bounding box [xmin, ymin, xmax, ymax] amodal_box = transformer.get_amodal_box_2d() print(amodal_box) # [423.2, 310.5, 687.4, 502.1] ``` -------------------------------- ### List Available Cityscapes Packages via CLI Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Command-line interface command to list all available Cityscapes dataset packages. ```bash # List available packages csDownload --list_available ``` -------------------------------- ### Ensure Output Directory Exists Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Create a directory if it does not already exist, useful for saving output files. Requires 'csHelpers' import. ```python from cityscapesscripts.helpers.csHelpers import ensurePath # Ensure an output directory exists ensurePath('/tmp/my_results/frankfurt') ``` -------------------------------- ### View Cityscapes Dataset Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Launches the csViewer tool for viewing images with overlaid annotations. Requires PyQt5. Set the CITYSCAPES_DATASET environment variable or use the GUI folder picker. ```bash # View images with overlaid annotations; requires PyQt5 csViewer # Set CITYSCAPES_DATASET env var first, or use the GUI folder picker. ``` -------------------------------- ### Initialize 3D Box Transformer from Camera Calibration Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Load camera calibration from a Cityscapes annotation file and initialize a 'Box3dImageTransform' object. Requires 'Annotation', 'CsObjectType', and 'Box3dImageTransform' imports. ```python from cityscapesscripts.helpers.box3dImageTransform import ( Box3dImageTransform, Camera, CRS_V, CRS_C, CRS_S ) from cityscapesscripts.helpers.annotation import Annotation, CsObjectType import json # Load camera calibration from a gtFine JSON ann = Annotation(objType=CsObjectType.BBOX3D) ann.fromJsonFile('/data/cityscapes/gtBbox3d/val/frankfurt/' 'frankfurt_000000_000294_gtBbox3d.json') camera = ann.camera # Camera object with fx, fy, u0, v0, sensor_T_ISO_8855 # Build a transformer from a 3D box annotation box_obj = ann.objects[0] transformer = Box3dImageTransform(camera=camera) transformer.initialize_box_from_annotation(box_obj, coordinate_system=CRS_V) ``` -------------------------------- ### Download Cityscapes Packages Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Use `csDownload` to download dataset packages. The `--resume` flag can be used to continue interrupted downloads. ```bash csDownload -d /data/cityscapes gtFine_trainvaltest.zip leftImg8bit_trainvaltest.zip ``` ```bash csDownload -d /data/cityscapes --resume leftImg8bit_trainvaltest.zip ``` -------------------------------- ### Annotate Cityscapes Dataset Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Launches the csLabelTool tool, which was used to create the original Cityscapes annotations. ```bash # The tool used to create the original Cityscapes annotations csLabelTool ``` -------------------------------- ### List Available Cityscapes Dataset Packages Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Retrieve a list of all available Cityscapes dataset packages that the authenticated user has access to. Requires an active session object. ```python from cityscapesscripts.download.downloader import login, list_available_packages # Authenticate (prompts for credentials; optionally caches them) session = login() # List all packages you have access to list_available_packages(session=session) # leftImg8bit.zip -> 11GB # gtFine_trainvaltest.zip -> 241MB # gtCoarse.zip -> 1.3GB # gtBbox3d.zip -> 12MB # ... ``` -------------------------------- ### Initialize Camera object Source: https://github.com/mcordts/cityscapesscripts/blob/master/docs/Box3DImageTransform.ipynb Creates a Camera object using intrinsic parameters (fx, fy, u0, v0) and the extrinsic transformation matrix (sensor_T_ISO_8855) from the vehicle coordinate system (V) to the camera coordinate system (C). ```python camera = Camera(fx=sample_annotation["sensor"]["fx"], fy=sample_annotation["sensor"]["fy"], u0=sample_annotation["sensor"]["u0"], v0=sample_annotation["sensor"]["v0"], sensor_T_ISO_8855=sample_annotation["sensor"]["sensor_T_ISO_8855"]) ``` -------------------------------- ### Check Cycle Consistency of 3D Box Transformations Source: https://github.com/mcordts/cityscapesscripts/blob/master/docs/Box3DImageTransform.ipynb Verifies that initializing a 3D box in one coordinate system (e.g., V) and converting it to others (S, C) and back to the original (V) yields the same parameters. Requires `np` and `sample_annotation`. ```python # Initialize box in V box3d_annotation.initialize_box(size=sample_annotation["objects"][0]["3d"]["dimensions"], quaternion=sample_annotation["objects"][0]["3d"]["rotation"], center=sample_annotation["objects"][0]["3d"]["center"], coordinate_system=CRS_V) size_VV, center_VV, rotation_VV = box3d_annotation.get_parameters(coordinate_system=CRS_V) # Retrieve parameters in C, initialize in C and retrieve in V size_C, center_C, rotation_C = box3d_annotation.get_parameters(coordinate_system=CRS_C) box3d_annotation.initialize_box(size=size_C, quaternion=rotation_C, center=center_C, coordinate_system=CRS_C) size_VC, center_VC, rotation_VC = box3d_annotation.get_parameters(coordinate_system=CRS_V) # Retrieve parameters in S, initialize in S and retrieve in V size_S, center_S, rotation_S = box3d_annotation.get_parameters(coordinate_system=CRS_S) box3d_annotation.initialize_box(size=size_S, quaternion=rotation_S, center=center_S, coordinate_system=CRS_S) size_VS, center_VS, rotation_VS = box3d_annotation.get_parameters(coordinate_system=CRS_V) assert np.isclose(size_VV, size_VC).all() and np.isclose(size_VV, size_VS).all() assert np.isclose(center_VV, center_VC).all() and np.isclose(center_VV, center_VS).all() assert (rotation_VV == rotation_VC) and (rotation_VV == rotation_VS) ``` -------------------------------- ### Download Cityscapes Dataset Packages Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Download specified Cityscapes dataset packages to a local directory. Supports resuming partial downloads and can be configured via 'resume' parameter. Requires an active session object. ```python from cityscapesscripts.download.downloader import login, download_packages # Authenticate (prompts for credentials; optionally caches them) session = login() # Download specific packages to a local directory download_packages( session=session, package_names=['gtFine_trainvaltest.zip', 'leftImg8bit_trainvaltest.zip'], destination_path='/data/cityscapes', resume=False # set True to continue a partial download ) ``` -------------------------------- ### Build Polygon Annotation Programmatically Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Create and save a polygon annotation object. Ensure the 'Point' and 'Annotation' classes are imported. ```python poly = CsPoly() poly.label = 'car' poly.polygon = [Point(100, 200), Point(300, 200), Point(300, 400), Point(100, 400)] poly.verified = 1 ann_out = Annotation() ann_out.imgWidth = 2048 ann_out.imgHeight = 1024 ann_out.objects.append(poly) ann_out.toJsonFile('/tmp/my_annotation.json') ``` -------------------------------- ### Initialize Box3dImageTransform and load annotation Source: https://github.com/mcordts/cityscapesscripts/blob/master/docs/Box3DImageTransform.ipynb Initializes the Box3dImageTransform object with the camera parameters. It then loads a 3D annotation from a JSON-like structure into a CsBox3d object and initializes the transform with this annotation in the ISO 8855 coordinate system (CRS_V). ```python # Create the Box3dImageTransform object box3d_annotation = Box3dImageTransform(camera=camera) # Create a CsBox3d object for the 3D annotation obj = CsBbox3d() obj.fromJsonText(sample_annotation["objects"][0]) # Initialize the 3D box with an annotation in coordinate system V. # You can alternatively pass CRS_S or CRS_C if you want to initalize the box in a different coordinate system. # Please note that the object's size is always given as [L, W, H] independently of the used coodrinate system. box3d_annotation.initialize_box_from_annotation(obj, coordinate_system=CRS_V) size_V, center_V, rotation_V = box3d_annotation.get_parameters(coordinate_system=CRS_V) ``` -------------------------------- ### Sample 3D Annotation Data Source: https://github.com/mcordts/cityscapesscripts/blob/master/docs/Box3DImageTransform.ipynb A sample dictionary representing a 3D object annotation, including image dimensions, sensor intrinsic and extrinsic parameters, and detailed 3D object properties in the ISO 8855 coordinate system. ```python sample_annotation = { "imgWidth": 2048, "imgHeight": 1024, "sensor": { "sensor_T_ISO_8855": [ [ 0.9990881051503779, -0.01948468779721943, -0.03799085532693703, -1.6501524664770573 ], [ 0.019498764210995674, 0.9998098810245096, 0.0, -0.1331288872611436 ], [ 0.03798363254444427, -0.0007407747301939942, 0.9992780868764849, -1.2836173638418473 ] ], "fx": 2262.52, "fy": 2265.3017905988554, "u0": 1096.98, "v0": 513.137, "baseline": 0.209313 }, "objects": [ { "2d": { "modal": [ 609, 420, 198, 111 ], "amodal": [ 602, 415, 214, 118 ] }, "3d": { "center": [ 33.95, 5.05, 0.57 ], "dimensions": [ 4.3, 1.72, 1.53 ], "rotation": [ 0.9735839424380041, -0.010751769161021867, 0.0027191710555974913, 0.22805988817753894 ], "type": "Mid Size Car", "format": "CRS_ISO8855" }, "occlusion": 0.0, "truncation": 0.0, "instanceId": 26010, "label": "car", "score": 1.0 } ] } ``` -------------------------------- ### Read 3D Bounding Box Annotations Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Load 3D bounding box annotations from a JSON file and iterate through the objects. Requires 'Annotation' and 'CsObjectType' imports. ```python ann3d = Annotation(objType=CsObjectType.BBOX3D) ann3d.fromJsonFile('/data/cityscapes/gtBbox3d/val/frankfurt/' 'frankfurt_000000_000294_gtBbox3d.json') for obj in ann3d.objects: print(f" {obj.label} center={obj.center} dims={obj.dims} depth={obj.depth}m") ``` -------------------------------- ### Inspect Cityscapes Labels Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Import and inspect all Cityscapes labels, looking them up by name, ID, training ID, or category. ```python from cityscapesscripts.helpers.labels import ( labels, name2label, id2label, trainId2label, category2labels ) # Inspect all labels for lbl in labels: print(f"{lbl.name:22s} id={lbl.id:3d} trainId={lbl.trainId:3d} " \ f"category={lbl.category:14s} hasInstances={lbl.hasInstances}") # unlabeled id= 0 trainId=255 category=void hasInstances=False # road id= 7 trainId= 0 category=flat hasInstances=False # person id= 24 trainId= 11 category=human hasInstances=True # car id= 26 trainId= 13 category=vehicle hasInstances=True # Lookup by name car_label = name2label['car'] print(car_label.id) # 26 print(car_label.color) # (0, 0, 142) # Lookup by pixel ID label = id2label[24] print(label.name) # person print(label.ignoreInEval) # False # Lookup by training ID (first defined label wins for duplicates) label = trainId2label[0] print(label.name) # road # All labels in a category vehicle_labels = category2labels['vehicle'] print([l.name for l in vehicle_labels]) # ['car', 'truck', 'bus', 'caravan', 'trailer', 'train', 'motorcycle', 'bicycle', 'license plate'] # Resolve group names to single-instance names from cityscapesscripts.helpers.labels import assureSingleInstanceName print(assureSingleInstanceName('cargroup')) # 'car' print(assureSingleInstanceName('skygroup')) # None (sky has no instances) ``` -------------------------------- ### Set Cityscapes Dataset Environment Variables Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Set environment variables to specify the dataset root and results directory for script auto-discovery. ```bash export CITYSCAPES_DATASET=/data/cityscapes export CITYSCAPES_RESULTS=/data/cityscapes/results ``` -------------------------------- ### Authenticate Cityscapes Dataset Downloader Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Log in to the Cityscapes dataset downloader to obtain a session object for subsequent operations. This function may prompt for credentials. ```python from cityscapesscripts.download.downloader import login, list_available_packages, download_packages # Authenticate (prompts for credentials; optionally caches them) session = login() ``` -------------------------------- ### Prepare Panoptic Images Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Converts standard Cityscapes `*_instanceIds.png` annotations to COCO panoptic segmentation format. This includes one RGB PNG per image and a COCO-compatible JSON metadata file. The `useTrainId` parameter can be set to `True` to use trainIds for `category_id`. ```python from cityscapesscripts.preparation.createPanopticImgs import convert2panoptic convert2panoptic( cityscapesPath='/data/cityscapes/gtFine', # or None to use $CITYSCAPES_DATASET/gtFine outputFolder='/data/cityscapes/gtFine', # JSON + PNG output location useTrainId=False, # True to use trainIds for category_id setNames=['val', 'train'] # which splits to process ) # Converting 500 annotation files for val set. # Json file: .../cityscapes_panoptic_val.json # PNGs: .../cityscapes_panoptic_val/ ``` ```bash # CLI equivalent: # csCreatePanopticImgs --dataset-folder /data/cityscapes/gtFine \ # --set-names val train ``` -------------------------------- ### Create TrainID Label Images Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Converts polygon JSON annotations to PNG label images using training IDs. This script is useful for training frameworks that expect contiguous class indices. Ensure the `CITYSCAPES_DATASET` environment variable is set or invoke the `main()` function programmatically. ```bash # Run via CLI (uses CITYSCAPES_DATASET env var) # csCreateTrainIdLabelImgs ``` ```python import os os.environ['CITYSCAPES_DATASET'] = '/data/cityscapes' from cityscapesscripts.preparation.createTrainIdLabelImgs import main main() # Processing 2975 annotation files # Progress: 100 % # Output: *_labelTrainIds.png alongside each *_polygons.json ``` ```python # Example: remap all void classes to trainId=255 (ignore) and # use 0-18 for the 19 standard evaluation classes. # Then regenerate: from cityscapesscripts.preparation.json2labelImg import json2labelImg json2labelImg( 'frankfurt_000000_000294_gtFine_polygons.json', 'frankfurt_000000_000294_gtFine_labelTrainIds.png', 'trainIds' ) ``` -------------------------------- ### Import necessary libraries Source: https://github.com/mcordts/cityscapesscripts/blob/master/docs/Box3DImageTransform.ipynb Imports required modules from numpy and the cityscapesscripts library for handling 3D annotations and coordinate transformations. ```python import numpy as np from cityscapesscripts.helpers.annotation import CsBbox3d from cityscapesscripts.helpers.box3dImageTransform import ( Camera, Box3dImageTransform, CRS_V, CRS_C, CRS_S ) ``` -------------------------------- ### Parse Cityscapes Filename Components Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Extract detailed information from a Cityscapes filename using 'getCsFileInfo'. Requires 'csHelpers' import. ```python from cityscapesscripts.helpers.csHelpers import ( getCsFileInfo, getCoreImageFileName, ensurePath, writeDict2JSON ) fname = 'frankfurt_000000_000294_gtFine_polygons.json' # Parse all components cs = getCsFileInfo(fname) print(cs.city) # frankfurt print(cs.sequenceNb) # 000000 print(cs.frameNb) # 000294 print(cs.type) # gtFine print(cs.type2) # polygons print(cs.ext) # json ``` -------------------------------- ### Read 2D Bounding Box Annotations (CityPersons) Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Load 2D bounding box annotations from a CityPersons JSON file. The 'bbox_amodal' and 'bbox_modal' attributes return bounding box coordinates. ```python ann2d = Annotation(objType=CsObjectType.BBOX2D) ann2d.fromJsonFile('/data/cityscapes/gtBboxCityPersons/val/frankfurt/' 'frankfurt_000000_000294_gtBboxCityPersons.json') for obj in ann2d.objects: # bbox_amodal and bbox_modal return [xmin, ymin, xmax, ymax] print(f" {obj.label} amodal={obj.bbox_amodal} modal={obj.bbox_modal}") ``` -------------------------------- ### Project 3D Vertices to 2D Image Coordinates Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Transform the 3D vertices of a bounding box into 2D image coordinates using a 'Box3dImageTransform' object. ```python from cityscapesscripts.helpers.box3dImageTransform import Box3dImageTransform, CRS_V from cityscapesscripts.helpers.annotation import Annotation, CsObjectType # Assuming 'transformer' is already initialized # transformer = Box3dImageTransform(camera=camera) # transformer.initialize_box_from_annotation(box_obj, coordinate_system=CRS_V) # Project 3D vertices to 2D image coordinates vertices_2d = transformer.get_vertices_2d() for loc, pt in vertices_2d.items(): print(f" {loc}: ({pt[0]:.1f}, {pt[1]:.1f})") ``` -------------------------------- ### Evaluate Instance-Level Semantic Labeling Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Evaluates instance segmentation using Average Precision (AP) at IoU thresholds 0.5–0.95. Ensure CITYSCAPES_DATASET and CITYSCAPES_RESULTS environment variables are set. ```python import os, glob os.environ['CITYSCAPES_DATASET'] = '/data/cityscapes' os.environ['CITYSCAPES_RESULTS'] = '/data/cityscapes/results' from cityscapesscripts.evaluation.evalInstanceLevelSemanticLabeling import ( evaluateImgLists, args ) gt_files = sorted(glob.glob('/data/cityscapes/gtFine/val/*/*_gtFine_instanceIds.png')) pred_files = [] for gt in gt_files: from cityscapesscripts.helpers.csHelpers import getCsFileInfo cs = getCsFileInfo(gt) pred_txt = f'/data/cityscapes/results/{cs.city}_{cs.sequenceNb}_{cs.frameNb}_pred.txt' pred_files.append(pred_txt) # Prediction .txt format (one instance per line): # masks/frankfurt_000000_000294_pred_000.png 26 0.95 # masks/frankfurt_000000_000294_pred_001.png 24 0.88 args.quiet = False results = evaluateImgLists(pred_files, gt_files, args) print(f"AP : {results['averages']['allAp']:.3f}") print(f"AP50% : {results['averages']['allAp50%']:.3f}") for cls, scores in results['averages']['classes'].items(): print(f" {cls:<12s} AP={scores['ap']:.3f} AP50%={scores['ap50%']:.3f}") ``` -------------------------------- ### Evaluate Panoptic Segmentation Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Evaluates panoptic segmentation using the Panoptic Quality (PQ) metric. Requires ground truth prepared by createPanopticImgs.py. The script prints results and can also save them to a JSON file. ```python from cityscapesscripts.evaluation.evalPanopticSemanticLabeling import evaluatePanoptic results = evaluatePanoptic( gt_json_file = '/data/cityscapes/gtFine/cityscapes_panoptic_val.json', gt_folder = '/data/cityscapes/gtFine/cityscapes_panoptic_val', pred_json_file = '/data/cityscapes/results/cityscapes_panoptic_val.json', pred_folder = '/data/cityscapes/results/cityscapes_panoptic_val', resultsFile = 'resultPanopticSemanticLabeling.json' ) # Printed output: # Category | PQ SQ RQ # road | 84.2 90.1 93.5 # person | 56.3 79.4 70.9 # car | 60.1 83.2 72.2 # ───────────────────────────────────── # | PQ SQ RQ N # All | 59.8 80.3 72.1 19 # Things | 52.1 78.9 64.5 8 # Stuff | 65.4 81.4 78.2 11 print(results['All']['pq']) # 0.598 print(results['Things']['pq']) # 0.521 print(results['Stuff']['pq']) # 0.654 ``` ```bash csEvalPanopticSemanticLabeling \ --gt-json-file $CITYSCAPES_DATASET/gtFine/cityscapes_panoptic_val.json \ --prediction-json-file $CITYSCAPES_RESULTS/cityscapes_panoptic_val.json \ --results_file my_panoptic_results.json ``` -------------------------------- ### Print 3D Box Vertices in Different Coordinate Systems Source: https://github.com/mcordts/cityscapesscripts/blob/master/docs/Box3DImageTransform.ipynb Retrieves and prints the vertices of a 3D box in V, C, and S coordinate systems. Ensure `box_vertices_V`, `box_vertices_C`, and `box_vertices_S` are populated before use. ```python print("Vertices in V:") print(" {:>8} {:>8} {:>8}".format("x[m]", "y[m]", "z[m]")) for loc, coord in box_vertices_V.items(): print("{}: {:8.2f} {:8.2f} {:8.2f}".format(loc, coord[0], coord[1], coord[2])) # Print in C coordinate system print("\nVertices in C:") print(" {:>8} {:>8} {:>8}".format("x[m]", "y[m]", "z[m]")) for loc, coord in box_vertices_C.items(): print("{}: {:8.2f} {:8.2f} {:8.2f}".format(loc, coord[0], coord[1], coord[2])) # Print in S coordinate system print("\nVertices in S:") print(" {:>8} {:>8} {:>8}".format("x[m]", "y[m]", "z[m]")) for loc, coord in box_vertices_S.items(): print("{}: {:8.2f} {:8.2f} {:8.2f}".format(loc, coord[0], coord[1], coord[2])) ``` -------------------------------- ### Generate Amodal 2D Bounding Box Source: https://github.com/mcordts/cityscapesscripts/blob/master/docs/Box3DImageTransform.ipynb Calculates an amodal 2D bounding box from 2D image coordinates of a 3D box. Assumes `box_vertices_I` is populated. ```python # generate amodal 2D box from these values xmin = int(min([p[0] for p in box_vertices_I.values()])) ymin = int(min([p[1] for p in box_vertices_I.values()])) xmax = int(max([p[0] for p in box_vertices_I.values()])) ymax = int(max([p[1] for p in box_vertices_I.values()])) bbox_amodal = [xmin, ymin, xmax, ymax] print("Amodal 2D bounding box") print(bbox_amodal) # load from CsBbox3d object, these 2 bounding boxes should be the same print(obj.bbox_2d.bbox_amodal) assert bbox_amodal == obj.bbox_2d.bbox_amodal ``` -------------------------------- ### Serialize Python Dict to JSON File Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Write a Python dictionary to a formatted JSON file. Requires 'csHelpers' import. ```python from cityscapesscripts.helpers.csHelpers import writeDict2JSON results = {'mIoU': 0.823, 'mAP': 0.741} writeDict2JSON(results, '/tmp/my_results/summary.json') ``` -------------------------------- ### Read Cityscapes Polygon Annotations Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Read polygon annotations from a Cityscapes JSON file using the Annotation class and its subclasses. ```python from cityscapesscripts.helpers.annotation import ( Annotation, CsPoly, CsBbox2d, CsBbox3d, CsIgnore2d, CsObjectType, Point ) # --- Read polygon annotations (gtFine) --- ann = Annotation(objType=CsObjectType.POLY) ann.fromJsonFile('/data/cityscapes/gtFine/val/frankfurt/' 'frankfurt_000000_000294_gtFine_polygons.json') print(f"Image size: {ann.imgWidth}x{ann.imgHeight}") for obj in ann.objects: if obj.deleted: continue print(f" {obj.label:20s} vertices={len(obj.polygon)}") # car vertices=12 # person vertices=8 ``` -------------------------------- ### Evaluate 3D Object Detection Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Evaluates monocular 3D object detection using the Cityscapes 3D benchmark. Predictions are per-image JSON files. The evaluator matches 2D and 3D bounding boxes to compute AP across depth ranges. Ensure CITYSCAPES_DATASET and CITYSCAPES_RESULTS environment variables are set. ```json { "objects": [ { "2d": { "modal": [423, 310, 264, 192], "amodal": [418, 305, 274, 200] }, "3d": { "center": [15.3, -1.2, 0.8], "dimensions": [4.5, 1.9, 1.6], "rotation": [0.999, 0.0, 0.04, 0.0] }, "label": "car", "score": 0.95 } ] } ``` ```python import os os.environ['CITYSCAPES_DATASET'] = '/data/cityscapes' os.environ['CITYSCAPES_RESULTS'] = '/data/cityscapes/results' # CLI — discovers GT and predictions from env vars # csEvalObjectDetection3d # Or invoke programmatically: from cityscapesscripts.evaluation.evalObjectDetection3d import Box3dEvaluator from cityscapesscripts.evaluation.objectDetectionHelpers import EvaluationParameters eval_params = EvaluationParameters( labels_to_evaluate=['car', 'truck', 'bus', 'motorcycle', 'bicycle', 'person', 'rider'], max_depth=100.0, min_iou_to_match=0.5, matching_method='modal' ) evaluator = Box3dEvaluator(evaluation_params=eval_params) evaluator.load_gts(gt_folder='/data/cityscapes/gtBbox3d/val') evaluator.load_predictions(pred_folder='/data/cityscapes/results/gtBbox3d/val') evaluator.evaluate() evaluator.save_results('results_3d.json') ``` -------------------------------- ### Evaluate Pixel-Level Semantic Labeling Source: https://context7.com/mcordts/cityscapesscripts/llms.txt Evaluates semantic segmentation predictions against `gtFine` ground truth, computing per-class IoU and normalized IoU (nIoU). Prediction images must be grayscale PNGs with pixel values encoding the official class ID. Ensure `CITYSCAPES_DATASET` and `CITYSCAPES_RESULTS` environment variables are set. ```python import os, glob os.environ['CITYSCAPES_DATASET'] = '/data/cityscapes' os.environ['CITYSCAPES_RESULTS'] = '/data/cityscapes/results' from cityscapesscripts.evaluation.evalPixelLevelSemanticLabeling import ( evaluateImgLists, args ) # Gather ground truth and prediction file lists gt_files = sorted(glob.glob( '/data/cityscapes/gtFine/val/*/*_gtFine_labelIds.png' )) pred_files = [] for gt in gt_files: # Prediction filename pattern: __*.png from cityscapesscripts.helpers.csHelpers import getCsFileInfo cs = getCsFileInfo(gt) pred = f'/data/cityscapes/results/{cs.city}_{cs.sequenceNb}_{cs.frameNb}_pred.png' pred_files.append(pred) args.quiet = False args.JSONOutput = True # args.exportFile is auto-set to $CITYSCAPES_DATASET/evaluationResults/resultPixelLevelSemanticLabeling.json results = evaluateImgLists(pred_files, gt_files, args) print(f"mIoU : {results['averageScoreClasses']:.4f}") print(f"mIoU* : {results['averageScoreInstClasses']:.4f}") # classes IoU nIoU # -------------------------------- # road : 0.982 nan # sidewalk : 0.841 nan # person : 0.793 0.621 # car : 0.931 0.872 # -------------------------------- # Score Average : 0.823 0.712 ``` ```bash # CLI: # No-argument mode: auto-discovers val GT and results from env vars csEvalPixelLevelSemanticLabeling ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.