Skip to content

Instantly share code, notes, and snippets.

@cinjon
Last active June 23, 2020 20:10
Show Gist options
  • Save cinjon/3cc72d91e7a5610f00671bd16c647a48 to your computer and use it in GitHub Desktop.
Save cinjon/3cc72d91e7a5610f00671bd16c647a48 to your computer and use it in GitHub Desktop.
# Get the eigenvectors
F, T, H, W = activations.shape
points = activations.reshape([F, T*H*W]).transpose()
A = construct_affinity_mat(points, sigmas=[6.0])
eigvecs = wncuts(A, num_eigenvectors=100)
eigvecs_reshaped = eigvecs.reshape([T, H, W, -1])
# frame_shape is [h, w, c]
frame_shape = frames[0].shape[:2]
target_vec = eigvecs_reshaped[0, 0, 0]
l2_diff = np.linalg.norm(eigvecs_reshaped - target_vec, axis=3)
hist = np.histogram(l2_diff, bins=25)
for count, interval in zip(hist[0], hist[1]):
print('interval ', interval, count)
l2_diff = np.exp(.01 * -l2_diff**2)
histexp = np.histogram(l2_diff, bins=25)
for count, interval in zip(histexp[0], histexp[1]):
print('interval ', interval, count)
# nearest neighbor from skimage.transform, colors from matplotlib
resized_img = resize(l2_diff, [len(frames), frame_shape[0], frame_shape[1]], order=0)
norm = colors.Normalize(resized_img.min(), resized_img.max())
cmap = plt.cm.RdBu
# get the image w/o the alpha channel.
rgb_img = np.delete(cmap(norm(resized_img)), 3, 3)
for num, (frame, resized) in enumerate(zip(frames, rgb_img)):
image = Image.fromarray(frame)
image = image.convert('HSV')
frame_hsv = np.array(image)
image = Image.fromarray((resized.squeeze() * 255).astype(np.uint8))
image = image.convert('HSV')
resized_hsv = np.array(image)
luma = frame_hsv[:, :, 2:]
chroma = resized_hsv[:, :, 1:2]
newimg = np.concatenate([resized_hsv[:, :, 0:1], chroma, luma], 2)
Image.fromarray(newimg, "HSV").convert(mode="RGB").save('combined.%04d.png' % num)
clips = [ImageClip(os.path.join('combined.%04d.png' % i)).set_duration(1./8)
for i in range(len(frames))]
concat_clip = concatenate_videoclips(clips, method="compose")
concat_clip.write_videofile("im-a-videofile.mp4", fps=8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment