File size: 1,817 Bytes
1d377f1 | 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 | """
Decode selected frame ranges of the test video, downscale to the detector's
analysis resolution, and write raw RGBA frames + timing so the ACTUAL
detector.js can be run in Node against real data.
Binary format:
header: <iiii aw, ah, fps_int, count
then per frame: <i frameIndex, then aw*ah*4 bytes RGBA
"""
import cv2, numpy as np, struct
PATH = r"E:\test\Kiro_flash_detection\Video\0921 (1)_flash.mp4"
OUT = r"E:\test\Kiro_flash_detection\tools\frames.bin"
ANALYSIS_W = 480
# Include >=15 lead-in frames before each window for the rolling background.
WINDOWS = [
(2580, 2660), # the real flash at 2612 (must still be detected)
(2330, 2380), # the reported false positive at 2353-2358 / peak 2355
(2510, 2560), # prior false-positive cluster ~2535
(2080, 2160), # prior false-positive cluster ~2105
]
cap = cv2.VideoCapture(PATH)
fps = cap.get(cv2.CAP_PROP_FPS) or 30.0
W = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)); H = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
scale = W/ANALYSIS_W if W > ANALYSIS_W else 1.0
aw = max(1, round(W/scale)); ah = max(1, round(H/scale))
want, seen = [], set()
for a, b in WINDOWS:
for i in range(a, b+1):
if i not in seen:
seen.add(i); want.append(i)
want.sort()
records = []
for idx in want:
cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
ok, f = cap.read()
if not ok: continue
s = cv2.resize(f, (aw, ah), interpolation=cv2.INTER_AREA)
rgba = cv2.cvtColor(s, cv2.COLOR_BGR2RGBA)
records.append((idx, rgba.tobytes()))
cap.release()
with open(OUT, "wb") as fo:
fo.write(struct.pack("<iiii", aw, ah, int(round(fps)), len(records)))
for idx, buf in records:
fo.write(struct.pack("<i", idx))
fo.write(buf)
print(f"wrote {len(records)} frames {aw}x{ah} fps~{fps} scale={scale:.3f} to {OUT}")
|