def profile_face_clf(img_lst, cascade_file, scale_factor, min_neighbors, min_size): """Classifies face based on profile image :param img_lst: list of image objects with profile image :param cascade_file, scale_factor, min_neighbors, min_size: tuning parameters :return pred_lst: list of predictions (face or not) """ pred_lst = [] i = 0 for img_obj in img_lst: image = img_obj["face_img"] 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(image) i += 1 print "Inserting prediction result ", i pred_lst.append(face_in_square) return pred_lst
def profile_face_clf(img_lst, cascade_file, scale_factor, min_neighbors, min_size): """Classifies face based on profile image :param img_lst: list of image objects with profile image :param cascade_file, scale_factor, min_neighbors, min_size: tuning parameters :return pred_lst: list of predictions (face or not) """ pred_lst = [] i = 0 for img_obj in img_lst: image = img_obj['face_img'] 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( image) i += 1 print "Inserting prediction result ", i pred_lst.append(face_in_square) return pred_lst
def classify_profile_photo(self, image_metadata): """Classifies the face in a profile 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') """ url = image_metadata['user']['profile_picture'] imageFile = requests.get(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 photo_id = url[url.rindex('https://') + 9:].replace('/', '@') clf_results = [] #list of classification results user_id = image_metadata['user']['id'] #####Optimized parameters after model tuning face_detector = FaceDetector('haarcascade_frontalface_default.xml', 1.1, 1, (20, 20)) face_detector_processor = FaceDetectorProcessor() face_detector_processor.detector = face_detector self._image_processor = face_detector_processor ##### cropped_face, face_in_square = self._image_processor.process_image( image) 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)
def classify_profile_photo(self, image_metadata): """Classifies the face in a profile 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') """ url = image_metadata['user']['profile_picture'] imageFile = requests.get(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 photo_id = url[url.rindex('https://')+9:].replace('/', '@') clf_results = [] #list of classification results user_id = image_metadata['user']['id'] #####Optimized parameters after model tuning face_detector = FaceDetector('haarcascade_frontalface_default.xml', 1.1, 1, (20,20)) face_detector_processor = FaceDetectorProcessor() face_detector_processor.detector = face_detector self._image_processor = face_detector_processor ##### cropped_face, face_in_square = self._image_processor.process_image(image) 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)
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
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
def __init__(self, clf, image_processor=FaceDetectorProcessor()): self._image_processor = image_processor self._clf = clf