如何检测闭眼三秒钟?

时间:2016-09-29 10:37:22

标签: python opencv

我发现了脸部,眼睛,眼睛两个容器等部位。通常情况下,我可以看到脸部,眼睛区域和两只眼睛。当我看到脸部,眼睛区域但看不到两只眼睛时,那是在检测到眼睛闭合时。现在,我想检测闭眼三秒钟。有人可以建议我一个解决方案。我试过time.sleep()函数,但它不起作用。它使Videostream进程停止。

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()

1 个答案:

答案 0 :(得分:0)

您将需要为此特定应用程序使用计时器。 time.sleep()基本上只是暂停程序,它没有跟踪时间。这应该适用于你想要的东西:

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

我在OpenCV上做了很多工作,所以我只是假设检测(eyes_closed)检测到一组闭眼,你必须自己纠正。 如果它检测到闭眼3秒,将打印一条消息。