def run(self):

        #  wait for initial detection
        while self.shared_variables.detection_box[self.index] is None:
            pass

        # initiate tracker
        self.create_custom_tracker()

        # tracking loop
        while self.shared_variables.system_running:

            self.start_time = datetime.datetime.now()

            self.frame = self.shared_variables.frame[self.index]
            self.object_custom_tracking()
            self.end_time = datetime.datetime.now()

            if self.shared_variables.debug:
                LOG.debug(
                    "Tracking time : " + str(self.end_time - self.start_time),
                    "SYSTEM-" + self.shared_variables.name)

        LOG.info("Stopped tracking " + str(self.index),
                 "SYSTEM-" + self.shared_variables.name)
Ejemplo n.º 2
0
    def run(self):
        LOG.info("Start blink frequency "+ str(self.index), "SYSTEM-"+self.shared_variables.name)

        # load model
        model = load_model('../../model/blinkModel.hdf5')

        close_counter = blinks = mem_counter= 0
        state = ''

        #Wait for detection
        while self.shared_variables.frame[self.index] is None:
            pass
        while self.shared_variables.system_running is not None:

            if self.shared_variables.frame[self.index] is not None:

                frame = self.shared_variables.frame[self.index]


                eyes = self.cropEyes(frame)
                if eyes is None:
                    continue
                else:
                    left_eye,right_eye = eyes


                prediction = (model.predict(self.cnnPreprocess(left_eye)) + model.predict(self.cnnPreprocess(right_eye)))/2.0

                if prediction > 0.5 :
                    state = 'open'
                    close_counter = 0
                else:
                    state = 'close'
                    close_counter += 1

                if state == 'open' and mem_counter > 1:
                    blinks += 1

                mem_counter = close_counter

                #save blinking
                #eye_state
                self.shared_variables.eye_state[self.index] = state
                #blinks
                self.shared_variables.blinks[self.index] = blinks
                #eye_left
                self.shared_variables.eye_left[self.index] = left_eye
                #eye_right
                self.shared_variables.eye_right[self.index] = right_eye

                if self.shared_variables.debug:
                    LOG.debug(str(state) + " " + str(blinks) + " from "+str(self.index),"SYSTEM-"+self.shared_variables.name)

        LOG.info("Ending blink freq " + str(self.index), "SYSTEM-"+self.shared_variables.name)
Ejemplo n.º 3
0
    def run(self):
        with tf.Session() as sess:
            LOG.info("Loading Tensorflow modell " + str(self.index),
                     "SYSTEM-" + self.shared_variables.name)

            # Load model
            self.pnet, self.rnet, self.onet = detect_and_align.create_mtcnn(
                sess, None)

            self.Loaded_model = True

            LOG.info("Start tf detections " + str(self.index),
                     "SYSTEM-" + self.shared_variables.name)

            # Start Loop
            while self.shared_variables.system_running:

                self.start_time = datetime.datetime.now()

                frame = self.shared_variables.frame[self.index]

                if self.do_flipp_test:
                    frame = imutils.rotate(
                        frame, self.flipp_test_degree * self.flipp_test_nr)

                # Do detection
                face_patches, padded_bounding_boxes, landmarks, score = detect_and_align.align_image(
                    frame, self.pnet, self.rnet, self.onet)

                # if found faces
                if len(face_patches) > 0:

                    self.shared_variables.detection_score[self.index] = score

                    self.no_face_count = 0

                    # Save landmark
                    #self.shared_variables.landmarks[self.index] = self.convert_to_dlib_landmarks(landmarks)
                    self.shared_variables.set_landmarks(
                        self.convert_to_dlib_landmarks(landmarks), self.index)

                    # Convert box from Tensorflow to OpenCV
                    i = 0
                    while i < len(padded_bounding_boxes):
                        face_box.append(
                            self.convert_tensorflow_box_to_openCV_box(
                                padded_bounding_boxes[i]))
                        i += 1

                    # Save boxes
                    self.shared_variables.face_box[self.index] = face_box

                    self.shared_variables.set_detection_box(
                        face_box, self.index)
                    #self.shared_variables.detection_box[self.index] = face_box

                    self.shared_variables.face_found[self.index] = True

                    # Do flipp test on detection
                    if self.shared_variables.flipp_test[
                            self.index] and self.do_flipp_test:
                        # save flipp as success
                        degree = self.shared_variables.flipp_test_degree[
                            self.
                            index] + self.flipp_test_nr * self.flipp_test_degree

                        degree = degree - (degree % 360) * 360

                        self.shared_variables.flipp_test_degree[
                            self.index] = degree

                        # log frame change
                        LOG.info(
                            "Flipp test successful add degree :" +
                            str(self.flipp_test_nr * self.flipp_test_degree),
                            self.shared_variables.name)

                        # end flipp test
                        self.do_flipp_test = False
                        self.flipp_test_nr = 1

                    # Wake tracking thread
                #    if not self.shared_variables.tracking_running:
                #    self.sleep_time = self.SHORT_SLEEP

                else:
                    # No face
                    self.shared_variables.face_found[self.index] = False

                    # if max face misses has been done, stop tracking and do less detections
                    if self.no_face_count >= self.NO_FACE_MAX:

                        # do flipp test
                        if self.shared_variables.flipp_test[self.index]:

                            # doing flipp test
                            if self.do_flipp_test:
                                self.flipp_test_nr = self.flipp_test_nr + 1

                                # flipp test did not find anything
                                if self.flipp_test_nr * self.flipp_test_degree >= 360:
                                    self.do_flipp_test = False
                                    self.flipp_test_nr = 1

                                    #self.sleep_time = self.LONG_SLEEP
                                    #self.shared_variables.tracking_running = False
                                    #LOG.log("Initiate energy save",self.shared_variables.name)

                            else:
                                self.do_flipp_test = True

                        else:
                            #self.sleep_time = self.LONG_SLEEP
                            #self.shared_variables.tracking_running = False
                            #LOG.log("Initiate energy save",self.shared_variables.name)
                            pass
                    else:
                        self.no_face_count = self.no_face_count + 1

                    if self.no_face_count >= self.flipp_test_long_intervall and self.shared_variables.flipp_test[
                            self.index]:
                        self.no_face_count = 0

            self.end_time = datetime.datetime.now()

            # Debug detection time
            if self.shared_variables.debug:
                LOG.debug(
                    'TF Detection time:' +
                    str(self.end_time - self.start_time),
                    self.shared_variables.name)

            time.sleep(self.sleep_time)  # sleep if wanted
        LOG.info('Ending tf detection' + str(self.index),
                 "SYSTEM-" + self.shared_variables.name)
Ejemplo n.º 4
0
    def run(self):
        # Load model
        LOG.info("Loading OPENCV model" + str(self.index),
                 "SYSTEM-" + self.shared_variables.name)

        face_cascade = cv2.CascadeClassifier(
            'utils/haarcascade_frontalface_default.xml')
        facial_features_cascade = cv2.CascadeClassifier(
            'utils/haarcascade_facial_features.xml')

        LOG.info("Start opencv detections " + str(self.index),
                 "SYSTEM-" + self.shared_variables.name)

        # Start Loop
        while self.shared_variables.system_running:

            self.start_time = datetime.datetime.now()

            frame = self.shared_variables.frame[self.index]

            if self.do_flipp_test:
                frame = imutils.rotate(
                    frame, self.flipp_test_degree * self.flipp_test_nr)

            # Do detection
            if frame is not None:
                gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

                landmarksAndFaces = []

                face_patches = face_cascade.detectMultiScale(gray, 1.3, 5)

                # if found faces
                if len(face_patches) > 0:

                    landmarksAndFaces.append(face_patches[0].tolist())

                    for (x, y, w, h) in face_patches:

                        roi_gray = gray[y:y + h, x:x + w]

                        # To dont use landmarks, instead use boxes
                        for (ex, ey, ew,
                             eh) in facial_features_cascade.detectMultiScale(
                                 roi_gray):
                            landmarksAndFaces.append([x + ex, y + ey, ew, eh])

                    self.no_face_count = 0

                    self.shared_variables.face_found[self.index] = True
                    # Save boxes
                    self.shared_variables.face_box[
                        self.index] = landmarksAndFaces
                    #self.shared_variables.detection_box[self.index] = face_box
                    self.shared_variables.set_detection_box(
                        landmarksAndFaces, self.index)

                    # Do flipp test on detection
                    if self.shared_variables.flipp_test[
                            self.index] and self.do_flipp_test:
                        # save flipp as success
                        degree = self.shared_variables.flipp_test_degree[
                            self.
                            index] + self.flipp_test_nr * self.flipp_test_degree

                        degree = degree - (degree % 360) * 360

                        self.shared_variables.flipp_test_degree[
                            self.index] = degree

                        # log frame change
                        LOG.log(
                            "Flipp test successful add degree :" +
                            str(self.flipp_test_nr * self.flipp_test_degree),
                            self.shared_variables.name)

                        # end flipp test
                        self.do_flipp_test = False
                        self.flipp_test_nr = 1

                else:
                    # No face
                    self.shared_variables.face_found[self.index] = False

                    # if max face misses has been done, stop tracking and do less detections
                    if self.no_face_count >= self.NO_FACE_MAX:

                        # do flipp test
                        if self.shared_variables.flipp_test:

                            # doing flipp test
                            if self.do_flipp_test:
                                self.flipp_test_nr = self.flipp_test_nr + 1

                                # flipp test did not find anything
                                if self.flipp_test_nr * self.flipp_test_degree >= 360:
                                    self.do_flipp_test = False
                                    self.flipp_test_nr = 1

                                    self.sleep_time = self.LONG_SLEEP

                            else:
                                self.do_flipp_test = True

                        else:
                            #self.sleep_time = self.LONG_SLEEP
                            #self.shared_variables.tracking_running = False
                            #LOG.log("Initiate energy save",self.shared_variables.name)
                            pass

                    else:
                        self.no_face_count = self.no_face_count + 1

                    if self.no_face_count >= self.flipp_test_long_intervall and self.shared_variables.flipp_test[
                            self.index]:
                        self.no_face_count = 0

            self.end_time = datetime.datetime.now()

            # Debug detection time
            if self.shared_variables.debug:
                LOG.debug(
                    'OPENCV Detection time:' +
                    str(self.end_time - self.start_time),
                    self.shared_variables.name)

            time.sleep(self.sleep_time)  # sleep if wanted

        LOG.info("Ending OPENCV detection " + str(self.index),
                 "SYSTEM-" + self.shared_variables.name)
Ejemplo n.º 5
0
    def run(self):
        if not self.Loaded_model:
            LOG.info("Loading Dlib modell" + str(self.index),
                     "SYSTEM-" + self.shared_variables.name)

            # Load model
            self.face_detector = dlib.get_frontal_face_detector()
            self.landmarks_predictor = dlib.shape_predictor(
                self.landmarks_model_path)

            #face_cascade = cv2.CascadeClassifier(face_cascade_path)
            self.Loaded_model = True

        LOG.info("Start dlib detections" + str(self.index),
                 "SYSTEM-" + self.shared_variables.name)

        #wait for first cam frame
        while self.shared_variables.frame[self.index] is None:
            pass

            # Start Loop
        while self.shared_variables.system_running:
            self.start_time = datetime.datetime.now()

            frame = self.shared_variables.frame[self.index]

            if self.do_flipp_test:
                frame = imutils.rotate(
                    frame, self.flipp_test_degree * self.flipp_test_nr)

                # Do detection
            success, face_box, landmarks, score = self.object_detection(frame)

            # if found faces
            if success:

                self.shared_variables.detection_score[self.index] = score

                self.no_face_count = 0

                # Save landmark
                #self.shared_variables.landmarks[self.index] = landmarks
                self.shared_variables.set_landmarks(landmarks, self.index)

                # Save boxes
                self.shared_variables.face_box[self.index] = face_box
                #self.shared_variables.detection_box[self.index] = face_box
                self.shared_variables.set_detection_box(face_box, self.index)

                self.shared_variables.face_found[self.index] = True
                # Do flipp test on detection
                if self.shared_variables.flipp_test[
                        self.index] and self.do_flipp_test:
                    # save flipp as success
                    degree = self.shared_variables.flipp_test_degree[
                        self.
                        index] + self.flipp_test_nr * self.flipp_test_degree

                    degree = degree - (degree % 360) * 360

                    self.shared_variables.flipp_test_degree[
                        self.index] = degree

                    # log frame change
                    LOG.info(
                        "Flipp test successful add degree :" +
                        str(self.flipp_test_nr * self.flipp_test_degree),
                        self.shared_variables.name)

                    # end flipp test
                    self.do_flipp_test = False
                    self.flipp_test_nr = 1

                # Wake tracking thread

                #if not self.shared_variables.tracking_running[self.index]:
                #    self.sleep_time = self.SHORT_SLEEP

            else:
                # No face
                self.shared_variables.face_found[self.index] = False

                # if max face misses has been done, do less detections
                if self.no_face_count >= self.NO_FACE_MAX:

                    # do flipp test
                    if self.shared_variables.flipp_test[self.index]:

                        # doing flipp test
                        if self.do_flipp_test:
                            self.flipp_test_nr = self.flipp_test_nr + 1

                            # flipp test did not find anything
                            if self.flipp_test_nr * self.flipp_test_degree >= 360:
                                self.do_flipp_test = False
                                self.flipp_test_nr = 1

                                if self.sleep_time == self.SHORT_SLEEP:
                                    #LOG.log("Initiate energy save",self.shared_variables.name)
                                    #self.sleep_time = self.LONG_SLEEP
                                    pass
                        else:
                            self.do_flipp_test = True

                    else:
                        #self.sleep_time = self.LONG_SLEEP
                        #self.shared_variables.tracking_running[self.index] = False
                        #LOG.log("Initiate energy save",self.shared_variables.name)
                        pass

                else:
                    self.no_face_count = self.no_face_count + 1

                if self.no_face_count >= self.flipp_test_long_intervall and self.shared_variables.flipp_test[
                        self.index]:
                    self.no_face_count = 0

            self.end_time = datetime.datetime.now()

            # Debug detection time
            if self.shared_variables.debug:
                LOG.debug(
                    'Dlib Detection time:' +
                    str(self.end_time - self.start_time),
                    self.shared_variables.name)

            time.sleep(self.sleep_time)  # sleep if wanted

        LOG.info("Ending dlib detection " + str(self.index),
                 "SYSTEM-" + self.shared_variables.name)
    def run(self):
        LOG.info("Start age and gender estimation "+ str(self.index), "SYSTEM-"+self.shared_variables.name)
        # load model and weights
        img_size = 64
        model = WideResNet(img_size, depth=16, k=8)()

        model.load_weights(self.pretrained_model_path)


        #wait for detection
        while self.shared_variables.frame[self.index] is None:
            pass

        img_size = 64

        while self.shared_variables.system_running:

            input_img = cv2.cvtColor(self.shared_variables.frame[self.index], cv2.COLOR_BGR2RGB)
            img_h, img_w, _ = np.shape(input_img)

            faces = np.empty((len([self.shared_variables.face_box[self.index]]), img_size, img_size, 3))



            w = self.shared_variables.face_box[self.index][0][0]
            h = self.shared_variables.face_box[self.index][0][1]
            x1 = self.shared_variables.face_box[self.index][0][2]
            y1 = self.shared_variables.face_box[self.index][0][3]
            x2 = w + x1
            y2 = h + y1

            xw1 = max(int(x1 - 0.4 * w), 0)
            yw1 = max(int(y1 - 0.4 * h), 0)
            xw2 = min(int(x2 + 0.4 * w), img_w - 1)
            yw2 = min(int(y2 + 0.4 * h), img_h - 1)

            faces[0, :, :, :] = cv2.resize(self.shared_variables.frame[self.index][yw1:yw2 + 1, xw1:xw2 + 1, :], (img_size, img_size))



    # predict ages and genders of the detected faces
            results = model.predict(faces)
            predicted_genders = results[0]
            ages = np.arange(0, 101).reshape(101, 1)
            predicted_ages = results[1].dot(ages).flatten()


    # Show
            if(self.shared_variables.debug):
                LOG.debug("Predicted age: " + str(predicted_ages[0]) + " from " + str(self.index), "SYSTEM-"+self.shared_variables.name)

            gender = ""

            if predicted_genders[0][0] > 0.5:
                gender = "Female"
            else:
                gender = "Male"

            if(self.shared_variables.debug):
                print("Predicted gender: " + gender)

            self.shared_variables.gender[self.index] = gender
            self.shared_variables.age[self.index] = predicted_ages[0]


    #Short version

            if(self.shared_variables.debug):

                label = "{}, {}".format(int(predicted_ages[0]),
                                    "F" if predicted_genders[0][0] > 0.5 else "M")
                LOG.debug(label +" from " + str(self.index), "SYSTEM-"+self.shared_variables.name)

        LOG.info("Stopped age gender estimation" + str(self.index), "SYSTEM-"+self.shared_variables.name)