File size: 1,105 Bytes
2bfd19c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np
import sys


def load_frames(path):
    cap = cv2.VideoCapture(path)
    frames = []
    while True:
        ret, img = cap.read()
        if not ret:
            break
        frames.append(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    cap.release()
    return frames


def psnr(gt, gen):
    gt = np.stack(gt).astype(np.float32)
    gen = np.stack(gen).astype(np.float32)
    if gt.shape != gen.shape:
        h, w = gt.shape[1], gt.shape[2]
        gen = np.stack([cv2.resize(f, (w, h)) for f in gen])
    mse = np.mean((gt - gen) ** 2)
    if mse == 0:
        return float("inf")
    return 10 * np.log10(255**2 / mse)


def black_ratio(frames, thresh=5):
    arr = np.stack(frames)
    return float(np.mean(arr.max(axis=-1) < thresh))


if __name__ == "__main__":
    gt_path = sys.argv[1]
    gen_path = sys.argv[2]
    gt = load_frames(gt_path)
    gen = load_frames(gen_path)
    print(f"GT: {len(gt)} frames {gt[0].shape}")
    print(f"Gen: {len(gen)} frames {gen[0].shape}")
    print(f"PSNR: {psnr(gt, gen):.2f} dB")
    print(f"Black ratio: {black_ratio(gen):.4f}")