From d1d842ae95ef0b469309d0d918c7b879958cec81 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 25 Sep 2019 10:40:57 +0200 Subject: [PATCH] Added MLX90640 infrared camera plugin --- .gitmodules | 3 + platypush/plugins/camera/ir/__init__.py | 0 .../plugins/camera/ir/mlx90640/__init__.py | 86 +++++++++++++++++++ platypush/plugins/camera/ir/mlx90640/lib | 1 + 4 files changed, 90 insertions(+) create mode 100644 platypush/plugins/camera/ir/__init__.py create mode 100644 platypush/plugins/camera/ir/mlx90640/__init__.py create mode 160000 platypush/plugins/camera/ir/mlx90640/lib diff --git a/.gitmodules b/.gitmodules index a91e818b9..cab1b6b0d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "platypush/backend/http/static/flag-icons"] path = platypush/backend/http/static/flag-icons url = https://github.com/BlackLight/flag-icon-css.git +[submodule "platypush/plugins/gpio/sensor/ir/mlx90640/lib"] + path = platypush/plugins/camera/ir/mlx90640/lib + url = https://github.com/pimoroni/mlx90640-library diff --git a/platypush/plugins/camera/ir/__init__.py b/platypush/plugins/camera/ir/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/platypush/plugins/camera/ir/mlx90640/__init__.py b/platypush/plugins/camera/ir/mlx90640/__init__.py new file mode 100644 index 000000000..e565ba27e --- /dev/null +++ b/platypush/plugins/camera/ir/mlx90640/__init__.py @@ -0,0 +1,86 @@ +import os +import subprocess +import time + +from platypush.plugins import Plugin, action + + +class CameraIrMlx90640Plugin(Plugin): + """ + 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 + + """ + + _img_size = (24, 32) + + def __init__(self, fps=16, skip_frames=2, scale_factor=10, rawrgb_path=None, **kwargs): + """ + :param fps: Frames per seconds (default: 16) + :param skip_frames: Number of frames to be skipped on sensor initialization/warmup (default: 2) + :param scale_factor: The camera outputs 24x32 pixels artifacts. Use scale_factor to scale them up to a larger image (default: 10) + :param rawrgb_path: Specify it if the rawrgb executable compiled from + https://github.com/pimoroni/mlx90640-library is in another folder than + `/lib/examples`. + """ + super().__init__(**kwargs) + + if not rawrgb_path: + rawrgb_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib', 'examples', 'rawrgb') + + assert fps > 0 + assert skip_frames >= 0 + assert os.path.isfile(rawrgb_path) + + self.fps = fps + self.skip_frames = skip_frames + self.scale_factor = scale_factor + self.rawrgb_path = rawrgb_path + + @action + def capture(self, frames=1, skip_frames=None): + """ + Capture one or multiple frames and return them as raw RGB + + :param frames: Number of frames to be captured (default: 1) + :param skip_frames: If set it overrides the skip_frames parameter specified on the object (default: None) + """ + + if skip_frames is None: + skip_frames = self.skip_frames + + input_size = self._img_size[0] * self._img_size[1] * 3 + sleep_time = 1.0 / self.fps + captured_frames = [] + + with subprocess.Popen([self.rawrgb_path, '{}'.format(self.fps)], stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) as camera: + while len(captured_frames) < frames: + frame = camera.stdout.read(input_size) + size = len(frame) + + if skip_frames > 0: + time.sleep(sleep_time) + skip_frames -= 1 + continue + + captured_frames.append(frame) + + return frames + + +# vim:sw=4:ts=4:et: diff --git a/platypush/plugins/camera/ir/mlx90640/lib b/platypush/plugins/camera/ir/mlx90640/lib new file mode 160000 index 000000000..f4549dc6c --- /dev/null +++ b/platypush/plugins/camera/ir/mlx90640/lib @@ -0,0 +1 @@ +Subproject commit f4549dc6cc416f8db05a8bfd1d4850aee5e7af75