import cv2
import sys

def genBuffMask(bufferFrames):
    'create bitwise mask for buffer length'
    buffMask = 1
    for i in range(0, BUFF_LEN-1):
        buffMask = (buffMask)<<1 | buffMask
    return buffMask

BUFF_LEN = 20 # 30 seems to be optimal number of frames for buffer

buffMask = genBuffMask(BUFF_LEN)

print sys.argv, len(sys.argv)
#cascPath = sys.argv[1]
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")

# input which camera to use
#if len(sys.argv)==3:
#    if sys.argv[2] == 0:
#        camera = 0
#    elif sys.argv[2] == 1:
#        camera = 1
#    else:
#        camera = 0
#else:
#    camera = 0
cap = cv2.VideoCapture(0)

# create the VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MJPG') # MJPG is encoding supported by Windows
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

currBuff = 0;

while True:

    # Capture frame-by-frame
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # begin face cascade
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.25,
        minNeighbors=5,
        minSize=(30, 30),
    )


    # updates current buffer
    pastBuff = currBuff
    currBuff = ( (currBuff << 1) | (len(faces)>0) ) & buffMask

    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    if(currBuff > 0):
        out.write(frame)
    #elif((pastBuff>0) & (currBuff == 0)): # after face detected, of face disappears for BUFF_LEN frames, stop recording
    #    break

    # Display the resulting frame
    cv2.imshow('Video', frame)
    # cv2.imshow('Video', gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
       break

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

@@ -20,846 +20,221 @@
 ys%0A%0A
-def genBuffMask(bufferFrames):%0A    'create bitwise mask for buffer length'%0A    buffMask = 1%0A    for i in range(0, BUFF_LEN-1):%0A        buffMask = (buffMask)%3C%3C1 %7C buffMask%0A    return buffMask%0A%0ABUFF_LEN = 20 # 30 seems to be optimal number of frames for buffer%0A%0AbuffMask = genBuffMask(BUFF_LEN)%0A%0Aprint sys.argv, len(sys.argv)%0A#cascPath = sys.argv%5B1%5D%0AfaceCascade = cv2.CascadeClassifier(%22haarcascade_frontalface_default.xml%22)%0A%0A# input which camera to use%0A#if len(sys.argv)==3:%0A#    if sys.argv%5B2%5D == 0:%0A#        camera = 0%0A#    elif sys.argv%5B2%5D == 1:%0A#        camera = 1%0A#    else:%0A#        camera = 0%0A#else:%0A#    camera = 0%0Acap = cv2.VideoCapture(0)%0A%0A# create the VideoWriter object%0Afourcc = cv2.VideoWriter_fourcc(*'MJPG') # MJPG is encoding supported by Windows%0Aout = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))%0A%0AcurrBuff = 0;
+# Create a Haar-like feature cascade classifier object%0AfaceCascade = cv2.CascadeClassifier(%22haarcascade_frontalface_default.xml%22)%0A%0A# Set the camera to the first camera detected by the NUC%0Acap = cv2.VideoCapture(0)
 %0A%0Awh
@@ -243,17 +243,16 @@
 e True:%0A
-%0A
     # Ca
@@ -520,405 +520,103 @@
  )%0A%0A
-%0A    # updates current buffer%0A    pastBuff = currBuff%0A    currBuff = ( (currBuff %3C%3C 1) %7C (len(faces)%3E0) ) & buffMask%0A%0A    for (x, y, w, h) in faces:%0A        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)%0A%0A    if(currBuff %3E 0):%0A        out.write(frame)%0A    #elif((pastBuff%3E0) & (currBuff == 0)): # after face detected, of face disappears for BUFF_LEN frames, stop recording%0A    #    break
+    for (x, y, w, h) in faces:%0A        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
 %0A%0A  
@@ -682,39 +682,68 @@
 me)%0A
+%0A
     # 
-cv2.imshow('Video', gray)
+If the user presses the 'q' key then quit the program
 %0A   
@@ -846,36 +846,8 @@
 ure%0A
-out.release()%0Acap.release()%0A
 cv2.

Simplified the example