def generate_embedding(self, face: Face) -> numpy.ndarray: """ Calculates the embedding for the given face :param face: A face object representing a face :return: The embedding for the given face """ # 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") prewhiten_face = facenet.prewhiten(face.image) # print(type(face.image)) # print(prewhiten_face) # rgb = misc.toimage(prewhiten_face) # cv2.imwrite("original.png", face.image) # cv2.imwrite("whitened.jpg", prewhiten_face) # Run forward pass to calculate embeddings feed_dict = { images_placeholder: [prewhiten_face], phase_train_placeholder: False } return self.sess.run(embeddings, feed_dict=feed_dict)[0]
def main(): faces = list() myfacenet = FACENET_EMBEDDINGS(modeldir) for img in glob.glob('cropped_images' + '/*.jpg'): frame = cv2.resize(cv2.imread(img), (160, 160), interpolation=cv2.INTER_LINEAR) faces.append(facenet.prewhiten(frame)) face_encoding = myfacenet.run(faces)
def load_image(path): image_size = 160 img = misc.imread(path) if img.ndim == 2: img = to_rgb(img) img = prewhiten(img) img = crop(img, False, image_size) img = flip(img, False) return img
def apply(self,image_path): self.load() img = PIL.Image.open(image_path).convert('RGB') img = img.resize((self.image_size,self.image_size)) img = np.array(img) img = facenet.prewhiten(img) images = np.zeros((1, self.image_size, self.image_size, 3)) images[0, :, :, :] = img feed_dict = {self.images_placeholder: images, self.phase_train_placeholder: False} return self.session.run(self.embeddings, feed_dict=feed_dict)
def raw_process(img): if img.ndim == 2: img = to_rgb(img) try: img = prewhiten(img) except: pass img = crop(img, False, 160) img = flip(img, False) return img
def predict_face(img, bounding_boxes=None, margin=44, image_size=(160, 160)): results = [] if bounding_boxes is None: bounding_boxes, _ = align.detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor) # Facenet Prediction nrof_faces = bounding_boxes.shape[0] if nrof_faces == 0: return results img_size = np.asarray(img.shape)[0:2] img_list = [None] * nrof_faces for i in range(nrof_faces): det = np.squeeze(bounding_boxes[i, 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, interp='bilinear') prewhitened = facenet.prewhiten(aligned) img_list[i] = prewhitened images = np.stack(img_list) feed_dict = {images_placeholder: images, phase_train_placeholder: False} emb = sess.run(embeddings, feed_dict=feed_dict) predictions = model.predict_proba(emb) best_class_indices = np.argmax(predictions, axis=1) best_class_probabilities = predictions[np.arange( len(best_class_indices)), best_class_indices] index = 0 for face_position in bounding_boxes: face_position = face_position.astype(int) face_pos = (face_position[0], face_position[1], face_position[2], face_position[3]) result = {} if best_class_probabilities[index] > 0.75: result["accuracy"] = best_class_probabilities[index] result["name"] = class_names[best_class_indices[0]] else: result["accuracy"] = "null" result["name"] = "Unknown"] index += 1 results += [result] return results
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 = [] face_area = [] file_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 for box in bounding_boxes: det = np.squeeze(box[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) face_area.append( Image.open(image).crop((bb[0], bb[1], bb[2], bb[3]))) file_list.append(image) images = np.stack(img_list) return images, face_area, file_list
def _detectSingleFace(path, minsize=50, threshold = [0.6, 0.7, 0.7], factor=0.709,pad=5,image_size=160,preprocsingFunc=None): I=imread(path,mode='RGB') if preprocsingFunc: I=preprocsingFunc(I) box, point=detect_face(I,minsize,pnet_fun,rnet_fun,onet_fun,threshold,factor) if len(box)==0: return None h,w=I.shape[0:2] x1, y1, x2, y2, acc = box[0] x1, y1, x2, y2 = max(x1-pad, 0), max(y1-pad, 0), min(x2+pad, w), min(y2+pad, h) x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) cropped=I[y1:y2,x1:x2] aligned = misc.imresize(cropped, (image_size, image_size), interp='bilinear') prewhitened=facenet.prewhiten(aligned) return prewhitened
def get_face_features(self, face_detections: List[FaceDetection]): prewhitened_face_images = [] for face_det in face_detections: try: face_img = cv2.resize( face_det.get_face_image(), (FACE_PREDICT_IMGSIZE, FACE_PREDICT_IMGSIZE)) prewhitened = facenet.prewhiten(face_img) prewhitened_face_images.append(prewhitened) except: print("error image") if not prewhitened_face_images: return None prewhitened_face_images = np.stack(prewhitened_face_images) face_features = self.fnet(prewhitened_face_images) face_features = np.array( [f[12::14] / np.linalg.norm(f[12::14]) for f in face_features]) for i, face_det in enumerate(face_detections): face_det.set_face_feature(face_features[i]) return face_features
def callback_image(self, img, points): try: cv_image = self.bridge.imgmsg_to_cv2(img, "bgr8") except CvBridgeError as e: rospy.logerr(e) return if not self.is_have_classifier: bounding_boxes, _ = facenet.align.detect_face.detect_face( cv_image, minsize, self.pnet, self.rnet, self.onet, threshold, factor) for (x1, y1, x2, y2, acc) in bounding_boxes: w = x2 - x1 h = y2 - y1 cv2.rectangle(cv_image, (int(x1), int(y1)), (int(x1 + w), int(y1 + h)), (255, 0, 0), 2) else: img_size = np.asarray(cv_image.shape)[0:2] bounding_boxes, _ = facenet.align.detect_face.detect_face( cv_image, minsize, self.pnet, self.rnet, self.onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] if nrof_faces == 0: result_msg = RecognizedResult() self.pub_result.publish(result_msg) try: self.pub_debug_image.publish( self.bridge.cv2_to_imgmsg(cv_image, "bgr8")) except CvBridgeError as e: print(e) return img_list = [None] * nrof_faces aligned = None for i in xrange(nrof_faces): det = np.squeeze(bounding_boxes[i, 0:4]) 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 = cv_image[bb[1]:bb[3], bb[0]:bb[2], :] aligned = misc.imresize(cropped, (160, 160), interp='bilinear') prewhitened = fn.prewhiten(aligned) img_list[i] = prewhitened images = np.stack(img_list) feed_dict = { self.images_placeholder: images, self.phase_train_placeholder: False } emb = self.sess.run(self.embeddings, feed_dict=feed_dict) predictions = self.model.predict_proba(emb) best_class_indeces = np.argmax(predictions, axis=1) best_class_probabilities = predictions[ np.arange(len(best_class_indeces)), best_class_indeces] result_msg = RecognizedResult() index = 0 for face in bounding_boxes: face = face.astype(int) cv2.rectangle(cv_image, (face[0], face[1]), (face[2], face[3]), (0, 255, 0), 2) if best_class_probabilities[index] >= 0.5: cv2.putText(cv_image, self.class_names[best_class_indeces[index]], (face[0], face[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1) result_msg.name.append( self.class_names[best_class_indeces[index]]) else: cv2.putText(cv_image, 'unknown%d' % index, (face[0], face[1]), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1) result_msg.name.append('unknown%d' % index) result_msg.confidence.append(best_class_probabilities[index]) margin = int((face[2] - face[0]) * 0.3) x1 = face[0] + margin x2 = face[2] - margin y1 = face[1] + margin y2 = face[3] - margin req_pt = [] for y in range(y1, y2): for x in range(x1, x2): req_pt.append([x, y]) depth_point = pc2.read_points(points, skip_nans=False, field_names=('x', 'y', 'z'), uvs=req_pt) pp = np.empty((0, 3)) for p in depth_point: if not math.isnan(p[0]): pp = np.append(pp, [[p[0], p[1], p[2]]], axis=0) pos = np.mean(pp, axis=0) ps1 = PointStamped() ps1.header.stamp = rospy.Time.now() ps1.header.frame_id = points.header.frame_id ps1.point.x = pos[0] ps1.point.y = pos[1] ps1.point.z = pos[2] result_msg.position.append(ps1) index += 1 self.pub_result.publish(result_msg) try: self.pub_debug_image.publish( self.bridge.cv2_to_imgmsg(cv_image, "bgr8")) except CvBridgeError as e: print(e)
# Init Network gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) facenet.load_model("facenet/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") # Load Image loaded_img = cv2.imread("images/obama-aligned.jpg") resized_img = cv2.resize(loaded_img, (160, 160), interpolation=cv2.INTER_LINEAR) # Resize to 160x160 images = [] images.append(facenet.prewhiten(resized_img)) images = np.stack(images) # Run on blank # Done because some networks do some setup on the first call... feed_dict = {images_placeholder: images, phase_train_placeholder: False} sess.run(embeddings, feed_dict=feed_dict) # Run Benchmark times_to_run = 1000 time1 = time.clock() for x in range(times_to_run): feed_dict = {images_placeholder: images, phase_train_placeholder: False} sess.run(embeddings, feed_dict=feed_dict)
def RecognizeFace(frames, model=None, class_names=None): 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 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) if model == None or class_names == None: with open(classifier_filename_exp, 'rb') as infile: (model, class_names) = pickle.load(infile) # video_capture = cv2.VideoCapture("akshay_mov.mp4") c = 0 HumanNames = class_names print(HumanNames) 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) total_faces_detected = {} for frame in frames: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 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) # print(best_class_indices) best_class_probabilities = predictions[ np.arange(len(best_class_indices)), best_class_indices] #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) for H_i in HumanNames: # print(H_i) if HumanNames[best_class_indices[ 0]] == H_i and best_class_probabilities >= 0.4: result_names = HumanNames[ best_class_indices[0]] if result_names in total_faces_detected: if predictions[0][best_class_indices[ 0]] > total_faces_detected[ result_names]: total_faces_detected[ result_names] = predictions[ 0][best_class_indices[ 0]] else: total_faces_detected[ result_names] = predictions[0][ best_class_indices[0]] else: print("BHAKKK") if len(total_faces_detected) == 0: return None else: x = sorted(total_faces_detected.items(), key=operator.itemgetter(1)) return [x[len(x) - 1][0]]
def load_image(img): image_size = 160 img = prewhiten(img) return img
def image_array_align_data(image_arr, image_path, pnet, rnet, onet, image_size=160, margin=32, detect_multiple_faces=True): """ 截取人脸的类 :param image_arr: 人脸像素点数组 :param image_path: 拍摄人脸存储路径 :param pnet: caffe模型 :param rnet: caffe模型 :param onet: caffe模型 :param image_size: 图像大小 :param margin: 边缘截取 :param gpu_memory_fraction: 允许的gpu内存大小 :param detect_multiple_faces: 是否可以识别多张脸,默认为False :return: 若成功,返回截取的人脸数组集合如果没有检测到人脸,直接返回一个1*3的0矩阵 """ minsize = MINSIZE # minimum size of face threshold = THRESHOLD # three steps's threshold factor = FACTOR # scale factor img = image_arr bounding_boxes, _ = detect_face(img, minsize, pnet, rnet, onet, threshold, factor) nrof_faces = bounding_boxes.shape[0] nrof_successfully_aligned = 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 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)) images = np.zeros((len(det_arr), image_size, image_size, 3)) 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], :] # 进行图片缩放 cv2.resize(img,(w,h)) scaled = misc.imresize(cropped, (image_size, image_size), interp='bilinear') nrof_successfully_aligned += 1 # 保存检测的头像 filename_base = BASE_DIR + os.sep + 'media' + os.sep + 'face_160' + os.sep + datetime.datetime.now( ).strftime('%Y-%m-%d') if not os.path.exists(filename_base): os.mkdir(filename_base) filename = os.path.basename(image_path) filename_name, file_extension = os.path.splitext(filename) # 多个人脸时,在picname后加_0 _1 _2 依次累加。 output_filename_n = "{}/{}_{}{}".format(filename_base, filename_name, i, file_extension) misc.imsave(output_filename_n, scaled) scaled = prewhiten(scaled) scaled = crop(scaled, False, 160) scaled = flip(scaled, False) images[i] = scaled if nrof_faces > 0: return images else: return np.zeros((1, 3))