カメラ入力解像度のデフォルトをフルHDに変更

動画圧縮機能の追加
This commit is contained in:
agomizi1cm committed 2026-10-02 22:56:17 +09:00
1 parent ca10876101
commit 6d6b6a0ede
4 files changed
+27 -4

No files matched your search

+18
View File
@@ -1,5 +1,6 @@
"""OpenCV camera and video input abstraction."""
import logging
from pathlib import Path
import cv2
@@ -8,6 +9,8 @@ import numpy as np
from .config_loader import InputSettings
from .exceptions import CameraOpenError, FrameReadError
logger = logging.getLogger(__name__)
class Camera:
"""Read frames from a camera device or video file."""
@@ -32,9 +35,24 @@ class Camera:
self.release()
raise CameraOpenError(f"Could not open {self.config.type} input: {source}")
if self.config.type == "camera":
# The pixel format must be set before the resolution: many webcams only offer
# Full HD at a usable frame rate in MJPG, and fall back to a lower mode otherwise.
if self.config.fourcc:
self._capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*self.config.fourcc))
self._capture.set(cv2.CAP_PROP_FRAME_WIDTH, self.config.width)
self._capture.set(cv2.CAP_PROP_FRAME_HEIGHT, self.config.height)
self._capture.set(cv2.CAP_PROP_FPS, self.config.fps)
actual_w, actual_h = self.get_actual_width(), self.get_actual_height()
logger.info(
"Camera opened: requested %dx%d@%.1f fourcc=%s, actual %dx%d@%.1f",
self.config.width, self.config.height, self.config.fps, self.config.fourcc or "default",
actual_w, actual_h, self.get_actual_fps(),
)
if (actual_w, actual_h) != (self.config.width, self.config.height):
logger.warning(
"Camera resolution differs from the request: requested %dx%d, got %dx%d",
self.config.width, self.config.height, actual_w, actual_h,
)
def read(self) -> np.ndarray | None:
if self._capture is None or not self._capture.isOpened():
+4
View File
@@ -42,6 +42,8 @@ class InputSettings:
backend: str
horizontal_flip: bool
max_consecutive_read_failures: int
# Four-character camera pixel format (e.g. "MJPG"); empty string leaves the driver default.
fourcc: str = ""
@dataclass(frozen=True, slots=True)
@@ -539,6 +541,8 @@ def _validate(config: AppConfig) -> None:
raise ConfigurationError("input dimensions, fps, and read failure limit must be positive")
if i.backend not in {"auto", "dshow", "msmf", "v4l2"}:
raise ConfigurationError("input.backend must be auto, dshow, msmf, or v4l2")
if not isinstance(i.fourcc, str) or (i.fourcc and len(i.fourcc) != 4):
raise ConfigurationError("input.fourcc must be empty or exactly 4 characters (e.g. MJPG)")
if not 0.0 <= d.confidence_threshold <= 1.0 or not 0.0 <= d.iou_threshold <= 1.0:
raise ConfigurationError("detector confidence and IoU thresholds must be in [0, 1]")
if d.image_size <= 0 or d.max_detections <= 0 or d.person_class_id < 0: