diff --git a/leads_vec/cli.py b/leads_vec/cli.py index 17ebcb5..9eae544 100644 --- a/leads_vec/cli.py +++ b/leads_vec/cli.py @@ -1,3 +1,4 @@ +from base64 import b64encode from datetime import datetime as _datetime from threading import Thread as _Thread from time import time as _time, sleep as _sleep @@ -72,8 +73,8 @@ def _() -> None: if rd.comm_stream.num_connections() < 1: _sleep(.01) for tag in FRONT_VIEW_CAMERA, LEFT_VIEW_CAMERA, RIGHT_VIEW_CAMERA, REAR_VIEW_CAMERA: - if (cam := get_camera(tag, Base64Camera)) and (frame := cam.read()): - rd.comm_stream_notify(tag, frame) + if (cam := get_camera(tag)) and (frame := cam.read_numpy()) is not None: + rd.comm_stream_notify(tag, b64encode(Base64Camera.encode(frame))) _Thread(name="comm streamer", target=_, daemon=True).start() diff --git a/leads_vec/devices_visual.py b/leads_vec/devices_visual.py index 9219f5f..2beede4 100644 --- a/leads_vec/devices_visual.py +++ b/leads_vec/devices_visual.py @@ -14,19 +14,19 @@ config: Config = require_config() CAMERA_RESOLUTION: tuple[int, int] | None = config.get("camera_resolution") CAMERA_TAGS: list[str] = [] -CAMERA_ARGS: list[tuple[int, tuple[int, int] | None]] = [] +CAMERA_ARGS: list[tuple[int, tuple[int, int] | None, int]] = [] if (port := config.get("front_view_camera_port")) is not None: CAMERA_TAGS.append(FRONT_VIEW_CAMERA) - CAMERA_ARGS.append((port, CAMERA_RESOLUTION)) + CAMERA_ARGS.append((port, CAMERA_RESOLUTION, 25)) if (port := config.get("left_view_camera_port")) is not None: CAMERA_TAGS.append(LEFT_VIEW_CAMERA) - CAMERA_ARGS.append((port, CAMERA_RESOLUTION)) + CAMERA_ARGS.append((port, CAMERA_RESOLUTION, 25)) if (port := config.get("right_view_camera_port")) is not None: CAMERA_TAGS.append(RIGHT_VIEW_CAMERA) - CAMERA_ARGS.append((port, CAMERA_RESOLUTION)) + CAMERA_ARGS.append((port, CAMERA_RESOLUTION, 25)) if (port := config.get("rear_view_camera_port")) is not None: CAMERA_TAGS.append(REAR_VIEW_CAMERA) - CAMERA_ARGS.append((port, CAMERA_RESOLUTION)) + CAMERA_ARGS.append((port, CAMERA_RESOLUTION, 25)) @device(CAMERA_TAGS, MAIN_CONTROLLER, CAMERA_ARGS) diff --git a/leads_video/camera.py b/leads_video/camera.py index 66eda86..050b9cc 100644 --- a/leads_video/camera.py +++ b/leads_video/camera.py @@ -95,11 +95,13 @@ def __init__(self, port: int, resolution: tuple[int, int] | None = None, quality def loop(self) -> None: super().loop() + @staticmethod + def encode(frame: _ndarray, quality: int = 100) -> bytes: + return _imencode(".jpg", _cvtColor(frame, _COLOR_RGB2BGR), (_IMWRITE_JPEG_QUALITY, quality))[1].tobytes() + def loop2(self) -> None: - if (local_frame := self._frame) is not None: - _, local_frame = _imencode(".jpg", _cvtColor(local_frame, _COLOR_RGB2BGR), - (_IMWRITE_JPEG_QUALITY, self._quality)) - self._bytes = local_frame.tobytes() + if (frame := self._frame) is not None: + self._bytes = Base64Camera.encode(frame, self._quality) self._base64 = _b64encode(self._bytes).decode() def run2(self) -> None: