It's a complete note. .. I feel like I use it often (?)
--This is a safety measure
from skvideo.io import vread
gif = vread("sample.gif")
# print(gif.shape) # => (Example): (21, 600, 600, 3)
――But this is slow, so I think ↓ is better
--Fast
import cv2
import numpy as np
# faster than `vread` in `skvideo.io`
def vread(path, T=30):
cap = cv2.VideoCapture(path)
gif = [cap.read()[1][:,:,::-1] for i in range(T)]
gif = np.array(gif)
cap.release()
return gif
gif = vread("sample.gif", T=21)
# print(gif.shape) # => (Example): (21, 600, 600, 3)
――The only drawback is that you have to give the number of frames.
--Example of normal writing without giving the number of frames (not list contents) ↓
import cv2
filepath = "data/raw/GH010005-0.mp4"
cap = cv2.VideoCapture(filepath)
while(cap.isOpened()):
ret, frame = cap.read()
if ret:
cv2.imshow("Frame", frame)
cv2.waitKey(1)
else:
cap.release()
cv2.destroyAllWindows()
import cv2
cv2.namedWindow("frame", cv2.WINDOW_NORMAL)
for t in range(len(gif)):
cv2.imshow("frame", gif[t])
cv2.waitKey(50) #It will not be displayed unless you wait for a while
cv2.destroyAllWindows()
import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(21,9))
for t in range(21):
plt.subplot(3,7,t+1)
plt.imshow(gif[t])
plt.show()
import moviepy.editor as mpy
def npy_to_gif(npy, filename):
clip = mpy.ImageSequenceClip(list(npy), fps=10)
clip.write_gif(filename)
--OpenCV wins!
(Gif borrowed from here https://miro.medium.com/max/1200/1*LnQ5sRu-tJmlvRWmDsdSvw.gif)
Recommended Posts