Skip to main content
Camera parts capture image data from various camera hardware including Raspberry Pi cameras, webcams, CSI cameras, and mock cameras for testing.

Base Classes

BaseCamera

Base class for all camera implementations. Methods:
run_threaded
() -> np.ndarray
Returns the most recently captured frame

Camera Implementations

PiCamera

Raspberry Pi camera using the Picamera2 library (Bullseye and later). Constructor:
PiCamera(image_w=160, image_h=120, image_d=3, vflip=False, hflip=False)
image_w
int
default:"160"
Image width in pixels
image_h
int
default:"120"
Image height in pixels
image_d
int
default:"3"
Image depth/channels (3 for RGB, 1 for grayscale)
vflip
bool
default:"false"
Flip image vertically
hflip
bool
default:"false"
Flip image horizontally
Methods:
run
() -> np.ndarray
Captures and returns a new frame from the camera buffer
update
() -> None
Continuously captures frames in a threaded loop
shutdown
() -> None
Stops the camera and releases resources
Usage Example:
from donkeycar.parts.camera import PiCamera

# Create camera with custom resolution
cam = PiCamera(image_w=160, image_h=120, vflip=True)

# Get a frame
frame = cam.run()

# For threaded mode
import threading
thread = threading.Thread(target=cam.update)
thread.start()
frame = cam.run_threaded()

Webcam

USB webcam using pygame camera interface. Constructor:
Webcam(image_w=160, image_h=120, image_d=3, framerate=20, camera_index=0)
image_w
int
default:"160"
Image width in pixels
image_h
int
default:"120"
Image height in pixels
image_d
int
default:"3"
Image depth/channels (3 for RGB, 1 for grayscale)
framerate
int
default:"20"
Target framerate in Hz
camera_index
int
default:"0"
Index of the camera device to use
Installation:
sudo apt-get install libsdl2-mixer-2.0-0 libsdl2-image-2.0-0 libsdl2-2.0-0
pip install pygame
Methods:
run
() -> np.ndarray
Captures and returns a new frame
update
() -> None
Continuously captures frames at the specified framerate
shutdown
() -> None
Stops the camera and releases resources

CSICamera

Jetson Nano CSI camera using GStreamer. Constructor:
CSICamera(image_w=160, image_h=120, image_d=3, capture_width=3280, 
          capture_height=2464, framerate=60, gstreamer_flip=0)
image_w
int
default:"160"
Output image width in pixels
image_h
int
default:"120"
Output image height in pixels
image_d
int
default:"3"
Image depth/channels
capture_width
int
default:"3280"
Sensor capture width
capture_height
int
default:"2464"
Sensor capture height
framerate
int
default:"60"
Camera framerate
gstreamer_flip
int
default:"0"
Flip method: 0=none, 1=rotate CCW 90°, 2=flip vertical, 3=rotate CW 90°
Methods:
run
() -> np.ndarray
Captures and returns a new frame
update
() -> None
Continuously captures frames in a threaded loop
shutdown
() -> None
Stops the camera and releases resources

V4LCamera

Video4Linux camera using v4l2capture library. Constructor:
V4LCamera(image_w=160, image_h=120, image_d=3, framerate=20,
          dev_fn="/dev/video0", fourcc='MJPG')
image_w
int
default:"160"
Image width in pixels
image_h
int
default:"120"
Image height in pixels
dev_fn
str
default:"/dev/video0"
Video device path
fourcc
str
default:"MJPG"
Video format FourCC code
Installation:
sudo apt-get install libv4l-dev
pip install python3-v4l2capture

Mock/Testing Cameras

MockCamera

Returns a static image for testing. Constructor:
MockCamera(image_w=160, image_h=120, image_d=3, image=None)
image
np.ndarray
default:"None"
Optional image array to use. If None, creates a blank image.

ImageListCamera

Returns images from a directory in sequence. Constructor:
ImageListCamera(path_mask='~/mycar/data/**/images/*.jpg')
path_mask
str
Glob pattern for image files
Methods:
run_threaded
() -> np.ndarray
Returns the next image in the sequence

Configuration

Cameras are typically configured in myconfig.py:
# Camera settings
CAMERA_TYPE = "PICAM"  # PICAM, WEBCAM, CVCAM, CSIC, etc.
IMAGE_W = 160
IMAGE_H = 120
IMAGE_DEPTH = 3
CAMERA_FRAMERATE = 20
CAMERA_VFLIP = False
CAMERA_HFLIP = False

Integration Example

From templates like complete.py:
if cfg.CAMERA_TYPE == "PICAM":
    from donkeycar.parts.camera import PiCamera
    cam = PiCamera(image_w=cfg.IMAGE_W, image_h=cfg.IMAGE_H,
                   image_d=cfg.IMAGE_DEPTH, vflip=cfg.CAMERA_VFLIP)
elif cfg.CAMERA_TYPE == "WEBCAM":
    from donkeycar.parts.camera import Webcam
    cam = Webcam(image_w=cfg.IMAGE_W, image_h=cfg.IMAGE_H,
                 image_d=cfg.IMAGE_DEPTH)

V.add(cam, outputs=['cam/image_array'], threaded=True)

Build docs developers (and LLMs) love