camera.ir.mlx90640#

Description#

Plugin to interact with a ML90640 infrared thermal camera.

In order to use this plugin you’ll need to download and compile the mlx90640 C++ bindings and examples for the device. Instructions on Raspbian:

# Install the dependencies
$ [sudo] apt-get install libi2c-dev
$ cd $PLATYPUSH_SRC_DIR
$ git submodule init
$ git submodule update
$ cd platypush/plugins/camera/ir/mlx90640/lib
$ make clean
$ make bcm2835
$ make examples/rawrgb I2C_MODE=LINUX
param kwargs:

Extra parameters to be passed to CameraPlugin.

Configuration#

camera.ir.mlx90640:
  # [Optional]
  # Specify it if the rawrgb executable compiled from
  # https://github.com/pimoroni/mlx90640-library is in another folder than
  # `<directory of this file>/lib/examples`.
  # rawrgb_path:   # type=Optional[str]

  # [Optional]
  # Device resolution (default: 32x24).
  # resolution: (32, 24)  # type=Tuple[int, int]

  # [Optional]
  # Number of frames to be skipped on sensor initialization/warmup (default: 2).
  # warmup_frames: 5  # type=Optional[int]

  # [Optional]
  # Identifier of the default capturing device.
  # device:   # type=Union[str, int, NoneType]

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

  # [Optional]
  # Number of seconds to wait before a picture is
  # taken or the first frame of a video/sequence is captured (default:
  # 0).
  # warmup_seconds: 0.0  # type=Optional[float]

  # [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]
  # Frames per second (default: 25).
  # fps: 16  # type=float

  # [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 format for the output when streamed to a
  # network device. Available:
  #
  #     - ``mjpeg`` (default)
  #     - ``h264`` (over ``ffmpeg``)
  #     - ``h265`` (over ``ffmpeg``)
  #     - ``mkv`` (over ``ffmpeg``)
  #     - ``mp4`` (over ``ffmpeg``)
  # stream_format: mjpeg  # type=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 Pillow

Alpine

apk add i2c-tools-dev py3-pillow py3-numpy

Debian

apt install python3-numpy libi2c-dev python3-pillow

Fedora

yum install python-numpy python-pillow i2c-tools

Arch Linux

pacman -S python-numpy python-pillow i2c-tools

Actions#

Module reference#

class platypush.plugins.camera.ir.mlx90640.CameraIrMlx90640Plugin(rawrgb_path: str | None = None, resolution: Tuple[int, int] = (32, 24), warmup_frames: int | None = 5, **kwargs)[source]#

Bases: CameraPlugin

Plugin to interact with a ML90640 infrared thermal camera.

In order to use this plugin you’ll need to download and compile the mlx90640 C++ bindings and examples for the device. Instructions on Raspbian:

# Install the dependencies
$ [sudo] apt-get install libi2c-dev
$ cd $PLATYPUSH_SRC_DIR
$ git submodule init
$ git submodule update
$ cd platypush/plugins/camera/ir/mlx90640/lib
$ make clean
$ make bcm2835
$ make examples/rawrgb I2C_MODE=LINUX
__init__(rawrgb_path: str | None = None, resolution: Tuple[int, int] = (32, 24), warmup_frames: int | None = 5, **kwargs)[source]#
Parameters:
  • rawrgb_path – Specify it if the rawrgb executable compiled from pimoroni/mlx90640-library is in another folder than <directory of this file>/lib/examples.

  • resolution – Device resolution (default: 32x24).

  • warmup_frames – Number of frames to be skipped on sensor initialization/warmup (default: 2).

  • kwargs – Extra parameters to be passed to platypush.plugins.camera.CameraPlugin.

capture(output_file=None, *args, **kwargs)[source]#

Back-compatibility alias for capture_image().

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

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_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_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(image)[source]#

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.