cityscapesscripts

repository·master·Indexed 25 days ago

https://github.com/mcordts/cityscapesscripts

A collection of Python scripts for inspecting, preparing, and evaluating the Cityscapes dataset of street scene video sequences. The library provides command-line tools for downloading data, visualization (csViewer, csLabelTool), and evaluation of pixel-level, instance-level, and panoptic semantic labeling, as well as 3D object detection. It includes utilities for converting polygons to PNG images and a Box3dImageTransform class for projecting 3D box annotations between vehicle, camera, and image coordinate systems.

Tokens
2.2K
Snippets
4
Records
13
Agent score
31%

What's inside cityscapesscripts

  1. Understand the Cityscapes dataset folder structure

    master

    The dataset follows a specific hierarchical structure:

    {root}/{type}{video}/{split}/{city}/{city}_{seq:0>6}_{frame:0>6}_{type}{ext}

    Key Components:

    • root: The base directory (can be set via CITYSCAPES_DATASET env var).
    • type: The data modality (e.g., gtFine, leftImg8bit).
    • split: The dataset split (e.g., train, val, test, train_extra, demoVideo).
    • city: The recording city.
    • seq: 6-digit sequence number.
    • frame: 6-digit frame number.
    • ext: File extension (e.g., .png, _polygons.json).
  2. Configure the Cityscapes dataset root directory

    master
    Many scripts in this package check for an environment variable named CITYSCAPES_DATASET. Setting this variable to the path of your Cityscapes dataset folder will allow scripts to use it as the default data source.
  3. Install cityscapesscripts

    master

    Install the core library using pip:

    python -m pip install cityscapesscripts

    If you need the graphical tools (the image viewer and the labeling tool), which are based on Qt5, install the GUI extra:

    python -m pip install cityscapesscripts[gui]
    python -m pip install cityscapesscripts
  4. Submit results to the Cityscapes evaluation server

    master

    To test your method on the official test set, run your approach on the provided test images and submit your results to the Submission Page.

    Result formats are defined within the specific evaluation scripts:

    • cityscapesscripts/evaluation/evalPixelLevelSemanticLabeling.py
    • cityscapesscripts/evaluation/evalInstanceLevelSemanticLabeling.py
    • cityscapesscripts/evaluation/evalPanopticSemanticLabeling.py
    • cityscapesscripts/evaluation/evalObjectDetection3d.py
  5. Enable Cython for faster evaluation

    master

    To enable the Cython plugin for faster evaluation (tested on Ubuntu), run the following command in the package directory:

    CYTHONIZE_EVAL= python setup.py build_ext --inplace
    CYTHONIZE_EVAL= python setup.py build_ext --inplace
  6. Understand 3D Box coordinate systems

    master

    The Cityscapes 3D annotations use four distinct coordinate systems for 3D boxes:

    1. Vehicle Coordinate System (V): Follows ISO 8855. Origin is on the ground below the rear axis center. $x$ points in the driving direction, $y$ points left, and $z$ points up. All Ground Truth (GT) annotations are provided in this system.
    2. Camera Coordinate System (C): Origin is at the camera's optical center, with the same orientation as $V$.
    3. Image Coordinate System (I): Origin is at the top-left image pixel. $u$ points right and $v$ points down.
    4. Coordinate System (S): Shares the same origin as $C$, but uses the orientation of $I$ ($x$ points right, $y$ points down, and $z$ points in the driving direction).

    When working with Box3dImageTransform, note that the object's size is always provided as [L, W, H] regardless of the coordinate system used.

  7. Transform 3D boxes using Box3dImageTransform

    master

    The Box3dImageTransform class is the primary tool for converting 3D box annotations between coordinate systems and projecting them into 2D image space.

    Workflow:

    1. Create a Box3dImageTransform instance using a Camera object.
    2. Load the 3D annotation into a CsBbox3d object.
    3. Initialize the transform object using initialize_box_from_annotation specifying the source coordinate system (usually CRS_V).
    4. Use get_vertices or get_parameters to retrieve data in target coordinate systems (CRS_V, CRS_C, or CRS_S).
    5. Use get_vertices_2d() to project the box into the image coordinate system ($I$).
  8. Reference available dataset splits

    master

    The following split values define the data subsets:

    SplitDescription
    trainTraining set (2975 images with fine/coarse annotations).
    valValidation set (500 images with fine/coarse annotations).
    testTest set for the evaluation server (annotations are private).
    train_extraOptional training set (19998 images with coarse annotations).
    demoVideoVideo sequences for qualitative evaluation (no annotations).
  9. Reference available data types (modality)

    master

    The following type values are used in the dataset path to distinguish between different data modalities:

    TypeDescription
    gtFineFine annotations (2975 train, 500 val, 1525 test). Uses json polygons and png label images.
    gtCoarseCoarse annotations for training/validation.
    gtBbox3d3D bounding box annotations for vehicles.
    gtBboxCityPersonsPedestrian bounding box annotations (x, y, w, h).
    leftImg8bitStandard 8-bit LDR left images.
    leftImg8bit_blurred8-bit LDR left images with faces/license plates blurred (use for visualization).
    leftImg16bit16-bit HDR left images (non-standard 16-bit PNGs).
    rightImg8bit8-bit LDR right stereo images.
    rightImg16bit16-bit HDR right stereo images.
    timestampRecording time in nanoseconds.
    disparityPrecomputed disparity depth maps (16-bit PNGs). Formula: d = (float(p) - 1.) / 256. where p > 0.
    cameraCamera calibration data.
    vehicleVehicle odometry, GPS, and temperature.
  10. Use Cityscapes command-line tools

    master

    Once installed, the following tools are exposed as command-line utilities:

    Download and Visualization

    • csDownload: Download Cityscapes packages.
    • csViewer: View images with annotation overlays.
    • csLabelTool: Labeling tool.

    Evaluation

    • csEvalPixelLevelSemanticLabeling: Evaluate pixel-level semantic labeling (val/test sets).
    • csEvalInstanceLevelSemanticLabeling: Evaluate instance-level semantic labeling (val/test sets).
    • csEvalPanopticSemanticLabeling: Evaluate panoptic segmentation (val/test sets).
    • csEvalObjectDetection3d: Evaluate 3D object detection (val/test sets).

    Data Preparation

    • csCreateTrainIdLabelImgs: Convert polygons to PNG images with train IDs (defined in helpers/labels.py).
    • csCreateTrainIdInstanceImgs: Convert polygons to PNG images with instance IDs.
    • csCreatePanopticImgs: Convert PNG annotations to COCO panoptic format.
    • csPlot3dDetectionResults: Visualize 3D detection results from .json files.
  11. Get 3D box vertices and parameters

    master

    You can retrieve the geometric properties of a 3D box in any supported coordinate system using get_vertices(coordinate_system=...) and get_parameters(coordinate_system=...).

    Supported Coordinate Systems:

    • CRS_V: Vehicle coordinate system.
    • CRS_C: Camera coordinate system.
    • CRS_S: Coordinate system with camera origin but image orientation.

    Vertex Location Encoding: Vertices are returned in a dictionary where keys are 3-character codes representing the corner location:

    • 0: Back (B) or Front (F)
    • 1: Left (L) or Right (R)
    • 2: Bottom (B) or Top (T) Example: BLT refers to the Back-Left-Top corner.
  12. Project 3D boxes to 2D image coordinates

    master

    To find the 2D projection of a 3D box (useful for generating amodal 2D bounding boxes), use the get_vertices_2d() method. This returns a dictionary of vertices in the image coordinate system ($I$), where keys are the 3-character location codes (e.g., BLT) and values are [u, v] pixel coordinates.

    # Returns vertices in image coordinate system (u, v)
    box_vertices_I = box3d_annotation.get_vertices_2d()
    
    # Example: calculating an amodal 2D bounding box from projected vertices
    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]