def __init__(self): print('[INFO] Initializing networks and loading parameters...') with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6) self.sess = tf.Session(config=tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False)) with self.sess.as_default(): self.pnet, self.rnet, self.onet = detect_face.create_mtcnn( self.sess, './npy') # Load dev variables print('[DEBUG] Loading development environment variables... ', end='') self.modeldir = './pretrained_model/20170511-185253.pb' self.svc_classifier_filename = './my_classifier/SVC.pkl' print('Done!') # Config variables print('[INFO] Initializing config parameters... ', end='') self.minsize = 150 # minimum size of face self.threshold = [0.6, 0.7, 0.7] # three steps's threshold self.factor = 0.709 # scale factor self.margin = 44 self.frame_interval = 3 self.image_size = 182 self.batch_size = 1000 self.input_image_size = 160 print('Done!') # get the face IDs form the db print('[INFO] Loading face Ids from Database...', end='') self.faceIds = sorted(os.listdir('./aligned_img_db/')) print('Done!') # Load the facenet model and weights from file print('[INFO] Loading feature extraction model...') facenet.load_model(self.modeldir) print('Done!') # Store references to layers in their # respective variables. self.images_placeholder = tf.get_default_graph( ).get_tensor_by_name("input:0") self.embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") self.phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") self.embedding_size = self.embeddings.get_shape()[1] # Load the trained SVM Classifier from file classifier_filename_exp = os.path.expanduser( self.svc_classifier_filename) with open(classifier_filename_exp, 'rb') as infile: (self.model, self.class_names) = pickle.load(infile) print('[INFO] Loaded SVC classifier file -> %s' % classifier_filename_exp)
def __init__(self, model_addr): """ Creates graph, session instance and loads the model :param model_addr: Path to model """ self.sess = tf.Session() facenet.load_model(model_addr) # Get input and output tensors self.images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") self.embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") self.phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0") self.embedding_size = self.embeddings.get_shape()[1]
def __init__(self): self.sess = tf.InteractiveSession() self.sess.run(tf.global_variables_initializer()) base_path = getcwd() dirName1 = "models" dirName2 = "facenet_model" model_name = "20180402-114759.pb" model_path = os.path.normpath( "%s/%s/%s/%s" % (base_path, dirName1, dirName2, model_name)) print(model_path) # model_path=r'./models/facenet_model/20180402-114759.pb' # Load the models facenet.load_model(model_path) # Get input and output tensors self.images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") self.tf_embeddings = tf.get_default_graph().get_tensor_by_name( "embeddings:0") self.phase_train_placeholder = tf.get_default_graph( ).get_tensor_by_name("phase_train:0")
def main(): global colours, img_size args = parse_args() video_dir = args.video_dir folder_containing_frame = args.folder_containing_frame final_output_name_frame_bounding_box = args.final_output_name_frame_bounding_box obid_mapping_classnames_file = args.obid_mapping_classnames_file data = pd.read_csv(obid_mapping_classnames_file, sep=".", names=['classname', 'obid']) obid_classname_count = data.groupby(['obid', 'classname' ]).size().reset_index(name='counts') obid_max_classname = obid_classname_count.loc[ obid_classname_count.reset_index().groupby(['obid' ])['counts'].idxmax()] dic_obid_max_classname = pd.Series( obid_max_classname.classname.values, index=obid_max_classname.obid).to_dict() dict_obid_classname = { int(k): str(v) for k, v in dic_obid_max_classname.items() } #output_path = args.output_path classifier_filename = args.classifer_path classifier_filename_exp = os.path.expanduser(classifier_filename) with open(classifier_filename_exp, 'rb') as f: (model, class_names) = pickle.load(f) print("Loaded classifier file") #classifier_path = "classifier\\knn_classifier_n1.pkl" #with open(classifier_path, 'rb') as f: #(model, class_names) = pickle.load(f) #print("Loaded classifier file") #output_path = "data\\output_label" output_path = args.output_path #fourcc = cv2.VideoWriter_fourcc(*'X264') # for disp colours = np.random.rand(32, 3) # init tracker tracker = Sort() # create instance of the SORT tracker print('Start track and extract......') with tf.Graph().as_default(): with tf.Session( config=tf.ConfigProto(gpu_options=tf.GPUOptions( allow_growth=True), log_device_placement=False)) as sess: project_dir = os.path.dirname(os.path.abspath(__file__)) pnet, rnet, onet = detect_face.create_mtcnn( sess, os.path.join(project_dir, "align")) #facenet_model_path ="model\\20180402-114759.pb" facenet_model_path = args.model_path facenet.load_model(facenet_model_path) 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] margin = 40 # if the face is big in your video ,you can set it bigger for tracking easiler minsize = 50 # minimum size of face for mtcnn to detect threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor input_image_size = 160 frame_interval = 3 # interval how many frames to make a detection,you need to keep a balance between performance and fluency scale_rate = 0.9 # if set it smaller will make input frames smaller show_rate = 0.8 # if set it smaller will dispaly smaller frames for filename in os.listdir(video_dir): suffix = filename.split('.')[1] if suffix != 'mp4' and suffix != 'avi': # you can specify more video formats if you need continue video_name = os.path.join(video_dir, filename) directoryname = os.path.join(output_path, filename.split('.')[0]) cam = cv2.VideoCapture(video_name) #width = cam.get(cv2.CAP_PROP_FRAME_WIDTH) # float #height = cam.get(cv2.CAP_PROP_FRAME_HEIGHT) # float #video_recording = cv2.VideoWriter('output.avi', fourcc, 10, (int(width), int(height))) c = 0 while True: final_faces = [] addtional_attribute_list = [] ret, frame = cam.read() if not ret: print("ret false") break if frame is None: print("frame drop") break frame = cv2.resize(frame, (0, 0), fx=scale_rate, fy=scale_rate) r_g_b_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) if c % frame_interval == 0: img_size = np.asarray(frame.shape)[0:2] faces, points = detect_face.detect_face( r_g_b_frame, minsize, pnet, rnet, onet, threshold, factor) face_sums = faces.shape[0] if face_sums > 0: face_list = [] for i, item in enumerate(faces): f = round(faces[i, 4], 6) if f > 0.99: det = np.squeeze(faces[i, 0:4]) face_list.append(item) # face cropped bb = np.array(det, dtype=np.int32) frame_copy = frame.copy() cropped = frame_copy[bb[1]:bb[3], bb[0]:bb[2], :] # use 5 face landmarks to judge the face is front or side squeeze_points = np.squeeze(points[:, i]) tolist = squeeze_points.tolist() facial_landmarks = [] for j in range(5): item = [tolist[j], tolist[(j + 5)]] facial_landmarks.append(item) dist_rate, high_ratio_variance, width_rate = judge_side_face( np.array(facial_landmarks)) # face addtional attribute(index 0:face score; index 1:0 represents front face and 1 for side face ) item_list = [ cropped, faces[i, 4], dist_rate, high_ratio_variance, width_rate ] addtional_attribute_list.append(item_list) final_faces = np.array(face_list) trackers = tracker.update(final_faces, img_size, directoryname, addtional_attribute_list, r_g_b_frame) #with open(r'all_tracker_saved_non_negative.txt', 'a+') as f: #f.write(" ".join(map(str, trackers)) + "\n") c += 1 for d in trackers: d = d.astype(np.int32) if all(i >= 0 for i in d): trackers_cropped = frame[d[1]:d[3], d[0]:d[2], :] try: scaled = cv2.resize( trackers_cropped, (input_image_size, input_image_size), interpolation=cv2.INTER_CUBIC) except Exception as e: print('the broken image') #scaled = cv2.resize(trackers_cropped, (input_image_size, input_image_size), interpolation=cv2.INTER_CUBIC) scaled = facenet.prewhiten(scaled) scaled_reshape = scaled.reshape( -1, input_image_size, input_image_size, 3) feed_dict = { images_placeholder: scaled_reshape, phase_train_placeholder: False } emb_array = sess.run(embeddings, feed_dict=feed_dict) 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] best_name = class_names[best_class_indices[0]] if best_class_probabilities > 0.7: try: cv2.rectangle(frame, (d[0], d[1]), (d[2], d[3]), colours[d[4] % 32, :] * 255, 3) cv2.putText(frame, dict_obid_classname[d[4]], (d[0] - 10, d[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.75, colours[d[4] % 32, :] * 255, 2) with open( final_output_name_frame_bounding_box, 'a+') as f: f.write(" ".join(map(str, d)) + '.' + dict_obid_classname[d[4]] + '.' + str(c) + "\n") frame_number = 'frame' + str(c) + '.jpg' name = os.path.join( folder_containing_frame, frame_number) cv2.imwrite(name, frame) print('successfully') except Exception as e: print('not existence')
import utils.facenet as facenet import utils.detect_face as detect_face from sklearn.svm import SVC with tf.Graph().as_default(): with tf.Session() as sess: datadir = './aligned_img_db/' dataset = facenet.get_dataset(datadir) paths, labels = facenet.get_image_paths_and_labels(dataset) print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the Facenet model and weights from file print('Loading feature extraction model...') modeldir = './pretrained_model/20170511-185253.pb' facenet.load_model(modeldir) 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 a forward pass to calculate the image # vector embeddings. print('Calculating image features...') batch_size = 500 # 1000 image_size = 160 nrof_images = len(paths) nrof_batches_per_epoch = int(math.ceil(1.0 * nrof_images / batch_size))
def main(): args = parse_args() align_command = 'python align_dataset_mtcnn.py ' + args.input_dir + args.align_dir + ' --image_size 182' + ' --margin 44' os.system(align_command) print("-------- Alignment Completed ----------") with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(666) datadir = args.align_dir embeddingdir = "data/new_person_embedding/" modeldir = args.model_path dataset = facenet.get_dataset(datadir) paths, labels = facenet.get_image_paths_and_labels(dataset) print(labels) # # Create a list of class names #class_names = [cls.name.replace('_', ' ') for cls in dataset] #label_name = [class_names[i] for i in labels] print('Number of classes: {}'.format(len(dataset))) print('Number of images: {}'.format(len(paths))) print('Loading feature extraction model') facenet.load_model(modeldir) 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] print(embedding_size) # Run forward pass to calculate embeddings print('Calculating features for images') batch_size = 200 image_size = 160 nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): print('{}/{}'.format(i, nrof_batches_per_epoch)) start_index = i * batch_size end_index = min((i + 1) * batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) # store embedding and labels np.savetxt(embeddingdir + 'embedding.csv', emb_array, delimiter=",") with open(embeddingdir + 'label.csv', 'w') as f: writer = csv.writer(f) writer.writerows(zip(labels, paths)) # merge 2 embedding files merge_embedding_files("data/embedding/", embeddingdir, "embedding.csv") merge_label_files("data/embedding/", embeddingdir, "label.csv") # re-train the classifier start = time.time() fname = "data/embedding/label.csv" labels = pd.read_csv(fname, header=None).as_matrix()[:, 1] labels = map(itemgetter(1), map(os.path.split, map(os.path.dirname, labels))) # Get the directory. print(labels) print('list labels') labels = list(labels) print(labels) fname = "data/embedding/embedding.csv" embeddings = pd.read_csv(fname, header=None).as_matrix() le = LabelEncoder().fit(labels) class_names = list(le.classes_) class_names = [i.replace("_", " ") for i in class_names] labelsNum = le.transform(labels) #print(labelsNum) print(class_names) print(labelsNum) classifier_filename_exp = os.path.expanduser( args.classifier_filename) print('Start training classifier') if (args.classifier == 'SVM'): model = SVC(kernel='linear', probability=True) elif (args.classifier == 'KNN'): model = KNeighborsClassifier(n_neighbors=1) elif (args.classifier == 'LinearSVC'): model = LinearSVC(random_state=0, tol=1e-5) else: model = RandomForestClassifier(n_estimators=2000, max_depth=100, max_features='sqrt', n_jobs=-1) model.fit(embeddings, labelsNum) print( "Re-train the classifier took {} seconds.".format(time.time() - start)) print('End training classifier') print(le) # saving classifier model with open(classifier_filename_exp, 'wb') as outfile: pickle.dump((model, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) print('Goodluck')
def main(args): minsize = 20 threshold = [0.6, 0.7, 0.7] factor = 0.709 image_size = 182 input_image_size = 160 # comment out these lines if you do not want video recording # USE FOR RECORDING VIDEO fourcc = cv2.VideoWriter_fourcc(*'X264') # Get the path of the classifier and load it project_root_folder = os.path.join(os.path.abspath(__file__), "..\\..") classifier_path = "classifier\\knn_classifier_n1.pkl" print (classifier_path) with open(classifier_path, 'rb') as f: (model, class_names) = pickle.load(f) print("Loaded classifier file") with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): # Bounding box pnet, rnet, onet = align.detect_face.create_mtcnn(sess, project_root_folder + "\\align") # Get the path of the facenet model and load it facenet_model_path ="model\\20180402-114759.pb" facenet.load_model(facenet_model_path) 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] # Start video capture people_detected = set() person_detected = collections.Counter() if args.webcam is True: video_capture = cv2.VideoCapture(0) else: video_path = "video\\" video_name = "graham_norton" full_original_video_path_name = video_path + video_name + '.mp4' video_capture_path = full_original_video_path_name if not os.path.isfile(full_original_video_path_name): print('Video not found at path ' + full_original_video_path_name + '. Commencing download from YouTube') # Note if the video ever gets removed this will cause issues YouTube(args.youtube_video_url).streams.first().download(output_path =video_path, filename=video_name) #yt = YouTube(args.youtube_video_url) #stream = yt.streams.first()() #finished = stream.download(output_path =video_path, filename=video_name) #finished = stream.download() #sys.exit() video_capture = cv2.VideoCapture(full_original_video_path_name) width = video_capture.get(cv2.CAP_PROP_FRAME_WIDTH) # float height = video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT) # float video_recording = cv2.VideoWriter('output.avi', fourcc, 10, (int(width), int(height))) total_frames_passed = 0 while True: try: ret, frame = video_capture.read() except Exception as e: break # Skip frames if video is to be speed up if args.video_speedup: total_frames_passed += 1 if total_frames_passed % args.video_speedup != 0: continue bounding_boxes, _ = align.detect_face.detect_face(frame, minsize, pnet, rnet, onet, threshold, factor) faces_found = bounding_boxes.shape[0] if faces_found > 0: det = bounding_boxes[:, 0:4] bb = np.zeros((faces_found, 4), dtype=np.int32) for i in range(faces_found): 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 inner of range!') continue cropped = frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :] scaled = cv2.resize(cropped, (input_image_size, input_image_size), interpolation=cv2.INTER_CUBIC) # cv2.imshow("Cropped and scaled", scaled) # cv2.waitKey(1) scaled = facenet.prewhiten(scaled) # cv2.imshow("\"Whitened\"", scaled) # cv2.waitKey(1) scaled_reshape = scaled.reshape(-1, input_image_size, input_image_size, 3) feed_dict = {images_placeholder: scaled_reshape, phase_train_placeholder: False} emb_array = sess.run(embeddings, feed_dict=feed_dict) 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] best_name = class_names[best_class_indices[0]] print("Name: {}, Probability: {}".format(best_name, best_class_probabilities)) if best_class_probabilities > 0.09: cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0), 2) #boxing face text_x = bb[i][0] text_y = bb[i][3] + 20 cv2.putText(frame, class_names[best_class_indices[0]], (text_x, text_y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), thickness=1, lineType=2) person_detected[best_name] += 1 # total_frames_passed += 1 # if total_frames_passed == 2: for person, count in person_detected.items(): if count > 4: print("Person Detected: {}, Count: {}".format(person, count)) people_detected.add(person) # person_detected.clear() # total_frames_passed = 0 cv2.putText(frame, "People detected so far:", (20, 20), cv2.FONT_HERSHEY_PLAIN, 1, (255, 0, 0), thickness=1, lineType=2) currentYIndex = 40 for idx, name in enumerate(people_detected): cv2.putText(frame, name, (20, currentYIndex + 20 * idx), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255), thickness=1, lineType=2) cv2.imshow("Face Detection and Identification", frame) video_recording.write(frame) if cv2.waitKey(1) & 0xFF == ord('q'): break video_recording.release() video_capture.release() cv2.destroyAllWindows()
def main(): args = parse_args() with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(666) datadir = args.align_dir modeldir = args.model_path dataset = facenet.get_dataset(datadir) #f_map = "data/embedding/maps.csv" paths, labels = facenet.get_image_paths_and_labels(dataset) label1 = [] for i in paths: person_name = i.split('/')[-2] label1.append(person_name) #map_test = pd.read_csv(f_map, header=None) #labelnum = [] #for i in label1: #ID = int(map_test.loc[map_test[1]==i, 0].values) #labelnum.append(ID) label1 = [i.replace("_", " ") for i in label1] print('Number of classes: {}'.format(len(dataset))) print('Number of images: {}'.format(len(paths))) print('Loading feature extraction model') facenet.load_model(modeldir) 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') batch_size = 200 image_size = 160 nrof_images = len(paths) nrof_batches_per_epoch = int( math.ceil(1.0 * nrof_images / batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): print('{}/{}'.format(i, nrof_batches_per_epoch)) start_index = i * batch_size end_index = min((i + 1) * batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) print('Testing classifier') classifier_filename_exp = os.path.expanduser( args.classifier_filename) with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) labelnum = [] for i in label1: num = class_names.index(i) labelnum.append(num) print('Loaded classifier model from file "%s"' % classifier_filename_exp) print(class_names) predictions = model.predict(emb_array) print(predictions) best_class_indices = predictions #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' % (i, class_names[best_class_indices[i]], best_class_probabilities[i])) print(best_class_indices) print('labelnum') print(labelnum) report = precision_recall_fscore_support(labelnum, best_class_indices, average='weighted') print(report[2])
def main(args): with tf.Graph().as_default(): with tf.Session() as sess: np.random.seed(seed=args.seed) embeddingdir = "data/embedding/" if args.use_split_dataset: dataset_tmp = facenet.get_dataset(args.data_dir) train_set, test_set = split_dataset( dataset_tmp, args.min_nrof_images_per_class, args.nrof_train_images_per_class) if (args.mode == 'TRAIN'): dataset = train_set elif (args.mode == 'CLASSIFY'): dataset = test_set else: dataset = facenet.get_dataset(args.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 = facenet.get_image_paths_and_labels(dataset) # Create a new label list containing names instead of numbers class_names = [cls.name.replace('_', ' ') for cls in dataset] label_name = [class_names[i] for i in labels] print('Number of classes: %d' % len(dataset)) print('Number of images: %d' % len(paths)) # Load the model print('Loading feature extraction model') facenet.load_model(args.model) # 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] print(embedding_size) # 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 / args.batch_size)) emb_array = np.zeros((nrof_images, embedding_size)) for i in range(nrof_batches_per_epoch): start_index = i * args.batch_size end_index = min((i + 1) * args.batch_size, nrof_images) paths_batch = paths[start_index:end_index] images = facenet.load_data(paths_batch, False, False, args.image_size) feed_dict = { images_placeholder: images, phase_train_placeholder: False } emb_array[start_index:end_index, :] = sess.run( embeddings, feed_dict=feed_dict) # Store embedding and labels np.savetxt(embeddingdir + 'embedding.csv', emb_array, delimiter=",") with open(embeddingdir + 'label.csv', 'w') as f: writer = csv.writer(f) writer.writerows(zip(labels, paths)) classifier_filename_exp = os.path.expanduser( args.classifier_filename) if (args.mode == 'TRAIN'): # Train classifier print('Training classifier') if (args.classifier == 'SVM'): model = SVC(kernel='linear', probability=True) elif (args.classifier == 'KNN'): model = KNeighborsClassifier(n_neighbors=1) elif (args.classifier == 'Softmax'): model = LogisticRegression(random_state=0, solver='lbfgs', multi_class='multinomial') else: model = RandomForestClassifier(n_estimators=1000, max_leaf_nodes=100, n_jobs=-1) 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: pickle.dump((model, class_names), outfile) print('Saved classifier model to file "%s"' % classifier_filename_exp) elif (args.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' % (i, class_names[best_class_indices[i]], best_class_probabilities[i])) accuracy = np.mean(np.equal(best_class_indices, labels)) report = precision_recall_fscore_support(labels, best_class_indices, average='weighted') print(report[2]) print('Accuracy: %.3f' % accuracy)
def f(x): return 0.0078125 * (x - 128) output_data_new = map(f, output_data[0]) #embeddings_after = output_data_new/np.linalg.norm(output_data_new) print("convert after:") print(output_data_new) print(np.linalg.norm(output_data_new)) with tf.Graph().as_default(): config = tf.ConfigProto() config.gpu_options.allow_growth = True sess = tf.Session(config=config) with sess.as_default(): facenet.load_model( "train_results/freeze_models/tflite_model/inception_v3/inception_v3_eval_model.pb" ) images_placeholder = tf.get_default_graph().get_tensor_by_name( "input:0") embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0") images = np.zeros((1, 160, 160, 3)) images[0, :, :, :] = image_w embeddings_ = sess.run(embeddings, feed_dict={images_placeholder: images}) print("converted before:") print(embeddings_[0]) print(np.linalg.norm(embeddings_[0])) similar = np.sum(output_data_new * embeddings_[0]) print("similar: %1.3f" % similar)
def main(): global colours, img_size args = parse_args() video_dir = args.video_dir output_path = args.output_path all_trackers_saved = args.all_trackers_saved obid_mapping_classnames = args.obid_mapping_classnames #classifier_path = "classifier\\knn_classifier_n1.pkl" classifier_filename = args.classifer_path classifier_filename_exp = os.path.expanduser(classifier_filename) with open(classifier_filename_exp, 'rb') as f: (model, class_names) = pickle.load(f) print("Loaded classifier file") #with open(classifier_path, 'rb') as f: #(model, class_names) = pickle.load(f) #print("Loaded classifier file") #output_path = "data\\output_label" # for disp # init tracker tracker = Sort() # create instance of the SORT tracker print('Start track and extract......') with tf.Graph().as_default(): with tf.Session( config=tf.ConfigProto(gpu_options=tf.GPUOptions( allow_growth=True), log_device_placement=False)) as sess: project_dir = os.path.dirname(os.path.abspath(__file__)) pnet, rnet, onet = detect_face.create_mtcnn( sess, os.path.join(project_dir, "align")) facenet_model_path = args.model_path #facenet_model_path ="model\\20180402-114759.pb" facenet.load_model(facenet_model_path) 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] margin = 40 # if the face is big in your video ,you can set it bigger for tracking easiler minsize = 50 # minimum size of face for mtcnn to detect threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor input_image_size = 160 frame_interval = 1 # interval how many frames to make a detection,you need to keep a balance between performance and fluency scale_rate = 0.9 # if set it smaller will make input frames smaller show_rate = 0.8 # if set it smaller will dispaly smaller frames for filename in os.listdir(video_dir): suffix = filename.split('.')[1] if suffix != 'mp4' and suffix != 'avi': # you can specify more video formats if you need continue video_name = os.path.join(video_dir, filename) directoryname = os.path.join(output_path, filename.split('.')[0]) cam = cv2.VideoCapture(video_name) c = 0 while True: final_faces = [] addtional_attribute_list = [] ret, frame = cam.read() if not ret: print("ret false") break if frame is None: print("frame drop") break frame = cv2.resize(frame, (0, 0), fx=scale_rate, fy=scale_rate) r_g_b_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) if c % frame_interval == 0: img_size = np.asarray(frame.shape)[0:2] faces, points = detect_face.detect_face( r_g_b_frame, minsize, pnet, rnet, onet, threshold, factor) face_sums = faces.shape[0] if face_sums > 0: face_list = [] for i, item in enumerate(faces): f = round(faces[i, 4], 6) if f > 0.99: det = np.squeeze(faces[i, 0:4]) face_list.append(item) # face cropped bb = np.array(det, dtype=np.int32) frame_copy = frame.copy() cropped = frame_copy[bb[1]:bb[3], bb[0]:bb[2], :] # use 5 face landmarks to judge the face is front or side squeeze_points = np.squeeze(points[:, i]) tolist = squeeze_points.tolist() facial_landmarks = [] for j in range(5): item = [tolist[j], tolist[(j + 5)]] facial_landmarks.append(item) dist_rate, high_ratio_variance, width_rate = judge_side_face( np.array(facial_landmarks)) # face addtional attribute(index 0:face score; index 1:0 represents front face and 1 for side face ) item_list = [ cropped, faces[i, 4], dist_rate, high_ratio_variance, width_rate ] addtional_attribute_list.append(item_list) final_faces = np.array(face_list) trackers = tracker.update(final_faces, img_size, directoryname, addtional_attribute_list, r_g_b_frame) #with open(r'all_tracker_saved_non_negative.txt', 'a+') as f: #f.write(" ".join(map(str, trackers)) + "\n") c += 1 for d in trackers: obid_map_classname = [] print(d) d = [int(i) for i in d] print(d) if all(i >= 0 for i in d): with open(all_trackers_saved, 'a+') as f: f.write(" ".join(map(str, d)) + '.' + str(c) + "\n") trackers_cropped = frame[d[1]:d[3], d[0]:d[2], :] try: scaled = cv2.resize( trackers_cropped, (input_image_size, input_image_size), interpolation=cv2.INTER_CUBIC) except Exception as e: print('the broken image') #scaled = cv2.resize(trackers_cropped, (input_image_size, input_image_size), interpolation=cv2.INTER_CUBIC) scaled = facenet.prewhiten(scaled) scaled_reshape = scaled.reshape( -1, input_image_size, input_image_size, 3) feed_dict = { images_placeholder: scaled_reshape, phase_train_placeholder: False } emb_array = sess.run(embeddings, feed_dict=feed_dict) 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] best_name = class_names[best_class_indices[0]] if best_class_probabilities > 0.09: with open(obid_mapping_classnames, 'a+') as f: f.write(best_name + '.' + str(d[4]) + "\n") else: with open(obid_mapping_classnames, 'a+') as f: f.write('error tracker' + str(c) + "\n")