Spaces:
Runtime error
Runtime error
File size: 1,794 Bytes
04fbff5 |
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
import numpy as np
from tqdm import tqdm
import cv2
from vbench.utils import load_dimension_info
def get_frames(video_path):
frames = []
video = cv2.VideoCapture(video_path)
while video.isOpened():
success, frame = video.read()
if success:
frames.append(frame)
else:
break
video.release()
assert frames != []
return frames
def mae_seq(frames):
ssds = []
for i in range(len(frames)-1):
ssds.append(calculate_mae(frames[i], frames[i+1]))
return np.array(ssds)
def calculate_mae(img1, img2):
"""Computing the mean absolute error (MAE) between two images."""
if img1.shape != img2.shape:
print("Images don't have the same shape.")
return
return np.mean(cv2.absdiff(np.array(img1, dtype=np.float32), np.array(img2, dtype=np.float32)))
def cal_score(video_path):
"""please ensure the video is static"""
frames = get_frames(video_path)
score_seq = mae_seq(frames)
return (255.0 - np.mean(score_seq).item())/255.0
def temporal_flickering(video_list):
sim = []
video_results = []
for video_path in tqdm(video_list):
try:
score_per_video = cal_score(video_path)
except AssertionError:
continue
video_results.append({'video_path': video_path, 'video_results': score_per_video})
sim.append(score_per_video)
avg_score = np.mean(sim)
return avg_score, video_results
def compute_temporal_flickering(json_dir, device, submodules_list):
video_list, _ = load_dimension_info(json_dir, dimension='temporal_flickering', lang='en')
all_results, video_results = temporal_flickering(video_list)
return all_results, video_results
|