How to detect closed eyes for three seconds?
<p>I have discovered the parts such as the face, eyes, 2 containers eyes. Normally, I can see face, eyes area and two eyes. When I see face, eyes area but not see two eyes, that's when detected eyes closed. And now, I want to detect when eyes closed for three seconds.Someone can suggest me a solution. I tried to time.sleep () function but it doesn't working . It makes Videostream process stopped.</p>

<pre><code>cas_path = os.getcwd()
eye_path = os.getcwd()
two_eyes_path = os.getcwd()

cas_path += "/haarcascade_frontalface_alt.xml"
eye_path += "/haarcascade_mcs_eyepair_big.xml"
two_eyes_path += "/haarcascade_eye.xml"


faceCascade = cv2.CascadeClassifier(cas_path)
eyesCascade = cv2.CascadeClassifier(eye_path)
twoeyesCascade = cv2.CascadeClassifier(two_eyes_path)

class VideoCamera(object):

    def __init__(self):

    self.status = "Sharing ?"
    self._image = np.zeros((100,200))
    self.video = cv2.VideoCapture(0)
    (self.video).set(3, 200)
    (self.video).set(4, 160)
    #success, self._image = self.video.read()
    # If you decide to use video.mp4, you must have this file in the folder
    # as the main.py.
    # self.video = cv2.VideoCapture('video.mp4')

def __del__(self):
    self.video.release()
def get_frame(self):
    global s
    s = ''
    global string
    string = ''
    success, image = self.video.read()
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.3,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )


    count  = 0

    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x + w, y + h), (255, 255, 0), 2)          
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = image[y:y+h, x:x+w]
        eyes = eyesCascade.detectMultiScale(roi_gray)
        if eyes is not():
            for (ex,ey,ew,eh) in eyes:
                cv2.rectangle(roi_color,(ex -10 ,ey - 10),(ex+ew + 10,ey+eh + 10),(0,255,0),2)
                twoeyes = twoeyesCascade.detectMultiScale(roi_gray)
                firsttime = 1
                if twoeyes is not():
                    for (exx,eyy,eww,ehh) in twoeyes:
                        cv2.rectangle(roi_color,(exx-5 ,eyy -5  ),(exx+eww -5,eyy+ehh -5 ),(0,0, 255),2)


    ret, jpeg = cv2.imencode('.jpg', image)
    self.string = jpeg.tostring()
    self._image = image
    return jpeg.tostring()
def GetBw(self):
    image = self._image
    ret, jpeg = cv2.imencode('.jpg', image)
    self.string = jpeg.tostring()
    return jpeg.tostring()
</code></pre>

<p>You will want to use a timer for this specific application. time.sleep() basically just halts the program, it does not keep track of time. This should work for what you want:</p>

<pre><code>closed = False
timer = 0
if detect(eyes_closed) and not closed:
   timer = time.time()
   closed = True
else if closed:
    if time.time() - timer &gt; 3000:
        print "Eyes have been closed for 3 seconds!"
else:
    closed = False
</code></pre>

<p>I haven't done much work in OpenCV, so I just made the assumption that detect(eyes_closed) detects a set of closed eyes, you will have to correct that yourself.
This will print a message if it detects closed eyes for 3 seconds. </p>

python opencv