Skip to content

Instantly share code, notes, and snippets.

@lukoshkin
Last active July 14, 2021 16:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukoshkin/6205455d5b931dcdbd923323003058f0 to your computer and use it in GitHub Desktop.
Save lukoshkin/6205455d5b931dcdbd923323003058f0 to your computer and use it in GitHub Desktop.
ACD metric (or similar to the one that is) used in mocogan
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