Skip to content

Instantly share code, notes, and snippets.

@sayakpaul
Last active December 28, 2019 11:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sayakpaul/d8f4bcd25df9f728ce82496dabaa248a to your computer and use it in GitHub Desktop.
Save sayakpaul/d8f4bcd25df9f728ce82496dabaa248a to your computer and use it in GitHub Desktop.
def separate_frames(output_dir, df):
if not os.path.exists(output_dir):
os.mkdir(output_dir)
# storing the frames from training videos
for i in tqdm(range(df.shape[0])):
count = 0
videoFile = df['video_name'][i]
videoPath = os.path.join("Videos", "UCF-101", videoFile)
cap = cv2.VideoCapture(videoPath) # capturing the video from the given path
frameRate = cap.get(5) #frame rate
x=1
while(cap.isOpened()):
frameId = cap.get(1) #current frame number
(grabbed, frame) = cap.read()
if (grabbed != True):
break
if (frameId % math.floor(frameRate) == 0):
# storing the frames in a new folder named train_1
frameName = videoFile.split('/')[1].split(' ')[0] + "_frame%d.jpg" % count
filename = os.path.join(output_dir, frameName)
count+=1
cv2.imwrite(filename, frame)
cap.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment