Last active
July 14, 2021 16:05
-
-
Save lukoshkin/6205455d5b931dcdbd923323003058f0 to your computer and use it in GitHub Desktop.
ACD metric (or similar to the one that is) used in mocogan
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
def ACD(filename): | |
""" | |
Calculates Average Content Distance for the given short video | |
Parameters: | |
----------- | |
filename : str or path-like - path to .mp4 or .gif file | |
""" | |
ViCap = cv2.VideoCapture(filename) | |
frames = [] | |
success = True | |
while success: | |
success, image = ViCap.read() | |
if success: | |
frames += [image] | |
ViCap.release() | |
cv2.destroyAllWindows() | |
frames = np.array(frames, dtype='int32') | |
assert len(frames) > 0, \ | |
"Sth went wrong, no frames were extracted" | |
N = np.multiply.reduce(frames.shape[1:-1]) | |
res = np.mean( | |
np.linalg.norm( | |
np.diff( | |
np.einsum('ijkl->il', frames), | |
axis=0) / N, | |
axis=1) | |
) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment