refactor(cameras): init abc class + config
This commit is contained in:
committed by
Steven Palma
parent
b0592d9bc8
commit
8a6412b0db
@@ -4,8 +4,12 @@ import numpy as np
|
|||||||
|
|
||||||
|
|
||||||
class Camera(abc.ABC):
|
class Camera(abc.ABC):
|
||||||
|
@abc.abstractproperty
|
||||||
|
def is_connected(self) -> bool:
|
||||||
|
pass
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def connect(self):
|
def connect(self) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
@@ -17,9 +21,5 @@ class Camera(abc.ABC):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def disconnect(self):
|
def disconnect(self) -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
if getattr(self, "is_connected", False):
|
|
||||||
self.disconnect()
|
|
||||||
|
|||||||
@@ -1,9 +1,15 @@
|
|||||||
import abc
|
import abc
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from enum import Enum
|
||||||
|
|
||||||
import draccus
|
import draccus
|
||||||
|
|
||||||
|
|
||||||
|
class ColorMode(Enum):
|
||||||
|
RGB = 0
|
||||||
|
BGR = 1
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class CameraConfig(draccus.ChoiceRegistry, abc.ABC):
|
class CameraConfig(draccus.ChoiceRegistry, abc.ABC):
|
||||||
@property
|
@property
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
from ..configs import CameraConfig
|
from ..configs import CameraConfig, ColorMode
|
||||||
|
|
||||||
|
|
||||||
@CameraConfig.register_subclass("opencv")
|
@CameraConfig.register_subclass("opencv")
|
||||||
@@ -17,22 +18,22 @@ class OpenCVCameraConfig(CameraConfig):
|
|||||||
```
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
camera_index: int
|
index_or_path: int | Path
|
||||||
fps: int | None = None
|
fps: int | None = None
|
||||||
width: int | None = None
|
width: int | None = None
|
||||||
height: int | None = None
|
height: int | None = None
|
||||||
color_mode: str = "rgb"
|
color_mode: ColorMode = ColorMode.RGB
|
||||||
channels: int | None = None
|
channels: int = 3
|
||||||
rotation: int | None = None
|
rotation: int | None = None
|
||||||
mock: bool = False
|
|
||||||
|
|
||||||
def __post_init__(self):
|
def __post_init__(self):
|
||||||
if self.color_mode not in ["rgb", "bgr"]:
|
if self.color_mode not in [ColorMode.RGB, ColorMode.BGR]:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"`color_mode` is expected to be 'rgb' or 'bgr', but {self.color_mode} is provided."
|
f"`color_mode` is expected to be 'rgb' or 'bgr', but {self.color_mode} is provided."
|
||||||
)
|
)
|
||||||
|
|
||||||
self.channels = 3
|
if self.channels != 3:
|
||||||
|
raise NotImplementedError(f"Unsupported number of channels: {self.channels}")
|
||||||
|
|
||||||
if self.rotation not in [-90, None, 90, 180]:
|
if self.rotation not in [-90, None, 90, 180]:
|
||||||
raise ValueError(f"`rotation` must be in [-90, None, 90, 180] (got {self.rotation})")
|
raise ValueError(f"`rotation` must be in [-90, None, 90, 180] (got {self.rotation})")
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class Stretch3RobotConfig(RobotConfig):
|
|||||||
cameras: dict[str, CameraConfig] = field(
|
cameras: dict[str, CameraConfig] = field(
|
||||||
default_factory=lambda: {
|
default_factory=lambda: {
|
||||||
"navigation": OpenCVCameraConfig(
|
"navigation": OpenCVCameraConfig(
|
||||||
camera_index="/dev/hello-nav-head-camera",
|
index_or_path="/dev/hello-nav-head-camera",
|
||||||
fps=10,
|
fps=10,
|
||||||
width=1280,
|
width=1280,
|
||||||
height=720,
|
height=720,
|
||||||
|
|||||||
Reference in New Issue
Block a user