mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-09-30 19:33:45 +08:00
ui diff: compare frame hashes instead of temp files (#37154)
* refactor: streamline frame comparison by using frame hashes instead of extracting frames * add vsync Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
import os
|
||||
import sys
|
||||
import subprocess
|
||||
import tempfile
|
||||
import webbrowser
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
@@ -11,17 +10,18 @@ from openpilot.common.basedir import BASEDIR
|
||||
DIFF_OUT_DIR = Path(BASEDIR) / "selfdrive" / "ui" / "tests" / "diff" / "report"
|
||||
|
||||
|
||||
def extract_frames(video_path, output_dir):
|
||||
output_pattern = str(output_dir / "frame_%04d.png")
|
||||
cmd = ['ffmpeg', '-i', video_path, '-vsync', '0', output_pattern, '-y']
|
||||
subprocess.run(cmd, capture_output=True, check=True)
|
||||
frames = sorted(output_dir.glob("frame_*.png"))
|
||||
return frames
|
||||
|
||||
|
||||
def compare_frames(frame1_path, frame2_path):
|
||||
result = subprocess.run(['cmp', '-s', frame1_path, frame2_path])
|
||||
return result.returncode == 0
|
||||
def extract_framehashes(video_path):
|
||||
cmd = ['ffmpeg', '-i', video_path, '-map', '0:v:0', '-vsync', '0', '-f', 'framehash', '-hash', 'md5', '-']
|
||||
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
||||
hashes = []
|
||||
for line in result.stdout.splitlines():
|
||||
if not line or line.startswith('#'):
|
||||
continue
|
||||
parts = line.split(',')
|
||||
if len(parts) < 4:
|
||||
continue
|
||||
hashes.append(parts[-1].strip())
|
||||
return hashes
|
||||
|
||||
|
||||
def create_diff_video(video1, video2, output_path):
|
||||
@@ -32,34 +32,26 @@ def create_diff_video(video1, video2, output_path):
|
||||
|
||||
|
||||
def find_differences(video1, video2):
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
tmpdir = Path(tmpdir)
|
||||
print(f"Hashing frames from {video1}...")
|
||||
hashes1 = extract_framehashes(video1)
|
||||
|
||||
print(f"Extracting frames from {video1}...")
|
||||
frames1_dir = tmpdir / "frames1"
|
||||
frames1_dir.mkdir()
|
||||
frames1 = extract_frames(video1, frames1_dir)
|
||||
print(f"Hashing frames from {video2}...")
|
||||
hashes2 = extract_framehashes(video2)
|
||||
|
||||
print(f"Extracting frames from {video2}...")
|
||||
frames2_dir = tmpdir / "frames2"
|
||||
frames2_dir.mkdir()
|
||||
frames2 = extract_frames(video2, frames2_dir)
|
||||
if len(hashes1) != len(hashes2):
|
||||
print(f"WARNING: Frame count mismatch: {len(hashes1)} vs {len(hashes2)}")
|
||||
min_frames = min(len(hashes1), len(hashes2))
|
||||
hashes1 = hashes1[:min_frames]
|
||||
hashes2 = hashes2[:min_frames]
|
||||
|
||||
if len(frames1) != len(frames2):
|
||||
print(f"WARNING: Frame count mismatch: {len(frames1)} vs {len(frames2)}")
|
||||
min_frames = min(len(frames1), len(frames2))
|
||||
frames1 = frames1[:min_frames]
|
||||
frames2 = frames2[:min_frames]
|
||||
print(f"Comparing {len(hashes1)} frames...")
|
||||
different_frames = []
|
||||
|
||||
print(f"Comparing {len(frames1)} frames...")
|
||||
different_frames = []
|
||||
for i, (h1, h2) in enumerate(zip(hashes1, hashes2, strict=False)):
|
||||
if h1 != h2:
|
||||
different_frames.append(i)
|
||||
|
||||
for i, (f1, f2) in enumerate(zip(frames1, frames2, strict=False)):
|
||||
is_different = not compare_frames(f1, f2)
|
||||
if is_different:
|
||||
different_frames.append(i)
|
||||
|
||||
return different_frames, len(frames1)
|
||||
return different_frames, len(hashes1)
|
||||
|
||||
|
||||
def generate_html_report(video1, video2, basedir, different_frames, total_frames, diff_video_name):
|
||||
|
||||
Reference in New Issue
Block a user