Skip to content

Instantly share code, notes, and snippets.

@hdh7485
Created August 15, 2021 16:00
Show Gist options
  • Save hdh7485/17c624296e5351716bb34de87e63c295 to your computer and use it in GitHub Desktop.
Save hdh7485/17c624296e5351716bb34de87e63c295 to your computer and use it in GitHub Desktop.
Extract images from videos that saved in same directory.
import cv2
import math
import os
video_files = next(os.walk('./'), (None, None, []))[2]
for video in video_files:
print(video)
cap = cv2.VideoCapture(video)
frameRate = cap.get(5) #frame rate
x=1
if not os.path.isdir(video):
file_path = os.path.splitext(video)[0]
file_name = file_path.split('/')[-1]
os.makedirs(file_name)
while(cap.isOpened()):
frameId = cap.get(1) #current frame number
ret, frame = cap.read()
if (ret != True):
break
if (frameId % math.floor(frameRate) == 0):
filename = './'+file_name+'/image' + str(int(x)) + ".jpg";x+=1
print(filename)
cv2.imwrite(filename, frame)
cap.release()
print ("Done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment