Skip to content

Instantly share code, notes, and snippets.

@skipperkongen
Last active October 11, 2018 11:44
Show Gist options
  • Save skipperkongen/3e6898deaffcdb1c7b3727b7d449b24b to your computer and use it in GitHub Desktop.
Save skipperkongen/3e6898deaffcdb1c7b3727b7d449b24b to your computer and use it in GitHub Desktop.
Capture video from web cam on Mac
import cv2
from time import sleep
CLASSES = ['SAFE', 'DANGER']
NEG_IDX = 0
POS_IDX = 1
FRAMES_PER_VIDEO = 100
VIDEOS_PER_CLASS = 2
def capture(num_frames, path='out.avi'):
# Create a VideoCapture object
cap = cv2.VideoCapture(0)
# Check if camera opened successfully
if (cap.isOpened() == False):
print("Unable to read camera feed")
# Default resolutions of the frame are obtained.The default resolutions are system dependent.
# We convert the resolutions from float to integer.
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# Define the codec and create VideoWriter object.The output is stored in 'outpy.avi' file.
out = cv2.VideoWriter(path, cv2.VideoWriter_fourcc('M','J','P','G'), 10, (frame_width,frame_height))
print('Recording started')
for i in range(num_frames):
ret, frame = cap.read()
if ret == True:
# Write the frame into the file 'output.avi'
out.write(frame)
# When everything done, release the video capture and video write objects
cap.release()
out.release()
for take in range(VIDEOS_PER_CLASS):
for cla in CLASSES:
path = 'data/{}{}.avi'.format(cla, take)
print('Get ready to act:', cla)
# Only works on Mac
subprocess.call(['say', 'get ready to act {}'.format(cla)])
capture(FRAMES_PER_VIDEO, path=path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment