Implementation:CARLA simulator Carla Python SensorData Bindings
| Knowledge Sources | |
|---|---|
| Domains | Python Bindings, Sensor Data, Image Processing |
| Last Updated | 2026-02-15 05:00 GMT |
Overview
SensorData.cpp provides Boost.Python bindings for all CARLA sensor data types, including cameras, LiDAR, RADAR, GNSS, IMU, collision, lane invasion, and DVS events, along with image conversion utilities.
Description
This binding file exposes CARLA's sensor measurement types to Python with string representations and data access methods.
Stream Output Operators
Defines operator<< for all sensor data types within the carla::sensor::data namespace:
- Image -- Outputs frame, timestamp, and size (width x height).
- OpticalFlowImage -- Same format as Image.
- LidarMeasurement / SemanticLidarMeasurement -- Outputs frame, timestamp, and point count.
- CollisionEvent / ObstacleDetectionEvent -- Outputs frame, timestamp, and other actor reference.
- LaneInvasionEvent -- Outputs frame and timestamp.
- GnssMeasurement -- Outputs frame, timestamp, latitude, longitude, and altitude.
- IMUMeasurement -- Outputs frame, timestamp, accelerometer, gyroscope, and compass readings.
- RadarMeasurement -- Outputs frame, timestamp, and detection count.
- DVSEvent -- Outputs individual event with x, y, t, pol fields.
- DVSEventArray -- Outputs frame, timestamp, dimensions, and event count.
- RadarDetection -- Outputs velocity, azimuth, altitude, and depth.
- LidarDetection / SemanticLidarDetection -- Outputs point coordinates and additional fields.
Color Conversion
The EColorConverter enum provides conversion modes: Raw, Depth, LogarithmicDepth, and CityScapesPalette. The ConvertImage template function applies these conversions in-place with GIL released.
Raw Data Access
GetRawDataAsBuffer creates a Python memory view (or buffer in Python 2) over the sensor data's raw memory, enabling zero-copy access to sensor data from Python/NumPy.
Optical Flow Visualization
The ColorCodedFlow function converts OpticalFlowImage data to RGB color-coded visualization using HSV-to-RGB conversion, where hue represents flow direction and value represents flow magnitude.
Usage
These bindings are automatically available when importing the carla Python module. Sensor data objects are received via sensor callbacks and can be inspected, converted, and accessed as raw buffers for efficient processing with NumPy.
Code Reference
Source Location
- Repository: CARLA
- File:
PythonAPI/carla/src/SensorData.cpp(557 lines)
Signature
// Stream operators for sensor data types
namespace carla { namespace sensor { namespace data {
std::ostream &operator<<(std::ostream &out, const Image &image);
std::ostream &operator<<(std::ostream &out, const LidarMeasurement &meas);
std::ostream &operator<<(std::ostream &out, const CollisionEvent &meas);
std::ostream &operator<<(std::ostream &out, const GnssMeasurement &meas);
std::ostream &operator<<(std::ostream &out, const IMUMeasurement &meas);
std::ostream &operator<<(std::ostream &out, const RadarMeasurement &meas);
std::ostream &operator<<(std::ostream &out, const DVSEventArray &events);
// ... and more
}}}
enum class EColorConverter { Raw, Depth, LogarithmicDepth, CityScapesPalette };
template <typename T>
static auto GetRawDataAsBuffer(T &self);
template <typename T>
static void ConvertImage(T &self, EColorConverter cc);
static FakeImage ColorCodedFlow(carla::sensor::data::OpticalFlowImage& image);
Import
#include <PythonAPI.h>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| self | Sensor data type (Image, LidarMeasurement, etc.) | Yes | The sensor data object to process |
| cc | EColorConverter | No | Color conversion mode for image data |
Outputs
| Name | Type | Description |
|---|---|---|
| string representation | std::string | Human-readable representation of sensor data |
| raw buffer | Python memoryview | Zero-copy access to raw sensor data bytes |
| converted image | FakeImage | RGB color-coded optical flow visualization |
Usage Examples
import carla
import numpy as np
# Sensor callback receiving image data
def camera_callback(image):
# Print sensor data info
print(image) # "Image(frame=42, timestamp=3.14, size=800x600)"
# Convert to depth visualization
image.convert(carla.ColorConverter.LogarithmicDepth)
# Access raw data as numpy array
array = np.frombuffer(image.raw_data, dtype=np.uint8)
array = array.reshape((image.height, image.width, 4))
# LiDAR callback
def lidar_callback(point_cloud):
print(point_cloud) # "LidarMeasurement(frame=42, ...)"
points = np.frombuffer(point_cloud.raw_data, dtype=np.float32)
Related Pages
- Environment:CARLA_simulator_Carla_Simulation_Runtime
- Implementation:CARLA_simulator_Carla_PythonAPI_Header - Master header included by this file
- Implementation:CARLA_simulator_Carla_ServerSideSensor - Server-side sensor that produces this data