How to loop and play gif video with openCV

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

  1. Play the video once and store all the frames in an array
  2. Play the video in an infinite loop
  3. Set termination conditions during processing You can do it.

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

output.gif

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

How to loop and play gif video with openCV
Loop video loading with opencv
[How to!] Learn and play Super Mario with Tensorflow !!
[Python] How to play with class variables with decorator and metaclass
Convert video to black and white with ffmpeg + python + opencv
Fractal to make and play with Python
How to make a surveillance camera (Security Camera) with Opencv and Python
Try to separate the background and moving object of the video with OpenCV
[REAPER] How to play with Reascript in Python
How to crop an image with Python + OpenCV
[Python] How to handle Japanese characters with openCV
How to share folders with Docker and Windows with tensorflow
How to extract null values and non-null values with pandas
How to take a captured image from a video (OpenCV)
How to get RGB and HSV histograms in OpenCV
How to update with SQLAlchemy?
How to cast with Theano
How to Alter with SQLAlchemy?
How to separate strings with','
How to RDP with Fedora31
How to Delete with SQLAlchemy?
Add fake tilt shift effect to video with OpenCV + Python
How to do Bulk Update with PyMySQL and notes [Python]
[Let's play with Python] Image processing to monochrome and dots
How to create dataframes and mess with elements in pandas
How to log in to AtCoder with Python and submit automatically
Shining life with Python and OpenCV
Python: How to use async with
Neural network with OpenCV 3 and Python 3
How to install and use Tesseract-OCR
Play with Poincare series and SymPy
How to import CSV and TSV files into SQLite with Python
How to use virtualenv with PowerShell
How to deal with imbalanced data
How to install python-pip with ubuntu20.04LTS
How to deal with errors when installing whitenoise and deploying to Heroku
How to deal with imbalanced data
How to install OpenCV on Mac
How to run Jupyter and Spark on Mac with minimal settings
How to get started with Scrapy
How to get started with Python
Create a video player with PySimpleGUI + OpenCV 2 Add ROI setting and save function (DIVX, MJPG, GIF)
How to deal with DistributionNotFound errors
How to install and configure blackbird
How to use .bash_profile and .bashrc
How to get started with Django
How to install CUDA and nvidia-driver
How to save only a part of a long video using OpenCV
How to install and use Graphviz
How to deal with errors when installing Python and pip with choco
How to use FTP with Python
Load gif images with Python + OpenCV
Prepare an environment to use OpenCV and Pillow with AWS Lambda
How to calculate date with python
How to install mysql-connector with pip3
How to INNER JOIN with SQLAlchemy
[ROS2] How to play a bag file with python format launch
How to use Python with Jw_cad (Part 2 Command explanation and operation)
How to install Anaconda with pyenv
How to build Python and Jupyter execution environment with VS Code
Draw shapes with OpenCV and PIL