take screenshot of video python 1

take screenshot of video python

import cv2
import os
import time

def get_frames(inputFile,outputFolder,step,count):

    inputFile - name of the input file with directoy
    outputFolder - name and path of the folder to save the results
    step - time lapse between each step (in seconds)
    count - number of screenshots
    'count' number of screenshots that are 'step' seconds apart created from video 'inputFile' and stored in folder 'outputFolder'
  Function Call:
    get_frames("test.mp4", 'data', 10, 10)

  #initializing local variables
  step = step
  frames_count = count

  currentframe = 0
  frames_captured = 0

  #creating a folder
      # creating a folder named data 
      if not os.path.exists(outputFolder): 
  #if not created then raise error 
  except OSError: 
      print ('Error! Could not create a directory') 
  #reading the video from specified path 
  cam = cv2.VideoCapture(inputFile) 

  #reading the number of frames at that particular second
  frame_per_second = cam.get(cv2.CAP_PROP_FPS)

  while (True):
      ret, frame = cam.read()
      if ret:
          if currentframe > (step*frame_per_second):  
              currentframe = 0
              #saving the frames (screenshots)
              name = './data/frame' + str(frames_captured) + '.jpg'
              print ('Creating...' + name) 
              cv2.imwrite(name, frame)       
              #breaking the loop when count achieved
              if frames_captured > frames_count-1:
                ret = False
          currentframe += 1           
      if ret == False:
  #Releasing all space and windows once done

Here is what the above code is Doing:
1. We are creating a folder named data.
2. We are reading the video from the specified path.
3. We are reading the number of frames at that particular second.
4. We are creating a loop to read the frames.
5. We are saving the frames in the folder.
6. We are breaking the loop when the count is achieved.
7. We are releasing all the space and windows once done.

Similar Posts