camera.pi.legacy#

Description#

Plugin to interact with a Pi Camera.

Warning

This plugin is DEPRECATED, as it relies on the old picamera module.

The picamera module used in this plugin is deprecated and no longer maintained. The picamera2 module is advised instead, which is used by platypush.plugins.camera.pi.CameraPiPlugin.

You may want to use this plugin if you are running an old OS that does not support the new picamera2 module. Even in that case, you may probably consider using platypush.plugins.camera.ffmpeg.FfmpegCameraPlugin or platypush.plugins.camera.gstreamer.GStreamerCameraPlugin, as picamera is not maintained anymore and may not work properly.

param camera:

Options for the base camera plugin (see CameraPlugin).

Configuration#

camera.pi.legacy:
  # [Optional]
  # Camera device number (default: 0). Only supported on
  # devices with multiple camera slots.
  # device: 0  # type=int

  # [Optional]
  # Frames per second (default: 30.0).
  # fps: 30.0  # type=float

  # [Optional]
  # Seconds to wait for the camera to warm up
  # before taking a photo (default: 2.0).
  # warmup_seconds: 2.0  # type=float

  # [Optional]
  # Sharpness level, as an integer between -100 and 100.
  # sharpness: 0  # type=int

  # [Optional]
  # Contrast level, as an integer between -100 and 100.
  # contrast: 0  # type=int

  # [Optional]
  # Brightness level, as an integer between 0 and 100.
  # brightness: 50  # type=int

  # [Optional]
  # Enable video stabilization (default: False).
  # video_stabilization: False  # type=bool

  # [Optional]
  # ISO level (default: 0).
  # iso: 0  # type=int

  # [Optional]
  # Exposure compensation level, as an
  # integer between -25 and 25.
  # exposure_compensation: 0  # type=int

  # [Optional]
  # Exposure mode. Allowed values:
  #
  # - ``off``
  # - ``auto`` (default)
  # - ``night``
  # - ``nightpreview``
  # - ``backlight``
  # - ``spotlight``
  # - ``sports``
  # - ``snow``
  # - ``beach``
  # - ``verylong``
  # - ``fixedfps``
  # - ``antishake``
  # - ``fireworks``
  # exposure_mode: auto  # type=str

  # [Optional]
  # Metering mode used for the exposure. Allowed values:
  #
  # - ``average`` (default)
  # - ``spot``
  # - ``backlit``
  # - ``matrix``
  # meter_mode: average  # type=str

  # [Optional]
  # Auto white balance mode. Allowed values:
  #
  # - ``off``
  # - ``auto`` (default)
  # - ``sunlight``
  # - ``cloudy``
  # - ``shade``
  # - ``tungsten``
  # - ``fluorescent``
  # - ``incandescent``
  # - ``flash``
  # - ``horizon``
  # awb_mode: auto  # type=str

  # [Optional]
  # Image effect applied to the camera. Allowed values:
  #
  # - ``none`` (default)
  # - ``negative``
  # - ``solarize``
  # - ``sketch``
  # - ``denoise``
  # - ``emboss``
  # - ``oilpaint``
  # - ``hatch``
  # - ``gpen``
  # - ``pastel``
  # - ``watercolor``
  # - ``film``
  # - ``blur``
  # - ``saturation``
  # - ``colorswap``
  # - ``washedout``
  # - ``posterise``
  # - ``colorpoint``
  # - ``colorbalance``
  # - ``cartoon``
  # - ``deinterlace1``
  # - ``deinterlace2``
  # image_effect: none  # type=str

  # [Optional]
  # LED PIN number, if the camera LED is wired to a GPIO
  # PIN and you want to control it.
  # led_pin:   # type=Optional[int]

  # [Optional]

  # color_effects:   # type=Union[str, List[str], NoneType]

  # [Optional]
  # Camera zoom, in the format ``(x, y, width, height)``
  # (default: ``(0.0, 0.0, 1.0, 1.0)``).
  # zoom: (0.0, 0.0, 1.0, 1.0)  # type=Tuple[float, float, float, float]

  # [Optional]
  # Default format for the output when streamed to a
  # network device. Available:
  #
  #     - ``h264`` (default)
  #     - ``mjpeg``
  # stream_format: h264  # type=str

  # [Optional]
  # Default resolution, as a tuple of two integers.
  # resolution: (640, 480)  # type=Tuple[int, int]

  # [Optional]
  # Directory where the camera frames will be stored
  # (default: ``~/.local/share/platypush/<plugin.name>/frames``)
  # frames_dir:   # type=Optional[str]

  # [Optional]
  # Cameras usually take a while to adapt their
  # luminosity and focus to the environment when taking a picture.
  # This parameter allows you to specify the number of "warmup" frames
  # to capture upon picture command before actually capturing a frame
  # (default: 5 but you may want to calibrate this parameter for your
  # camera)
  # warmup_frames: 5  # type=int

  # [Optional]
  # Maximum number of seconds to wait between the
  # programmed termination of a capture session and the moment the
  # device is released.
  # capture_timeout: 20.0  # type=Optional[float]

  # [Optional]
  # If set, the images will be scaled along the x-axis by
  # the specified factor
  # scale_x:   # type=Optional[float]

  # [Optional]
  # If set, the images will be scaled along the y-axis by
  # the specified factor
  # scale_y:   # type=Optional[float]

  # [Optional]
  # If set, the images will be rotated by the specified
  # number of degrees
  # rotate:   # type=Optional[float]

  # [Optional]
  # Whether the output should be converted to grayscale.
  # grayscale:   # type=Optional[bool]

  # [Optional]
  # Color transformation to apply to the images.
  # color_transform:   # type=Union[str, int, NoneType]

  # [Optional]
  # If set, the images will be flipped on the
  # horizontal axis.
  # horizontal_flip: False  # type=bool

  # [Optional]
  # If set, the images will be flipped on the vertical
  # axis.
  # vertical_flip: False  # type=bool

  # [Optional]
  # Plugin-specific format/type for the input stream.
  # input_format:   # type=Optional[str]

  # [Optional]
  # Plugin-specific format/type for the output videos.
  # output_format:   # type=Optional[str]

  # [Optional]
  # Default port to be used for streaming over TCP
  # (default: 5000).
  # listen_port: 5000  # type=Optional[int]

  # [Optional]
  # Default bind address for TCP streaming (default:
  # 0.0.0.0, accept connections on any network interface).
  # bind_address: 0.0.0.0  # type=str

  # [Optional]
  # If set, the camera will start streaming on the
  # specified ``bind_address`` and ``listen_port`` as soon as the plugin
  # is started. Otherwise, the stream will be started only when the
  # `CameraPlugin.start_streaming <https://docs.platypush.tech/platypush/plugins/camera.html#platypush.plugins.camera.CameraPlugin.start_streaming>`_ method is called. Default: False.
  # stream_on_start: False  # type=bool

  # [Optional]
  # Path to the ffmpeg binary (default: ``ffmpeg``).
  # ffmpeg_bin: ffmpeg  # type=str

  # [Optional]
  # Specify the ffmpeg video codec (``-vcodec``) used
  # for the input.
  # input_codec:   # type=Optional[str]

  # [Optional]
  # Specify the ffmpeg video codec (``-vcodec``) to be
  # used for encoding the output. For some ffmpeg output formats (e.g.
  # ``h264`` and ``rtp``) this may default to ``libxvid``.
  # output_codec:   # type=Optional[str]

  # [Optional]
  # How often the `RunnablePlugin.loop <https://docs.platypush.tech/platypush/plugins/.html#platypush.plugins.RunnablePlugin.loop>`_ function should be
  # executed (default: 15 seconds). *NOTE*: For back-compatibility
  # reasons, the `poll_seconds` argument is also supported, but it's
  # deprecated.
  # poll_interval: 15  # type=Optional[float]

  # [Optional]
  # How long we should wait for any running
  # threads/processes to stop before exiting (default: 5 seconds).
  # stop_timeout: 5  # type=Optional[float]

  # [Optional]
  # If set to True then the plugin will not monitor
  # for new events. This is useful if you want to run a plugin in
  # stateless mode and only leverage its actions, without triggering any
  # events. Defaults to False.
  # disable_monitor: False  # type=bool

Dependencies#

pip

pip install numpy picamera Pillow

Alpine

apk add py3-pillow ffmpeg py3-numpy

Debian

apt install python3-pillow python3-numpy ffmpeg

Fedora

yum install python-pillow python-numpy ffmpeg

Arch Linux

pacman -S python-pillow python-numpy ffmpeg

Actions#

Module reference#

class platypush.plugins.camera.pi.legacy.CameraPiLegacyPlugin(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: int | None = None, color_effects: str | List[str] | None = None, zoom: Tuple[float, float, float, float] = (0.0, 0.0, 1.0, 1.0), stream_format: str = 'h264', **camera)[source]#

Bases: CameraPlugin

Plugin to interact with a Pi Camera.

Warning

This plugin is DEPRECATED, as it relies on the old picamera module.

The picamera module used in this plugin is deprecated and no longer maintained. The picamera2 module is advised instead, which is used by platypush.plugins.camera.pi.CameraPiPlugin.

You may want to use this plugin if you are running an old OS that does not support the new picamera2 module. Even in that case, you may probably consider using platypush.plugins.camera.ffmpeg.FfmpegCameraPlugin or platypush.plugins.camera.gstreamer.GStreamerCameraPlugin, as picamera is not maintained anymore and may not work properly.

__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: int | None = None, color_effects: str | List[str] | None = None, zoom: Tuple[float, float, float, float] = (0.0, 0.0, 1.0, 1.0), stream_format: str = 'h264', **camera)[source]#
Parameters:
  • device – Camera device number (default: 0). Only supported on devices with multiple camera slots.

  • fps – Frames per second (default: 30.0).

  • warmup_seconds – Seconds to wait for the camera to warm up before taking a photo (default: 2.0).

  • sharpness – Sharpness level, as an integer between -100 and 100.

  • contrast – Contrast level, as an integer between -100 and 100.

  • brightness – Brightness level, as an integer between 0 and 100.

  • video_stabilization – Enable video stabilization (default: False).

  • iso – ISO level (default: 0).

  • exposure_compensation – Exposure compensation level, as an integer between -25 and 25.

  • exposure_mode

    Exposure mode. Allowed values:

    • off

    • auto (default)

    • night

    • nightpreview

    • backlight

    • spotlight

    • sports

    • snow

    • beach

    • verylong

    • fixedfps

    • antishake

    • fireworks

  • meter_mode

    Metering mode used for the exposure. Allowed values:

    • average (default)

    • spot

    • backlit

    • matrix

  • awb_mode

    Auto white balance mode. Allowed values:

    • off

    • auto (default)

    • sunlight

    • cloudy

    • shade

    • tungsten

    • fluorescent

    • incandescent

    • flash

    • horizon

  • image_effect

    Image effect applied to the camera. Allowed values:

    • none (default)

    • negative

    • solarize

    • sketch

    • denoise

    • emboss

    • oilpaint

    • hatch

    • gpen

    • pastel

    • watercolor

    • film

    • blur

    • saturation

    • colorswap

    • washedout

    • posterise

    • colorpoint

    • colorbalance

    • cartoon

    • deinterlace1

    • deinterlace2

  • led_pin – LED PIN number, if the camera LED is wired to a GPIO PIN and you want to control it.

  • zoom – Camera zoom, in the format (x, y, width, height) (default: (0.0, 0.0, 1.0, 1.0)).

  • stream_format

    Default format for the output when streamed to a network device. Available:

    • h264 (default)

    • mjpeg

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

capture_frame(device: Camera, *_, **__)[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, device: str | int | None = None, preview: bool = False, **camera) str#

Capture an image.

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

  • device – Name/path/ID of the device to capture from (default: None, use the default device).

  • 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(device: int | str | None = None, duration: float | None = None, n_frames: int | None = None, **camera) dict[source]#

Start a camera preview session.

Parameters:
  • device – Name/path/ID of the device to capture from (default: None, use the default device).

  • 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(device: str | int | None = None, duration: float | None = None, n_frames: int | None = None, preview: bool = False, **camera) str | None#

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

Parameters:
  • device – Name/path/ID of the device to capture from (default: None, use the default device).

  • 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(device: str | int | None = None, duration: float | None = None, video_file: str | None = None, preview: bool = False, **camera) str | dict | None#

Capture a video.

Parameters:
  • device – Name/path/ID of the device to capture from (default: None, use the default device).

  • 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: Camera, duration: float | None = None, video_file: str | None = None, image_file: str | None = None, n_frames: int | None = 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: 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.

main()#

Implementation of the main loop of the plugin.

open(device: str | int | None = None, stream: bool = False, redis_queue: str | None = None, **info) Generator[Camera, None, None]#

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.

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

  • info – Camera parameters override - see constructors parameters.

Returns:

The initialized platypush.plugins.camera.Camera object.

open_device(device: str | int | None = None, stream: bool = False, redis_queue: str | None = None, ctx: dict | None = None, **params) Camera#

Initialize and open a device.

Returns:

The initialized camera device.

Raises:

platypush.plugins.camera.CaptureSessionAlreadyRunningException

prepare_device(device: Camera, **_)[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: Camera)[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: float | int | None = 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: float | None = None, scale_y: float | None = 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()#

Start the plugin.

start_camera(camera: Camera, *args, preview: bool = False, **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: Camera)[source]#

Start camera preview.

start_streaming(device: str | int | None = None, duration: float | None = None, stream_format: str | None = None, **camera) dict#

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

When the streaming is started, the plugin will listen on the specified bind_address and listen_port and stream camera frames to connected clients. If stream_format is a video format (H264, H265, MKV, MP4 etc.) then the camera stream can be viewed using a video player - for example, using vlc:

vlc tcp://<host>:<port>
Parameters:
  • device – Name/path/ID of the device to capture from (default: None, use the default device).

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

  • stream_format – Format of the output stream - e.g. h264, mjpeg, mkv etc. If not specified, the stream_format configured on the plugin will be used.

  • camera – Camera object properties - see constructor parameters.

Returns:

The status of the device.

status(device: str | int | None = None)#

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

stop()#

Stop the plugin.

stop_capture(device: str | int | None = 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: Camera)[source]#

Stop camera preview.

stop_streaming(device: str | int | None = 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: str | None = 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, device: str | int | None = None, **camera) str#

Alias for capture_image().

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

  • device – Name/path/ID of the device to capture from (default: None, use the default device).

  • camera – Camera parameters override - see constructors parameters.

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: 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.

wait_stop(timeout=None)#

Wait until a stop event is received.