File size: 9,838 Bytes
d725335
 
 
 
 
 
20905e1
d725335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114ea19
8536e24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114ea19
8536e24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d725335
20905e1
d725335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20905e1
 
 
 
 
 
 
 
 
 
114ea19
8536e24
 
 
 
 
 
 
114ea19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8536e24
 
 
114ea19
8536e24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20905e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d725335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20905e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d725335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20905e1
 
d725335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20905e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d725335
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from __future__ import annotations

from pathlib import Path
from typing import Any

from tiny_trigger.models import ActionEvent, Detection
from tiny_trigger.video import _create_browser_mp4_writer, process_video, render_automation_video


class FakeDetector:
    class_names = ["cat"]

    def detect(
        self,
        frame: Any,
        *,
        frame_index: int,
        timestamp_sec: float,
        confidence: float,
        image_size: int | None = None,
        max_detections: int | None = None,
    ) -> list[Detection]:
        assert image_size == 960
        assert max_detections == 20
        return [
            Detection(
                frame_index=frame_index,
                timestamp_sec=timestamp_sec,
                label="cat",
                confidence=0.99,
                bbox_xyxy=(2.0, 2.0, 12.0, 12.0),
                bbox_xyxy_norm=(0.1, 0.1, 0.6, 0.6),
            )
        ]


class TrackedDetector:
    class_names = ["cat"]

    def detect(
        self,
        frame: Any,
        *,
        frame_index: int,
        timestamp_sec: float,
        confidence: float,
        image_size: int | None = None,
        max_detections: int | None = None,
    ) -> list[Detection]:
        offset = frame_index * 0.01
        return [
            Detection(
                frame_index=frame_index,
                timestamp_sec=timestamp_sec,
                label="cat",
                confidence=0.99,
                bbox_xyxy=(2.0 + frame_index, 2.0, 12.0 + frame_index, 12.0),
                bbox_xyxy_norm=(0.1 + offset, 0.1, 0.2 + offset, 0.2),
                track_id=7,
            )
        ]


class DuplicateDetector:
    class_names = ["cat"]

    def detect(
        self,
        frame: Any,
        *,
        frame_index: int,
        timestamp_sec: float,
        confidence: float,
        image_size: int | None = None,
        max_detections: int | None = None,
    ) -> list[Detection]:
        return [
            Detection(
                frame_index=frame_index,
                timestamp_sec=timestamp_sec,
                label="cat",
                confidence=0.62,
                bbox_xyxy=(2.0, 2.0, 16.0, 16.0),
                bbox_xyxy_norm=(0.1, 0.1, 0.5, 0.5),
            ),
            Detection(
                frame_index=frame_index,
                timestamp_sec=timestamp_sec,
                label="cat",
                confidence=0.91,
                bbox_xyxy=(3.0, 3.0, 17.0, 17.0),
                bbox_xyxy_norm=(0.11, 0.11, 0.51, 0.51),
            ),
        ]


def test_process_video_with_fake_detector(tmp_path: Path) -> None:
    cv2 = __import__("cv2")
    video_path = _make_video(tmp_path)

    result = process_video(
        video_path=str(video_path),
        class_prompt="cat",
        frame_stride=2,
        max_frames=2,
        image_size=960,
        max_detections=20,
        detector=FakeDetector(),
        output_dir=str(tmp_path),
    )

    assert Path(result.output_video_path).exists()
    assert result.processed_frames == 2
    assert [item.frame_index for item in result.detections] == [0, 2]
    assert result.frame_stride == 2
    assert result.sample_interval_sec is None
    capture = cv2.VideoCapture(result.output_video_path)
    try:
        assert capture.get(cv2.CAP_PROP_FRAME_COUNT) == 4
        assert capture.get(cv2.CAP_PROP_FPS) == 10.0
    finally:
        capture.release()


def test_process_video_does_not_synthesize_track_ids(tmp_path: Path) -> None:
    video_path = _make_video(tmp_path, fps=10.0, frames=4)

    result = process_video(
        video_path=str(video_path),
        class_prompt="cat",
        frame_stride=1,
        max_frames=3,
        detector=FakeDetector(),
        image_size=960,
        max_detections=20,
        output_dir=str(tmp_path),
    )

    assert [item.track_id for item in result.detections] == [None, None, None]


def test_process_video_preserves_detector_track_ids(tmp_path: Path) -> None:
    video_path = _make_video(tmp_path, fps=10.0, frames=4)

    result = process_video(
        video_path=str(video_path),
        class_prompt="cat",
        frame_stride=1,
        max_frames=3,
        detector=TrackedDetector(),
        output_dir=str(tmp_path),
    )

    assert [item.track_id for item in result.detections] == [7, 7, 7]


def test_process_video_suppresses_duplicate_same_label_boxes(tmp_path: Path) -> None:
    video_path = _make_video(tmp_path, fps=10.0, frames=2)

    result = process_video(
        video_path=str(video_path),
        class_prompt="cat",
        frame_stride=1,
        max_frames=1,
        detector=DuplicateDetector(),
        output_dir=str(tmp_path),
    )

    assert len(result.detections) == 1
    assert result.detections[0].confidence == 0.91


def test_process_video_samples_once_per_second(tmp_path: Path) -> None:
    video_path = _make_video(tmp_path, fps=30.0, frames=95)

    result = process_video(
        video_path=str(video_path),
        class_prompt="cat",
        frame_stride=2,
        sample_interval_sec=1.0,
        max_frames=3,
        image_size=960,
        max_detections=20,
        detector=FakeDetector(),
        output_dir=str(tmp_path),
    )

    assert result.frame_stride == 30
    assert result.sample_interval_sec == 1.0
    assert [item.frame_index for item in result.detections] == [0, 30, 60]


def test_process_video_samples_half_second_intervals(tmp_path: Path) -> None:
    video_path = _make_video(tmp_path, fps=10.0, frames=16)

    result = process_video(
        video_path=str(video_path),
        class_prompt="cat",
        sample_interval_sec=0.5,
        max_frames=3,
        image_size=960,
        max_detections=20,
        detector=FakeDetector(),
        output_dir=str(tmp_path),
    )

    assert result.frame_stride == 5
    assert [item.frame_index for item in result.detections] == [0, 5, 10]


def test_render_automation_video_with_fired_event(tmp_path: Path) -> None:
    video_path = _make_video(tmp_path)
    detections = [
        Detection(
            frame_index=0,
            timestamp_sec=0.0,
            label="cat",
            confidence=0.99,
            bbox_xyxy=(2.0, 2.0, 12.0, 12.0),
            bbox_xyxy_norm=(0.1, 0.1, 0.6, 0.6),
        )
    ]
    events = [
        ActionEvent(
            rule="cat-rule",
            action="feed cat",
            type="simulate",
            frame_index=0,
            timestamp_sec=0.0,
            status="simulated",
        )
    ]

    output_path = render_automation_video(
        source_video_path=str(video_path),
        detections=detections,
        events=events,
        frame_stride=2,
        max_frames=2,
        output_dir=str(tmp_path),
    )

    assert Path(output_path).exists()
    assert output_path.endswith("-automated.mp4")


def test_render_automation_video_uses_computed_stride(tmp_path: Path) -> None:
    video_path = _make_video(tmp_path, fps=30.0, frames=95)
    result = process_video(
        video_path=str(video_path),
        class_prompt="cat",
        sample_interval_sec=1.0,
        max_frames=3,
        image_size=960,
        max_detections=20,
        detector=FakeDetector(),
        output_dir=str(tmp_path),
    )

    output_path = render_automation_video(
        source_video_path=str(video_path),
        detections=result.detections,
        events=[],
        frame_stride=result.frame_stride,
        max_frames=3,
        output_dir=str(tmp_path),
    )

    assert Path(output_path).exists()


def test_process_video_writes_full_motion_clip_with_sampled_overlays(tmp_path: Path) -> None:
    cv2 = __import__("cv2")
    video_path = _make_video(tmp_path, fps=5.0, frames=15)

    result = process_video(
        video_path=str(video_path),
        class_prompt="cat",
        frame_stride=5,
        max_frames=3,
        image_size=960,
        max_detections=20,
        detector=FakeDetector(),
        output_dir=str(tmp_path),
    )

    capture = cv2.VideoCapture(result.output_video_path)
    try:
        assert capture.get(cv2.CAP_PROP_FPS) == 5.0
        assert capture.get(cv2.CAP_PROP_FRAME_COUNT) == 15
    finally:
        capture.release()


def test_process_video_writes_faststart_mp4(tmp_path: Path) -> None:
    video_path = _make_video(tmp_path, fps=5.0, frames=6)

    result = process_video(
        video_path=str(video_path),
        class_prompt="cat",
        frame_stride=2,
        max_frames=2,
        image_size=960,
        max_detections=20,
        detector=FakeDetector(),
        output_dir=str(tmp_path),
    )

    data = Path(result.output_video_path).read_bytes()
    assert data.find(b"moov") < data.find(b"mdat")


def test_browser_mp4_writer_uses_mp4v_only(monkeypatch, tmp_path: Path) -> None:
    calls: list[str] = []

    class Writer:
        def isOpened(self) -> bool:
            return True

        def release(self) -> None:
            return None

    class CV2:
        @staticmethod
        def VideoWriter_fourcc(*codec):
            calls.append("".join(codec))
            return 1234

        @staticmethod
        def VideoWriter(path, fourcc, fps, frame_size):
            return Writer()

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

    writer = _create_browser_mp4_writer(tmp_path / "out.mp4", 8.0, (32, 32))

    assert writer is not None
    assert calls == ["mp4v"]


def _make_video(tmp_path: Path, *, fps: float = 10.0, frames: int = 4) -> Path:
    cv2 = __import__("cv2")
    video_path = tmp_path / "input.mp4"
    writer = cv2.VideoWriter(str(video_path), cv2.VideoWriter_fourcc(*"mp4v"), fps, (32, 32))
    for index in range(frames):
        frame = __import__("numpy").zeros((32, 32, 3), dtype="uint8")
        frame[:] = (index * 20) % 256
        writer.write(frame)
    writer.release()
    return video_path