跳转到内容

C101 实验:你的第一次采集

这个实验做一段简短的录制,然后像处理流程的最初几个阶段那样去看它:文件是否完整,每一帧是否按时到达,有哪些传感器数据流、速率是多少,以及时钟在整个录制时段中是否保持稳定。它不需要特殊的硬件。任何相机都可以;GoPro 相机、运行 OpenCamera Sensors 的 Android 手机、Project Aria(VRS)和 ROS 2 采集设备(MCAP)各有一个额外的步骤。

下面的每一条命令都是从工具自己的文档中复制的,或者按照文档给出的形式、换上你自己的文件名;每一步都链接了出处。方括号标出由你填写的值。这些命令在 FFmpeg 6.1 上、并在 GoPro 的 gpmf-parser 仓库附带的示例文件上运行过。

先学完第 1 课到第 4 课,并把检查清单放在手边。

安装 FFmpeg(它提供 ffprobe)和 Python 3。然后在录制之前记下:

  • 相机型号和序列号;
  • 分辨率、帧率以及镜头模式或视场角模式;
  • 防抖是否开启,以及是否录制音频。

这些设置决定了素材以后能用来做什么。序列号把素材与某一台相机的标定绑定在一起:同一型号的各台相机互有差异(RoboCap 的整批设备焦距跨 590–630 px),所以素材要用它自己那台相机的数值来处理,决不用整个型号通用的默认值(标称标定与逐台标定)。对 SLAM 和标定来说,防抖是个问题:它逐帧旋转和裁切图像,所以相机模型每一帧都在变(用于采集的运动相机(GoPro):镜头模式、破坏标定的镜头效应:对焦、热、冲击、滤光片、快门和模糊)。如果你的相机允许关闭它,就关闭并记下来。

在使用 OpenCamera Sensors 的 Android 手机上,该应用的 README 提到两项设置:音频录制默认是禁用的,因为 “When recording video with audio recording enabled, MediaRecorder adds extra frames to the video to match the sound”(意为:在启用音频录制的情况下录制视频时,MediaRecorder 会向视频中添加额外的帧来匹配声音),并且它建议禁用视频防抖(OpenCamera Sensors;用智能手机做采集设备)。

规划大约十分钟、在桌边做的一项简单任务,桌子你要能看清楚,独自一人在一个私密的空间里。录制其他任何人都需要他们的知情同意:见佩戴者的知情同意和旁观者与在共享空间中录制。

规划两个同步事件:在相机看得清清楚楚的地方把一盏灯打开再关上,一次在最开始,一次在最末尾,并记下每一次的挂钟时间。灯光是一种视觉事件,每台看到它的相机都在同一时刻记录下它,没有传播延迟,这与声音不同(LED 与灯光闪烁同步)。如果你有第二台录制设备,例如一部手机,就让两台设备都看到这盏灯:这样第 8 步就能测量它们之间的偏移和漂移。

把相机戴上(头带、帽子或眼镜,你有什么就用什么),并在它的预览中确认你的双手和任务都在视野内。然后:

  1. 开始录制。
  2. 做出第一个同步事件。
  3. 做任务,大约十分钟。以正常速度转头几次,这样素材里就包含处理流程必须应对的头部运动。
  4. 做出第二个同步事件。
  5. 停止录制。

记下开始和停止的时间、任何出了问题的地方,以及相机显示的文件名。

第 4 步:复制文件并计算校验和

Section titled “第 4 步:复制文件并计算校验和”

把文件从相机复制到你的计算机,然后分别计算原件和副本的校验和并比较它们,之后才从相机上删除任何东西。实用指南的规则是:在删除采集设备上的那份之前,检查每一个文件(存储、上传与数据量:复制之后的完整性检查)。使用 GNU coreutils:

终端窗口
sha256sum [your_file.mp4] > [your_file.mp4].sha256
sha256sum -c [your_file.mp4].sha256

-c(--check)从你给它的文件中读取文件名和校验和,并报告 “whether the checksums match the contents of the named files”(意为:这些校验和是否与所列文件的内容相符)(GNU coreutils: cksum common options)。在相机的文件夹里运行第一行,在副本的文件夹里运行第二行。

为什么要比较校验和:相机一旦清空,副本就是唯一剩下的一份,所以在原件消失之前,必须证明副本与原件完全相同。SHA-256 一致说明副本保存的字节与原件相同;任何差异,哪怕只有一个字节,都会改变校验和。

你应该看到 [your_file.mp4]: OK。

对于 VRS 文件(Project Aria),实用指南列出了用于逐字节比较的 vrs checksum-verbatim file.vrs,以及 vrs check file.vrs,后者 “decodes every record in the VRS file and prints how many records were decoded successfully”(意为:解码 VRS 文件中的每一条记录,并打印成功解码了多少条记录)(VRS CLI tool)。

ffprobe 可以读取任何视频文件,并报告它的容器和数据流。下面这些选项都来自 ffprobe 文档:-v 设置日志级别,-show_format 和 -show_streams 打印容器和每一路数据流,-of(-output_format)选择输出格式。

终端窗口
ffprobe -v error -show_format -show_streams -of json [your_file.mp4]

如果只想看元数据标签,就用文档自己为 -show_entries 给出的示例参数:

终端窗口
ffprobe -v error -show_entries "stream_tags : format_tags" [your_file.mp4]

你应该看到每一路数据流一个条目,带有它的 codec_type(video、audio、data)、r_frame_rate 和 avg_frame_rate,还有时长,以及 creation_time 之类的标签。像 creation_time 这样的标签是整个文件的一个时间;逐帧的时间戳在第 6 步。

在 GoPro 上,除了视频和音频还会有更多。GoPro 的文档写明:“Telemetry carrying MP4 files will have a minimum of four tracks: Video, audio, timecode and telemetry (GPMF).”(意为:带有遥测的 MP4 文件至少有四条轨道:视频、音频、时间码和遥测(GPMF)。)遥测数据流显示为 codec_type=data,带有 codec_tag_string=gpmd(用于采集的运动相机(GoPro):GPMF 轨道)。GoPro 仓库中的 hero8.mp4 示例显示有视频、音频、一路 tmcd 时间码数据流、gpmd 遥测数据流,以及另外一路数据流。

Inside MCAP, VRS and GPMF files Three file layouts side by side, top of file to bottom. MCAP: magic bytes, a header, then the data section holding schema and channel records and chunks; each chunk batches messages, each with log_time and publish_time, and is compressed with zstd or lz4; a message index follows each chunk; attachments, which can hold calibration, and metadata records sit outside chunks; a data end record; then a summary section with indexes and statistics, a summary offset section, a footer and the magic bytes again. VRS: file tags readable on open, then streams, each identified by a device type and instance such as 200-1 for a camera and 372-1 for motion data; each stream has tags and three record types, configuration, state and data, and the index is written last at the end of the file, so an interrupted recording can have it rebuilt. GPMF: an MP4 file with four tracks, video, audio, timecode and a meta track whose data format is gpmd; the telemetry is key-length-value items with four-character keys, a DEVC device block nesting STRM streams, sticky modifier keys such as SCAL, SIUN and TYPE, and sensor data such as ACCL and GYRO. MCAP VRS GPMF in a GoPro MP4 Magic + Header Data section Schema, Channel records Chunk (zstd / lz4)Messages: log_time, publish_timeMessage Index Chunk (zstd / lz4)Messages: log_time, publish_timeMessage Index Attachment (calibration), Metadata Data End record Summary: indexes, statistics Summary Offset, Footer, Magic File tags (read on open) Stream 200-1 (camera) stream tags Configuration records State records (often empty) Data records: images, IMU Stream 372-1 (motion) stream tags Configuration records State records (often empty) Data records: images, IMU Index, written last cut off? vrs fix-index rebuilds it MP4 tracks vide: video soun: audio tmcd: timecode meta: telemetry (gpmd) KLV: FourCC, type, size, repeat DEVC (device) STRM (stream) SCAL, SIUN, TYPE (sticky) ACCL samples STRM (stream) SCAL, SIUN, TYPE (sticky) GYRO samples lens model FOVL, readout SROT in global metadata
What is inside each file, top to bottom. MCAP batches timestamped messages into compressed chunks and writes indexes in a summary at the end. VRS keeps one stream per device, each with configuration, state and data records, and writes its index last, so a cut-off file can be repaired. A GoPro MP4 carries telemetry as a fourth track of nested key-length-value items. From the MCAP spec, the VRS docs and the gpmf-parser README.

对于 VRS 和 MCAP 文件,改用它们自己的工具:

  • 不带子命令的 vrs [file.vrs] 打印数据流、记录数、数据记录的时间范围和每秒记录数(VRS CLI tool)。
  • mcap info [file.mcap] “Report statistics about an MCAP file”(意为:报告一个 MCAP 文件的统计信息),而 mcap doctor [file.mcap] “Check an MCAP file structure”(意为:检查一个 MCAP 文件的结构)(MCAP CLI guide)。在装有 ROS 2 的环境中,ros2 bag info [my_bag] 也能给出一个 bag 的概要(录制容器:VRS、MCAP 与 GPMF)。

一个文件可以报告平均 30 fps,却仍然有冻结或跳过的帧;要看出这一点,办法是读取每一帧的时间戳(质量控制:丢帧和帧时序)。实用指南的质量控制页面描述了一个已发表的脚本,它用 ffprobe 做这件事,并对照目标值 1000 / fps 毫秒报告平均间隔、第 99 百分位间隔和最大间隔。它还提醒,较新的 FFmpeg 版本在 frame 部分报告的是 pts_time,而不是 pkt_pts_time。FFmpeg 6.1 正是这样:它的 frame 部分列出 pts_time、pkt_dts_time、best_effort_timestamp_time 和 duration_time。

把下面的内容保存为 frame_gaps.py。它向 ffprobe 索取视频流的帧率和每一帧的 pts_time,然后打印间隔统计以及每种间隔出现的次数:

import json, subprocess, sys
from collections import Counter
from fractions import Fraction
path = sys.argv[1]
def probe(entries):
out = subprocess.run(
["ffprobe", "-v", "error", "-select_streams", "v:0",
"-show_entries", entries, "-of", "json", path],
capture_output=True, text=True, check=True).stdout
return json.loads(out)
stream = probe("stream=r_frame_rate,avg_frame_rate")["streams"][0]
fps = float(sys.argv[2]) if len(sys.argv) > 2 else float(Fraction(stream["r_frame_rate"]))
t = sorted(float(f["pts_time"]) for f in probe("frame=pts_time")["frames"] if "pts_time" in f)
gaps = [(b - a) * 1000 for a, b in zip(t, t[1:])]
s = sorted(gaps)
r, avg = stream['r_frame_rate'], stream['avg_frame_rate']
p99 = s[int(0.99 * (len(s) - 1))]
print(f'frames: {len(t)}, first {t[0]:.3f} s, last {t[-1]:.3f} s')
print(f'r_frame_rate {r}, avg_frame_rate {avg}')
print(f'target gap: {1000 / fps:.3f} ms')
print(f'mean {sum(s) / len(s):.3f} ms, p99 {p99:.3f} ms, max {s[-1]:.3f} ms')
print('gap in ms, rounded to 0.1, and its count:')
for g, n in sorted(Counter(round(g, 1) for g in gaps).items()):
print(f' {g:8.1f} : {n}')

运行它,可以选择附上你在相机上设置的帧率:

终端窗口
python3 frame_gaps.py [your_file.mp4] [your_fps]

-select_streams v:0 选取第一路视频流,-show_entries frame=pts_time 解码这路数据流并打印每一帧的显示时间(ffprobe 文档)。文件长的话,这要花一些时间。

你应该看到几乎每一个间隔都等于目标值。在 GoPro 的 hero8.mp4 示例上,29.97 fps 下全部 378 个间隔都是 33.4 ms。

怎样解读。平均值在目标上而最大值很大,是一次画面冻结;第 99 百分位偏高,是反复出现的卡顿(第一人称素材的质量控制)。约为目标值两倍的间隔,是缺了一个帧间隔。有些录制程序改用重复帧的办法把丢帧掩盖起来:在 Android 上,MediaRecorder 为匹配音频而添加的帧意味着“帧序号 × (1/30 s)”不是一个可靠的时间(用智能手机做采集设备:不稳定的帧时序),而从 HERO9 起,GoPro 会记录 MSKP,即 “Number frames skips or duplicated from sensor image captured to encoded frame. Normally 0.”(意为:从传感器采集图像到编码帧之间被跳过或重复的帧数。正常为 0。)(对采集流程重要的 GPMF 键)。

你能检查什么取决于相机。

GoPro:GPMF 遥测轨道。按照 gpmf-parser README 给出的方法构建 GoPro 的演示工具,然后在你的文件上运行它:

终端窗口
git clone https://github.com/gopro/gpmf-parser
cd gpmf-parser/demo/
make
./gpmfdemo [path/to/your_file.mp4]

README 说这 “will return a brief overview of what metadata is stored in the given video file”(意为:会返回给定视频文件中存有哪些元数据的简要概览)。在演示程序的源码(demo/GPMF_demo.c)中,计算出的采样率默认会显示;-h 列出其他开关。

你应该看到视频帧率和帧数,然后是每一路数据流一行,例如 ACCL sampling rate = …Hz 和 GYRO sampling rate = …Hz。在 hero8.mp4 示例上,ACCL 和 GYRO 算出来都约为 202.47 Hz,GPS5 约为 18.17 Hz。实测速率与标称速率不同是意料之中的:GPMF 不为每个传感器存储采样率,因为 “Camera internal sensors or data via a connected Bluetooth device, will have timing errors in the range ±1%”(意为:相机内部的传感器,或经由所连接的蓝牙设备传来的数据,会有 ±1% 范围内的时序误差),而时钟是在采集之后对照 MP4 自己的时序推算出来的(用于采集的运动相机(GoPro):给 IMU 采样打时间戳)。

使用 OpenCamera Sensors 的 Android。每一次录制都在 DCIM/OpenCamera 中、视频旁边写出 CSV 文件:{VIDEO_NAME}_gyro.csv 和 {VIDEO_NAME}_accel.csv,每个采样带一个 timestamp (ns),以及 {VIDEO_NAME}_timestamps.csv,里面是每一个视频帧的时间戳(用智能手机做采集设备)。把 _timestamps.csv 的行数与第 6 步得到的帧数做比较,并由陀螺仪的第一个和最后一个时间戳算出它的速率。只有当相机的 SENSOR_INFO_TIMESTAMP_SOURCE 是 REALTIME 时,帧时间戳和 IMU 时间戳才可以相互比较;该应用的 README 说,启用 RecSync 时可以在预览消息上检查这一点:“Timestamp source”(时间戳来源)应当显示 “realtime”(实时)(Android 上的相机和 IMU 时间戳)。

其他相机。如果相机不记录运动数据,就把这一点记下来。一条需要由 SLAM 得到头部位姿的处理流程,会希望有一个与相机在同一时钟上的 IMU(第 3 课)。

The IMU fills the gaps between camera frames A 200 millisecond timeline. The camera takes a frame every 33 milliseconds, at 30 frames per second. The IMU reports every 5 milliseconds, at 200 Hz, so several inertial measurements fall between each pair of frames and carry the motion across the gap. Camera30 fps IMU200 Hz between frames, only the IMU knows how the head moved 200 ms shown
Camera frames are tens of milliseconds apart. The IMU reports many times in between, so it carries the motion across each gap, and through blur.

第 8 步:找到同步事件并测量时钟

Section titled “第 8 步:找到同步事件并测量时钟”

在每个灯光事件前后逐帧查看视频,例如用一个显示帧时间的播放器,并记下灯光已经发生变化的第一帧的时间。要找帧时间,用第 6 步得到的 pts_time 列表。

  1. 一台设备。把视频中两个灯光事件之间的时间,与你的挂钟时间笔记上两者之间的时间做比较。手写的时间比一帧粗得多,所以这检查的是文件的时间轴有没有大的错误,而不是时钟的漂移。
  2. 两台设备。在两段录制中都找到这两个事件。每个事件处的偏移,是两台设备给出的该事件时间之差。漂移是偏移的变化量除以两个事件之间的时间。实用指南的算例:开始时偏移 1240 ms,六十分钟后为 1312 ms,即 (1312 − 1240) ms ÷ 3,600,000 ms = 20 × 10⁻⁶,也就是 20 ppm(为什么同步很重要)。
Clock drift between two cameras over an hour A plot of the time offset between camera B and camera A against recording time. A cluster of QR readings at the start gives an offset of about 1240 ms, and a cluster at the end, sixty minutes later, about 1312 ms. A straight line through both clusters gives the offset for every frame in between; the 72 ms of drift corresponds to the clocks disagreeing by 20 parts per million. 13201300128012601240 015304560 Recording time (minutes) Offset, B minus A (ms) QR at the start QR at the end Every frame in between reads its offset off this line 72 ms of drift
Clock drift between two cameras over an hour. Sync readings at the start give an offset of about 1240 ms and readings at the end, sixty minutes later, about 1312 ms. A straight line through both gives the offset for every frame in between; 72 ms of drift in an hour means the clocks run 20 parts per million apart.

单个事件只能同步到最近的一帧,所以除非曝光是同步的,否则要预计每个偏移有最多一帧的不确定度(为什么同步很重要)。在 50 ppm 下,十分钟产生 30 ms 的漂移,在 30 fps 时约为一帧,所以一小段录制时段里看不出多少;C203 课程会正式地测量漂移。

为这个录制时段写一份简短的记录,就像处理流程的入库阶段所期望的那样。放在视频旁边的一个 JSON 文件就可以。其中包括:

  • 日期、操作员、场所类型、任务描述;
  • 第 1 步中的相机型号、序列号和设置;
  • 第 4 步中的文件名及其 SHA-256 校验和;
  • 第 5 步中找到的数据流和第 7 步中的传感器速率;
  • 第 6 步中的帧间隔结果;
  • 同步事件的时间,以及如果你在第 8 步测了的话,偏移和漂移;
  • 任何出了问题的地方。

这些字段对应已发表的处理流程所记录的内容:EgoVerse 给每次上传打上 operator、lab、task、embodiment、scene 和 objects 等字段,Egocentric-10K 则给每个片段配一个 .json,其中有 duration_sec、width、height、fps、size_bytes 和 codec(采集流程剖析,阶段 5;质量控制:清单)。这样入库脚本就可以对照文件本身检查每一个字段。

当你有了文件、一致的校验和、一份帧间隔报告、一份数据流及其速率的列表,以及录制时段记录,你就完成了。把它们留好:提示词页面上有逐一复核它们的提示词。