camera.pi

class platypush.plugins.camera.pi.CameraPiPlugin(device: int = 0, fps: float = 30.0, warmup_seconds: float = 2.0, sharpness: int = 0, contrast: int = 0, brightness: int = 50, video_stabilization: bool = False, iso: int = 0, exposure_compensation: int = 0, exposure_mode: str = 'auto', meter_mode: str = 'average', awb_mode: str = 'auto', image_effect: str = 'none', led_pin: Optional[int] = None, color_effects: Optional[Union[str, List[str]]] = None, zoom: Tuple[float, float, float, float] = (0.0, 0.0, 1.0, 1.0), **camera)[source]

Plugin to control a Pi camera.

Requires:

  • picamera (pip install picamera)

  • numpy (pip install numpy)

  • Pillow (pip install Pillow)

__init__(device: int = 0, fps: float = 30.0, warmup_seconds: float = 2.0, sharpness: int = 0, contrast: int = 0, brightness: int = 50, video_stabilization: bool = False, iso: int = 0, exposure_compensation: int = 0, exposure_mode: str = 'auto', meter_mode: str = 'average', awb_mode: str = 'auto', image_effect: str = 'none', led_pin: Optional[int] = None, color_effects: Optional[Union[str, List[str]]] = None, zoom: Tuple[float, float, float, float] = (0.0, 0.0, 1.0, 1.0), **camera)[source]

See https://www.raspberrypi.org/documentation/usage/camera/python/README.md for a detailed reference about the Pi camera options.

Parameters

camera – Options for the base camera plugin (see platypush.plugins.camera.CameraPlugin).

capture_frame(camera: platypush.plugins.camera.model.camera.Camera, *args, **kwargs)[source]

Capture a frame from a device using the plugin-specific logic - to be implemented by the derived classes.

Parameters

device – An initialized platypush.plugins.camera.Camera object.

capture_image(image_file: str, preview: bool = False, **camera) str

Capture an image.

Parameters
  • image_file – Path where the output image will be stored.

  • camera – Camera parameters override - see constructors parameters.

  • preview – Show a preview of the camera frames.

Returns

The local path to the saved image.

capture_preview(duration: Optional[float] = None, n_frames: Optional[int] = None, **camera) dict[source]

Start a camera preview session.

Parameters
  • duration – Preview duration (default: until stop_capture() is called).

  • n_frames – Number of frames to display before closing (default: until stop_capture() is called).

  • camera – Camera object properties.

Returns

The status of the device.

capture_sequence(duration: Optional[float] = None, n_frames: Optional[int] = None, preview: bool = False, **camera) str

Capture a sequence of frames from a camera and store them to a directory.

Parameters
  • duration – Duration of the sequence in seconds (default: until stop_capture() is called).

  • n_frames – Number of images to be captured (default: until stop_capture() is called).

  • camera – Camera parameters override - see constructors parameters. frames_dir and fps in particular can be specifically tuned for capture_sequence.

  • preview – Show a preview of the camera frames.

Returns

The directory where the image files have been stored.

capture_video(duration: Optional[float] = None, video_file: Optional[str] = None, preview: bool = False, **camera) Union[str, dict]

Capture a video.

Parameters
  • duration – Record duration in seconds (default: None, record until stop_capture).

  • video_file – If set, the stream will be recorded to the specified video file (default: None).

  • camera – Camera parameters override - see constructors parameters.

  • preview – Show a preview of the camera frames.

Returns

If duration is specified, the method will wait until the recording is done and return the local path to the recorded resource. Otherwise, it will return the status of the camera device after starting it.

capturing_thread(camera: platypush.plugins.camera.model.camera.Camera, duration: Optional[float] = None, video_file: Optional[str] = None, image_file: Optional[str] = None, n_frames: Optional[int] = None, preview: bool = False, **kwargs)

Camera capturing thread.

Parameters
  • camera – An initialized platypush.plugins.camera.Camera object.

  • duration – Capturing session duration in seconds (default: until stop_capture() is called).

  • video_file – If set, the session will be recorded to this output video file (video capture mode).

  • image_file – If set, the output of the session will be a single image file (photo mode).

  • n_frames – Number of frames to be captured (default: until stop_capture() is called).

  • preview – Start a preview window.

  • kwargs – Extra arguments to be passed to capture_frame().

close_device(camera: platypush.plugins.camera.model.camera.Camera, wait_capture: bool = True) None

Close and release a device.

static encode_frame(frame, encoding: str = 'jpeg') bytes

Encode a frame to a target type. The default implementation assumes that frame is a PIL.Image object.

Parameters
  • frame – Image frame (default: a PIL.Image object).

  • encoding – Image encoding (e.g. jpeg).

static flip_frame(frame, horizontal_flip: bool = False, vertical_flip: bool = False)

Frame flip logic. Does nothing unless implemented by a derived plugin.

Parameters
  • frame – Image frame (default: a PIL.Image object).

  • horizontal_flip – Flip along the horizontal axis.

  • vertical_flip – Flip along the vertical axis.

open(device: Optional[Union[str, int]] = None, stream: bool = None, **info) platypush.plugins.camera.model.camera.Camera

Initialize and open a device using a context manager pattern.

Parameters
  • device – Capture device by name, path or ID.

  • stream – If set, the frames will be streamed to camera.stream.

  • info – Camera parameters override - see constructors parameters.

Returns

The initialized platypush.plugins.camera.Camera object.

open_device(device: Optional[Union[str, int]] = None, stream: bool = False, **params) platypush.plugins.camera.model.camera.Camera

Initialize and open a device.

Returns

The initialized camera device.

Raises

platypush.plugins.camera.CaptureSessionAlreadyRunningException

prepare_device(device: platypush.plugins.camera.pi.model.PiCamera)[source]

Prepare a device using the plugin-specific logic - to be implemented by the derived classes.

Parameters

device – An initialized platypush.plugins.camera.Camera object.

release_device(device: platypush.plugins.camera.pi.model.PiCamera)[source]

Release a device using the plugin-specific logic - to be implemented by the derived classes.

Parameters

device – An initialized platypush.plugins.camera.Camera object.

static rotate_frame(frame, rotation: Optional[Union[float, int]] = None)

Frame rotation logic. The default implementation assumes that frame is a PIL.Image object.

Parameters
  • frame – Image frame (default: a PIL.Image object).

  • rotation – Rotation angle in degrees.

static scale_frame(frame, scale_x: Optional[float] = None, scale_y: Optional[float] = None)

Frame scaling logic. The default implementation assumes that frame is a PIL.Image object.

Parameters
  • frame – Image frame (default: a PIL.Image object).

  • scale_x – X-scale factor.

  • scale_y – Y-scale factor.

start_camera(camera: platypush.plugins.camera.model.camera.Camera, preview: bool = False, *args, **kwargs)

Start a camera capture session.

Parameters
  • camera – An initialized platypush.plugins.camera.Camera object.

  • preview – Show a preview of the camera frames.

start_preview(camera: platypush.plugins.camera.model.camera.Camera)[source]

Start camera preview.

start_streaming(duration: Optional[float] = None, stream_format: str = 'h264', **camera) dict[source]

Expose the video stream of a camera over a TCP connection.

Parameters
  • duration – Streaming thread duration (default: until stop_streaming() is called).

  • stream_format – Format of the output stream - e.g. h264, mjpeg, mkv etc. (default: mkv).

  • camera – Camera object properties - see constructor parameters.

Returns

The status of the device.

status(device: Optional[Union[str, int]] = None)

Returns the status of the specified camera or all the active cameras if device is None.

stop_capture(device: Optional[Union[str, int]] = None)

Stop any capturing session on the specified device.

Parameters

device – Name/path/ID of the device to stop (default: all the active devices).

stop_preview(camera: platypush.plugins.camera.model.camera.Camera)[source]

Stop camera preview.

stop_streaming(device: Optional[Union[str, int]] = None)

Stop a camera over TCP session.

Parameters

device – Name/path/ID of the device to stop (default: all the active devices).

static store_frame(frame, filepath: str, format: Optional[str] = None)

Capture a frame to the filesystem using the PIL library - it can be overridden by derived classes.

Parameters
  • frame – Frame object (default: a byte-encoded object or a PIL.Image object).

  • filepath – Destination file.

  • format – Output format.

take_picture(image_file: str, preview: bool = False, **camera) str

Alias for capture_image().

Parameters
  • image_file – Path where the output image will be stored.

  • camera – Camera parameters override - see constructors parameters.

  • preview – Show a preview of the camera frames.

Returns

The local path to the saved image.

to_grayscale(frame)

Convert a frame to grayscale. The default implementation assumes that frame is a PIL.Image object.

Parameters

frame – Image frame (default: a PIL.Image object).

static transform_frame(frame, color_transform)

Frame color space (e.g. RGB24, YUV etc.) transform logic. Does nothing unless implemented by a derived plugin.

wait_capture(camera: platypush.plugins.camera.model.camera.Camera) None

Wait until a capture session terminates.

Parameters

camera – Camera object. camera.info.capture_timeout is used as a capture thread termination timeout if set.