File size: 2,816 Bytes
e33425b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Measure native/export correspondence; acceptance tolerance is calibrated externally."""
from pathlib import Path
import numpy as np
from PIL import Image
from media import decode_rgb, probe, MediaError


def compare(video, scene, base, size=(160, 120)):
    frames = scene.get('frames', [])
    if scene.get('frames_complete') is not True or not frames:
        return {'status': 'unsupported', 'reason': 'Native render range incomplete'}
    metadata=probe(video)
    if metadata.get('decoded') is not True:
        return {'status':'verifier_error' if metadata.get('status')=='verifier_error' else 'mismatch',
                'reason':'Candidate video unavailable for correspondence','media_status':metadata.get('status')}
    if metadata.get('frame_count')!=len(frames):
        return {'status':'mismatch','reason':'Native/export ranges differ; explicit export-range mapping required',
                'native_frame_count':len(frames),'export_frame_count':metadata.get('frame_count')}
    try:
        candidate = decode_rgb(video, size, max_frames=len(frames))
    except MediaError as exc:
        return {'status': 'verifier_error', 'reason': str(exc)}
    if len(candidate) != len(frames):
        return {'status': 'mismatch', 'reason': 'Frame counts differ',
                'native_frame_count': len(frames), 'export_frame_count': len(candidate)}
    errors = []
    for index, frame in enumerate(frames):
        path = frame.get('image_path') or str(Path(base) / frame.get('image', ''))
        try:
            with Image.open(path) as image:
                # MP4 encodes the composited scene; transparent pixels composite
                # over black only when the scene background confirms black.
                if image.mode == 'RGBA' and image.getchannel('A').getextrema()[0] < 255:
                    if scene.get('transparent_composite_background') != 'black':
                        return {'status': 'unsupported', 'reason': 'Transparent scene export background not confirmed'}
                    background = Image.new('RGBA', image.size, (0, 0, 0, 255))
                    image = Image.alpha_composite(background, image)
                native = np.asarray(image.convert('RGB').resize(size, Image.Resampling.BOX))
            errors.append(float(np.mean(np.abs(native.astype(float) - candidate[index].astype(float))) / 255))
        except (OSError, ValueError) as exc:
            return {'status': 'verifier_error', 'reason': str(exc)}
    return {'status': 'measured', 'mean_absolute_rgb_error': float(np.mean(errors)),
            'maximum_frame_error': float(max(errors)), 'frame_errors': errors,
            'native_frame_count': len(frames), 'export_frame_count': len(candidate),
            'threshold_status': 'Requires original-vs-modified-runtime and codec calibration'}