File size: 7,435 Bytes
9422383
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Run YOLO26n detection on prerecorded video."""

from __future__ import annotations

import argparse
import hashlib
import json
from dataclasses import dataclass
from pathlib import Path
from time import perf_counter

import cv2
import numpy as np
from arm_yolo26n import DetectionResult, Yolo26Detector


@dataclass(frozen=True, slots=True)
class _Arguments:
    runtime: Path
    model: Path
    video: Path
    output: Path
    frame_count: int


@dataclass(frozen=True, slots=True)
class _PreloadedVideo:
    frames: tuple[np.ndarray, ...]
    source_width: int
    source_height: int
    source_fps: float
    decode_resize_ms: float


def _digest(path: Path) -> str:
    value = hashlib.sha256()
    with path.open("rb") as stream:
        for block in iter(lambda: stream.read(1024 * 1024), b""):
            value.update(block)
    return value.hexdigest()


def _parse_args() -> _Arguments:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--runtime", type=Path, required=True)
    parser.add_argument("--model", type=Path, required=True)
    parser.add_argument("--video", type=Path, required=True)
    parser.add_argument("--output", type=Path, required=True)
    parser.add_argument("--frames", type=int, default=200)
    args = parser.parse_args()
    if args.frames <= 0:
        parser.error("--frames must be positive")
    if not args.runtime.is_file():
        parser.error(f"--runtime is not a file: {args.runtime}")
    if not args.model.is_dir():
        parser.error(f"--model is not a directory: {args.model}")
    if not args.video.is_file():
        parser.error(f"--video is not a file: {args.video}")
    if args.output.resolve() == args.video.resolve():
        parser.error("--output must not overwrite --video")
    return _Arguments(args.runtime, args.model, args.video, args.output, args.frames)


def _preload(video: Path, frame_count: int, input_width: int) -> _PreloadedVideo:
    started_at = perf_counter()
    capture = cv2.VideoCapture(str(video))
    if not capture.isOpened():
        raise RuntimeError(f"OpenCV could not open video: {video}")
    source_width = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH))
    source_height = int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
    source_fps = float(capture.get(cv2.CAP_PROP_FPS))
    if source_width <= 0 or source_height <= 0 or source_fps <= 0.0:
        capture.release()
        raise RuntimeError(
            "video must report positive width, height, and frame rate: "
            f"{source_width}x{source_height} at {source_fps} fps"
        )
    input_height = round(source_height * input_width / source_width)
    frames = []
    try:
        while len(frames) < frame_count:
            available, frame = capture.read()
            if not available:
                break
            frames.append(
                cv2.resize(frame, (input_width, input_height), interpolation=cv2.INTER_AREA)
            )
    finally:
        capture.release()
    if len(frames) != frame_count:
        raise RuntimeError(
            f"video provided {len(frames)} decodable frames; --frames requested {frame_count}"
        )
    return _PreloadedVideo(
        frames=tuple(frames),
        source_width=source_width,
        source_height=source_height,
        source_fps=source_fps,
        decode_resize_ms=(perf_counter() - started_at) * 1000,
    )


def _detection_record(result: DetectionResult) -> list[dict[str, object]]:
    return [
        {
            "box": list(detection.box),
            "confidence": detection.confidence,
            "class_id": detection.class_id,
            "class_name": detection.class_name,
        }
        for detection in result.detections
    ]


def _run_model(
    detector: Yolo26Detector,
    video: _PreloadedVideo,
) -> tuple[list[dict[str, object]], dict[str, float | int]]:
    detector.detect(video.frames[0], pixel_format="BGR")
    records = []
    model_seconds = 0.0
    preprocess_ms = 0.0
    inference_ms = 0.0
    postprocess_ms = 0.0
    for index, frame in enumerate(video.frames):
        started_at = perf_counter()
        result = detector.detect(frame, pixel_format="BGR")
        model_seconds += perf_counter() - started_at
        preprocess_ms += result.timings.preprocess_ms
        inference_ms += result.timings.inference_ms
        postprocess_ms += result.timings.postprocess_ms
        records.append(
            {
                "frame_index": index,
                "timestamp_seconds": index / video.source_fps,
                "completed_seconds": model_seconds,
                "timings": {
                    "preprocess_ms": result.timings.preprocess_ms,
                    "inference_ms": result.timings.inference_ms,
                    "postprocess_ms": result.timings.postprocess_ms,
                },
                "detections": _detection_record(result),
            }
        )
    frame_count = len(records)
    return records, {
        "frames": frame_count,
        "model_fps": frame_count / model_seconds,
        "preprocess_mean_ms": preprocess_ms / frame_count,
        "inference_mean_ms": inference_ms / frame_count,
        "postprocess_mean_ms": postprocess_ms / frame_count,
        "model_mean_ms": model_seconds * 1000 / frame_count,
        "decode_resize_ms": video.decode_resize_ms,
    }


def _write_results(
    args: _Arguments,
    video: _PreloadedVideo,
    records: list[dict[str, object]],
    summary: dict[str, float | int],
) -> None:
    input_height, input_width = video.frames[0].shape[:2]
    document = {
        "version": 1,
        "coordinate_space": "inference_frame_pixels_xyxy",
        "source": {
            "video": str(args.video.resolve()),
            "sha256": _digest(args.video),
            "width": video.source_width,
            "height": video.source_height,
            "fps": video.source_fps,
        },
        "inference_frame": {"width": input_width, "height": input_height},
        "runtime_library": str(args.runtime.resolve()),
        "model_directory": str(args.model.resolve()),
        "runtime": "yolo26n",
        "measurement": {
            "decode_and_resize": "preloaded_before_model_timing",
            "warmup_frames": 1,
            "serialization": "outside_model_timing",
        },
        "summary": summary,
        "frames": records,
    }
    args.output.parent.mkdir(parents=True, exist_ok=True)
    args.output.write_text(json.dumps(document, indent=2, sort_keys=True) + "\n", encoding="utf-8")


def main() -> int:
    args = _parse_args()
    with Yolo26Detector(runtime=args.runtime, model=args.model) as detector:
        video = _preload(args.video, args.frame_count, detector.input_shape[3])
        records, summary = _run_model(detector, video)
    _write_results(args, video, records, summary)
    print(
        "Detection performance:\n"
        f"  throughput: {summary['model_fps']:.2f} FPS\n"
        f"  detection time: {summary['model_mean_ms']:.2f} ms/frame\n"
        f"  frames: {summary['frames']}\n"
        "  mean timing: "
        f"preprocess {summary['preprocess_mean_ms']:.2f} ms, "
        f"inference {summary['inference_mean_ms']:.2f} ms, "
        f"postprocess {summary['postprocess_mean_ms']:.2f} ms\n"
        f"  video decode and resize: {summary['decode_resize_ms']:.2f} ms total"
    )
    return 0


if __name__ == "__main__":
    raise SystemExit(main())