fix(from_video_info): fixing early validation issue in from_video_info

This commit is contained in:
CarolinePascal
2026-05-26 18:06:22 +02:00
parent ca7168f413
commit f7ee453de7
+20 -17
View File
@@ -19,8 +19,8 @@
from __future__ import annotations from __future__ import annotations
import logging import logging
from dataclasses import dataclass, field, fields from dataclasses import dataclass, field
from typing import Any, ClassVar from typing import Any, ClassVar, Self
from lerobot.utils.import_utils import require_package from lerobot.utils.import_utils import require_package
@@ -112,9 +112,9 @@ class VideoEncoderConfig:
self.validate() self.validate()
@classmethod @classmethod
def from_video_info(cls, video_info: dict | None) -> VideoEncoderConfig: def _kwargs_from_video_info(cls, video_info: dict | None) -> dict[str, Any]:
"""Reconstruct a :class:`VideoEncoderConfig` from a video feature's ``info`` block. """Parse the ``video.*`` keys of a feature ``info`` block into
Missing or ``None`` values fall back to the class defaults. constructor kwargs.
""" """
video_info = video_info or {} video_info = video_info or {}
kwargs: dict[str, Any] = {} kwargs: dict[str, Any] = {}
@@ -133,7 +133,15 @@ class VideoEncoderConfig:
continue continue
kwargs[field_name] = value kwargs[field_name] = value
return cls(**kwargs) return kwargs
@classmethod
def from_video_info(cls, video_info: dict | None) -> Self:
"""Reconstruct an encoder config from a video feature's ``info`` block.
Missing or ``None`` values fall back to the class defaults.
"""
return cls(**cls._kwargs_from_video_info(video_info))
def detect_available_encoders(self, encoders: list[str] | str) -> list[str]: def detect_available_encoders(self, encoders: list[str] | str) -> list[str]:
"""Return the subset of available encoders based on the specified video backend. """Return the subset of available encoders based on the specified video backend.
@@ -291,23 +299,18 @@ class DepthEncoderConfig(VideoEncoderConfig):
_DEFAULT_CHANNELS: ClassVar[int] = 1 _DEFAULT_CHANNELS: ClassVar[int] = 1
@classmethod @classmethod
def from_video_info(cls, video_info: dict | None) -> DepthEncoderConfig: def _kwargs_from_video_info(cls, video_info: dict | None) -> dict[str, Any]:
"""Reconstruct a :class:`DepthEncoderConfig` from a depth feature's ``info`` block. """Layer the depth-specific tuning (``depth_min`` / ``depth_max`` /
``shift`` / ``use_log``) on top of the base parser. Missing keys
Reuses :meth:`VideoEncoderConfig.from_video_info` for the base fall back to the class defaults.
codec/tuning fields and then layers the depth-specific tuning
(``depth_min`` / ``depth_max`` / ``shift`` / ``use_log``) on top.
Missing keys fall back to the class defaults.
""" """
base = VideoEncoderConfig.from_video_info(video_info) kwargs = super()._kwargs_from_video_info(video_info)
kwargs: dict[str, Any] = {f.name: getattr(base, f.name) for f in fields(base) if f.init}
video_info = video_info or {} video_info = video_info or {}
for name in DEPTH_ENCODER_INFO_FIELD_NAMES: for name in DEPTH_ENCODER_INFO_FIELD_NAMES:
value = video_info.get(f"video.{name}") value = video_info.get(f"video.{name}")
if value is not None: if value is not None:
kwargs[name] = value kwargs[name] = value
return cls(**kwargs) return kwargs
def depth_encoder_defaults() -> DepthEncoderConfig: def depth_encoder_defaults() -> DepthEncoderConfig: