File size: 1,324 Bytes
7a87926
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import types
from typing import List
import numpy as np
import pytest


class _FakeVideoCapture:
    def __init__(self, frames_bgr: List[np.ndarray]):
        self._frames = list(frames_bgr)
        self._idx = 0

    def isOpened(self) -> bool:
        return True

    def read(self):
        if self._idx >= len(self._frames):
            return False, None
        f = self._frames[self._idx]
        self._idx += 1
        return True, f

    def release(self):
        return None


def install_fake_cv2(monkeypatch: pytest.MonkeyPatch, frames_rgb: List[np.ndarray]) -> None:
    # Minimal cv2 shim for our pipelines
    frames_bgr = [f[..., ::-1].copy() for f in frames_rgb]
    cv2 = types.SimpleNamespace()

    def VideoCapture(path: str):
        return _FakeVideoCapture(frames_bgr)

    def cvtColor(img, code):
        # BGR <-> RGB by channel flip
        return img[..., ::-1].copy()

    cv2.VideoCapture = VideoCapture
    cv2.cvtColor = cvtColor
    cv2.COLOR_BGR2RGB = 0
    cv2.COLOR_RGB2BGR = 1

    monkeypatch.setitem(__import__("sys").modules, "cv2", cv2)


@pytest.fixture
def fake_frames_rgb() -> List[np.ndarray]:
    # 6 frames of 8x8 RGB (enough for temporal_window=5 tests)
    rng = np.random.default_rng(0)
    return [rng.integers(0, 255, size=(8, 8, 3), dtype=np.uint8) for _ in range(6)]