def face_recognition_image(model_path, dataset_path, filename, image_path): # 加载数据库的数据 dataset_emb, names_list = load_dataset(dataset_path, filename) # 初始化mtcnn人脸检测 face_detect = face_recognition.FaceDetection() # 初始化facenet face_net = face_recognition.facenetEmbedding(model_path) # 读取待检图片 image = image_processing.read_image_gbk(image_path) print("image_processing.read_image_gbk:", type(image), image.shape) # <class 'numpy.ndarray'>, (616, 922, 3),(高,宽,通道) # 获取 判断标识 bounding_box crop_image bboxes, landmarks = face_detect.detect_face(image) bboxes, landmarks = face_detect.get_square_bboxes( bboxes, landmarks, fixed="height") # 以高为基准,获得等宽的举行 if bboxes == [] or landmarks == []: print("-----no face") exit(0) print("-----image have {} faces".format(len(bboxes))) face_images = image_processing.get_bboxes_image( image, bboxes, resize_height, resize_width) # 按照bboxes截取矩形框 face_images = image_processing.get_prewhiten_images(face_images) # 图像归一化 pred_emb = face_net.get_embedding(face_images) # 获取facenet特征 pred_name, pred_score = compare_embadding(pred_emb, dataset_emb, names_list) # 在图像上绘制人脸边框和识别的结果 show_info = [n + ':' + str(s)[:5] for n, s in zip(pred_name, pred_score)] image_processing.show_image_bboxes_text("face_reco", image, bboxes, show_info)
def get_face_embedding(model_path, files_list, names_list): ''' 获得embedding数据 :param files_list: 图像列表 :param names_list: 与files_list一一的名称列表 :return: ''' # 转换颜色空间RGB or BGR colorSpace = "RGB" # 初始化mtcnn人脸检测 face_detect = face_recognition.FaceDetection() # 初始化facenet face_net = face_recognition.facenetEmbedding(model_path) embeddings = [] # 用于保存人脸特征数据库 label_list = [] # 保存人脸label的名称,与embeddings一一对应 for image_path, name in zip(files_list, names_list): print("processing image: {}".format(image_path)) image = image_processing.read_image_gbk(image_path, colorSpace=colorSpace) # 进行人脸检测,获得bounding_box bboxes, landmarks = face_detect.detect_face(image) bboxes, landmarks = face_detect.get_square_bboxes(bboxes, landmarks, fixed="height") # image_processing.show_image_boxes("image",image,bboxes) if bboxes == [] or landmarks == []: print("-----no face") continue if len(bboxes) >= 2 or len(landmarks) >= 2: print("-----image total {} faces".format(len(bboxes))) continue # 获得人脸区域 face_images = image_processing.get_bboxes_image( image, bboxes, resize_height, resize_width) # 人脸预处理,归一化 face_images = image_processing.get_prewhiten_images(face_images, normalization=True) # 获得人脸特征 pred_emb = face_net.get_embedding(face_images) embeddings.append(pred_emb) # 可以选择保存image_list或者names_list作为人脸的标签 # 测试时建议保存image_list,这样方便知道被检测人脸与哪一张图片相似 # label_list.append(image_path) label_list.append(name) return embeddings, label_list
fps_display_interval = 5 # seconds frame_rate = 0 frame_count = 0 normalization = False # 是否标准化,默认否 input_path = "D:/奇辉电子/120_150_2.mp4" output_path = "D:/奇辉电子/120_150_2-人脸识别.mp4" # 输出文件,为空则认为不需要输出 if __name__ == '__main__': model_path = 'models/20180408-102900' dataset_path = 'dataset/emb/faceEmbedding.npy' # 人脸特征 filename = 'dataset/emb/name.txt' # 人脸列表 dataset_emb, names_list = load_dataset(dataset_path, filename) # 加载数据库的数据 face_detect = face_recognition.FaceDetection() # 初始化mtcnn face_net = face_recognition.facenetEmbedding(model_path) # 初始化facenet cap = cv2.VideoCapture(input_path) start_time = time.time() video_FourCC = int(cap.get(cv2.CAP_PROP_FOURCC)) video_fps = cap.get(cv2.CAP_PROP_FPS) video_size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) isOutput = True if output_path != "" else False if isOutput: print("!!! TYPE:", type(output_path), type(video_FourCC), type(video_fps), type(video_size)) out = cv2.VideoWriter(output_path, video_FourCC, video_fps, video_size) total_frame_count = cap.get(7) # 获取总帧数