Skip to content

Commit

Permalink
Supported uncompressed image for comm stream. (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
ATATC committed Sep 30, 2024
1 parent caa0ff2 commit a5f4f6d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
5 changes: 3 additions & 2 deletions leads_vec/cli.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()

Expand Down
10 changes: 5 additions & 5 deletions leads_vec/devices_visual.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions leads_video/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a5f4f6d

Please sign in to comment.