| 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}") | |