def main(data_dir, output_dir, resize_scale): output_dir = output_dir + '\\' + '{0}'.format(resize_scale) dataset = face_net.get_dataset(data_dir) for cls in dataset: output_class_dir = os.path.join(output_dir, cls.name) if not os.path.exists(output_class_dir): os.makedirs(output_class_dir) for image_path in cls.image_paths: filename = os.path.splitext(os.path.split(image_path)[1])[0] fileex = os.path.splitext(os.path.split(image_path)[1])[1] output_filename = os.path.join(output_class_dir, filename + '.' + fileex) img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED) scale_percent = resize_scale # percent of original size width = int(img.shape[1] * scale_percent / 100) height = int(img.shape[0] * scale_percent / 100) dim = (width, height) # resize image resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA) cv2.imwrite(output_filename, resized) print('The output is ' + output_dir) print("Finish!!!!")
def main(data_dir, output_dir): dataset = face_net.get_dataset(data_dir) for cls in dataset: for i in range(len(cls.image_paths)): image_path = cls.image_paths[i] filename = os.path.splitext(os.path.split(image_path)[1])[0] fileex = os.path.splitext(os.path.split(image_path)[1])[1] person_name = filename.split('_')[0] output_class_dir = os.path.join(output_dir, person_name) if not os.path.exists(output_class_dir): os.makedirs(output_class_dir) output_filename = os.path.join(output_class_dir, filename + '.' + fileex) img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED) cv2.imwrite(output_filename, img) print('The output is ' + output_dir) print("Finish!!!!")
def main(data_dir, output_dir): dataset = face_net.get_dataset(data_dir) for cls in dataset: for i in range(len(cls.image_paths)): image_path = cls.image_paths[i] filename = os.path.split(image_path)[1] # fileex = os.path.splitext(os.path.split(image_path)[1])[1] print(filename) # # filenamearr = [] = filename.split('_') # filename = '{0}_{1}_{2}_{3}_{4}'.format(filenamearr[]) # # person_name = filename.split('_')[0] # output_class_dir = os.path.join(output_dir, person_name) # if not os.path.exists(output_class_dir): # os.makedirs(output_class_dir) # output_filename = os.path.join(output_class_dir, filename + '.' + fileex) # img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED) # cv2.imwrite(output_filename, img) print('The output is ' + output_dir) print("Finish!!!!")
def main(data_dir, min_size): with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.0) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, npy) minsize = min_size # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor dataset = face_net.get_dataset(data_dir) number_sample = 0 number_face_detected = 0 for cls in dataset: for image_path in cls.image_paths: number_sample = number_sample + 1 img = cv2.imread(image_path, cv2.IMREAD_UNCHANGED) if img.ndim == 2: img = face_net.to_rgb(img) img = img[:, :, 0:3] bounding_boxes, _ = detect_face.detect_face( img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] if (nrof_faces == 0): print('{0} face detected : {1}'.format( nrof_faces, image_path)) elif (nrof_faces == 2): print('{0} face detected : {1}'.format( nrof_faces, image_path)) number_face_detected = number_face_detected + 1 else: number_face_detected = number_face_detected + 1 print("Finish!!!!") print('Number face detected {0}'.format(number_face_detected))
def main(data_dir, output_dir): sleep(random.random()) if not os.path.exists(output_dir): os.makedirs(output_dir) # Store some git revision info in a text file in the log directory src_path, _ = os.path.split(os.path.realpath(__file__)) # facenet.store_revision_info(src_path, output_dir, ' '.join('argument default')) dataset = face_net.get_dataset(data_dir) print('Creating networks and loading parameters') with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=constants.GPU_MEMORY_FRACTION_DEFAULT) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, None) minsize = constants.FACE_REG_MINSIZE # minimum size of face threshold = constants.ALIGN_THRESHOLD # three steps's threshold factor = constants.ALIGN_FACTOR # scale factor # Add a random key to the filename to allow alignment using multiple processes random_key = np.random.randint(0, high=99999) face_detected_list = [] nrof_images_total = 0 nrof_successfully_aligned = 0 if constants.ALIGN_RANDOM_ORDER: random.shuffle(dataset) for cls in dataset: output_class_dir = os.path.join(output_dir, cls.name) if not os.path.exists(output_class_dir): os.makedirs(output_class_dir) if constants.ALIGN_RANDOM_ORDER: random.shuffle(cls.image_paths) for image_path in cls.image_paths: nrof_images_total += 1 filename = os.path.splitext(os.path.split(image_path)[1])[0] output_filename = os.path.join(output_class_dir, filename + '.png') if not os.path.exists(output_filename): try: img = misc.imread(image_path) except (IOError, ValueError, IndexError) as e: error_message = '{}: {}'.format(image_path, e) print(error_message) else: if img.ndim < 2: print('Unable to align "%s"' % image_path) face_detected_list.append('Unable to align {0}'.format(image_path)) # text_file.write('%s\n' % (output_filename)) continue if img.ndim == 2: img = face_net.to_rgb(img) img = img[:, :, 0:3] bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] if nrof_faces > 0: det = bounding_boxes[:, 0:4] det_arr = [] img_size = np.asarray(img.shape)[0:2] if nrof_faces > 1: if constants.ALIGN_DETECT_MULTIPLE_FACES: for i in range(nrof_faces): det_arr.append(np.squeeze(det[i])) else: bounding_box_size = (det[:, 2] - det[:, 0]) * (det[:, 3] - det[:, 1]) img_center = img_size / 2 offsets = np.vstack([(det[:, 0] + det[:, 2]) / 2 - img_center[1], (det[:, 1] + det[:, 3]) / 2 - img_center[0]]) offset_dist_squared = np.sum(np.power(offsets, 2.0), 0) index = np.argmax(bounding_box_size - offset_dist_squared * 2.0) # some extra weight on the centering det_arr.append(det[index, :]) else: det_arr.append(np.squeeze(det)) for i, det in enumerate(det_arr): det = np.squeeze(det) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - constants.COMPARE_MARGIN_DEFAULT / 2, 0) bb[1] = np.maximum(det[1] - constants.COMPARE_MARGIN_DEFAULT / 2, 0) bb[2] = np.minimum(det[2] + constants.COMPARE_MARGIN_DEFAULT / 2, img_size[1]) bb[3] = np.minimum(det[3] + constants.COMPARE_MARGIN_DEFAULT / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = misc.imresize(cropped, (constants.ALIGN_IMAGE_SIZE, constants.ALIGN_IMAGE_SIZE), interp='bilinear') nrof_successfully_aligned += 1 filename_base, file_extension = os.path.splitext(output_filename) if constants.ALIGN_DETECT_MULTIPLE_FACES: output_filename_n = "{}_{}{}".format(filename_base, i, file_extension) else: output_filename_n = "{}{}".format(filename_base, file_extension) misc.imsave(output_filename_n, scaled) face_detected_list.append('%s --- BOX [%d , %d , %d , %d]\n' % (output_filename_n, bb[0], bb[1], bb[2], bb[3])) else: face_detected_list.append('Unable to align {0}'.format(image_path)) print('Total number of images: %d' % nrof_images_total) print('Number of successfully aligned images: %d' % nrof_successfully_aligned) return nrof_images_total, nrof_successfully_aligned, face_detected_list
def main(data_dir, model_dir, output_dir): if constants.CLASSIFIER_MODE == 'TRAIN': classifier_filename_exp = os.path.expanduser(output_dir + '/classifier_{0}.pkl'.format(datetime.strftime(datetime.now(), '%Y%m%d%H%M%S'))) elif constants.CLASSIFIER_MODE == 'CLASSIFY': classifier_filename_exp = os.path.expanduser(output_dir + '/classifier_test.pkl') with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(seed=constants.CLASSIFIER_SEED) if constants.CLASSIFIER_USE_SPLIT_DATASET: dataset_tmp = face_net.get_dataset(data_dir) train_set, test_set = split_dataset(dataset_tmp, constants.CLASSIFIER_MIN_NROF_IMAGES_PER_CLASS, constants.CLASSIFIER_NROF_TRAIN_IMAGES_PER_CLASS) if constants.CLASSIFIER_MODE == 'TRAIN': dataset = train_set elif constants.CLASSIFIER_MODE == 'CLASSIFY': dataset = test_set else: dataset = face_net.get_dataset(constants.CLASSIFIER_DATA_DIR) # Check that there are at least one training image per class for cls in dataset: assert len(cls.image_paths) > 0, 'There must be at least one image for each class in the dataset' paths, labels = face_net.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the model print('Loading feature extraction model') face_net.load_model(model_dir) # Get input and output tensors images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0") embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] # Run forward pass to calculate embeddings print('Calculating features for images') nrof_images = len(paths) nrof_batches_per_epoch = int(math.ceil(1.0 * nrof_images / constants.CLASSIFIER_BATCH_SIZE)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * constants.CLASSIFIER_BATCH_SIZE end_index = min((i + 1) * constants.CLASSIFIER_BATCH_SIZE, nrof_images) paths_batch = paths[start_index:end_index] images = face_net.load_data(paths_batch, False, False, constants.IMAGE_SIZE) feed_dict = {images_placeholder: images, phase_train_placeholder: False} emb_array[start_index:end_index, :] = sess.run(embeddings, feed_dict=feed_dict) if constants.CLASSIFIER_MODE == 'TRAIN': # Train classifier print('Training classifier') model = SVC(kernel='linear', probability=True) model.fit(emb_array, labels) # Create a list of class names class_names = [cls.name.replace('_', ' ') for cls in dataset] # Saving classifier model with open(classifier_filename_exp, 'wb') as outfile: # Serialize & De-Serialize object pickle.dump((model, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) elif constants.CLASSIFIER_MODE == 'CLASSIFY': # Classify images print('Testing classifier') with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"' % classifier_filename_exp) predictions = model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[np.arange(len(best_class_indices)), best_class_indices] for i in range(len(best_class_indices)): print('%4d %s: %.3f - %s' % (i, class_names[best_class_indices[i]], best_class_probabilities[i], paths[i])) accuracy = np.mean(np.equal(best_class_indices, labels)) print('Accuracy: %.3f' % accuracy) return classifier_filename_exp
def main(test_dir, data_dir, model_dir, classifier_file): with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=constants. GPU_MEMORY_FRACTION_DEFAULT) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, None) minsize = constants.FACE_REG_MINSIZE # minimum size of face threshold = constants.ALIGN_THRESHOLD # three steps's threshold factor = constants.ALIGN_FACTOR # scale factor image_size = 160 input_image_size = 160 human_names = os.listdir(data_dir) human_names.sort() print('Loading feature extraction model') face_net.load_model(model_dir) images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") embedding_size = embeddings.get_shape()[1] classifier_filename_exp = os.path.expanduser(classifier_file) with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) c = 0 print('Start Recognition!') dataset = face_net.get_dataset(test_dir) number_of_face_recognition = 0 for cls in dataset: for image_path in cls.image_paths: frame = cv2.imread(image_path, 0) if frame.ndim == 2: frame = face_net.to_rgb(frame) frame = frame[:, :, 0:3] bounding_boxes, _ = detect_face.detect_face( frame, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] if nrof_faces > 0: det = bounding_boxes[:, 0:4] cropped = [] scaled = [] scaled_reshape = [] bb = np.zeros((nrof_faces, 4), dtype=np.int32) for i in range(nrof_faces): emb_array = np.zeros((1, embedding_size)) bb[i][0] = det[i][0] bb[i][1] = det[i][1] bb[i][2] = det[i][2] bb[i][3] = det[i][3] # inner exception if bb[i][0] <= 0 or bb[i][1] <= 0 or bb[i][ 2] >= len( frame[0]) or bb[i][3] >= len(frame): print( 'Face is too close {0}'.format(image_path)) break cropped.append(frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :]) cropped[i] = face_net.flip(cropped[i], False) scaled.append( misc.imresize(cropped[i], (image_size, image_size), interp='bilinear')) scaled[i] = cv2.resize( scaled[i], (input_image_size, input_image_size), interpolation=cv2.INTER_CUBIC) scaled[i] = face_net.prewhiten(scaled[i]) scaled_reshape.append(scaled[i].reshape( -1, input_image_size, input_image_size, 3)) feed_dict = { images_placeholder: scaled_reshape[i], phase_train_placeholder: False } emb_array[0, :] = sess.run(embeddings, feed_dict=feed_dict) predictions = model.predict_proba(emb_array) best_class_indices = np.argmax(predictions, axis=1) # print(best_class_indices) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] # plot result idx under box for H_i in human_names: # print(H_i) if human_names[best_class_indices[0]] == H_i \ and H_i in image_path: print('{0} : {1}'.format( best_class_probabilities, image_path)) number_of_face_recognition = number_of_face_recognition + 1 else: print('Unable to recognition {0}'.format(image_path)) print("Finish!!!!") print('Number face detected {0}'.format(number_of_face_recognition))