Exemple #1
0
    def classify_tagged_photo(self, image_metadata):
        """Classifies faces in a tagged photo

        Uses the CropImageProcessor to crop the image, passes it on to
        the image_processor then use the clf to classify the image

        :param image_metadata: metadata of the image 
        :return: classification result ([] if 'Content not found')

        """
        imageFile = requests.get(image_metadata['images']\
                                 ['standard_resolution']['url']).content

        if imageFile == 'Content not found':
            return []

        fout = open('temp.jpg', 'w')
        fout.write(imageFile)
        fout.close()

        image = cv2.imread('temp.jpg')
        height, width, channels = image.shape
        users_tagged = image_metadata['users_in_photo']

        url = image_metadata['images']['standard_resolution']['url']
        try:
            photo_id = url[url.rindex('cdninstagram.com') + 17:].replace(
                '/', '@')
        except:
            photo_id = url[url.rindex('https://') + 9:].replace('/', '@')

        markers_list = []  #list of tagged position markers
        clf_results = []  #list of classification results

        for user in users_tagged:
            user_id = user['user']['id']

            y_coord = float(user['position']['y']) * height
            x_coord = float(user['position']['x']) * width

            crop_processor = CropImageProcessor((x_coord, y_coord),
                                                tagged_radius=100)
            cropped_square = crop_processor.process_image(image)
            cropped_face, face_in_square = self._image_processor.process_image(
                cropped_square)

            try:
                if face_in_square:
                    #stores it in directory
                    fname = self._image_processor.save_image(
                        image=cropped_face, user_id=user_id, photo_id=photo_id)
                    input_image = caffe.io.load_image(fname)
                    clf_results.append(
                        (user_id, photo_id, self._clf(input_image)))
            except Exception, e:
                print str(e)
Exemple #2
0
def tagged_face_clf(img_lst, cascade_file, scale_factor, min_neighbors,
                    min_size, crop_radius):
    """Classifies face based on full image and crop position

    :param img_lst: list of image objects with full image and crop 
    position information
    :param cascade_file, scale_factor, min_neighbors, min_size,
    crop_radius: tuning parameters
    :return pred_lst: list of predictions (face or not)
    """
    pred_lst = []

    i = 0

    for img_obj in img_lst:

        image = img_obj['full_img']

        if image == None:
            face_in_square = None

        else:
            pos_x = img_obj['crop_pos'][0]
            pos_y = img_obj['crop_pos'][1]

            height, width, channels = image.shape

            x_coord = float(pos_x) * width
            y_coord = float(pos_y) * height

            crop_processor = CropImageProcessor((x_coord, y_coord),
                                                tagged_radius=crop_radius)
            cropped_square = crop_processor.process_image(image)
            face_detector = FaceDetector(cascade_file, scale_factor,
                                         min_neighbors, min_size)
            face_detector_processor = FaceDetectorProcessor()
            face_detector_processor.detector = face_detector
            cropped_face, face_in_square = face_detector_processor.process_image(
                cropped_square)

            i += 1
            print "Inserting prediction result", i

        pred_lst.append(face_in_square)

    return pred_lst