mirror of
https://github.com/huggingface/lerobot.git
synced 2026-05-22 20:19:43 +00:00
fc8a388a25
* feat(cameras): make backend configurable to the CLI * chore(cameras): address feedback * feat(Enum error messages): adding better instanciation error messages for Enum classes * chore(Enum error messages): propagating Enum error messages to all camera classes * chore(comments): removing superfluous comments * chore(format): applying ruff checks --------- Co-authored-by: CarolinePascal <caroline8.pascal@gmail.com>
77 lines
3.5 KiB
Python
77 lines
3.5 KiB
Python
# Copyright 2024 The HuggingFace Inc. team. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
from dataclasses import dataclass
|
|
from pathlib import Path
|
|
|
|
from ..configs import CameraConfig, ColorMode, Cv2Backends, Cv2Rotation
|
|
|
|
__all__ = ["OpenCVCameraConfig", "ColorMode", "Cv2Rotation", "Cv2Backends"]
|
|
|
|
|
|
@CameraConfig.register_subclass("opencv")
|
|
@dataclass
|
|
class OpenCVCameraConfig(CameraConfig):
|
|
"""Configuration class for OpenCV-based camera devices or video files.
|
|
|
|
This class provides configuration options for cameras accessed through OpenCV,
|
|
supporting both physical camera devices and video files. It includes settings
|
|
for resolution, frame rate, color mode, and image rotation.
|
|
|
|
Example configurations:
|
|
```python
|
|
# Basic configurations
|
|
OpenCVCameraConfig(0, 30, 1280, 720) # 1280x720 @ 30FPS
|
|
OpenCVCameraConfig(/dev/video4, 60, 640, 480) # 640x480 @ 60FPS
|
|
|
|
# Advanced configurations with FOURCC format
|
|
OpenCVCameraConfig(128422271347, 30, 640, 480, rotation=Cv2Rotation.ROTATE_90, fourcc="MJPG") # With 90° rotation and MJPG format
|
|
OpenCVCameraConfig(0, 30, 1280, 720, fourcc="YUYV") # With YUYV format
|
|
```
|
|
|
|
Attributes:
|
|
index_or_path: Either an integer representing the camera device index,
|
|
or a Path object pointing to a video file.
|
|
fps: Requested frames per second for the color stream.
|
|
width: Requested frame width in pixels for the color stream.
|
|
height: Requested frame height in pixels for the color stream.
|
|
color_mode: Color mode for image output (RGB or BGR). Defaults to RGB.
|
|
rotation: Image rotation setting (0°, 90°, 180°, or 270°). Defaults to no rotation.
|
|
warmup_s: Time reading frames before returning from connect (in seconds)
|
|
fourcc: FOURCC code for video format (e.g., "MJPG", "YUYV", "I420"). Defaults to None (auto-detect).
|
|
backend: OpenCV backend identifier (https://docs.opencv.org/3.4/d4/d15/group__videoio__flags__base.html). Defaults to ANY.
|
|
|
|
Note:
|
|
- Only 3-channel color output (RGB/BGR) is currently supported.
|
|
- FOURCC codes must be 4-character strings (e.g., "MJPG", "YUYV"). Some common FOUCC codes: https://learn.microsoft.com/en-us/windows/win32/medfound/video-fourccs#fourcc-constants
|
|
- Setting FOURCC can help achieve higher frame rates on some cameras.
|
|
"""
|
|
|
|
index_or_path: int | Path
|
|
color_mode: ColorMode = ColorMode.RGB
|
|
rotation: Cv2Rotation = Cv2Rotation.NO_ROTATION
|
|
warmup_s: int = 1
|
|
fourcc: str | None = None
|
|
backend: Cv2Backends = Cv2Backends.ANY
|
|
|
|
def __post_init__(self) -> None:
|
|
self.color_mode = ColorMode(self.color_mode)
|
|
self.rotation = Cv2Rotation(self.rotation)
|
|
self.backend = Cv2Backends(self.backend)
|
|
|
|
if self.fourcc is not None and (not isinstance(self.fourcc, str) or len(self.fourcc) != 4):
|
|
raise ValueError(
|
|
f"`fourcc` must be a 4-character string (e.g., 'MJPG', 'YUYV'), but '{self.fourcc}' is provided."
|
|
)
|