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) # 获取 判断标识 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) face_images = image_processing.get_prewhiten_images(face_images) pred_emb = face_net.get_embedding(face_images) 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)] print(show_info)
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 face_recognition_video(model_path,dataset_path, filename,video_path): # 加载数据库的数据 dataset_emb, names_list = load_dataset(dataset_path, filename) # 初始化mtcnn人脸检测 face_detect = face_recognition.Facedetection() # 初始化facenet face_net = face_recognition.facenetEmbedding(model_path) cap = cv2.VideoCapture(video_path) # 获取视频数据 face_cnt = 0 # 控制视频读取时候,每秒截取指定数量图片进行一次识别 while cap.isOpened(): ok, frame = cap.read() face_cnt += 1 if not ok: break if(face_cnt % 5 == 0): frame = image_processing.read_image1(frame) bboxes, landmarks = face_detect.detect_face(frame) bboxes, landmarks = face_detect.get_square_bboxes(bboxes, landmarks, fixed="height") if bboxes == [] or landmarks == []: print("-----no face") continue print("-----image have {} faces".format(len(bboxes))) face_images = image_processing.get_bboxes_image(frame, bboxes, resize_height, resize_width) face_images = image_processing.get_prewhiten_images(face_images) pred_emb = face_net.get_embedding(face_images) 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)] print("showinfo:", show_info) image_processing.show_image_bboxes_text("face_recognition", frame, bboxes, show_info) cap.release() cv2.destroyWindow("face_recognition")
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(image_path) # 进行人脸检测,获得bounding_box bounding_box, points = face_detect.detect_face(image) bounding_box = bounding_box[:, 0:4].astype(int) # 获得人脸区域 face_images = image_processing.get_crop_images(image, bounding_box, resize_height, resize_width, whiten=True) # image_processing.show_image("face", face_images[0,:,:,:]) pred_emb = face_net.get_embedding(face_images) pred_name = compare_embadding(pred_emb, dataset_emb, names_list) # 在图像上绘制人脸边框和识别的结果 bgr_image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) image_processing.cv_show_image_text("face_recognition", bgr_image, bounding_box, pred_name) cv2.waitKey(0)
def create_embedding(model_path, emb_face_dir): face_net = face_recognition.facenetEmbedding(model_path) image_list, names_list = file_processing.gen_files_labels(emb_face_dir, postfix='jpg') images = image_processing.get_images(image_list, 160, 160, whiten=True) compare_emb = face_net.get_embedding(images) h5file = h5py.File('face.h5', 'w') h5file['X_train'] = compare_emb h5file.close() file_processing.write_data('name.txt', image_list, model='w')
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
def create_embedding(model_path, emb_face_dir, out_emb_path, out_filename): ''' 产生embedding数据库,这些embedding其实就是人脸特征 ''' print('#3rd--FaceNet人脸库') face_net = face_recognition.facenetEmbedding(model_path) image_list,names_list=file_processing.gen_files_labels(emb_face_dir,postfix='jpg') images= image_processing.get_images(image_list,resize_height,resize_width,whiten=True) compare_emb = face_net.get_embedding(images) np.save(out_emb_path, compare_emb) file_processing.write_data(out_filename, names_list, model='w')
def get_pair_scores(faces_data, issames_data, model_path, save_path=None): ''' 计算分数 :param faces_data: :param issames_data: :param model_path: insightFace模型路径 :param save_path: :return: ''' faces_list1, faces_list2 = split_data(faces_data) face_net = face_recognition.facenetEmbedding(model_path) pred_score = [] i = 0 for face1, face2, issame in zip(faces_list1, faces_list2, issames_data): # pred_id, pred_scores = faceRec.predict(faces) # 或者使用get_faces_embedding()获得embedding,再比较compare_embedding() face_images1 = image_processing.get_prewhiten_images( [face1], normalization=False) face_images2 = image_processing.get_prewhiten_images( [face2], normalization=False) face_emb1 = face_net.get_embedding(face_images1) face_emb2 = face_net.get_embedding(face_images2) # face_emb1 = face_net.get_faces_embedding([face1]) # face_emb2 = face_net.get_faces_embedding([face2]) dist = np.sqrt(np.sum(np.square(np.subtract(face_emb1, face_emb2)))) pred_score.append(dist) i += 1 if i % 100 == 0: print('processing data :', i) pred_score = np.array(pred_score).reshape(-1) issames_data = issames_data + 0 # 将true和false转为1/0 if save_path is not None: issames_path = os.path.join(save_path, "issames.npy") pred_score_path = os.path.join(save_path, "pred_score.npy") np.save(issames_path, issames_data) np.save(pred_score_path, pred_score) return pred_score, issames_data
def create_embedding(model_path, emb_face_dir, out_emb_path, out_filename): ''' 产生embedding数据库,保存在out_data_path中,这些embedding其实就是人脸的特征 :param model_path: :param emb_face_dir: :param out_emb_path: :param out_filename: :return: ''' face_net = face_recognition.facenetEmbedding(model_path) # image_list=file_processing.get_files_list(emb_face_dir,postfix='jpg') image_list, names_list = file_processing.gen_files_labels(emb_face_dir, postfix='jpg') images = image_processing.get_images(image_list, resize_height, resize_width, whiten=True) compare_emb = face_net.get_embedding(images) np.save(out_emb_path, compare_emb) # 可以选择保存image_list或者names_list作为人脸的标签 # 测试时建议保存image_list,这样方便知道被检测人脸与哪一张图片相似 file_processing.write_data(out_filename, image_list, model='w')
# 人脸抓拍进程 queues = [mp.Queue(maxsize=10) for _ in camera_ip_l] processes = [] for queue, camera_ip in zip(queues, camera_ip_l): processes.append( mp.Process(target=image_put, args=(queue, user_name, user_pwd, camera_ip))) processes.append( mp.Process(target=image_get_comparison, args=(queue, camera_ip))) for process in processes: process.daemon = True process.start() for process in processes: process.join() # 初始化facenet faceNet_model_path = '../models/FaceNet_model/20180408-102900' face_net = face_recognition.facenetEmbedding(faceNet_model_path) # 初始化数据库特征值 dataset_path = '../dataset/emb/faceEmbedding.npy' filename = '../dataset/emb/name.txt' dataset_emb, names_list = load_dataset(dataset_path, filename) # 初始化MNN interpreter = MNN.Interpreter(args.model_path) session = interpreter.createSession() input_tensor = interpreter.getSessionInput(session) if __name__ == "__main__": run_multi_camera() # with 1 + n threads
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) # 获取总帧数 while True:
def face_recognition_for_bzl(model_path, test_dataset, filename): # 加载数据库的数据 dataset_emb, names_list = predict.load_dataset(dataset_path, filename) print("loadind dataset...\n names_list:{}".format(names_list)) # 初始化mtcnn人脸检测 face_detect = face_recognition.Facedetection() # 初始化facenet face_net = face_recognition.facenetEmbedding(model_path) #获得测试图片的路径和label filePath_list, label_list = file_processing.gen_files_labels(test_dataset) label_list = [name.split('_')[0] for name in label_list] print("filePath_list:{},label_list{}".format(len(filePath_list), len(label_list))) right_num = 0 wrong_num = 0 detection_num = 0 test_num = len(filePath_list) for image_path, label_name in zip(filePath_list, label_list): print("image_path:{}".format(image_path)) # 读取图片 image = image_processing.read_image_gbk(image_path) # 人脸检测 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") continue if len(bboxes) >= 2 or len(landmarks) >= 2: print("-----image have {} 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) # face_images = image_processing.get_prewhiten_images(face_images,normalization=True) pred_emb = face_net.get_embedding(face_images) pred_name, pred_score = predict.compare_embadding(pred_emb, dataset_emb, names_list, threshold=1.3) # 在图像上绘制人脸边框和识别的结果 # show_info = [n + ':' + str(s)[:5] for n, s in zip(pred_name, pred_score)] # image_processing.show_image_text("face_recognition", image, bboxes, show_info) index = 0 pred_name = pred_name[index] pred_score = pred_score[index] if pred_name == label_name: right_num += 1 else: wrong_num += 1 detection_num += 1 print( "-------------label_name:{},pred_name:{},score:{:3.4f},status:{}". format(label_name, pred_name, pred_score, (label_name == pred_name))) # 准确率 accuracy = right_num / detection_num # 漏检率 misdetection = (test_num - detection_num) / test_num print("-------------right_num/detection_num:{}/{},accuracy rate:{}".format( right_num, detection_num, accuracy)) print( "-------------misdetection/all_num:{}/{},misdetection rate:{}".format( (test_num - detection_num), test_num, misdetection))
def face_reco(model_path, dataset_path, filename): ############################################################## # my_knn_model = KNeighborsClassifier() # my_knn_model = joblib.load('./models/knn.model')####################################################### #TIPS print("人脸识别系统正在启动>>>\n") # 先传入 "Embedding特征向量数据集" ,再 传入标签数组 dataset_emb, names_list = load_dataset(dataset_path, filename) # 构造 MTCNN人脸检测器 face_detect = face_recognition.Facedetection() print("加载Google-Facenet模型>>>") # 传入系统库模型 从而 构造 "Facenet特征提取器" face_net = face_recognition.facenetEmbedding(model_path) #打开摄像头,传入待检测的 VideoFrame camera = cv2.VideoCapture(0) #见L87 # counter = 1 ############################################################################################################# ############################################################################################################# ############################################################################################################# print("KNN>>>\n") print("\n加载预训练模型,即将开始人脸检测>>>\n") while True: read, image = camera.read() #每一张视频帧都是一个三维矩阵 #传入待检测的 StillImage # image=cv2.imread("/home/oliver/test_images/05.png")#first image_path # image = cv2.imread('/home/oliver/test_images/001.png') # ######## bgr_image = image resize_height = 0 resize_width = 0 # normalization=False # 若是灰度图(即二维矩阵)则转为三通道(即:三维矩阵) if len(bgr_image.shape) == 2: print("非法的灰度图!正在转换为BGR图>>>", image) bgr_image = cv2.cvtColor(bgr_image, cv2.COLOR_GRAY2BGR) rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB) # 将BGR转为RGB 格式转化 if resize_height > 0 and resize_width > 0: rgb_image = cv2.resize(rgb_image, (resize_width, resize_height)) rgb_image = np.asarray(rgb_image) #几何图像代数化,便于运算、分析 # ######### #刷新参数:宽度 和 高度 resize_width = 160 resize_height = 160 # 对StillImage 进行 MTCNN人脸检测,获得bounding_box bounding_box, points = face_detect.detect_face(rgb_image) bounding_box = bounding_box[:, 0:4].astype(int) if rgb_image is None: continue # 在StillImage上 ,基于bounding_box 进行Crop裁剪操作 face_images = image_processing.get_crop_images(rgb_image, bounding_box, resize_height, resize_width, whiten=True) if face_images is True: continue # 基于Facenet特征提取器 , 得到 摄像头实时返回的视频帧 的特征向量 pred_emb = face_net.get_embedding(face_images) #FaceNET 特征向量 # 把 StillImage特征向量 和 "Embedding特征向量数据集" 进行比较 ,最后返回一个 "数据库中的已有标签" 或者 "Unknown标签" pred_name = compare_embadding(pred_emb, dataset_emb, names_list) #FaceNet 标签数组 if pred_name is True: continue # 因为L37 把图片转化为RGB的格式 - 所以此处 还原 测试图片 为BGR图像 bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2BGR) # bgr_image = cv2.resize(bgr_image,(450,550)) # while True: # 调用函数,在StillImage上绘制人脸边框和 "相对应的标签" image_processing.cv_show_image_text("【@ZTJ-FaceReco人脸识别系统】", bgr_image, bounding_box, pred_name) # if cv2.waitKey(1) & 0xff == ord("q"): print("由于您按下了\"Q\"键,系统即将退出!") break ############################################################################################################# ############################################################################################################# ############################################################################################################# camera.release() #释放占用的摄像头 cv2.destroyAllWindows() #杀死窗口进程
face = cv2.resize(face, (200, 200)) params = face_model.predict(face) person_name = face_names[int(params[0])] detected_name_list.append(person_name) frame = render_frame(frame, face_pos) return frame, detected_name_list # 加载数据库的数据 dataset_emb, names_list = predict.load_dataset(mtcnn_config['dataset_path'], mtcnn_config['filename']) # 初始化mtcnn人脸检测 face_detect = face_recognition.Facedetection() # 初始化facenet face_net = face_recognition.facenetEmbedding(mtcnn_config['model_path']) def face_detect_mtcnn(frame): global dataset_emb global names_list global face_detect global face_net gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) pred_name, face_pos = predict.face_recognition_image_nn( dataset_emb, names_list, face_detect, face_net, gray) if pred_name is None: pred_name = []