Exemplo n.º 1
0
    def create_thumbnail(self, file_name, frame, objects, resolution):
        """Create thumbnails, sent to MQTT and/or saved to disk based on config."""
        draw_objects(
            frame.decoded_frame_umat_rgb,
            objects,
            resolution,
        )
        cv2.imwrite(file_name, frame.decoded_frame_umat_rgb)

        if self.config.recorder.thumbnail.save_to_disk:
            thumbnail_folder = os.path.join(self.config.recorder.folder,
                                            "thumbnails",
                                            self.config.camera.name)
            self.create_directory(thumbnail_folder)

            self._logger.debug(f"Saving thumbnail in {thumbnail_folder}")
            if not cv2.imwrite(
                    os.path.join(thumbnail_folder, "latest_thumbnail.jpg"),
                    frame.decoded_frame_umat_rgb,
            ):
                self._logger.error("Failed saving thumbnail to disk")

        if self.config.recorder.thumbnail.send_to_mqtt and self._mqtt_devices:
            ret, jpg = cv2.imencode(".jpg", frame.decoded_frame_umat_rgb)
            if ret:
                self._mqtt_devices["latest_thumbnail"].publish(jpg.tobytes())
Exemplo n.º 2
0
    def publish_image(self, object_frame, motion_frame, zones, resolution):
        """Publish image to MQTT."""
        if self.mqtt_queue:
            # Draw on the object frame if it is supplied
            frame = object_frame if object_frame else motion_frame
            if self.config.motion_detection.mask:
                draw_mask(
                    frame.decoded_frame_mat_rgb,
                    self.config.motion_detection.mask,
                )

            if motion_frame and frame.motion_contours:
                draw_contours(
                    frame.decoded_frame_mat_rgb,
                    frame.motion_contours,
                    resolution,
                    self.config.motion_detection.area,
                )

            draw_zones(frame.decoded_frame_mat_rgb, zones)
            draw_objects(
                frame.decoded_frame_mat_rgb,
                frame.objects,
                resolution,
            )

            # Write a low quality image to save bandwidth
            ret, jpg = cv2.imencode(".jpg", frame.decoded_frame_mat_rgb,
                                    [int(cv2.IMWRITE_JPEG_QUALITY), 75])
            if ret:
                self.devices["camera"].publish(jpg.tobytes())
Exemplo n.º 3
0
    async def process_frame(self, nvr: FFMPEGNVR, frame, mjpeg_stream_config):
        """Return JPG with drawn objects, zones etc."""
        if mjpeg_stream_config["width"] and mjpeg_stream_config["height"]:
            resolution = mjpeg_stream_config["width"], mjpeg_stream_config["height"]
            frame.resize(
                "tornado", mjpeg_stream_config["width"], mjpeg_stream_config["height"]
            )
            # TODO move this to a preprocess pylint: disable=fixme
            processed_frame = frame.get_preprocessed_frame(
                "tornado"
            ).get()  # Convert to Mat
        else:
            resolution = nvr.camera.resolution
            processed_frame = frame.decoded_frame_mat_rgb

        if mjpeg_stream_config["draw_motion_mask"] and nvr.config.motion_detection.mask:
            draw_motion_mask(
                processed_frame,
                nvr.config.motion_detection.mask,
            )

        if mjpeg_stream_config["draw_object_mask"] and nvr.config.object_detection.mask:
            draw_object_mask(
                processed_frame,
                nvr.config.object_detection.mask,
            )

        if mjpeg_stream_config["draw_motion"] and frame.motion_contours:
            draw_contours(
                processed_frame,
                frame.motion_contours,
                resolution,
                nvr.config.motion_detection.area,
            )

        if mjpeg_stream_config["draw_zones"]:
            draw_zones(processed_frame, nvr.zones)

        if mjpeg_stream_config["draw_objects"]:
            draw_objects(
                processed_frame,
                frame.objects,
                resolution,
            )

        if mjpeg_stream_config["rotate"]:
            processed_frame = imutils.rotate_bound(
                processed_frame, mjpeg_stream_config["rotate"]
            )

        if mjpeg_stream_config["mirror"]:
            processed_frame = cv2.flip(processed_frame, 1)

        # Write a low quality image to save bandwidth
        ret, jpg = cv2.imencode(
            ".jpg", processed_frame, [int(cv2.IMWRITE_JPEG_QUALITY), 100]
        )

        return ret, jpg