Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CARLA simulator Carla Draw Skeleton Example

From Leeroopedia
Knowledge Sources
Domains Simulation, Visualization, Computer Vision
Last Updated 2026-02-15 05:00 GMT

Overview

Example script that visualizes pedestrian skeletal bone positions projected onto an RGB camera image in real-time using pygame, demonstrating 3D-to-2D projection of walker bone data.

Description

This script (draw_skeleton.py) demonstrates how to extract pedestrian bone positions from the CARLA simulation and project them onto a 2D camera view. It uses the CarlaSyncMode context manager to synchronize sensor output, building a camera projection matrix with build_projection_matrix() using the camera's field of view. The get_screen_points() function transforms 3D bone positions from world coordinates to camera coordinates using a 4x4 inverse transformation matrix, then projects them to 2D screen coordinates using the camera intrinsic matrix K. The get_image_as_array() function converts raw camera data to numpy arrays for rendering. The script spawns a pedestrian and an RGB camera, then continuously renders the camera feed with overlaid skeleton points using pygame. Key helper functions include draw_image() for pygame surface rendering and get_font() for text display.

Usage

Use this example to learn how to work with CARLA's walker bone system, project 3D coordinates onto 2D camera views, and implement synchronized multi-sensor rendering with pygame.

Code Reference

Source Location

  • Repository: CARLA
  • File: PythonAPI/examples/draw_skeleton.py

Signature

class CarlaSyncMode(object):
    """Context manager for synchronous sensor output."""
    def __init__(self, world, *sensors, **kwargs): ...
    def __enter__(self): ...
    def tick(self, timeout): ...
    def __exit__(self, *args, **kwargs): ...

def build_projection_matrix(w, h, fov):
    """Build camera intrinsic matrix K from image dimensions and FOV."""

def get_image_as_array(image):
    """Convert carla.Image raw_data to numpy RGB array."""

def draw_image(surface, array, blend=False):
    """Render numpy array to pygame surface."""

def get_font():
    """Get pygame font for text rendering."""

def get_screen_points(camera, K, image_w, image_h, points3d):
    """Project 3D world points to 2D screen coordinates."""

Import

# Run as a command-line tool:
python draw_skeleton.py
# Requires: pygame, numpy, PIL

I/O Contract

Input Type Description
CARLA simulation Server connection Running CARLA server with pedestrians
Camera sensor carla.Sensor RGB camera attached to spectator/vehicle
Walker bones 3D coordinates Pedestrian skeletal bone positions
Output Type Description
Pygame window Visual display RGB camera view with skeleton overlay
Projected points 2D screen coordinates Bone positions mapped to image pixels

Usage Examples

# Run the skeleton visualization
python draw_skeleton.py

# The script internally uses CarlaSyncMode:
with CarlaSyncMode(world, camera, fps=20) as sync_mode:
    while True:
        data = sync_mode.tick(timeout=1.0)
        # Process camera image and bone positions
        image = get_image_as_array(data[1])
        screen_points = get_screen_points(camera, K, w, h, bone_positions)
        draw_image(display, image)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment