Created
July 20, 2021 13:04
-
-
Save Hanlin-Dong/a4ad8fceeeb651faab4fe04b3c902262 to your computer and use it in GitHub Desktop.
Capture a video into image every N seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
from os import mkdir | |
def capture(name, interval): | |
"""Capture a video into a series of pictures | |
Args: | |
name (str): The file name of the video | |
interval (float): The time interval to split the video, in seconds. | |
""" | |
video = cv2.VideoCapture(f"{name}") | |
fps = video.get(5) | |
every = int(interval * fps) | |
folder = name.replace(".", "_") | |
mkdir(folder) | |
success, frame = video.read() | |
i, j = 0, 0 | |
while success: | |
i += 1 | |
if (i % every == 0): | |
j += 1 | |
cv2.imwrite(f"{folder}/{j}.jpg", frame) | |
success, frame = video.read() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment