Exemple #1
0
def analyze_image(image_path, params=None):
    """
    Execute caption recognition on given image

    :type image_path: string
    :param image_path: path of image to be analyzed

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

    :rtype: tuple
    :returns: a (label, tag, face) tuple,
              where label is the assigned label,
              tag the assigned tag
              and face the detected face in image (as an OpenCV image)

    ============================================  ========================================  =============================
    Key                                           Value                                     Default value
    ============================================  ========================================  =============================
    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
    ============================================  ========================================  =============================
    """
    
    label = -1
    
    tag = -1

    align_path = c.ALIGNED_FACES_PATH
    if (params is not None) and (c.ALIGNED_FACES_PATH_KEY in params):
        align_path = params[c.ALIGNED_FACES_PATH_KEY]

    # Face detection
    face = get_detected_cropped_face(image_path, align_path, params)
    
    if face is not None:
            
        cap_rec_res = get_tag_from_image(image_path, params)
        
        label = cap_rec_res[c.ASSIGNED_LABEL_KEY]
        
        tag = cap_rec_res[c.ASSIGNED_TAG_KEY]

    return label, tag, face
    def __read_images(self, path, sz=None):

        l = 0
        X, y = [], []
        
        # Set parameters
        align_path = c.ALIGNED_FACES_PATH
        use_eyes_pos_in_training = ce.USE_EYES_POSITION_IN_TRAINING
        use_eye_det_in_training = ce.USE_EYE_DETECTION_IN_TRAINING
        use_face_det_in_training = ce.USE_FACE_DETECTION_IN_TRAINING
        offset_pct_x = c.OFFSET_PCT_X
        offset_pct_y = c.OFFSET_PCT_Y
        
        if self._params is not None:
            
            align_path = self._params[c.ALIGNED_FACES_PATH_KEY]
            use_eyes_pos_in_training = (self._params
                [ce.USE_EYES_POSITION_IN_TRAINING_KEY])
            use_eye_det_in_training = (
                self._params[ce.USE_EYE_DETECTION_IN_TRAINING_KEY])
            use_face_det_in_training = (
                self._params[ce.USE_FACE_DETECTION_IN_TRAINING_KEY])
            offset_pct_x = self._params[c.OFFSET_PCT_X_KEY]
            offset_pct_y = self._params[c.OFFSET_PCT_Y_KEY]
        
        for dirname, dirnames, filenames in os.walk(path):
            for subdirname in dirnames:
                # print "creating model for", subdirname
                subject_path = os.path.join(dirname, subdirname)
                # print "subject path:", subject_path
                file_counter = 0
                for filename in os.listdir(subject_path):
                    # print "image path", os.path.join(subject_path, filename)
                    try:
                        
                        if use_face_det_in_training:
                            im = fd.get_detected_cropped_face(
                                os.path.join(subject_path, filename),
                                align_path, self._params,
                                return_always_face=False)

                        elif use_eyes_pos_in_training:
                            
                            if use_eye_det_in_training:
                                im = None
                                crop_result = fd.get_cropped_face(
                                    os.path.join(subject_path, filename),
                                    align_path, self._params,
                                    return_always_face=False)
                                if crop_result:
                                    im = crop_result[c.FACE_KEY]

                            else:
                                im = fd.get_cropped_face_using_fixed_eye_pos(
                                    os.path.join(subject_path, filename),
                                    align_path,
                                    offset_pct=(offset_pct_x, offset_pct_y),
                                    dest_size=sz)
                        
                        else:
                            im = cv2.imread(
                                os.path.join(subject_path, filename),
                                cv2.IMREAD_GRAYSCALE)
                            # resize to given size (if given)
                            if (im is not None) and (sz is not None):
                                im = cv2.resize(im, sz)

                        if im is not None:

                            X.append(np.asarray(im, dtype=np.uint8))
                            y.append(l)
                            self._tags[l] = str(subdirname)

                        else:
                            print "Image", os.path.join(subject_path, filename), "not considered"

                    except IOError, (errno, strerror):
                        print "I/O error({0}): {1}".format(errno, strerror)
                    except:
                        print "Unexpected error:", sys.exc_info()[0]
                        raise