def load_and_align_data(image_paths, image_size, margin, gpu_memory_fraction): minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor print('Creating networks and loading parameters') with tf.Graph().as_default(): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=gpu_memory_fraction) # noqa: E501 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) nrof_samples = len(image_paths) img_list = [None] * nrof_samples for i in xrange(nrof_samples): print(image_paths[i]) img = imageio.imread(os.path.expanduser(image_paths[i])) img_size = np.asarray(img.shape)[0:2] bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) det = np.squeeze(bounding_boxes[0, 0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] aligned = resize(cropped, (image_size, image_size)) prewhitened = facenet.prewhiten(aligned) img_list[i] = prewhitened images = np.stack(img_list) return images
def __setup_mtcnn(self): with tf.Graph().as_default(): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=self.gpu_memory_fraction) sess = tf.Session(config=tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): return detect_face.create_mtcnn(sess, None)
def __init__(self): self.graph = tf.Graph() with self.graph.as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): self.pnet, self.rnet, self.onet = FaceDet.create_mtcnn(sess, None)
def run(self): minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor margin = 44 image_size = 160 gpu_memory_fraction = 1.0 with tf.Graph().as_default(): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=gpu_memory_fraction) sess = tf.Session(config=tf.ConfigProto( gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): p_net, r_net, o_net = detect_face.create_mtcnn(sess, None) while True: img = self.inq.get() img_size = np.asarray(img.shape)[0:2] bounding_boxes, _ = detect_face.detect_face( img, minsize, p_net, r_net, o_net, threshold, factor) src = img.copy() dist_white_ends = [] for num in range(bounding_boxes.shape[0]): det = np.squeeze(bounding_boxes[num, 0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] if (bb[0] >= 0) & (bb[0] < src.shape[1]): src[bb[1]:bb[3], bb[0], :] = 255 else: src[bb[1]:bb[3], src.shape[1] - 1, :] = 255 if (bb[2] >= 0) & (bb[2] < src.shape[1]): src[bb[1]:bb[3], bb[2], :] = 255 else: src[bb[1]:bb[3], src.shape[1] - 1, :] = 255 if (bb[1] >= 0) & (bb[1] < src.shape[0]): src[bb[1], bb[0]:bb[2], :] = 255 else: src[src.shape[0] - 1, bb[0]:bb[2], :] = 255 if (bb[3] >= 0) & (bb[3] < src.shape[0]): src[bb[3], bb[0]:bb[2], :] = 255 else: src[src.shape[0] - 1, bb[0]:bb[2], :] = 255 pil_im = Image.fromarray(cropped) aligned = pil_im.resize((image_size, image_size), Image.BILINEAR) aligned = np.array(aligned) pre_whitened = facenet.prewhiten(aligned) dist_white_ends.append(pre_whitened) self.out_q.put({"src": src, "dst": dist_white_ends})
def create_network_face_detection(gpu_memory_fraction): with tf.Graph().as_default(): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=gpu_memory_fraction) 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) return pnet, rnet, onet
def __init__(self, modelpath="facenet/src/align", minsize=20, threshold=(0.6, 0.7, 0.7), factor=0.709): self.minsize = minsize self.threshold = threshold self.factor = factor self.sess = tf.Session() self.funs = detect_face.create_mtcnn(self.sess, "facenet/src/align")
def __init__(self, path, optimize, minfacesize): import tensorflow as tf # lazy loading import facenet.src.align.detect_face as facenet # lazy loading self._optimize = optimize self._minfacesize = minfacesize with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.3) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): self._pnet, self._rnet, self._onet = facenet.create_mtcnn(sess, None)
def __init__(self): with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.85) 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 = df.create_mtcnn( self.sess, None) self.minsize = 20 # minimum size of face self.threshold = [0.6, 0.7, 0.7] # three steps's threshold self.factor = 0.709 # scale factor
def getBoundingBoxes(img, minsize, threshold, factor): with tf.Graph().as_default(): sess = tf.Session() with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, None) bounding_boxes, points = detect_face.detect_face( img, minsize, pnet, rnet, onet, threshold, factor) for (x1, y1, x2, y2, acc) in bounding_boxes: rectangle = cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2) return rectangle, bounding_boxes
def align(image_paths, image_size=160, margin=32, gpu_memory_fraction=1.0): minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor # print('Creating networks and loading parameters') with tf.Graph().as_default(): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=gpu_memory_fraction) 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) aligned_indices = [] aligned_images = [] #aligned_images = [None] * len(image_paths) # aligned_image_paths = [] for i, image_path in enumerate(image_paths): # print('%1d: %s' % (i, image_path)) try: img = misc.imread(str(image_path)) img = img[:, :, 0:3] # apply for 32bit image img_size = np.asarray(img.shape)[0:2] bounding_boxes, _ = detect_face.detect_face( img, minsize, pnet, rnet, onet, threshold, factor) if len(bounding_boxes) == 0: print('No bounding boxes: {}'.format(image_path)) continue det = np.squeeze(bounding_boxes[0, 0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] aligned = misc.imresize(cropped, (image_size, image_size), interp='bilinear') # prewhitened = facenet.prewhiten(aligned) # do in the FaceNetModel aligned_indices.append(i) aligned_images.append(aligned) #img_list[i] = prewhitened # aligned_image_paths.append(image_path) except: print('Cannot align: {}'.format(image_path)) if 0 < len(aligned_images): aligned_images = np.stack(aligned_images) return aligned_images, aligned_indices
def process(self): bounding_boxes_filename = os.path.join( self.out_text_dir, 'bounding_boxes_{}.txt'.format(self.task_index)) result_filename = os.path.join(self.out_result_dir, 'result_{}.txt'.format(self.task_index)) gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=self.gpu_memory_fraction) config = tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False) with tf.Session(self.server.target, config=config) as sess: K.set_session(sess) self.pnet, self.rnet, self.onet = detect_face.create_mtcnn( sess, None) with tf.io.gfile.GFile(bounding_boxes_filename, "w") as text_file: with tf.io.gfile.GFile(result_filename, 'w') as result_file: for class_name, image_paths in self.generate_rdd_data(): self.process_data(class_name, image_paths, text_file, result_file)
def detect_faces(image_paths, image_size=160, margin=44): minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor gpu_memory_fraction = 0.5 print('Creating networks and loading parameters') with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = facenet_detect_face.create_mtcnn(sess, None) if isinstance(image_paths, str): image_paths = [image_paths] result_list = [] tmp_image_paths = copy.copy(image_paths) for image in tmp_image_paths: img = imageio.imread(os.path.expanduser(image), pilmode='RGB') bounding_boxes, points = facenet_detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) if len(bounding_boxes) < 1: print("can't detect face, remove ", image) source_img = Image.open(os.path.expanduser(image)) for i in range(bounding_boxes.shape[0]): draw = ImageDraw.Draw(source_img) draw.rectangle(bounding_boxes[i,0:4].tolist(), outline="lime") font_location = bounding_boxes[i,0:2] - np.array([0, 30]) confidence = "{:.6f}".format(bounding_boxes[i,4] * 100) draw.text(font_location, str(confidence) + "%", fill="white", font=ImageFont.truetype("arial", 20)) for j in range(5): point_x = points[j,i] point_y = points[j+5,i] r = 2 draw.ellipse((point_x-r, point_y-r, point_x+r, point_y+r), fill="lime") source_img.save(os.path.splitext(os.path.expanduser(image))[0] + "_result.jpg", "JPEG") return result_list
def __init__(self, model): print("model path:{0}".format(model)) self.gpu_memory_fraction = 0.4 self.minsize = 20 # 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.image_size = 160 self.compare_threshold = 0.99 print('Creating networks and loading parameters') gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=self.gpu_memory_fraction, allow_growth = True) self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) self.pnet, self.rnet, self.onet = detect_face.create_mtcnn(self.sess, None) # Load the model facenet.load_model(model) # 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")
def load_and_align_data(image_paths, image_size, margin, gpu_memory_fraction): minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor print("Creating networks and loading parameters") with tf.Graph().as_default(): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=gpu_memory_fraction) 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) tmp_image_paths = copy.copy(image_paths) img_list = [] for image in tmp_image_paths: img = misc.imread(os.path.expanduser(image), mode="RGB") img_size = np.asarray(img.shape)[0:2] bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) if len(bounding_boxes) < 1: image_paths.remove(image) print("can't detect face, remove ", image) continue det = np.squeeze(bounding_boxes[0, 0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] aligned = misc.imresize(cropped, (image_size, image_size), interp="bilinear") prewhitened = facenet.prewhiten(aligned) img_list.append(prewhitened) images = np.stack(img_list) return images
def detect_mtcnn(path): ''' Face detection using facenet & tensorflow package ''' print('Creating networks and loading parameters') with tf.Graph().as_default(): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=gpu_memory_fraction) 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) img = misc.imread(path) bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) num_faces = bounding_boxes.shape[0] print('////////////{} faces founded////////////'.format(nrof_faces)) print(bounding_boxes) crop_faces = [] for face_position in bounding_boxes: face_position = face_position.astype(int) print(face_position[0:4]) cv2.rectangle(img, (face_position[0], face_position[1]), (face_position[2], face_position[3]), (0, 255, 0), 2) crop = img[face_position[1]:face_position[3], face_position[0]:face_position[2], ] crop = cv2.resize(crop, (96, 96), interpolation=cv2.INTER_CUBIC) print(crop.shape) crop_faces.append(crop) plt.imshow(crop) plt.show() plt.imshow(img) plt.show()
import tensorflow as tf import os.path from tensorflow.python.platform import gfile #Supress warning about tensorflow not compiled for current CPU os.environ['TF_CPP_MIN_LOG_LEVEL']='2' THRESHOLD = [0.7, 0.8, 0.8] MINSIZE=50 FACTOR = 0.709 cap = cv2.VideoCapture(0) sess = tf.Session() pnet_fun, rnet_fun, onet_fun = detect_face.create_mtcnn(sess, model_path=None) while(True): # Capture frame-by-frame ret, frame = cap.read() # # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses) rgb_frame = frame[:, :, ::-1] # Find all the faces and face enqcodings in the frame of video total_boxes, points = detect_face.detect_face(rgb_frame, minsize=MINSIZE, pnet=pnet_fun, rnet=rnet_fun, onet=onet_fun, threshold=THRESHOLD, factor=FACTOR) #face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
# fetch images image_dir = '_tests/' # create a list of your images images = os.listdir(image_dir) # Start code from facenet/src/compare.py print('Creating networks and loading parameters') with tf.Graph().as_default(): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=gpu_memory_fraction) 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) # end code from facenet/src/compare.py for i in images: img = misc.imread(os.path.expanduser(image_dir + i)) # run detect_face from the facenet library bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) print(bounding_boxes) # for each box for (x1, y1, x2, y2, acc) in bounding_boxes: w = x2 - x1 h = y2 - y1 # plot the box using cv2 cv2.rectangle(img, (int(x1), int(y1)), (int(x1 + w), int(y1 + h)),
@app.route("/predict") def predict_page(): """Renders the 'predict.html' page for manual image file uploads for prediction.""" return render_template("predict.html") if __name__ == '__main__': """Server and FaceNet Tensorflow configuration.""" # Load FaceNet model and configure placeholders for forward pass into the FaceNet model to calculate embeddings model_path = 'model/20170512-110547/20170512-110547.pb' facenet_model = load_model(model_path) config = tf.ConfigProto() config.gpu_options.allow_growth = True image_size = 160 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") # Initiate persistent FaceNet model in memory facenet_persistent_session = tf.Session(graph=facenet_model, config=config) # Create Multi-Task Cascading Convolutional (MTCNN) neural networks for Face Detection pnet, rnet, onet = detect_face.create_mtcnn( sess=facenet_persistent_session, model_path=None) # Start flask application on waitress WSGI server serve(app=app, host='0.0.0.0', port=5000)
import tensorflow as tf from facenet.src import facenet from facenet.src.align import detect_face fileDir = os.path.dirname(os.path.realpath(__file__)) facenetDir = os.path.join(fileDir, 'facenet') facenetModelDir = os.path.join( facenetDir, 'src', 'align', ) session = None graph = None # Actual models used for face detection pnet = None rnet = None onet = None graph = tf.Graph() session = tf.Session( graph=graph ) #config=tf.ConfigProto(inter_op_parallelism_threads=24, intra_op_parallelism_threads=24)) with graph.as_default(): with session.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(session, facenetModelDir) graph.finalize()
def _face_detection_nets(self): with tf.Graph().as_default(): sess = tf.Session() return _FaceDetectionNets(*detect_face.create_mtcnn(sess, None))
def CreateFoldersByIdentity(self, folderPath, OutputFolderPath): with tf.Graph().as_default(): with tf.Session() as sess: with open('Network/locations.p', 'rb') as f: self.ArrayOfTimeAndLocations = pickle.load(f) if not os.path.exists(OutputFolderPath): os.makedirs(OutputFolderPath) # Load MTCNN alignment model self.pnet, self.rnet, self.onet = detect_face.create_mtcnn( sess, None) # Load the FaceNet model print('Loading FaceNet model') protobuf_file = 'Network/201804_model/20180402-114759.pb' facenet.load_model(protobuf_file) # 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.control_placeholder = tf.placeholder(tf.int32, shape=(None, 1), name='control') directoryWithPictures = os.listdir(folderPath) known_encodings = [] maximum = 0 for item in directoryWithPictures: number = item.split('_')[0] if maximum < int(number): maximum = int(number) nOfFacesInEachFrame = np.zeros((maximum + 1), dtype=int) for item in directoryWithPictures: number = item.split('_')[0] nOfFacesInEachFrame[int(number)] += 1 foundFirstFace = False print('Identify people in video') for frameNumber in range(len(nOfFacesInEachFrame)): try: if frameNumber % 100 == 0: print('Frame number', frameNumber) if nOfFacesInEachFrame[frameNumber] > 0: if foundFirstFace: distancesAndIndexes = [] for face_number in range( nOfFacesInEachFrame[frameNumber]): imgPath = folderPath + '/' + str( frameNumber) + '_' + str( face_number) + '.png' img_encoding = self.ReadDetectAndEncode( imgPath, sess) if len(img_encoding) == 0: continue if True: distances = [] for known_encoding in known_encodings: dist = facenet.distance( [img_encoding], [known_encoding], distance_metric=0) distances.append(dist) index = np.argmin(distances) distancesAndIndexes.append( (distances[index], index, face_number)) distancesAndIndexes.sort() cannotBeThem = [] for cnt, item in enumerate( distancesAndIndexes): imgPath = folderPath + '/' + str( frameNumber) + '_' + str( item[2]) + '.png' if item[1] not in cannotBeThem and item[ 0] < 0.8: os.rename( imgPath, OutputFolderPath + '/' + str(item[1]) + '/' + str(frameNumber) + '.png') for face in self.ArrayOfTimeAndLocations[ frameNumber]: if face != []: face[0] = str(item[1]) cannotBeThem.append(item[1]) else: img_encoding = self.ReadDetectAndEncode( imgPath, sess, n_jitters=100) if len(img_encoding) == 0: continue # ------------------------------------------------------------------- known_encodings.append(img_encoding) if not os.path.exists( OutputFolderPath + '/' + str(len(known_encodings) - 1)): os.makedirs( OutputFolderPath + '/' + str(len(known_encodings) - 1)) os.rename( imgPath, OutputFolderPath + '/' + str(len(known_encodings) - 1) + '/' + str(frameNumber) + '.png') for face in self.ArrayOfTimeAndLocations[ frameNumber]: if face != []: face[0] = str( len(known_encodings) - 1) else: foundFace = False temporary = 0 for face_number in range( nOfFacesInEachFrame[frameNumber]): imgPath = folderPath + '/' + str( frameNumber) + '_' + str( face_number) + '.png' img_encoding = self.ReadDetectAndEncode( imgPath, sess, n_jitters=100) if len(img_encoding) == 0: temporary += 1 continue known_encodings.append(img_encoding) if not os.path.exists( OutputFolderPath + '/' + str(face_number - temporary)): os.makedirs(OutputFolderPath + '/' + str(face_number - temporary)) # foundFace = True os.rename( imgPath, OutputFolderPath + '/' + str(face_number - temporary) + '/' + str(frameNumber) + '.png') for face in self.ArrayOfTimeAndLocations[ frameNumber]: if face != []: face[0] = str(face_number - temporary) if foundFace: foundFirstFace = True except Exception as ex: print(ex) self.LastPhase('Identities', sess) if os.path.exists(folderPath): shutil.rmtree(folderPath, ignore_errors=False, onerror=None) with open('Network/locations.p', 'wb') as f: pickle.dump(self.ArrayOfTimeAndLocations, f)
def ConvertUseTensorflowMtcnn(in_queue, msg_q): margin = 32 index_num = 0 target_dir = os.path.join(img_dir, "images_data_%d" % target_img_size) output_user_dir = os.path.join(target_dir, name) if not os.path.exists(output_user_dir): os.makedirs(output_user_dir) minsize = 30 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.25) 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) while 1: file_name = in_queue.get() print("====== file: %s" % file_name) img = cv2.imread(file_name) bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) print(bounding_boxes) nrof_faces = bounding_boxes.shape[0] print(nrof_faces) if nrof_faces > 0: det = bounding_boxes[:, 0:4] det_arr = [] img_size = np.asarray(img.shape)[0:2] print("====nrof_face: %d img size:" % nrof_faces) print(img_size) if nrof_faces == 1: 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, :]) for i, det in enumerate(det_arr): det = np.squeeze(det) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = misc.imresize(cropped, (target_img_size, target_img_size), interp='bilinear') output_filename = os.path.join(output_user_dir, ("%d" % index_num) + '.png') filename_base, file_extension = os.path.splitext( output_filename) output_filename_n = "{}{}".format(filename_base, file_extension) misc.imsave(output_filename_n, scaled) index_num += 1 #if os.path.exists(file_name): #os.remove(file_name) if index_num >= 5: msg_q.put("complate_msg") msg_q.put("complate_msg") msg_q.put("complate_msg") break
def main(args): sleep(random.random()) output_dir = os.path.expanduser(args.output_dir) 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(sys.argv)) dataset = facenet.get_dataset(args.input_dir) print('Creating networks and loading parameters') with tf.Graph().as_default(): gpu_options = tf.GPUOptions( per_process_gpu_memory_fraction=args.gpu_memory_fraction) 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 = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor # Add a random key to the filename to allow alignment using multiple processes random_key = np.random.randint(0, high=99999) bounding_boxes_filename = os.path.join( output_dir, 'bounding_boxes_%05d.txt' % random_key) with open(bounding_boxes_filename, "w") as text_file: nrof_images_total = 0 nrof_successfully_aligned = 0 if args.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 args.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') print(image_path) if not os.path.exists(output_filename): try: img = misc.imread(image_path) except (IOError, ValueError, IndexError) as e: errorMessage = '{}: {}'.format(image_path, e) print(errorMessage) else: if img.ndim < 2: print('Unable to align "%s"' % image_path) text_file.write('%s\n' % (output_filename)) continue if img.ndim == 2: img = facenet.to_rgb(img) img = img[:, :, 0:3] bounding_boxes, _ = align.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 args.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] - args.margin / 2, 0) bb[1] = np.maximum(det[1] - args.margin / 2, 0) bb[2] = np.minimum(det[2] + args.margin / 2, img_size[1]) bb[3] = np.minimum(det[3] + args.margin / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = misc.imresize( cropped, (args.image_size, args.image_size), interp='bilinear') nrof_successfully_aligned += 1 filename_base, file_extension = os.path.splitext( output_filename) if args.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) text_file.write('%s %d %d %d %d\n' % (output_filename_n, bb[0], bb[1], bb[2], bb[3])) else: print('Unable to align "%s"' % image_path) text_file.write('%s\n' % (output_filename)) print('Total number of images: %d' % nrof_images_total) print('Number of successfully aligned images: %d' % nrof_successfully_aligned)
def _setup_mtcnn(self): with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=gpu_memory_fraction) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): return align_detect_face.create_mtcnn(sess, None)
def main(): MINSIZE = 20 THRESHOLD = [0.6, 0.7, 0.7] FACTOR = 0.709 IMAGE_SIZE = 182 INPUT_IMAGE_SIZE = 160 CLASSIFIER_PATH = 'facenet/Models/Own/Own.pkl' VIDEO_PATH = args.path FACENET_MODEL_PATH = 'facenet/Models/facenet/20180402-114759.pb' # Load The Custom Classifier with open(CLASSIFIER_PATH, 'rb') as file: model, class_names = pickle.load(file) print("Custom Classifier, Successfully loaded") 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(): # Load the model print('Loading feature extraction model') facenet.load_model(FACENET_MODEL_PATH) # 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] pnet, rnet, onet = detect_face.create_mtcnn( sess, "facenet/src/align") people_detected = set() person_detected = collections.Counter() from Config import webcam, feed webcam = webcam() webcam.init("Microsoft® LifeCam HD-3000 - 1") webcam.set_callback() webcam.set_callback_properties() webcam.grabber_cb.grab_sample() webcam.run() images_queue = queue.Queue() lock = Lock() thread1 = feed(lock, webcam.queue, images_queue) count = 0 detector = MTCNN() while True: if images_queue.empty() == True: print("empty images queue") continue img = images_queue.get(0) print("heeererea") result = detector.detect_faces(img) try: if result != []: for person in result: bounding_box = person['box'] keypoints = person['keypoints'] print(person['confidence']) print(result) x1 = bounding_box[0] y1 = bounding_box[1] x2 = bounding_box[0] + bounding_box[2] y2 = bounding_box[1] + bounding_box[3] cropped = img[y1:y2, x1:x2] scaled = cv2.resize( 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]] print("Name: {}, Probability: {}".format( best_name, best_class_probabilities)) cv2.rectangle(img, (x1, y1), (x2, y2), (0, 155, 255), 2) text_x = x1 text_y = y2 + 20 if best_class_probabilities > 0.45: name = class_names[best_class_indices[0]] else: name = "Unknown" cv2.putText(img, name, (text_x, text_y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), thickness=1, lineType=2) cv2.putText(img, str( round(best_class_probabilities[0], 3)), (text_x, text_y + 17), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), thickness=1, lineType=2) person_detected[best_name] += 1 except: pass cv2.imshow('Face Recognition', img) if cv2.waitKey(1) & 0xFF == ord('x'): break cv2.destroyAllWindows()
def face_recog(): if request.method == "POST": file = request.files["image"] filename = secure_filename(file.filename) names = [] img_name = str(filename) img_path = "attendance/facenet/dataset/test-images/" + img_name modeldir = "attendance/facenet/src/20180402-114759/" classifier_filename = "attendance/facenet/src/20180402-114759/my_classifier.pkl" npy = "" train_img = "attendance/facenet/dataset/raw" workbook = xlsxwriter.Workbook( 'C:\\Users\\Dell\\Attendance\\Reports\\Report_for_' + datetime.datetime.now().strftime("%Y_%m_%d-%H") + '.xlsx') worksheet = workbook.add_worksheet() conn = sqlite3.connect('C:\\Users\\Dell\\Attendance\\attendance\\site.db') c = conn.cursor() students = c.execute("SELECT stuname FROM 'add'") 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(): pnet, rnet, onet = detect_face.create_mtcnn(sess, npy) minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor margin = 32 frame_interval = 3 batch_size = 1000 image_size = 160 input_image_size = 160 HumanNames = os.listdir(train_img) HumanNames.sort() 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] classifier_filename_exp = os.path.expanduser(classifier_filename) with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) # video_capture = cv2.VideoCapture("akshay_mov.mp4") c = 0 print('Start Recognition!') prevTime = 0 # ret, frame = video_capture.read() frame = cv2.imread(img_path, 0) #frame = cv2.resize(frame, (0,0), fx=0.5, fy=0.5) #resize frame (optional) curTime = time.time() + 1 # calc fps timeF = frame_interval if (c % timeF == 0): find_results = [] if frame.ndim == 2: frame = facenet.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] print('Face Detected: %d' % nrof_faces) if nrof_faces > 0: det = bounding_boxes[:, 0:4] img_size = np.asarray(frame.shape)[0:2] 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') break cropped.append(frame[bb[i][1]:bb[i][3], bb[i][0]:bb[i][2], :]) cropped[i] = facenet.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] = facenet.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) #print(predictions) best_class_indices = np.argmax(predictions, axis=1) # no print(best_class_indices) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] #print(best_class_probabilities) cv2.rectangle(frame, (bb[i][0], bb[i][1]), (bb[i][2], bb[i][3]), (0, 255, 0), 2) #boxing face #plot result idx under box text_x = bb[i][0] text_y = bb[i][3] + 20 #print('Result Indices: ', best_class_indices[0]) print(HumanNames[best_class_indices[0]]) names.append(HumanNames[best_class_indices[0]]) for H_i in HumanNames: if HumanNames[best_class_indices[ 0]] == H_i and best_class_probabilities > 0.43: result_names = HumanNames[best_class_indices[0]] cv2.putText(frame, result_names, (text_x, text_y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), thickness=1, lineType=1) else: print('Unable to align') for i, row in enumerate(students): for j, value in enumerate(row): worksheet.write_string(i, j + 2, 'Absent') for name in names: if name == value: worksheet.write_string(i, j + 2, 'Present') worksheet.write_string(i, j, str(value)) # reg_no = c.execute("SELECT regno FROM 'add'") # for i, row in enumerate(reg_no): # for j, value in enumerate(row): # worksheet.write(i,j+1,value) cv2.imshow('Image', frame) cv2.imwrite('output/' + img_path.split('/')[-1], frame) if cv2.waitKey(9000) & 0xFF == ord('q'): sys.exit("Thanks") workbook.close() cv2.destroyAllWindows() flash('The students faces were recognized successfully!', 'success') return render_template('take.html', title="Take Attendance")
minsize = 60 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor margin = 44 image_size = 160 detect_model_dir = '/home/liubo-it/FaceRecognization/facenet/data' recognize_model_dir = '/home/liubo-it/FaceRecognization/facenet/models/casia_facenet/20170208-100636/valid' # 由于检测模型和识别模型都比较大,所以分开测试(实际使用时, 放在不同的gpu上) detect_graph = tf.Graph() with detect_graph.as_default() as detect_graph: gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.5) detect_session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with detect_session.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(detect_session, detect_model_dir) def align_face(pic_path): if os.path.exists(pic_path): try: img = misc.imread(pic_path) except (IOError, ValueError, IndexError) as e: errorMessage = '{}: {}'.format(pic_path, e) print(errorMessage) if img.ndim < 2: print('Unable to align "%s"' % pic_path) return if img.ndim == 2: img = facenet.to_rgb(img)
bottomline = np.bounding_box[0] for faceimg in face4: plt.imshow(faceimg.image) plt.show() # ## 3. Loading Pretrained Face Detection Network # In[6]: tf.reset_default_graph() sess = tf.Session() # In[7]: pnet, rnet, onet = df.create_mtcnn(sess, det_path) # In[ ]: # Not sure how to set these parameters threshold = [0.5, 0.5, 0.3] factor = 0.79 minsize = 10 boxes, points = df.detect_face(test_img, minsize, pnet, rnet, onet, threshold, factor) # In[21]: print(boxes) print(points)
import os import sys import tensorflow as tf from facenet.src import facenet from facenet.src.align import detect_face fileDir = os.path.dirname(os.path.realpath(__file__)) facenetDir = os.path.join(fileDir, 'facenet') facenetModelDir = os.path.join(facenetDir, 'src', 'align',) session = None graph = None # Actual models used for face detection pnet = None rnet = None onet = None graph = tf.Graph() session = tf.Session(graph=graph) #config=tf.ConfigProto(inter_op_parallelism_threads=24, intra_op_parallelism_threads=24)) with graph.as_default(): with session.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(session, facenetModelDir) graph.finalize()
def gen(camera): sess = tf.Session() with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, None) facenet.load_model( '/home/rohitner/models/facenet/20180402-114759/20180402-114759.pb') 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") classifier_filename_exp = '/home/rohitner/models/lfw_classifier.pkl' with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) print('Loaded classifier model from file "%s"' % classifier_filename_exp) minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor file_index = 0 while True: success, img = camera.read() results = tfnet.return_predict(img) for result in results: cv2.rectangle( img, (result["topleft"]["x"], result["topleft"]["y"]), (result["bottomright"]["x"], result["bottomright"]["y"]), (255, 0, 0), 4) text_x, text_y = result["topleft"]["x"] - 10, result[ "topleft"]["y"] - 10 cv2.putText(img, result["label"], (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2, cv2.LINE_AA) if img.ndim < 2: print('Unable to align') continue if img.ndim == 2: img = facenet.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 True: # args.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] - 44 / 2, 0) bb[1] = np.maximum(det[1] - 44 / 2, 0) bb[2] = np.minimum(det[2] + 44 / 2, img_size[1]) bb[3] = np.minimum(det[3] + 44 / 2, img_size[0]) cropped = img[bb[1]:bb[3], bb[0]:bb[2], :] scaled = misc.imresize(cropped, (160, 160), interp='bilinear') scaled = prewhiten_and_expand(scaled) emb = sess.run(embeddings, feed_dict={ images_placeholder: scaled, phase_train_placeholder: False }) predictions = model.predict_proba(emb) best_class_indices = np.argmax(predictions) best_class_probabilities = predictions[0, best_class_indices] font = cv2.FONT_HERSHEY_SIMPLEX cv2.rectangle(img, (bb[0], bb[1]), (bb[2], bb[3]), (0, 255, 0), 5) cv2.putText(img, class_names[best_class_indices], (bb[0], bb[1] - 10), font, 0.5, (255, 0, 0), 2, cv2.LINE_AA) else: print('No face detected') ret, jpeg = cv2.imencode('.jpg', img) frame = jpeg.tobytes() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
from camera.VideoStream import VideoStream from scipy import misc import tensorflow as tf logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__name__) cam = VideoStream(use_pi_camera=False, resolution=(1920, 1080), src=0).start() with tf.Graph().as_default(): gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1.0) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options, log_device_placement=False)) with sess.as_default(): pnet, rnet, onet = mtcnn.create_mtcnn(sess, None) minsize = 20 # minimum size of face threshold = [0.6, 0.7, 0.7] # three steps's threshold factor = 0.709 # scale factor def look_for_faces(): while True: img = cam.read() total_boxes, points = mtcnn.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) faces = [] logger.info(f'Found {len(total_boxes)} faces') for idx, (bounding_box,
def build(self): with self._graph.as_default(): self._pnet, self._rnet, self._onet = detect_face.create_mtcnn( self._sess, None)
def face_verification(img_pairs_list): model = r'facenet\src\align' model_facenet = './20170512-110547.pb' # mtcnn相关参数 minsize = 40 threshold = [0.4, 0.5, 0.6] # pnet、rnet、onet三个网络输出人脸的阈值,大于阈值则保留,小于阈值则丢弃 factor = 0.709 # scale factor # 创建mtcnn网络 with tf.Graph().as_default(): sess = tf.compat.v1.Session() with sess.as_default(): pnet, rnet, onet = detect_face.create_mtcnn(sess, model) margin = 44 image_size = 160 with tf.Graph().as_default(): with tf.compat.v1.Session() as sess: # 根据模型文件载入模型 facenet.load_model(model_facenet) # 得到输入、输出等张量 images_placeholder = tf.compat.v1.get_default_graph().get_tensor_by_name("input:0") embeddings = tf.compat.v1.get_default_graph().get_tensor_by_name("embeddings:0") phase_train_placeholder = tf.compat.v1.get_default_graph().get_tensor_by_name("phase_train:0") # 设置可视化进度条相关参数 jd = '\r %2d%%\t [%s%s]' bar_num_total = 50 total_num = len(img_pairs_list) result, dist = [], [] for i in range(len(img_pairs_list)): # 画进度条 if i % (total_num / bar_num_total) == 0 or i == total_num - 1: bar_num_alright = round(bar_num_total * i / total_num) alright = '#' * bar_num_alright not_alright = '□' * (bar_num_total - bar_num_alright) percent = (bar_num_alright / bar_num_total) * 100 print(jd % (percent, alright, not_alright), end='') # 读取一对人脸图像 img_pairs = img_pairs_list[i] img_list = [] img1 = cv2.imread(img_pairs[0]) img2 = cv2.imread(img_pairs[1]) img_size1 = np.asarray(img1.shape)[0:2] img_size2 = np.asarray(img2.shape)[0:2] # 检测该对图像中的人脸 bounding_box1, _1 = detect_face.detect_face(img1, minsize, pnet, rnet, onet, threshold, factor) bounding_box2, _2 = detect_face.detect_face(img2, minsize, pnet, rnet, onet, threshold, factor) # 未检测到人脸,则将结果标为-1,后续计算准确率时排除 if len(bounding_box1) < 1 or len(bounding_box2) < 1: result.append(-1) dist.append(-1) continue # 将图片1加入img_list det = np.squeeze(bounding_box1[0, 0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size1[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size1[0]) cropped = img1[bb[1]:bb[3], bb[0]:bb[2], :] aligned = cv2.resize(cropped, (image_size, image_size)) prewhitened = facenet.prewhiten(aligned) img_list.append(prewhitened) # 将图片2加入img_list det = np.squeeze(bounding_box2[0, 0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, img_size2[1]) bb[3] = np.minimum(det[3] + margin / 2, img_size2[0]) cropped = img2[bb[1]:bb[3], bb[0]:bb[2], :] aligned = cv2.resize(cropped, (image_size, image_size)) prewhitened = facenet.prewhiten(aligned) img_list.append(prewhitened) images = np.stack(img_list) # 将两个人脸转化为512维的向量 feed_dict = {images_placeholder: images, phase_train_placeholder: False} emb = sess.run(embeddings, feed_dict=feed_dict) # 计算两个人脸向量的距离 ed = np.sqrt(np.sum(np.square(np.subtract(emb[0], emb[1])))) dist.append(ed) # 根据得出的人脸间的距离,判断是否属于同一个人 if ed <= 1.1: result.append(1) else: result.append(0) return result, dist