AndyZ2001's picture
Upload 27 completed Mingru MuJoCo runs with full campaign state and provenance (part 9)
37d21bb verified
Raw History Blame Contribute Delete
3.91 kB
"""What an agent receives says nothing about where its inputs came from.
Staging copies media through here: videos are remuxed without container or stream metadata (no re-encode; rotation is
kept, audio is dropped), JPEG and PNG files lose EXIF, comments and text chunks without being re-encoded. Text files are
written by the staging code itself with neutral words. `SOURCE_WORDS` is the deny-list the tests scan every staged
snapshot with, text and media metadata both. Nothing under data/ is changed.
"""
from pathlib import Path
import re
import shutil
import struct
import subprocess
import imageio_ffmpeg
# Dataset, capture-device and lab names that would tell an agent where an input came from. Lab and brand names that are also
# ordinary words (an apple, a rail) count only in their proper spelling.
SOURCE_WORDS = re.compile(
r"arkit|iphone|ipad|lidar|\bdroid\b|\bycb|deform360|\bgauge\b|hypersim|\breplica\b|realestate10k|room_capture|\brcd_|samsung|"
r"android|core ?media|google|\bcalli\b|berkeley|brics|odroid|\d{4}-\d{2}-\d{2}-\d{2}h-\d{2}m", re.I)
SOURCE_NAMES = re.compile(r"\b(Apple|Pixel|AUTOLab|CLVR|IRIS|TRI|ILIAD|RAIL|IPRL|GuptaLab|PennPAL|RPL|WEIRD|REAL)\b")
def source_words(text):
"""Source words found in text."""
return [m.group(0) for pattern in (SOURCE_WORDS, SOURCE_NAMES) for m in pattern.finditer(text)]
def clean_video(source, destination):
"""Stream copy of the video track only, with no metadata; the display rotation is side data and stays."""
subprocess.run([imageio_ffmpeg.get_ffmpeg_exe(), "-v", "error", "-y", "-i", str(source), "-map", "0:v", "-c", "copy",
"-map_metadata", "-1", "-map_chapters", "-1", "-metadata:s:v", "handler_name=", "-fflags", "+bitexact",
"-movflags", "+faststart", str(destination)], check=True)
def _clean_jpeg(data):
"""Drop APP1-APP15 (EXIF, XMP, ICC kept out too) and COM segments; the scan data is copied as is."""
if data[:2] != b"\xff\xd8":
raise ValueError("not a JPEG")
out, i = [data[:2]], 2
while i < len(data):
if data[i] != 0xFF:
raise ValueError("broken JPEG segment")
marker = data[i+1]
if marker == 0xDA: # start of scan: the rest is image data
out.append(data[i:])
break
length = struct.unpack(">H", data[i+2:i+4])[0]
if not (0xE1 <= marker <= 0xEF or marker == 0xFE):
out.append(data[i:i+2+length])
i += 2+length
return b"".join(out)
def _clean_png(data):
"""Keep only the chunks that draw the image."""
if data[:8] != b"\x89PNG\r\n\x1a\n":
raise ValueError("not a PNG")
out, i = [data[:8]], 8
while i < len(data):
length = struct.unpack(">I", data[i:i+4])[0]
kind = data[i+4:i+8]
if kind in (b"IHDR", b"PLTE", b"IDAT", b"IEND", b"tRNS", b"gAMA", b"sRGB", b"cHRM", b"sBIT", b"pHYs"):
out.append(data[i:i+12+length])
i += 12+length
return b"".join(out)
def copy_media(source, destination):
"""Copy one input file for an agent, without metadata that could name its source."""
source, destination = Path(source), Path(destination)
suffix = source.suffix.lower()
if suffix in {".mp4", ".mov", ".m4v", ".mkv", ".webm"}:
clean_video(source, destination)
elif suffix in {".jpg", ".jpeg"}:
destination.write_bytes(_clean_jpeg(source.read_bytes()))
elif suffix == ".png":
destination.write_bytes(_clean_png(source.read_bytes()))
else:
shutil.copyfile(source, destination)
def media_tags(path):
"""The file's own metadata as ffmpeg reads it (the input section of its report), as text for the tests' scan."""
report = subprocess.run([imageio_ffmpeg.get_ffmpeg_exe(), "-hide_banner", "-i", str(path)], capture_output=True, text=True).stderr
return report.split("At least one output file")[0]