Exemple #1
0
def load_and_align_data(face_graph, image_paths, do_display):
    detector = MTCNNDetector(face_graph)
    nrof_samples = len(image_paths)
    img_list = [None] * nrof_samples
    for i in range(nrof_samples):
        img = misc.imread(os.path.expanduser(image_paths[i]))
        bounding_boxes, landmarks = detector.detect_face(img)

        all_face_marks_x = landmarks[0:5, :]
        all_face_marks_y = landmarks[5:10, :]

        face_marks_x = all_face_marks_x[:, 0]
        face_marks_y = all_face_marks_y[:, 0]

        print(face_marks_x)
        print(face_marks_y)

        # draw landmarks on image
        if do_display:
            frame = img.copy()
            for x, y in zip(face_marks_x, face_marks_y):
                cv2.circle(frame, (int(x), int(y)), 2, (0, 0, 255), -1)
            cv2.imshow(image_paths[i], frame)
            cv2.waitKey()
        bounding_box = bounding_boxes[0]
        img_list[i] = CropperUtils.crop_face(img, bounding_box)
    images = np.stack(img_list)
    return images
Exemple #2
0
def generate_video_sample(cam_url, area):
    '''generating'''
    print('Generating... ')
    print("Cam URL: {}".format(cam_url))
    print("Area: {}".format(area))
    # Variables for tracking faces
    frame_counter = 0

    # Variables holding the correlation trackers and the name per faceid
    frame_sample = {}

    face_rec_graph = FaceGraph()
    face_extractor = FacenetExtractor(face_rec_graph)
    detector = MTCNNDetector(face_rec_graph)
    preprocessor = Preprocessor()
    if args.cam_url is not None:
        frame_reader = URLFrameReader(args.cam_url, scale_factor=1)
    else:
        frame_reader = RabbitFrameReader(rabbit_mq)

    try:
        while True:  # frame_reader.has_next():
            frame = frame_reader.next_frame()
            frame_sample[frame_counter] = FrameSample()
            frame_sample[frame_counter].read_image = frame
            if frame is None:
                print("Waiting for the new image")
                continue

            print("Frame ID: %d" % frame_counter)

            if frame_counter % Config.Frame.FRAME_INTERVAL == 0:
                origin_bbs, points = detector.detect_face(frame)
                frame_sample[frame_counter].origin_bbs = origin_bbs
                frame_sample[frame_counter].points = points
                for _, origin_bb in enumerate(origin_bbs):
                    cropped_face = CropperUtils.crop_face(frame, origin_bb)

                    # Calculate embedding
                    preprocessed_image = preprocessor.process(cropped_face)
                    emb_array, coeff = face_extractor.extract_features(
                        preprocessed_image)
                    frame_sample[frame_counter].embs.append(emb_array)

            frame_counter += 1
    except KeyboardInterrupt:
        print('Keyboard Interrupt !!! Release All !!!')
        print('Saved this video sample as ../session/db/sample.pkl')
        PickleUtils.save_pickle('../session/db/sample.pkl', frame_sample)
Exemple #3
0
class MTCNNFaceDetection(object):
    '''
    Use mtcnn model to detect face in frame
    '''
    def __init__(self):
        face_graph = FaceGraph()
        self.detector = MTCNNDetector(face_graph)

    def detect(self, image, **kwargs):
        faces = []
        margin = 0
        img_h, img_w = image.shape[:2]
        origin_bbs, landmarks = self.detector.detect_face(image, **kwargs)

        landmarks = landmarks.reshape(-1, 10).tolist()
        for origin_bb, landmark in zip(origin_bbs, landmarks):
            face = {}
            det = np.squeeze(origin_bb)
            bb = np.zeros(4, dtype=np.int32)
            bb[0] = np.maximum(det[0] - margin / 2, 0)
            bb[1] = np.maximum(det[1] - margin / 2, 0)
            bb[2] = np.minimum(det[2] + margin / 2, img_w)
            bb[3] = np.minimum(det[3] + margin / 2, img_h)
            x = bb[0]
            y = bb[1]
            w = bb[2] - bb[0]
            h = bb[3] - bb[1]
            face['face'] = (x, y, w, h)

            points = []
            # 0: right eye
            # 1: left eye
            # 2: nose
            # 3: right of mount
            # 4: left of mount
            for ix, iy in zip(landmark[:5], landmark[5:]):
                points.append((ix, iy))
            # Draw right eye
            eye_width = int(w / 3)
            right_eye = draw_eye(image, points[0], eye_width, eye_width)
            left_eye = draw_eye(image, points[1], eye_width, eye_width)
            # Draw mouth
            mouth = draw_mouth(image, points[3], points[4])

            face['right_eye'] = right_eye
            face['left_eye'] = left_eye
            face['mouth'] = mouth
            faces.append(face)
        return faces
Exemple #4
0
def main(cam_url, recording_area):

    rb = RabbitMQ((Config.Rabbit.USERNAME, Config.Rabbit.PASSWORD),
                  (Config.Rabbit.IP_ADDRESS, Config.Rabbit.PORT))
    detector = MTCNNDetector(FaceGraph())
    frame_reader = URLFrameReader(cam_url)
    edit_image = utils.CropperUtils()
    face_angle = utils.FaceAngleUtils()
    feature_extractor = FacenetExtractor(FaceGraph())
    pre_process = Preprocessor(whitening)

    while frame_reader.has_next():

        embedding_images = []
        embedding_vectors = []
        display_images = []
        display_image_bounding_boxes = []

        frame = frame_reader.next_frame()
        bounding_boxes, points = detector.detect_face(frame)

        for index, bounding_box in enumerate(bounding_boxes):

            if face_angle.is_acceptable_angle(points[:, index]) is True:

                embedding_image = edit_image.crop_face(frame, bounding_box)
                embedding_images.append(embedding_image)

                display_image, display_image_bounding_box = edit_image.crop_display_face(
                    frame, bounding_box)
                display_images.append(display_image)
                display_image_bounding_boxes.append(display_image_bounding_box)

                whitened_image = pre_process.process(embedding_image)
                embedding_vector, coeff = feature_extractor.extract_features(
                    whitened_image)

                embedding_vectors.append(embedding_vector)

        if len(embedding_vectors) > 0:

            rb.send_multi_embedding_message(display_images, embedding_vectors,
                                            recording_area, time.time(),
                                            display_image_bounding_boxes,
                                            rb.SEND_QUEUE_WORKER)
        else:
            print("No Face Detected")
from cv_utils import show_frame, CropperUtils
from preprocess import Preprocessor
from matcher import KdTreeMatcher
from frame_reader import URLFrameReader
import time

matcher = KdTreeMatcher()
face_graph = FaceGraph()
face_detector = MTCNNDetector(face_graph)
feature_extractor = FacenetExtractor(face_graph)
preprocessor = Preprocessor()
frame_reader = URLFrameReader(cam_url=0, scale_factor=2)

while frame_reader.has_next():
    frame = frame_reader.next_frame()
    bouncing_boxes, landmarks = face_detector.detect_face(frame)
    nrof_faces = len(bouncing_boxes)
    start = time.time()
    for i in range(nrof_faces):
        cropped = CropperUtils.crop_face(frame, bouncing_boxes[i])
        display_face, padded_bb_str = CropperUtils.crop_display_face(
            frame, bouncing_boxes[i])
        reverse_face = CropperUtils.reverse_display_face(
            display_face, padded_bb_str)
        process_img = preprocessor.process(cropped)
        show_frame(reverse_face, 'Reverse')
        show_frame(cropped, 'Cropped')
        emb, coeff = feature_extractor.extract_features(process_img)
        predict_id, top_match_ids = matcher.match(emb)
        print('Predict', predict_id)
Exemple #6
0
MASK_DIR = '%s/data/Mask/' % Config.ROOT
NOMASK_DIR = '%s/data/No_Mask/' % Config.ROOT
GLASSES_DIR = '%s/data/Glasses/' % Config.ROOT
NOGLASSES_DIR = '%s/data/No_Glasses/' % Config.ROOT

create_if_not_exist(MASK_DIR)
create_if_not_exist(NOMASK_DIR)
create_if_not_exist(GLASSES_DIR)
create_if_not_exist(NOGLASSES_DIR)

while frame_reader.has_next():
    frame = frame_reader.next_frame()
    if frame is None:
        break

    bbs, pts = face_detector.detect_face(frame)
    preprocessed = preprocessor.process(frame)

    has_mask = None
    has_glasses = None

    if len(bbs) > 0:
        # cropped = CropperUtils.crop_face(frame, bbs[0], return_size=224)
        has_mask = mask_classifier.is_wearing_mask(preprocessed)[0]
        has_glasses = glasses_classifier.is_wearing_glasses(preprocessed)[0]
        show_frame(
            frame,
            bb=bbs[0],
            pt=pts[:, 0],
            id='Has mask %s, has_glasses: %s' % (has_mask, has_glasses))
        #if has_glasses == True: print("Allert!!!!!!!!!!!!!!!!")
Exemple #7
0
def cam_worker_function(cam_url, area):
    '''
    Cam worker function
    '''
    print("Cam URL: {}".format(cam_url))
    print("Area: {}".format(area))

    # Modify Config
    Config.Track.TRACKING_QUEUE_CAM_TO_CENTRAL = True

    rabbit_mq = RabbitMQ((Config.Rabbit.USERNAME, Config.Rabbit.PASSWORD),
                         (Config.Rabbit.IP_ADDRESS, Config.Rabbit.PORT))

    frame_counter = 0

    # Variables holding the correlation trackers and the name per faceid
    list_of_trackers = TrackersList()

    face_rec_graph = FaceGraph()
    face_extractor = FacenetExtractor(face_rec_graph)
    detector = MTCNNDetector(face_rec_graph)
    preprocessor = Preprocessor()
    matcher = KdTreeMatcher()
    if Config.CALC_FPS:
        start_time = time.time()
    if args.cam_url is not None:
        frame_reader = URLFrameReader(args.cam_url, scale_factor=1.5)
    else:
        frame_reader = RabbitFrameReader(rabbit_mq)

    try:
        while True:  # frame_reader.has_next():
            frame = frame_reader.next_frame()
            if frame is None:
                print("Waiting for the new image")
                list_of_trackers.check_delete_trackers(matcher,
                                                       rabbit_mq,
                                                       history_mode=False)
                continue

            print("Frame ID: %d" % frame_counter)

            if Config.CALC_FPS:
                fps_counter = time.time()

            list_of_trackers.update_dlib_trackers(frame)

            if frame_counter % Config.Frame.FRAME_INTERVAL == 0:
                origin_bbs, points = detector.detect_face(frame)
                for i, origin_bb in enumerate(origin_bbs):
                    display_face, _ = CropperUtils.crop_display_face(
                        frame, origin_bb)
                    print("Display face shape")
                    print(display_face.shape)
                    if 0 in display_face.shape:
                        continue
                    cropped_face = CropperUtils.crop_face(frame, origin_bb)

                    # Calculate embedding
                    preprocessed_image = preprocessor.process(cropped_face)
                    emb_array, coeff = face_extractor.extract_features(
                        preprocessed_image)

                    # Calculate angle
                    angle = FaceAngleUtils.calc_angle(points[:, i])

                    # TODO: refractor matching_detected_face_with_trackers
                    matched_fid = list_of_trackers.matching_face_with_trackers(
                        frame, origin_bb, emb_array)

                    # Update list_of_trackers
                    list_of_trackers.update_trackers_list(
                        matched_fid, origin_bb, display_face, emb_array, angle,
                        area, frame_counter, matcher, rabbit_mq)

                    if Config.Track.TRACKING_QUEUE_CAM_TO_CENTRAL:
                        track_tuple = (matched_fid, display_face, emb_array,
                                       area, time.time(), origin_bb, angle)
                        rabbit_mq.send_tracking(
                            track_tuple,
                            rabbit_mq.RECEIVE_CAM_WORKER_TRACKING_QUEUE)

            # Check detete current trackers time
            list_of_trackers.check_delete_trackers(matcher,
                                                   rabbit_mq,
                                                   history_mode=False)

            frame_counter += 1
            if Config.CALC_FPS:
                print("FPS: %f" % (1 / (time.time() - fps_counter)))

    except KeyboardInterrupt:
        print('Keyboard Interrupt !!! Release All !!!')
        if Config.CALC_FPS:
            print('Time elapsed: {}'.format(time.time() - start_time))
            print('Avg FPS: {}'.format(
                (frame_counter + 1) / (time.time() - start_time)))
        frame_reader.release()