Beispiel #1
0
 def setup(self):
     self.detector = FaceDetector(show_cam=False)
     self.present = True
Beispiel #2
0
class FacePresence(Thread):
    def __init__(self):
        self.setup()
        Thread.__init__(self)
        self.search_faces = False
        self.face_thread = None

    def setup(self):
        self.detector = FaceDetector(show_cam=False)
        self.present = True

    def run (self):
        """Start the loop that keeps tracking the user's face."""
        if self.search_faces == False:
            self.search_faces = True

        self.detect()

    def stop(self):
        self.search_faces = False

        # stop the face detection thread (if running)
        if self.face_thread != None:
            self.face_thread.join()
            self.face_thread = None

    def detect(self):
        # remember the number of seconds since we last detected a
        # face, and assume we detected one at the start        
        last_time = datetime.now() 

        # if we don't detect anything for 1 seconds, assume user is away
        away_treshold = timedelta(seconds=1)

        # eliminate false positives by requiring at least 15 detections
        # in a period of 5 seconds to allow the status to be changed
        # to available.
        fp_num = 15
        fp_duration = timedelta(seconds=5)
        fp_list = [last_time]
        fp_list[1:] = [datetime.fromtimestamp(0)] * (fp_num - 1)

        fi = 0
        self.present = False
        while self.search_faces:
            # detect a face/body
            (detected, is_face) = self.detector.fetch_and_detect()
            if detected:
                fi = fi + 1
                last_time = datetime.now()
                fp_list[fi % fp_num] = last_time

                if not self.present:
                    # possible switch to available
                    if fp_list[fi % fp_num] - fp_list[0] < fp_duration:
                        # we detected {fp_num} faces during the last 5 seconds
                        #print '%s - %s < %s' % (fp_list[fp_num-1], fp_list[0], fp_duration)
                        self.present = True
            else:
                if self.present:
                    # possible switch to away
                    if datetime.now() - last_time > away_treshold:
                        self.present = False