Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nulllpointer/21d65041bfcf95bd049eaf807db70ac4 to your computer and use it in GitHub Desktop.
Save nulllpointer/21d65041bfcf95bd049eaf807db70ac4 to your computer and use it in GitHub Desktop.

Revisions

  1. @keithweaver keithweaver created this gist May 10, 2017.
    43 changes: 43 additions & 0 deletions split-video-by-frame.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    '''
    Using OpenCV takes a mp4 video and produces a number of images.
    Requirements
    ----
    You require OpenCV 3.2 to be installed.
    Run
    ----
    Open the main.py and edit the path to the video. Then run:
    $ python main.py
    Which will produce a folder called data with the images. There will be 2000+ images for example.mp4.
    '''
    import cv2
    import numpy as np
    import os

    # Playing video from file:
    cap = cv2.VideoCapture('example.mp4')

    try:
    if not os.path.exists('data'):
    os.makedirs('data')
    except OSError:
    print ('Error: Creating directory of data')

    currentFrame = 0
    while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Saves image of the current frame in jpg file
    name = './data/frame' + str(currentFrame) + '.jpg'
    print ('Creating...' + name)
    cv2.imwrite(name, frame)

    # To stop duplicate images
    currentFrame += 1

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()