Last active
January 13, 2022 07:23
-
-
Save pietrocolombo/3484d071304dd1820727a2db55883311 to your computer and use it in GitHub Desktop.
Extract frame from a video
This file contains hidden or 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 | |
import os | |
# Read the video from specified path | |
cam = cv2.VideoCapture("/content/gdrive/MyDrive/Foto & Video/2021/Casargo/Volo 2/DJI_0049.MP4") | |
try: | |
# creating a folder named data | |
if not os.path.exists('/content/gdrive/MyDrive/Foto & Video/2021/Casargo/Volo 2/DJI_0049'): | |
os.makedirs('/content/gdrive/MyDrive/Foto & Video/2021/Casargo/Volo 2/DJI_0049') | |
# if not created then raise error | |
except OSError: | |
print ('Error: Creating directory of data') | |
# frame | |
currentframe = 0 | |
while(True): | |
# reading from frame | |
ret,frame = cam.read() | |
if ret: | |
# if video is still left continue creating images | |
# save frame | |
name = '/content/gdrive/MyDrive/Foto & Video/2021/Casargo/Volo 2/DJI_0049/frame' + str(currentframe) + '.jpg' | |
print ('Creating...' + name) | |
# writing the extracted images | |
cv2.imwrite(name, frame) | |
# increasing counter so that it will | |
# show how many frames are created | |
currentframe += 30 # i.e. at 30 fps, this advances one second | |
cam.set(1, currentframe) | |
else: | |
break | |
# Release all space and windows once done | |
cam.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment