Last active
October 11, 2018 11:44
-
-
Save skipperkongen/3e6898deaffcdb1c7b3727b7d449b24b to your computer and use it in GitHub Desktop.
Capture video from web cam on Mac
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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