예제 #1
0
def main(args):
    global face_detector
    global classifier_object

    if (not args.checkpoint_path):
        raise ValueError(
            'You must supply the checkpoint path with --checkpoint_path')
    if (not os.path.exists(args.checkpoint_path)):
        print(
            'The checkpoint path is missing. Error processing the data source without the checkpoint path.'
        )
        return (False)

    if (not args.dataset_dir):
        raise ValueError(
            'You must supply the dataset directory with --dataset_dir')
    if (not os.path.exists(args.dataset_dir)):
        print(
            'The dataset directory is missing. Error processing the data source without the dataset directory.'
        )
        return (False)

    if (args.model_root_dir):
        model_root_dir = args.model_root_dir
    else:
        model_root_dir = NetworkFactory.model_deploy_dir()

    last_network = 'ONet'
    face_detector = FaceDetector(last_network, model_root_dir)

    classifier_object = Classifier()
    if (not classifier_object.load_dataset(args.dataset_dir)):
        return (False)
    if (not classifier_object.load_model(args.checkpoint_path, args.model_name,
                                         args.gpu_memory_fraction)):
        return (False)

    image1 = cv2.imread(args.image1)
    input_image_height, input_image_width, input_image_channels = image1.shape
    #print(input_image_height, input_image_width)

    image2 = cv2.imread(args.image2)
    input_image_height, input_image_width, input_image_channels = image2.shape
    #print(input_image_height, input_image_width)

    i1 = features(image1)
    i2 = features(image2)

    #if(not (i1 and i2)):
    #return(False)

    result = l1_loss(i1, i2)
    print("The answer is " + str(result))

    result = l2_loss(i1, i2)
    print("The answer is " + str(result))

    result = dot_product(i1, i2)
    print("The answer is " + str(result))
예제 #2
0
    def generate_samples(self, annotation_image_dir, annotation_file_name,
                         model_train_dir, network_name, minimum_face_size,
                         target_root_dir):
        status, dataset = self._read(annotation_image_dir,
                                     annotation_file_name)
        if (not status):
            return (False)

        test_data = InferenceBatch(dataset['images'])

        previous_network = NetworkFactory.previous_network(network_name)
        if (not model_train_dir):
            model_train_dir = NetworkFactory.model_train_dir()
        face_detector = FaceDetector(previous_network, model_train_dir)
        face_detector.set_min_face_size(minimum_face_size)
        face_detector.set_threshold([0.6, 0.7, 0.7])

        detected_boxes, landmarks = face_detector.detect_face(test_data)

        return (self._generate_hard_samples(dataset, network_name,
                                            detected_boxes, minimum_face_size,
                                            target_root_dir))
def main(args):
    probability_threshold = 50.0

    if (not args.input_tsv_file):
        raise ValueError(
            'You must supply input TSV file with --input_tsv_file.')
    if (not args.output_tsv_file):
        raise ValueError(
            'You must supply output TSV file with --output_tsv_file.')

    if (not os.path.isfile(args.input_tsv_file)):
        return (False)

    model_root_dir = NetworkFactory.model_deploy_dir()
    last_network = 'ONet'
    face_detector = FaceDetector(last_network, model_root_dir)

    classifier_object = Classifier()
    if (not classifier_object.load_dataset(args.dataset_dir)):
        return (False)
    if (not classifier_object.load_model(args.checkpoint_path, args.model_name,
                                         args.gpu_memory_fraction)):
        return (False)

    network_size = classifier_object.network_image_size()

    number_of_images = 0
    good_images = 0
    input_tsv_file = open(args.input_tsv_file, 'r')
    output_tsv_file = open(args.output_tsv_file, 'w')
    while (True):
        input_data = input_tsv_file.readline().strip()
        if (not input_data):
            break

        number_of_images += 1
        fields = input_data.split('\t')
        line_number = str(fields[0])
        image_string = fields[1]

        decoded_image_string = base64.b64decode(image_string)
        image_data = np.fromstring(decoded_image_string, dtype=np.uint8)
        input_image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
        height, width, channels = input_image.shape

        cv2.imwrite('image.png', input_image)
        #misc.imsave('image.png', input_image)
        input_image = misc.imread('image.png')

        input_clone = np.copy(input_image)
        boxes_c, landmarks = face_detector.detect(input_clone)

        face_probability = 0.0
        found = False
        crop_box = []
        for index in range(boxes_c.shape[0]):
            if (boxes_c[index, 4] > face_probability):
                found = True
                face_probability = boxes_c[index, 4]
                bounding_box = boxes_c[index, :4]
                crop_box = [
                    int(max(bounding_box[0], 0)),
                    int(max(bounding_box[1], 0)),
                    int(min(bounding_box[2], width)),
                    int(min(bounding_box[3], height))
                ]
        if (found):
            cropped_image = input_image[crop_box[1]:crop_box[3],
                                        crop_box[0]:crop_box[2], :]
        else:
            cropped_image = input_image

        #resized_image = cv2.resize(cropped_image, (network_size, network_size), interpolation=cv2.INTER_LINEAR)
        resized_image = misc.imresize(cropped_image,
                                      (network_size, network_size),
                                      interp='bilinear')

        class_names_probabilities = classifier_object.classify(
            resized_image, print_results=False)
        predicted_name = ''
        probability = 0.0
        if (len(class_names_probabilities) > 0):
            names = map(operator.itemgetter(0), class_names_probabilities)
            probabilities = map(operator.itemgetter(1),
                                class_names_probabilities)
            predicted_name = str(names[0])
            probability = probabilities[0]
            if ((probability > probability_threshold)
                    or (probability >
                        (probabilities[1] + probabilities[2] / 2.0))):
                good_images += 1

        print(number_of_images, ', predicted_name -', predicted_name,
              ', probability -', probability)
        print('Accuracy = ', (good_images * 100.0) / number_of_images, ' for ',
              number_of_images, ' images.')

        #cv2.imshow('image', cropped_image)
        #cv2.waitKey();

        output_tsv_file.write(line_number + '\t' + str(predicted_name) + '\t' +
                              str(probability) + '\n')

    print('Accuracy = ', (good_images * 100.0) / number_of_images, ' for ',
          number_of_images, ' images.')
    return (True)
예제 #4
0
def main(args):

    if (not args.checkpoint_path):
        raise ValueError(
            'You must supply the checkpoint path with --checkpoint_path')
    if (not os.path.exists(args.checkpoint_path)):
        print(
            'The checkpoint path is missing. Error processing the data source without the checkpoint path.'
        )
        return (False)

    if (not args.dataset_dir):
        raise ValueError(
            'You must supply the dataset directory with --dataset_dir')
    if (not os.path.exists(args.dataset_dir)):
        print(
            'The dataset directory is missing. Error processing the data source without the dataset directory.'
        )
        return (False)

    if (args.model_root_dir):
        model_root_dir = args.model_root_dir
    else:
        model_root_dir = NetworkFactory.model_deploy_dir()

    last_network = 'ONet'
    face_detector = FaceDetector(last_network, model_root_dir)

    classifier_object = Classifier()
    if (not classifier_object.load_dataset(args.dataset_dir)):
        return (False)
    if (not classifier_object.load_model(args.checkpoint_path, args.model_name,
                                         args.gpu_memory_fraction)):
        return (False)

    webcamera = cv2.VideoCapture(args.webcamera_id)
    webcamera.set(3, 600)
    webcamera.set(4, 800)

    image = cv2.imread('/git-space/16.jpg')
    input_image_height, input_image_width, input_image_channels = image.shape
    print(input_image_height, input_image_width)

    face_probability = 0.75
    minimum_face_size = 24
    while True:
        start_time = cv2.getTickCount()
        status, current_frame = webcamera.read()

        is_busy = False
        if status:
            current_image = np.array(current_frame)
            image_clone = np.copy(current_image)

            if (is_busy):
                continue

            is_busy = True
            boxes_c, landmarks = face_detector.detect(image_clone)

            end_time = cv2.getTickCount()
            time_duration = (end_time - start_time) / cv2.getTickFrequency()
            frames_per_sec = 1.0 / time_duration
            cv2.putText(current_frame, '{:.2f} FPS'.format(frames_per_sec),
                        (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0),
                        2)

            for index in range(boxes_c.shape[0]):
                bounding_box = boxes_c[index, :4]
                probability = boxes_c[index, 4]

                crop_box = []
                if (probability > face_probability):
                    height, width, channels = image_clone.shape

                    crop_box = [
                        int(max(bounding_box[0], 0)),
                        int(max(bounding_box[1], 0)),
                        int(min(bounding_box[2], width)),
                        int(min(bounding_box[3], height))
                    ]
                    cropped_image = image_clone[crop_box[1]:crop_box[3],
                                                crop_box[0]:crop_box[2], :]

                    crop_height, crop_width, crop_channels = cropped_image.shape
                    if (crop_height < minimum_face_size) or (
                            crop_width < minimum_face_size):
                        continue

                    cv2.rectangle(image_clone, (crop_box[0], crop_box[1]),
                                  (crop_box[2], crop_box[3]), (0, 255, 0), 1)

                    class_names_probabilities = classifier_object.classify(
                        cropped_image, 1)
                    predicted_name = class_names_probabilities[0][0]
                    probability = class_names_probabilities[0][1]

                    if (probability > args.threshold):
                        cv2.putText(
                            image_clone,
                            predicted_name + ' - {:.2f}'.format(probability),
                            (crop_box[0], crop_box[1] - 2),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

            cv2.imshow("", image_clone)
            is_busy = False

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            print('Error detecting the webcamera.')
            break

    webcamera.release()
예제 #5
0
def main(args):
    if (args.model_root_dir):
        model_root_dir = args.model_root_dir
    else:
        if (args.test_mode):
            model_root_dir = NetworkFactory.model_train_dir()
        else:
            model_root_dir = NetworkFactory.model_deploy_dir()

    last_network = 'ONet'
    face_detector = FaceDetector(last_network, model_root_dir)
    webcamera = cv2.VideoCapture(args.webcamera_id)
    webcamera.set(3, 600)
    webcamera.set(4, 800)

    while True:
        start_time = cv2.getTickCount()
        status, current_frame = webcamera.read()
        if status:
            input_bgr_image = np.array(current_frame)
            image_height, image_width, image_channels = input_bgr_image.shape
            input_rgb_image = cv2.cvtColor(input_bgr_image, cv2.COLOR_BGR2RGB)

            #boxes_c, landmarks = face_detector.detect(input_bgr_image)
            boxes_c, landmarks = face_detector.detect(input_rgb_image)

            end_time = cv2.getTickCount()
            time_duration = (end_time - start_time) / cv2.getTickFrequency()
            frames_per_sec = 1.0 / time_duration
            cv2.putText(input_bgr_image, '{:.2f} FPS'.format(frames_per_sec),
                        (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0),
                        2)

            for index in range(boxes_c.shape[0]):
                bounding_box = boxes_c[index, :4]
                probability = boxes_c[index, 4]
                crop_box = [
                    int(max(bounding_box[0], 0)),
                    int(max(bounding_box[1], 0)),
                    int(min(bounding_box[2], image_width)),
                    int(min(bounding_box[3], image_height))
                ]

                crop_width = crop_box[3] - crop_box[1]
                crop_height = crop_box[2] - crop_box[0]

                if (crop_height < args.minimum_face_size) or (
                        crop_width < args.minimum_face_size):
                    continue

                if (probability > args.threshold):
                    cv2.rectangle(input_bgr_image, (crop_box[0], crop_box[1]),
                                  (crop_box[2], crop_box[3]), (0, 255, 0), 1)
                    cv2.putText(input_bgr_image,
                                'Score - {:.2f}'.format(probability),
                                (crop_box[0], crop_box[1] - 2),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

            cv2.imshow("", input_bgr_image)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            print('Error detecting the webcamera.')
            break

    webcamera.release()
    cv2.destroyAllWindows()
예제 #6
0
def main(args):

	output_dir = os.path.expanduser(args.output_dir)
    	if(not os.path.exists(output_dir)):
        	os.mkdir(output_dir)

	is_processed = {}	
	probability_threshold = [ 0.95, 0.90, 0.85, 0.80 ]

	if(not args.input_tsv_file):
		raise ValueError('You must supply input TSV file with --input_tsv_file.')
	if(not os.path.isfile(args.input_tsv_file)):
		return(False)

	model_root_dir = NetworkFactory.model_deploy_dir()
	last_network='ONet'
	face_detector = FaceDetector(last_network, model_root_dir)

	classifier_object = Classifier()
	if(not classifier_object.load_dataset(args.dataset_dir)):
		return(False)
	if(not classifier_object.load_model(args.checkpoint_path, args.model_name, args.gpu_memory_fraction)):
		return(False)

	network_size = classifier_object.network_image_size()

	celebrity_count = 0	
	for current_threshold in probability_threshold:
		input_tsv_file = open(args.input_tsv_file, 'r')

		while( True ):
		
			input_data = input_tsv_file.readline().strip()
			if( not input_data ):
       				break			

       			fields = input_data.split('\t')		
       			class_name = str(fields[2]) 
			if class_name in is_processed.keys():
				continue

       			image_string = fields[1]
			image_search_rank = fields[3]
       			decoded_image_string = base64.b64decode(image_string)
       			image_data = np.fromstring(decoded_image_string, dtype=np.uint8)
       			input_image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
			height, width, channels = input_image.shape

			class_dir = fields[2] 
			img_name = class_dir + '.png'

			cv2.imwrite('image.png', input_image)
			#misc.imsave('image.png', input_image)
			input_image = misc.imread('image.png')	

			input_clone = np.copy(input_image)
			boxes_c, landmarks = face_detector.detect(input_clone)

			face_probability = 0.0
			found = False
			crop_box = []
       			for index in range(boxes_c.shape[0]):      			
				if(boxes_c[index, 4] > face_probability):
					found = True
      					face_probability = boxes_c[index, 4]
					bounding_box = boxes_c[index, :4]
      					crop_box = [int(max(bounding_box[0],0)), int(max(bounding_box[1],0)), int(min(bounding_box[2], width)), int(min(bounding_box[3], height))]
			if(found):
				cropped_image = input_image[crop_box[1]:crop_box[3],crop_box[0]:crop_box[2],:]			
			else:
				cropped_image = input_image

			#resized_image = cv2.resize(cropped_image, (network_size, network_size), interpolation=cv2.INTER_LINEAR)
			resized_image = misc.imresize(cropped_image, (network_size, network_size), interp='bilinear')

			class_names_probabilities = classifier_object.classify(resized_image, print_results=False)
			predicted_name = ''
			probability = 0.0
			if(len(class_names_probabilities) > 0):
				names = map(operator.itemgetter(0), class_names_probabilities)
				probabilities = map(operator.itemgetter(1), class_names_probabilities)
				predicted_name = str(names[0])
				probability = probabilities[0]
				if( class_name != predicted_name ):
					continue

				if(probability < current_threshold):
					continue  

			is_processed[class_name] = True

       			full_class_dir = os.path.join(output_dir, class_dir)
       			if not os.path.exists(full_class_dir):
       				os.mkdir(full_class_dir)
       				celebrity_count = celebrity_count + 1

       			full_path = os.path.join(full_class_dir, img_name)
       			cv2.imwrite(full_path, resized_image)            		

			#cv2.imshow('image', cropped_image)
			#cv2.waitKey();

	print('Processed ', celebrity_count, 'celebrities.')
	return(True)
예제 #7
0
    def create_detector(self, last_network, model_root_dir):
        self._face_detector = FaceDetector(last_network, model_root_dir)

        minimum_face_size = datasets_constants.minimum_face_size
        self._face_detector.set_min_face_size(minimum_face_size)
        return (True)
예제 #8
0
class ModelEvaluator(object):
    def __init__(self):
        self._test_dataset = None
        self._face_detector = None

    def load(self, dataset_name, annotation_image_dir, annotation_file_name):
        face_dataset = DatasetFactory.face_dataset(dataset_name)
        if (not face_dataset):
            return (False)

        if (not face_dataset.read(annotation_image_dir, annotation_file_name)):
            return (False)

        self._test_dataset = face_dataset.data()
        return (True)

    def create_detector(self, last_network, model_root_dir):
        self._face_detector = FaceDetector(last_network, model_root_dir)

        minimum_face_size = datasets_constants.minimum_face_size
        self._face_detector.set_min_face_size(minimum_face_size)
        return (True)

    def evaluate(self, print_result=False):
        if (not self._test_dataset):
            return (False)

        if (not self._face_detector):
            return (False)

        test_data = InferenceBatch(self._test_dataset['images'])
        detected_boxes, landmarks = self._face_detector.detect_face(test_data)

        image_file_names = self._test_dataset['images']
        ground_truth_boxes = self._test_dataset['bboxes']
        number_of_images = len(image_file_names)

        if (not (len(detected_boxes) == number_of_images)):
            return (False)

        number_of_positive_faces = 0
        number_of_part_faces = 0
        number_of_ground_truth_faces = 0
        accuracy = 0.0
        for image_file_path, detected_box, ground_truth_box in zip(
                image_file_names, detected_boxes, ground_truth_boxes):
            ground_truth_box = np.array(
                ground_truth_box, dtype=np.float32).reshape(-1, 4)
            number_of_ground_truth_faces = number_of_ground_truth_faces + len(
                ground_truth_box)
            if (detected_box.shape[0] == 0):
                continue

            detected_box = convert_to_square(detected_box)
            detected_box[:, 0:4] = np.round(detected_box[:, 0:4])

            current_image = cv2.imread(image_file_path)

            for box in ground_truth_box:

                #x_left, y_top, x_right, y_bottom, _ = box.astype(int)
                #width = x_right - x_left + 1
                #height = y_bottom - y_top + 1

                #if( (x_left < 0) or (y_top < 0) or (x_right > (current_image.shape[1] - 1) ) or (y_bottom > (current_image.shape[0] - 1 ) ) ):
                #	continue

                current_IoU = IoU(box, detected_box)
                maximum_IoU = np.max(current_IoU)

                accuracy = accuracy + maximum_IoU
                if (maximum_IoU > datasets_constants.positive_IoU):
                    number_of_positive_faces = number_of_positive_faces + 1
                elif (maximum_IoU > datasets_constants.part_IoU):
                    number_of_part_faces = number_of_part_faces + 1

        if (print_result):
            print('Positive faces       - ', number_of_positive_faces)
            print('Partial faces        - ', number_of_part_faces)
            print('Total detected faces - ',
                  (number_of_positive_faces + number_of_part_faces))
            print('Ground truth faces   - ', number_of_ground_truth_faces)
            print('Positive accuracy    - ',
                  number_of_positive_faces / number_of_ground_truth_faces)
            print('Detection accuracy   - ',
                  (number_of_positive_faces + number_of_part_faces) /
                  number_of_ground_truth_faces)
            print('Accuracy             - ',
                  accuracy / number_of_ground_truth_faces)

        return (True)
예제 #9
0
def align_faces(args):

    class_names = DatasetAnalyzer.read_class_names(args.class_name_file)
    if (len(class_names) == 0):
        class_names = DatasetAnalyzer.get_class_names(args.source_dir)

    if (len(class_names) == 0):
        return (False)

    source_path = os.path.expanduser(args.source_dir)

    target_path = os.path.expanduser(args.target_dir)
    if (not os.path.exists(target_path)):
        os.makedirs(target_path)

    if (args.model_root_dir):
        model_root_dir = args.model_root_dir
    else:
        model_root_dir = NetworkFactory.model_deploy_dir()

    last_network = 'ONet'
    face_detector = FaceDetector(last_network, model_root_dir)
    face_detector.set_threshold([0.6, 0.7, 0.7])
    face_detector.set_min_face_size(20)

    total_no_of_images = 0
    successfully_aligned_images = 0

    if (args.class_name_file):
        prefix = args.class_name_file + '-'
    else:
        prefix = ""

    successful_images = open(prefix + 'successful.txt', 'w')
    unsuccessful_images = open(prefix + 'unsuccessful.txt', 'w')

    for class_name in class_names:

        source_class_dir = os.path.join(source_path, class_name)
        if (not os.path.isdir(source_class_dir)):
            continue

        target_class_dir = os.path.join(target_path, class_name)
        if (not os.path.exists(target_class_dir)):
            os.makedirs(target_class_dir)

        image_filenames = os.listdir(source_class_dir)
        for image_filename in image_filenames:

            source_relative_path = os.path.join(class_name, image_filename)
            source_filename = os.path.join(source_class_dir, image_filename)
            if (not os.path.isfile(source_filename)):
                continue

            total_no_of_images += 1

            target_filename = os.path.join(target_class_dir, image_filename)
            if (os.path.exists(target_filename)):
                continue

            try:
                current_image = cv2.imread(source_filename, cv2.IMREAD_COLOR)
            except (IOError, ValueError, IndexError) as error:
                unsuccessful_images.write(source_relative_path + os.linesep)
                continue

            if (current_image is None):
                continue

            image_height = current_image.shape[0]
            image_width = current_image.shape[1]

            boxes_c, landmarks = face_detector.detect(current_image)

            face_probability = 0.0
            found = False
            crop_box = np.zeros(4, dtype=np.int32)
            for index in range(boxes_c.shape[0]):
                if (boxes_c[index, 4] > face_probability):
                    found = True
                    face_probability = boxes_c[index, 4]
                    bounding_box = boxes_c[index, :4]

                    bounding_box_width = bounding_box[2] - bounding_box[0]
                    bounding_box_height = bounding_box[3] - bounding_box[1]

                    width_offset = (bounding_box_width * args.margin) / (2 *
                                                                         100.0)
                    height_offset = (bounding_box_height *
                                     args.margin) / (2 * 100.0)

                    crop_box[0] = int(max((bounding_box[0] - width_offset), 0))
                    crop_box[1] = int(max((bounding_box[1] - height_offset),
                                          0))
                    crop_box[2] = int(
                        min((bounding_box[2] + width_offset), image_width))
                    crop_box[3] = int(
                        min((bounding_box[3] + height_offset), image_height))

            if (found):
                cropped_image = current_image[crop_box[1]:crop_box[3],
                                              crop_box[0]:crop_box[2], :]
                resized_image = cv2.resize(cropped_image,
                                           (args.image_size, args.image_size),
                                           interpolation=cv2.INTER_LINEAR)

                cv2.imwrite(target_filename, resized_image)
                successful_images.write(source_relative_path + os.linesep)

                successfully_aligned_images += 1
            else:
                unsuccessful_images.write(source_relative_path + os.linesep)

    print('Total number of images are - %d' % total_no_of_images)
    print('Number of successfully aligned images are - %d' %
          successfully_aligned_images)
    print('Number of unsuccessful images are - %d' %
          (total_no_of_images - successfully_aligned_images))

    return (True)