Пример #1
0
        ce.FACE_RECOGNITION_EXPERIMENT_RESULTS_FILE_NAME + '.yml')
    file_check = os.path.isfile(all_results_YAML_file_path)

    experiments = list()
    if file_check:
        experiments = load_experiment_results(all_results_YAML_file_path)
        number_of_already_done_experiments = len(experiments)
        new_experiment_dict[ce.EXPERIMENT_NUMBER_KEY] = (
            number_of_already_done_experiments + 1)
    else:
        new_experiment_dict[ce.EXPERIMENT_NUMBER_KEY] = 1

    new_experiment_dict_extended = {ce.EXPERIMENT_KEY: new_experiment_dict}
    experiments.append(new_experiment_dict_extended)
    experiments_dict = {ce.EXPERIMENTS_KEY: experiments}
    save_YAML_file(all_results_YAML_file_path, experiments_dict)

    # Update csv file with results related to all the experiments
    all_results_CSV_file_path = os.path.join(results_path,
        ce.FACE_RECOGNITION_EXPERIMENT_RESULTS_FILE_NAME + '.csv')
    save_rec_experiments_in_CSV_file(all_results_CSV_file_path, experiments)

    # Save file with results related to this experiment
    file_name = 'FaceRecognitionExperiment' + str(
        number_of_already_done_experiments + 1) + 'Results.yml'
    results_file_path = os.path.join(results_path, file_name)
    save_YAML_file(results_file_path, rec_dict)

if __name__ == "__main__":
    
    import argparse
Пример #2
0
def clothing_recognition_experiments(dataset_path, params=None):
    """
    Execute clothing recognition experiments

    :type dataset_path: string
    :param dataset_path: path of directory with first frame sequence

    :type params: dictionary
    :param params: configuration parameters (see table)

    ============================================  ========================================  =============================
    Key (params)                                  Value                                     Default value
    ============================================  ========================================  =============================
    clothes_bounding_box_height                   Height of bounding box for clothes
                                                  (in % of the face bounding box height)    1.0
    clothes_bounding_box_width                    Width of bounding box for clothes         2.0
                                                  (in % of the face bounding box width)
    clothing_recognition_K                        Multiplier for intra distances            1
                                                  for calculating local threshold
                                                  in clothing recognition
    neck_height                                   Height of neck (in % of the               0.0
                                                  face bounding box height)
    nr_of_HSV_channels_in_clothing_recognition    Number of HSV channels used
                                                  in clothing recognition (1-3)             3
    use_mask_in_clothing_recognition              If True, use mask for HSV values          True
                                                  in clothing recognition
    use_motion_mask_in_clothing_recognition       If True, calculate histograms only        False
                                                  on regions where motion is detected
    classifiers_dir_path                          Path of directory with OpenCV
                                                  cascade classifiers
    face_detection_algorithm                      Classifier for face detection             'HaarCascadeFrontalFaceAlt2'
                                                  ('HaarCascadeFrontalFaceAlt',
                                                  'HaarCascadeFrontalFaceAltTree',
                                                  'HaarCascadeFrontalFaceAlt2',
                                                  'HaarCascadeFrontalFaceDefault',
                                                  'HaarCascadeProfileFace',
                                                  'HaarCascadeFrontalAndProfileFaces',
                                                  'HaarCascadeFrontalAndProfileFaces2',
                                                  'LBPCascadeFrontalface',
                                                  'LBPCascadeProfileFace' or
                                                  'LBPCascadeFrontalAndProfileFaces')
    flags                                         Flags used in face detection              'DoCannyPruning'
                                                  ('DoCannyPruning', 'ScaleImage',
                                                  'FindBiggestObject', 'DoRoughSearch').
                                                  If 'DoCannyPruning' is used, regions
                                                  that do not contain lines are discarded.
                                                  If 'ScaleImage' is used, image instead
                                                  of the detector is scaled
                                                  (it can be advantegeous in terms of
                                                  memory and cache use).
                                                  If 'FindBiggestObject' is used,
                                                  only the biggest object is returned
                                                  by the detector.
                                                  'DoRoughSearch', used together with
                                                  'FindBiggestObject',
                                                  terminates the search as soon as
                                                  the first candidate object is found
    min_neighbors                                 Mininum number of neighbor bounding       5
                                                  boxes for retaining face detection
    min_size_height                               Minimum height of face detection          20
                                                  bounding box (in pixels)
    min_size_width                                Minimum width of face detection           20
                                                  bounding box (in pixels)
    scale_factor                                  Scale factor between two scans            1.1
                                                  in face detection
    clothing_recognition_results_path             Directory that will contain results
                                                  of clothing recognition experiments
    video_name                                    Name of video to be tested
    ============================================  ========================================  =============================
    """

    # Set parameters
    cloth_models_dir_path = None
    clothing_rec_k = c.CLOTHING_REC_K
    hsv_channels = c.CLOTHING_REC_HSV_CHANNELS_NR
    results_path = ce.CLOTHING_RECOGNITION_RESULTS_PATH
    use_LBP = c.CLOTHING_REC_USE_LBP
    use_mask = c.CLOTHING_REC_USE_MASK
    use_motion_mask = c.CLOTHING_REC_USE_MOTION_MASK
    video_name = ce.TEST_VIDEO_NAME
    if params is not None:
        if ce.CLOTH_MODELS_DIR_PATH_KEY in params:
            cloth_models_dir_path = params[ce.CLOTH_MODELS_DIR_PATH_KEY]
        if c.CLOTHING_REC_K_KEY in params:
            clothing_rec_k = params[c.CLOTHING_REC_K_KEY]
        if c.CLOTHING_REC_HSV_CHANNELS_NR_KEY in params:
            hsv_channels = params[c.CLOTHING_REC_HSV_CHANNELS_NR_KEY]
        if ce.CLOTHING_RECOGNITION_RESULTS_PATH_KEY in params:
            results_path = params[ce.CLOTHING_RECOGNITION_RESULTS_PATH_KEY]
        if c.CLOTHING_REC_USE_LBP_KEY in params:
            use_LBP = params[c.CLOTHING_REC_USE_LBP_KEY]
        if c.CLOTHING_REC_USE_MASK_KEY in params:
            use_mask = params[c.CLOTHING_REC_USE_MASK_KEY]
        if c.CLOTHING_REC_USE_MOTION_MASK_KEY in params:
            use_motion_mask = params[c.CLOTHING_REC_USE_MOTION_MASK_KEY]
        if ce.VIDEO_NAME_KEY in params:
            video_name = params[ce.VIDEO_NAME_KEY]

    # Number of correctly matched pairs of frame sequences
    rec_pairs_nr = 0

    # Number of checked pairs of frame sequences
    total_test_pairs_nr = 0

    # Iterate through videos
    for video in os.listdir(dataset_path):
        video_path = os.path.join(dataset_path, video)
        if video != video_name:
            continue

        # If directory for clothing models is provided and
        # file for this video exists, load models from file
        file_path = None
        loaded = False
        models = None
        if cloth_models_dir_path:
            file_path = os.path.join(cloth_models_dir_path, video_name)
            if os.path.exists(file_path):
                with open(file_path) as f:
                    models = pickle.load(f)
                    loaded = True

        if not loaded:

            # Calculate clothing models
            models = []
            for subj in os.listdir(video_path):
                subj_path = os.path.join(video_path, subj)
                frame_seq = []
                for im in os.listdir(subj_path):
                    im_path = os.path.join(subj_path, im)
                    frame_seq.append(im_path)
                model = pt.get_clothing_model_from_sequence(frame_seq, params)
                models.append(model)

            # If directory for clothing models is provided,
            # save models into a file
            if cloth_models_dir_path:
                with open(file_path, 'w') as f:
                    pickle.dump(models, f)

        # Compare each possible pair of frame sequences in the same video
        counter_1 = 0
        for subj_1 in os.listdir(video_path):

            counter_2 = 0
            for subj_2 in os.listdir(video_path):
                if subj_2 != subj_1:

                    model_1 = models[counter_1]
                    model_2 = models[counter_2]

                    if model_1 and model_2:
                        total_test_pairs_nr += 1

                        # If LBP histograms are used, there is only one channel
                        if use_LBP:
                            if params is None:
                                params = {}
                            params[c.CLOTHING_REC_HSV_CHANNELS_NR_KEY] = 1

                        (sim, dist_ratio) = utils.compare_clothes(
                            model_1, model_2, '', '', 0, None, params,
                            clothing_rec_k)

                        # First part of directory name contains person's tag
                        tag1 = subj_1.split('-')[0]
                        tag2 = subj_2.split('-')[0]

                        # Check correctness of result
                        if ((sim and (tag1 == tag2))
                                or (not sim and (tag1 != tag2))):
                            rec_pairs_nr += 1
                counter_2 += 1
            counter_1 += 1

    recognition_rate = 0
    if total_test_pairs_nr != 0:
        recognition_rate = float(rec_pairs_nr) / float(total_test_pairs_nr)

    print("\n ### RESULTS ###\n")

    print('Number of checked pairs of image sequences: ' + str(total_test_pairs_nr))
    print('Number of correctly matched pairs of image sequences: ' + str(rec_pairs_nr))
    print('Recognition rate: ' + str(recognition_rate * 100) + '%')

    # Update YAML file with the results of all the experiments

    new_experiment_dict = {ce.VIDEO_NAME_KEY: video_name,
                           c.CLOTHING_REC_HSV_CHANNELS_NR_KEY: hsv_channels,
                           c.CLOTHING_REC_K_KEY: clothing_rec_k,
                           c.CLOTHING_REC_USE_LBP_KEY: use_LBP,
                           c.CLOTHING_REC_USE_MASK_KEY: use_mask,
                           c.CLOTHING_REC_USE_MOTION_MASK_KEY: use_motion_mask,
                           ce.TEST_IMAGES_NR_KEY: total_test_pairs_nr,
                           ce.RECOGNITION_RATE_KEY: recognition_rate}

    experiment_results_file_name = ce.EXPERIMENT_RESULTS_FILE_NAME

    yaml_results_file_name = experiment_results_file_name + '.yaml'

    all_results_YAML_file_path = os.path.join(
        results_path, yaml_results_file_name)
    file_check = os.path.isfile(all_results_YAML_file_path)

    experiments = list()
    if file_check:
        experiments = load_experiment_results(all_results_YAML_file_path)
        number_of_already_done_experiments = len(experiments)
        new_experiment_dict[ce.EXPERIMENT_NUMBER_KEY] = (
            number_of_already_done_experiments + 1)
    else:
        new_experiment_dict[ce.EXPERIMENT_NUMBER_KEY] = 1

    new_experiment_dict_extended = {ce.EXPERIMENT_KEY: new_experiment_dict}
    experiments.append(new_experiment_dict_extended)
    experiments_dict = {ce.EXPERIMENTS_KEY: experiments}
    utils.save_YAML_file(all_results_YAML_file_path, experiments_dict)
Пример #3
0
def fr_video_experiments(params, show_results):
    """
    Execute face recognition experiments on video files

    :type params: dictionary
    :param params: configuration parameters to be used for the experiment

    :type show_results: boolean
    :param show_results: show (True) or do not show (False)
                         image with detected faces

    ============================================  ========================================  ==============
    Key                                           Value                                     Default value
    ============================================  ========================================  ==============
    annotations_path                              Path of directory containing the
                                                  manual annotations for the images
    check_eye_positions                           If True, check eye positions              True
    classifiers_dir_path                          Path of directory with OpenCV
                                                  cascade classifiers
    eye_detection_classifier                      Classifier for eye detection              'haarcascade_mcs_lefteye.xml'
    face_detection_algorithm                      Classifier for face detection             'HaarCascadeFrontalFaceAlt2'
                                                  ('HaarCascadeFrontalFaceAlt',
                                                  'HaarCascadeFrontalFaceAltTree',
                                                  'HaarCascadeFrontalFaceAlt2',
                                                  'HaarCascadeFrontalFaceDefault',
                                                  'HaarCascadeProfileFace',
                                                  'HaarCascadeFrontalAndProfileFaces',
                                                  'HaarCascadeFrontalAndProfileFaces2',
                                                  'LBPCascadeFrontalface',
                                                  'LBPCascadeProfileFace' or
                                                  'LBPCascadeFrontalAndProfileFaces')
    flags                                         Flags used in face detection              'DoCannyPruning'
                                                  ('DoCannyPruning', 'ScaleImage',
                                                  'FindBiggestObject', 'DoRoughSearch').
                                                  If 'DoCannyPruning' is used, regions
                                                  that do not contain lines are discarded.
                                                  If 'ScaleImage' is used, image instead
                                                  of the detector is scaled
                                                  (it can be advantegeous in terms of
                                                  memory and cache use).
                                                  If 'FindBiggestObject' is used,
                                                  only the biggest object is returned
                                                  by the detector.
                                                  'DoRoughSearch', used together with
                                                  'FindBiggestObject',
                                                  terminates the search as soon as
                                                  the first candidate object is found
    min_neighbors                                 Mininum number of neighbor bounding       5
                                                  boxes for retaining face detection
    min_size_height                               Minimum height of face detection          20
                                                  bounding box (in pixels)
    min_size_width                                Minimum width of face detection           20
                                                  bounding box (in pixels)
    face_detection_results_path                   Path of directory where
                                                  test results will be saved
    scale_factor                                  Scale factor between two scans            1.1
                                                  in face detection
    max_eye_angle                                 Maximum inclination of the line           0.125
                                                  connecting the eyes
                                                  (in % of pi radians)
    min_eye_distance                              Minimum distance between eyes             0.25
                                                  (in % of the width of the face
                                                  bounding box)
    nose_detection_classifier                     Classifier for nose detection             'haarcascade_mcs_nose.xml'
    software_test_file                            Path of image to be used for
                                                  software test
    test_set_path                                 path of directory
                                                  containing test set
    use_nose_pos_in_detection                     If True, detections with no good          False
                                                  nose position are discarded
    aligned_faces_path                            Default path of directory
                                                  for aligned faces
    cropped_face_height                           Height of aligned faces (in pixels)       400
    cropped_face_width                            Width of aligned faces (in pixels)        200
    dataset_already_divided                       If True, dataset is already divided       False
                                                  between training and test set
    dataset_path                                  Path of whole dataset, used if dataset
                                                  is not already divided between
                                                  training and test set
    db_name                                       Name of single file
                                                  containing face models
    db_models_path                                Path of directory containing face models
    face_model_algorithm                          Algorithm for face recognition            'LBP'
                                                  ('Eigenfaces', 'Fisherfaces' or 'LBP')
    face_recognition_results_path                 Path of directory where
                                                  test results will be saved
    test_set_path                                 Path of directory containing
                                                  test set
    training_set_path                             Path of directory containing
                                                  training set
    LBP_grid_x                                    Number of columns in grid                 4
                                                  used for calculating LBP
    LBP_grid_y                                    Number of columns in grid                 8
                                                  used for calculating LBP
    LBP_neighbors                                 Number of neighbors                       8
                                                  used for calculating LBP
    LBP_radius                                    Radius used                               1
                                                  for calculating LBP (in pixels)
    max_frames_with_missed_detections             Maximum number of frames with             5
                                                  missed detection that
                                                  does not interrupt tracking
    offset_pct_x                                  % of the image to keep next to            0.20
                                                  the eyes in the horizontal direction
    offset_pct_y                                  % of the image to keep next to            0.50
                                                  the eyes in the vertical direction
    sim_tracking                                  If True, results from all frames in       False
                                                  video are aggregated
    sliding_window_size                           Size of sliding window in seconds         5.0
    software_test_file                            Path of image to be used for
                                                  software test
    test_video_path                               Path of test video
    training_images_nr                            Number of images per person used in
                                                  training set
    use_captions                                  If True, training set                     False
                                                  for face recognition is built
                                                  on the base of captions
    use_eye_detection                             If True, use eye detection for detecting  True
                                                  eye position for aligning faces in
                                                  test images
    use_eye_detection_in_training                 If True, use eye detection for detecting  True
                                                  eye position for aligning faces in
                                                  training images
    use_eyes_position                             If True, align faces in test images       True
                                                  by using eye positions
    use_eyes_position_in_training                 If True, align faces in training images   True
                                                  by using eye positions
    use_face_detection_in_training                If True, use face detection               False
                                                  for images in training set
    use_majority_rule                             If True, in aggregating results           True
                                                  from several frames,
                                                  final tag is the tag that was
                                                  assigned to the majority of frames
    use_mean_confidence_rule                      If True, in aggregating results           False
                                                  from several frames,
                                                  final tag is the tag that received
                                                  the minimum value for the mean of
                                                  confidences among frames
    use_min_confidence_rule                       If True, in aggregating results           True
                                                  from several frames,
                                                  final tag is the tag that received
                                                  the minimum confidence value
    use_NBNN                                      If True,                                  False
                                                  use Naive Bayes Nearest Neighbor
    use_one_file_for_face_models                  If True, use one file for face models     True
    use_original_fps                              If True, original frame rate is used      False
    use_original_fps_in_training                  If True, use original frame rate          False
                                                  of video when creating training set
    use_resizing                                  If True, resize images                    True
    use_sliding_window                            If True, use sliding window               False
                                                  in face extraction
    use_tracking                                  If True,                                  False
                                                  use tracking in face extraction
    use_weighted_regions                          If True, use weighted LBP                 False
    used_fps                                      Frame rate at which video                 5.0
                                                  is analyzed (in frames per second)
    used_fps_in_training                          Frame rate at which video is analyzed     1.0
                                                  in training from captions
                                                  (in frames per second)
    ============================================  ========================================  ==============
    """

    # Folder with results
    results_path = ce.FACE_RECOGNITION_RESULTS_PATH
    sim_tracking = ce.SIM_TRACKING
    # Folder with test files
    test_set_path = ce.FACE_RECOGNITION_TEST_SET_PATH
    use_captions = ce.USE_CAPTIONS
    use_tracking = ce.USE_TRACKING
    video_path = ce.TEST_VIDEO_PATH
    if params is not None:
        if ce.RESULTS_PATH_KEY in params:
            results_path = params[ce.RESULTS_PATH_KEY]
        if ce.SIM_TRACKING_KEY in params:
            sim_tracking = params[ce.SIM_TRACKING_KEY]
        if ce.TEST_SET_PATH_KEY in params:
            test_set_path = params[ce.TEST_SET_PATH_KEY]
        if ce.USE_CAPTIONS_KEY in params:
            use_captions = params[ce.USE_CAPTIONS_KEY]
        if use_captions:
            if ce.TEST_VIDEO_PATH_KEY in params:
                video_path = params[ce.TEST_VIDEO_PATH_KEY]
        else:
            video_path = None
        if ce.USE_TRACKING_KEY in params:
            use_tracking = params[ce.USE_TRACKING_KEY]


    tot_rec_frames_nr = 0  # Number of correctly recognized frames
    tot_test_frames_nr = 0  # Number of total test frames
    mean_rec_time = 0  # Mean recognition time for videos

    # List of confidence values for true positives
    true_pos_confidence_list = []
    # List of confidence values for false positives
    false_pos_confidence_list = []
    # List of tags of people that appear in test set
    tested_people_tag_list = []

    fm = FaceModels(params=params, video_path=video_path)
    # Number of people
    people_nr = fm.get_people_nr()

    # Initialize dictionaries with people
    people_true_positives_dict = {}
    people_false_positives_dict = {}
    people_test_frames_nr_dict = {}

    tags = fm.get_tags()
    
    for tag in tags:
        
        people_true_positives_dict[tag] = 0
        people_false_positives_dict[tag] = 0
        people_test_frames_nr_dict[tag] = 0

    # Iterate over all videos
    
    experiment_dict_list = []
    
    number_of_anal_video = 0
    
    for video in os.listdir(test_set_path):
        
        # Dictionary for YAML file with results
        experiment_dict = {}
        experiment_dict_frames = []
        
        assigned_tag = 'Undefined'
        final_confidence = -1

        video_delta_xs = []
        video_delta_ys = []
        video_delta_ws = []

        ann_face_tag, file_ext = os.path.splitext(video)

        print('Annotated face tag: ', ann_face_tag)

        tested_people_tag_list.append(ann_face_tag)

        video_complete_path = test_set_path + video

        print(video_complete_path)

        try:

            fe = FaceExtractor(fm)

            handle = fe.extract_faces_from_video(video_complete_path)

            results = fe.get_results(handle)

            error = results[c.ERROR_KEY]

            if error:

                print('Warning')
                print(error)

            else:

                video_test_frames_nr = results[ce.TOT_FRAMES_NR_KEY]

                tot_test_frames_nr = tot_test_frames_nr + video_test_frames_nr

                people_test_frames_nr_dict[ann_face_tag] = video_test_frames_nr
                
                mean_rec_time = mean_rec_time + results[c.ELAPSED_CPU_TIME_KEY]

                if use_tracking:

                    segments = results[c.SEGMENTS_KEY]

                    tot_segments_frames_nr = 0

                    true_positives_in_segment = 0

                    for segment in segments:

                        segment_frames = segment[c.FRAMES_KEY]

                        frames_nr = segment[c.SEGMENT_TOT_FRAMES_NR_KEY]

                        assigned_tag = segment[c.ASSIGNED_TAG_KEY]

                        print('assigned_tag = ' + assigned_tag)

                        final_confidence = segment[c.CONFIDENCE_KEY]

                        tot_segments_frames_nr = tot_segments_frames_nr + frames_nr

                        if assigned_tag == ann_face_tag:

                            people_true_positives_dict[assigned_tag] += frames_nr
                            true_positives_in_segment = true_positives_in_segment + frames_nr
                            tot_rec_frames_nr = tot_rec_frames_nr + frames_nr
                            true_pos_confidence_list.append(final_confidence)

                        else:

                            if assigned_tag != 'Undefined':

                                people_false_positives_dict[assigned_tag] = people_false_positives_dict[assigned_tag] + frames_nr
                                false_pos_confidence_list.append(final_confidence)


                    # Tot number of frames in segments
                    # cannot be more than number of frames in video
                    if true_positives_in_segment > video_test_frames_nr:
                        print('### WARNING! ###')
                        print 'true_positives_in_segment = ' + str(
                            true_positives_in_segment)
                        print 'video_test_frames_nr = ' + str(
                            video_test_frames_nr)
                        people_false_positives_dict[
                            ann_face_tag] += (tot_segments_frames_nr -
                                              video_test_frames_nr)
                        tot_rec_frames_nr -= (tot_segments_frames_nr -
                                              video_test_frames_nr)

                elif sim_tracking:

                    # Simulate tracking
                    # (every frame of this video contains the same person)

                    frames = results[c.FRAMES_KEY]

                    [assigned_tag, final_confidence] = aggregate_frame_results_in_sim_tracking(frames, fm)

                    print('assigned_tag = ' + assigned_tag)

                    if assigned_tag == ann_face_tag:

                        people_true_positives_dict[assigned_tag] += len(frames)
                        tot_rec_frames_nr += len(frames)
                        true_pos_confidence_list.append(final_confidence)

                    else:

                        if assigned_tag != 'Undefined':

                            people_false_positives_dict[assigned_tag] += len(
                                frames)
                            false_pos_confidence_list.append(final_confidence)

                else:

                    frames = results[c.FRAMES_KEY]

                    frame_counter = 0
                    prev_frame_counter = -1
                    for frame in frames:

                        assigned_tag = 'Undefined'
                        confidence = -1

                        faces = frame[c.FACES_KEY]
                        
                        time_stamp = frame[c.ELAPSED_VIDEO_TIME_KEY]
                        
                        experiment_dict_frame = {
                        c.ELAPSED_VIDEO_TIME_KEY: time_stamp}

                        if len(faces) != 0:

                            face = faces[0]

                            assigned_tag = face[c.ASSIGNED_TAG_KEY]
                            
                            experiment_dict_faces = []
                            
                            experiment_dict_face = {
                            c.ASSIGNED_TAG_KEY: assigned_tag}

                            experiment_dict_faces.append(experiment_dict_face)
                            
                            if len(faces) > 1:
                                
                                for face_counter in range(1, len(faces)):
                                    
                                    other_face = faces[face_counter]
                                    
                                    other_face_assigned_tag = other_face[c.ASSIGNED_TAG_KEY]
                                    
                                    experiment_dict_face = {
                                    c.ASSIGNED_TAG_KEY: other_face_assigned_tag}

                                    experiment_dict_faces.append(experiment_dict_face)

                            confidence = face[c.CONFIDENCE_KEY]

                            bbox = face[c.BBOX_KEY]

                            if((frame_counter > 0) and
                                   (frame_counter <= (prev_frame_counter + c.MAX_FR_WITH_MISSED_DET + 1))):

                                bbox_x = bbox[0]
                                bbox_y = bbox[1]
                                bbox_w = bbox[2]
                                
                                prev_bbox_x = prev_bbox[0]
                                prev_bbox_y = prev_bbox[1]
                                prev_bbox_w = prev_bbox[2]

                                delta_x = (abs(bbox_x - prev_bbox_x) /
                                           float(prev_bbox_w))
                                delta_y = (abs(bbox_x - prev_bbox_x) /
                                           float(prev_bbox_w))
                                delta_w = (abs(bbox_w - prev_bbox_w) /
                                           float(prev_bbox_w))

                                video_delta_xs.append(delta_x)
                                video_delta_ys.append(delta_y)
                                video_delta_ws.append(delta_w)

                                if((delta_x > ce.MAX_DELTA_PCT_X) or
                                           (delta_y > ce.MAX_DELTA_PCT_Y) and
                                           (delta_w > ce.MAX_DELTA_PCT_W)):

                                    print('delta_x: ', delta_x)
                                    print('delta_y: ', delta_y)
                                    print('delta_w: ', delta_w)

                            prev_frame_counter = frame_counter

                            prev_bbox = bbox
                            
                            experiment_dict_frame[c.FACES_KEY] = experiment_dict_faces

                            experiment_dict_frame[c.ASSIGNED_TAG_KEY] = assigned_tag
                            
                        else:
                            
                            experiment_dict_frame[c.FACES_KEY] = "No face detected"

                            experiment_dict_frame[c.ASSIGNED_TAG_KEY] = "No face detected"
                            
                        experiment_dict_frame[c.CONFIDENCE_KEY] = confidence
                            
                        experiment_dict_frames.append(experiment_dict_frame)

                        if assigned_tag == ann_face_tag:

                            people_true_positives_dict[assigned_tag] += 1
                            tot_rec_frames_nr += 1
                            true_pos_confidence_list.append(confidence)

                        else:

                            if assigned_tag != 'Undefined':

                                people_false_positives_dict[assigned_tag] += 1
                                false_pos_confidence_list.append(confidence)

                        frame_counter += 1
                
                experiment_dict[ce.VIDEO_COUNTER_KEY] = number_of_anal_video
        
                experiment_dict[c.ANN_TAG_KEY] = ann_face_tag
                
                if sim_tracking:
                    
                    experiment_dict[c.ASSIGNED_TAG_KEY] = assigned_tag
                    experiment_dict[c.CONFIDENCE_KEY] = final_confidence
                
                else:
                
                    experiment_dict[c.FRAMES_KEY] = experiment_dict_frames
                
                save_YAML_file(
                    results_path + ann_face_tag + ".yml", experiment_dict)
                
                experiment_dict_list.append(experiment_dict)

                number_of_anal_video += 1
                    
        except IOError, (errno, strerror):
            print "I/O error({0}): {1}".format(errno, strerror)
        except:
Пример #4
0
def train_by_images(path, db_file_name, params=None):
    """
    Train an LBP face recognizer by using captions in images

    :type path: string
    :param path: path of directory with images

    :type db_file_name: string
    :param db_file_name: path of file that will contain the face models

    :type params: dictionary
    :param params: configuration parameters (see table)

    :rtype: tuple
    :return: a (model, tags) tuple, where model is an LBPHFaceRecognizer
             and tags is the list of found tags

    ============================================  ========================================  =============================
    Key                                           Value                                     Default value
    ============================================  ========================================  =============================
    LBP_grid_x                                    Number of columns in grid                 4
                                                  used for calculating LBP
    LBP_grid_y                                    Number of columns in grid                 8
                                                  used for calculating LBP
    LBP_neighbors                                 Number of neighbors                       8
                                                  used for calculating LBP
    LBP_radius                                    Radius used                               1
                                                  for calculating LBP (in pixels)
    use_one_file_for_face_models                  If True, use one file for face models     True
    align_path                                    Path of directory
                                                  where aligned faces are saved
    check_eye_positions                           If True, check eye positions              True
    classifiers_dir_path                          Path of directory with OpenCV
                                                  cascade classifiers
    eye_detection_classifier                      Classifier for eye detection              'haarcascade_mcs_lefteye.xml'
    face_detection_algorithm                      Classifier for face detection             'HaarCascadeFrontalFaceAlt2'
                                                  ('HaarCascadeFrontalFaceAlt',
                                                   'HaarCascadeFrontalFaceAltTree',
                                                   'HaarCascadeFrontalFaceAlt2',
                                                   'HaarCascadeFrontalFaceDefault',
                                                   'HaarCascadeProfileFace',
                                                   'HaarCascadeFrontalAndProfileFaces',
                                                   'HaarCascadeFrontalAndProfileFaces2',
                                                   'LBPCascadeFrontalface',
                                                   'LBPCascadeProfileFace' or
                                                   'LBPCascadeFrontalAndProfileFaces')
    flags                                         Flags used in face detection              'DoCannyPruning'
                                                  ('DoCannyPruning', 'ScaleImage',
                                                  'FindBiggestObject', 'DoRoughSearch').
                                                  If 'DoCannyPruning' is used, regions
                                                  that do not contain lines are discarded.
                                                  If 'ScaleImage' is used, image instead
                                                  of the detector is scaled
                                                  (it can be advantegeous in terms of
                                                  memory and cache use).
                                                  If 'FindBiggestObject' is used,
                                                  only the biggest object is returned
                                                  by the detector.
                                                  'DoRoughSearch', used together with
                                                  'FindBiggestObject',
                                                  terminates the search as soon as
                                                  the first candidate object is found
    min_neighbors                                 Mininum number of neighbor bounding       5
                                                  boxes for retaining face detection
    min_size_height                               Minimum height of face detection          20
                                                  bounding box (in pixels)
    min_size_width                                Minimum width of face detection           20
                                                  bounding box (in pixels)
    scale_factor                                  Scale factor between two scans            1.1
                                                  in face detection
    max_eye_angle                                 Maximum inclination of the line           0.125
                                                  connecting the eyes
                                                  (in % of pi radians)
    min_eye_distance                              Minimum distance between eyes             0.25
                                                  (in % of the width of the face
                                                  bounding box)
    nose_detection_classifier                     Classifier for nose detection             'haarcascade_mcs_nose.xml'
    software_test_file                            Path of image to be used for
                                                  software test
    use_nose_pos_in_detection                     If True, detections with no good          False
                                                  nose position are discarded
    lev_ratio_pct_threshold                       Minimum threshold for considering         0.8
                                                  captions in frame
    min_tag_length                                Minimum length of tags considered         10
                                                  in caption recognition
    tags_file_path                                Path of text file containing
                                                  list of tags
    tesseract_parent_dir_path                     Path of directory containing
                                                  'tesseract' directory
    use_blacklist                                 If True, use blacklist of items           True
                                                  that make the results of the
                                                  caption recognition on a frame
                                                  rejected
    use_levenshtein                               If True, words found in image             True
                                                  by caption recognition and tags
                                                  are compared by using
                                                  the Levenshtein distance
    ============================================  ========================================  =============================
    """

    # Set parameters
    lbp_radius = c.LBP_RADIUS
    lbp_neighbors = c.LBP_NEIGHBORS
    lbp_grid_x = c.LBP_GRID_X
    lbp_grid_y = c.LBP_GRID_Y
    use_one_file = ce.USE_ONE_FILE_FOR_FACE_MODELS
    if params:
        if c.LBP_RADIUS_KEY in params:
            lbp_radius = params[c.LBP_RADIUS_KEY]
        if c.LBP_NEIGHBORS_KEY in params:
            lbp_radius = params[c.LBP_NEIGHBORS_KEY]
        if c.LBP_GRID_X_KEY in params:
            lbp_radius = params[c.LBP_GRID_X]
        if c.LBP_GRID_Y_KEY in params:
            lbp_radius = params[c.LBP_GRID_Y_KEY]
        if ce.USE_ONE_FILE_FOR_FACE_MODELS_KEY in params:
            use_one_file = params[ce.USE_ONE_FILE_FOR_FACE_MODELS_KEY]

    # Save processing time
    start_time = cv2.getTickCount()
    
    model = None
    
    X, y = [], []
    tags = {}
    
    image_counter = 0
     
    for image in os.listdir(path):
    
        image_complete_path = path + os.sep + image
                
        [label, tag, face] = analyze_image(image_complete_path, params)
        
        if label != -1:
            
            X.append(np.asarray(face, dtype=np.uint8))
            y.append(label)
            tags[label] = tag

            image_counter += 1
    
    # Save file with face models
    
    if use_one_file:
            
        model = cv2.createLBPHFaceRecognizer(
        lbp_radius,
        lbp_neighbors,
        lbp_grid_x,
        lbp_grid_y)
        model.train(np.asarray(X), np.asarray(y))
        model.save(db_file_name)
        
    else:
        
        y_set = set(y)
            
        for label in y_set:
            
            person_X = []
            person_y = []
            
            for i in range(0, len(y)):
                
                if y[i] == label:
                    
                    person_X.append(X[i])
                    person_y.append(label)
                       
            save_model_file(person_X, person_y, params, db_file_name)
    
    # Save labels in YAML file
    save_YAML_file(db_file_name + "-Tags", tags)     
        
    # Calculate processing time in seconds
    time_in_clocks = cv2.getTickCount() - start_time
    time_in_s = time_in_clocks / cv2.getTickFrequency()
    
    print('Creation time: ' + str(time_in_s) + ' s\n')
    
    return model, tags
Пример #5
0
    def create(self, video_path=None, db_file_name=None):
        """
        Create the face models data structure

        :type video_path: String
        :param video_path: path of video used for creating the models

        :type db_file_name: String
        :param db_file_name: the name of the file containing
                             the dump of the face models data structure

        :rtype: FaceRecognizer
        :returns: the face models data structure
        """

        print('\n### CREATING DB ####\n')

        model = None

        use_captions = ce.USE_CAPTIONS
        
        if (self._params is not None) and (ce.USE_CAPTIONS_KEY in self._params):
            
            use_captions = self._params[ce.USE_CAPTIONS_KEY]

        if use_captions:

            db_file_name = self._db_name + "-" + self._algorithm
            [model, tags] = train_by_captions(video_path, db_file_name)
            self.model = model
            self._tags = tags

        else:

            start_time = cv2.getTickCount()

            if db_file_name is None:

                db_file_name = self._db_name + "-" + self._algorithm

            sz = None
            
            use_resizing = ce.USE_RESIZING
        
            if self._params is not None:
            
                use_resizing = self._params[ce.USE_RESIZING_KEY]
            
            if use_resizing:
                
                width = c.CROPPED_FACE_WIDTH
                height = c.CROPPED_FACE_HEIGHT
        
                if self._params is not None:
            
                    width = self._params[c.CROPPED_FACE_WIDTH_KEY]
                    height = self._params[c.CROPPED_FACE_HEIGHT_KEY]
                    
                sz = (width, height)

            [X, y] = self.__read_images(self._dbpath, sz)

            if len(self._tags) > 0:

                use_one_file = ce.USE_ONE_FILE_FOR_FACE_MODELS
        
                if self._params is not None:
                    
                    use_one_file = (
                        self._params[ce.USE_ONE_FILE_FOR_FACE_MODELS_KEY])

                if use_one_file:

                    algorithm = ce.FACE_MODEL_ALGORITHM
                
                    if self._params is not None:
                        
                        algorithm = self._params[ce.FACE_MODEL_ALGORITHM_KEY]
                    
                    model = None
                    
                    if algorithm == 'Eigenfaces':
                        
                        model = cv2.createEigenFaceRecognizer()
                    
                    elif algorithm == 'Fisherfaces':
                        
                        model = cv2.createFisherFaceRecognizer()
                        
                    elif algorithm == 'LBP':
                        
                        radius = c.LBP_RADIUS
                        neighbors = c.LBP_NEIGHBORS
                        grid_x = c.LBP_GRID_X
                        grid_y = c.LBP_GRID_Y
                        
                        if self._params is not None:

                            radius = self._params[c.LBP_RADIUS_KEY]
                            neighbors = self._params[c.LBP_NEIGHBORS_KEY]
                            grid_x = self._params[c.LBP_GRID_X_KEY]
                            grid_y = self._params[c.LBP_GRID_Y_KEY]
                        
                        model = cv2.createLBPHFaceRecognizer(
                        radius,
                        neighbors,
                        grid_x,
                        grid_y)
                        
                    model.train(np.asarray(X), np.asarray(y))
                    model.save(db_file_name)
                    self.model = model

                # Save tags in YAML file
                utils.save_YAML_file(db_file_name + "-Tags", self._tags)

                time_in_clocks = cv2.getTickCount() - start_time
                time_in_seconds = time_in_clocks / cv2.getTickFrequency()
                
                self.model_creation_time = time_in_seconds
                
                print('Creation time: ' + str(time_in_seconds) + ' s\n')

            else:

                print "No model was created"
        
        return model