def main(args): if (not args.annotation_file_name): raise ValueError( 'You must supply input face dataset annotations file with --annotation_file_name.' ) if (not args.annotation_image_dir): raise ValueError( 'You must supply input face dataset training image directory with --annotation_image_dir.' ) model_evaluator = ModelEvaluator() status = model_evaluator.load(args.dataset_name, args.annotation_image_dir, args.annotation_file_name) if (not status): print('Error loading the test dataset.') if (args.model_root_dir): model_root_dir = args.model_root_dir else: model_root_dir = NetworkFactory.model_deploy_dir() last_network = 'ONet' status = model_evaluator.create_detector(last_network, model_root_dir) if (not status): print('Error creating the face detector.') status = model_evaluator.evaluate(print_result=True) if (not status): print('Error evaluating the model')
def _generate_image_samples(self, annotation_image_dir, annotation_file_name, base_number_of_images, target_root_dir): face_dataset = SimpleFaceDataset() return (face_dataset.generate_samples( annotation_image_dir, annotation_file_name, base_number_of_images, NetworkFactory.network_size(self.network_name()), target_root_dir))
def _generate_landmark_samples(self, landmark_image_dir, landmark_file_name, base_number_of_images, target_root_dir): landmark_dataset = LandmarkDataset() return (landmark_dataset.generate( landmark_image_dir, landmark_file_name, base_number_of_images, NetworkFactory.network_size(self.network_name()), target_root_dir))
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))
def __init__(self, last_network='ONet', model_root_dir=None): if (not model_root_dir): self._model_root_dir = NetworkFactory.model_deploy_dir() else: self._model_root_dir = model_root_dir self._min_face_size = datasets_constants.minimum_face_size self._threshold = [0.9, 0.6, 0.7] #self._scale_factor = 0.79 self._scale_factor = 1.0 / (math.sqrt(2.0)) status_ok = True if (last_network in ['PNet', 'RNet', 'ONet']): self._pnet = NetworkFactory.network('PNet') pnet_model_path = os.path.join(self._model_root_dir, self._pnet.network_name()) status_ok = self._pnet.setup_inference_network( pnet_model_path) and status_ok else: self._pnet = None if (last_network in ['RNet', 'ONet']): self._rnet = NetworkFactory.network('RNet') rnet_model_path = os.path.join(self._model_root_dir, self._rnet.network_name()) status_ok = self._rnet.setup_inference_network( rnet_model_path) and status_ok else: self._rnet = None if (last_network in ['ONet']): self._onet = NetworkFactory.network('ONet') onet_model_path = os.path.join(self._model_root_dir, self._onet.network_name()) status_ok = self._onet.setup_inference_network( onet_model_path) and status_ok else: self._onet = None if (not status_ok): raise SystemExit
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): if (not (args.network_name in ['PNet', 'RNet', 'ONet'])): raise ValueError( 'The network name should be either PNet, RNet or ONet.') if (not args.dataset_root_dir): raise ValueError( 'You must supply the input dataset directory with --dataset_root_dir.' ) if (not args.test_annotation_file): raise ValueError( 'You must supply face dataset annotations file used for evaluating the trained model with --test_annotation_file.' ) if (not args.test_annotation_image_dir): raise ValueError( 'You must supply face dataset image directory used for evaluating the trained model with --test_annotation_image_dir.' ) if (args.network_name == 'PNet'): trainer = SimpleNetworkTrainer(args.network_name) else: trainer = HardNetworkTrainer(args.network_name) status = trainer.load_test_dataset(args.test_dataset, args.test_annotation_image_dir, args.test_annotation_file) if (not status): print( 'Error loading the test dataset for evaluating the trained model.') if (args.train_root_dir): train_root_dir = args.train_root_dir else: train_root_dir = NetworkFactory.model_train_dir() status = trainer.train(args.network_name, args.dataset_root_dir, train_root_dir, args.base_learning_rate, args.max_number_of_epoch, args.log_every_n_steps) if (status): print(args.network_name + ' - network is trained and weights are generated at ' + train_root_dir) else: print('Error training the model.')
def _generate_image_samples(self, annotation_file_name, annotation_image_dir, model_train_dir, base_number_of_images, target_root_dir): status = True hard_face_dataset = HardFaceDataset() status = hard_face_dataset.generate_samples( annotation_image_dir, annotation_file_name, model_train_dir, self.network_name(), self._minimum_face_size, target_root_dir) and status simple_face_dataset = SimpleFaceDataset() status = simple_face_dataset.generate_samples( annotation_image_dir, annotation_file_name, base_number_of_images, NetworkFactory.network_size(self.network_name()), target_root_dir) and status return (status)
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)
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()
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()
def train(self, network_name, dataset_root_dir, train_root_dir, base_learning_rate, max_number_of_epoch, log_every_n_steps): network_train_dir = self.network_train_dir(train_root_dir) if (not os.path.exists(network_train_dir)): os.makedirs(network_train_dir) image_size = self.network_size() image_batch, label_batch, bbox_batch, landmark_batch = self._read_data( dataset_root_dir) class_loss_ratio, bbox_loss_ratio, landmark_loss_ratio = NetworkFactory.loss_ratio( network_name) input_image = tf.placeholder( tf.float32, shape=[self._batch_size, image_size, image_size, 3], name='input_image') target_label = tf.placeholder( tf.float32, shape=[self._batch_size], name='target_label') target_bounding_box = tf.placeholder( tf.float32, shape=[self._batch_size, 4], name='target_bounding_box') target_landmarks = tf.placeholder( tf.float32, shape=[self._batch_size, 10], name='target_landmarks') output_class_probability, output_bounding_box, output_landmarks = self._network.setup_training_network( input_image) class_loss_op = class_loss_ohem(output_class_probability, target_label) bounding_box_loss_op = bounding_box_loss_ohem( output_bounding_box, target_bounding_box, target_label) landmark_loss_op = landmark_loss_ohem(output_landmarks, target_landmarks, target_label) class_accuracy_op = self._calculate_accuracy(output_class_probability, target_label) L2_loss_op = tf.add_n(tf.losses.get_regularization_losses()) total_loss = class_loss_ratio * class_loss_op + bbox_loss_ratio * bounding_box_loss_op + landmark_loss_ratio * landmark_loss_op + L2_loss_op train_op, learning_rate_op = self._train_model(base_learning_rate, total_loss) init = tf.global_variables_initializer() self._session = tf.Session() #saver = tf.train.Saver(save_relative_paths=True, max_to_keep=3) saver = tf.train.Saver( save_relative_paths=True, max_to_keep=0) # All checkpoint files are kept. self._session.run(init) tf.summary.scalar("class_loss", class_loss_op) tf.summary.scalar("bounding_box_loss", bounding_box_loss_op) tf.summary.scalar("landmark_loss", landmark_loss_op) tf.summary.scalar("class_accuracy", class_accuracy_op) summary_op = tf.summary.merge_all() logs_dir = os.path.join(network_train_dir, "logs") if (not os.path.exists(logs_dir)): os.makedirs(logs_dir) summary_writer = tf.summary.FileWriter(logs_dir, self._session.graph) coordinator = tf.train.Coordinator() threads = tf.train.start_queue_runners( sess=self._session, coord=coordinator) current_step = 0 max_number_of_steps = int(self._number_of_samples / self._batch_size + 1) * max_number_of_epoch global_step = 0 epoch = 0 skip_model_saving = False if (self._network.load_model(self._session, network_train_dir)): model_path = self._network.model_path() global_step = tf.train.global_step(self._session, self._global_step) epoch = int( global_step * self._batch_size / self._number_of_samples) skip_model_saving = True print( 'Model is restored from model path - %s with global step - %s.' % (model_path, global_step)) network_train_file_name = os.path.join(network_train_dir, self.network_name()) self._session.graph.finalize() try: for step in range(global_step, max_number_of_steps): current_step = current_step + 1 if coordinator.should_stop(): break image_batch_array, label_batch_array, bbox_batch_array, landmark_batch_array = self._session.run( [image_batch, label_batch, bbox_batch, landmark_batch]) image_batch_array, landmark_batch_array = self._random_flip_images( image_batch_array, label_batch_array, landmark_batch_array) _, _, summary = self._session.run( [train_op, learning_rate_op, summary_op], feed_dict={ input_image: image_batch_array, target_label: label_batch_array, target_bounding_box: bbox_batch_array, target_landmarks: landmark_batch_array }) if ((step + 1) % log_every_n_steps == 0): current_class_loss, current_bbox_loss, current_landmark_loss, current_L2_loss, current_lr, current_accuracy = self._session.run( [ class_loss_op, bounding_box_loss_op, landmark_loss_op, L2_loss_op, learning_rate_op, class_accuracy_op ], feed_dict={ input_image: image_batch_array, target_label: label_batch_array, target_bounding_box: bbox_batch_array, target_landmarks: landmark_batch_array }) print( "(epoch - %d, step - %d) - (accuracy - %3f, class loss - %4f, bbox loss - %4f, landmark loss - %4f, L2 loss - %4f, lr - %f )" % ((epoch + 1), (step + 1), current_accuracy, current_class_loss, current_bbox_loss, current_landmark_loss, current_L2_loss, current_lr)) summary_writer.add_summary( summary, global_step=global_step) if (current_step * self._batch_size >= self._number_of_samples): if (skip_model_saving): skip_model_saving = False else: print("epoch - %d, step - %d" % ((epoch + 1), (step + 1))) saver.save( self._session, network_train_file_name, global_step=self._global_step) self._evaluate(network_name, train_root_dir) epoch = epoch + 1 current_step = 0 except tf.errors.OutOfRangeError: print("Error") finally: coordinator.request_stop() summary_writer.close() coordinator.join(threads) self._session.close() return (True)
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)
def _generate_hard_samples(self, dataset, network_name, detected_boxes, minimum_face_size, target_root_dir): image_file_names = dataset['images'] ground_truth_boxes = dataset['bboxes'] number_of_images = len(image_file_names) if (not (len(detected_boxes) == number_of_images)): return (False) target_face_size = NetworkFactory.network_size(network_name) positive_dir = os.path.join(target_root_dir, 'positive') part_dir = os.path.join(target_root_dir, 'part') negative_dir = os.path.join(target_root_dir, 'negative') if (not os.path.exists(positive_dir)): os.makedirs(positive_dir) if (not os.path.exists(part_dir)): os.makedirs(part_dir) if (not os.path.exists(negative_dir)): os.makedirs(negative_dir) positive_file = open( SimpleFaceDataset.positive_file_name(target_root_dir), 'w') part_file = open(SimpleFaceDataset.part_file_name(target_root_dir), 'w') negative_file = open( SimpleFaceDataset.negative_file_name(target_root_dir), 'w') generated_negative_samples = 0 generated_positive_samples = 0 generated_part_samples = 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) 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) per_image_face_images = 0 for box in detected_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 ((width < minimum_face_size) or (height < minimum_face_size) or (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, ground_truth_box) cropped_image = current_image[y_top:y_bottom + 1, x_left:x_right + 1, :] resized_image = cv2.resize( cropped_image, (target_face_size, target_face_size), interpolation=cv2.INTER_LINEAR) if (np.max(current_IoU) < DatasetFactory.negative_IoU()): continue else: idx = np.argmax(current_IoU) assigned_gt = ground_truth_box[idx] x1, y1, x2, y2 = assigned_gt offset_x1 = (x1 - x_left) / float(width) offset_y1 = (y1 - y_top) / float(height) offset_x2 = (x2 - x_right) / float(width) offset_y2 = (y2 - y_bottom) / float(height) if (np.max(current_IoU) >= DatasetFactory.positive_IoU()): file_path = os.path.join( positive_dir, "hard-positive-%s.jpg" % generated_positive_samples) positive_file.write( file_path + ' 1 %.2f %.2f %.2f %.2f' % (offset_x1, offset_y1, offset_x2, offset_y2) + os.linesep) cv2.imwrite(file_path, resized_image) generated_positive_samples += 1 per_image_face_images += 1 elif (np.max(current_IoU) >= DatasetFactory.part_IoU()): file_path = os.path.join( part_dir, "hard-part-%s.jpg" % generated_part_samples) part_file.write( file_path + ' -1 %.2f %.2f %.2f %.2f' % (offset_x1, offset_y1, offset_x2, offset_y2) + os.linesep) cv2.imwrite(file_path, resized_image) generated_part_samples += 1 per_image_face_images += 1 needed_negative_images = int((1.0 * per_image_face_images * datasets_constants.negative_ratio) / (datasets_constants.positive_ratio + datasets_constants.part_ratio)) needed_negative_images = max(1, needed_negative_images) current_negative_images = 0 for box in detected_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 ((width < minimum_face_size) or (height < minimum_face_size) or (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, ground_truth_box) cropped_image = current_image[y_top:y_bottom + 1, x_left:x_right + 1, :] resized_image = cv2.resize( cropped_image, (target_face_size, target_face_size), interpolation=cv2.INTER_LINEAR) if ((np.max(current_IoU) < DatasetFactory.negative_IoU()) and (current_negative_images < needed_negative_images)): file_path = os.path.join( negative_dir, "hard-negative-%s.jpg" % generated_negative_samples) negative_file.write(file_path + ' 0' + os.linesep) cv2.imwrite(file_path, resized_image) generated_negative_samples += 1 current_negative_images += 1 negative_file.close() part_file.close() positive_file.close() return (True)
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)
def __init__(self, network_name): self._network = NetworkFactory.network(network_name) self._number_of_samples = 0 self._batch_size = 384