Background Currently, I am creating a web page that introduces minor OpenCV by myself. https://featurepoints.jp/opencv_doc/
So, I am doing image processing by converting GIF from GIFMAGAZIN to MP4 as a data set, but as it is cv2.imshow ("window name" " When I try to play a video with, mat) , it loops like the browser display and ends in a short time without displaying repeatedly.
Here, I will write how to loop the video playback using cv2.imshow ().
gif to mp4 There are two ways to convert from gif to mp4. One is to display the gif in a browser, right-click and select "Save as video as" to convert. The other is to convert using ffmpeg. (See Convert gif animation to mp4 with FFmpeg)
ffmpeg -i [gif file path] -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" [mp4 file path]
Method How to display repeatedly
Development
The video ends with [ESC], but you can save the frame image by pressing [S].
import numpy as np
import cv2
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--src')
args = parser.parse_args()
cap = cv2.VideoCapture(args.src)
fps = cap.get(cv2.CAP_PROP_FPS)
frames = []
#Play the video once and store the frames in an array
while cap.isOpened():
_ret, frame = cap.read()
if frame is None:
break
frames.append(frame)
cap.release()
end = False
while True:
if end:
break
for frame in frames:
cv2.imshow("loop a gif",frame)
k = cv2.waitKey(int(fps)) & 0xFF
#End with ESC
if k == 27:
end = True
break
#Save image with S
elif k == ord("s"):
cv2.imwrite("output.png ", frame)
if __name__ == '__main__':
main()
Consequence

Afterword The video (image) data is Stop Sento-kun! I tried using GIF game. If you press the S button at the right time, the image displayed as "nice" will be saved. It's quite difficult to grasp. Have a nice time: cake:
Recommended Posts