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_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 create_face(images_dir, out_face_dir): ''' 生成人脸数据图库,保存在out_face_dir中,这些数据库将用于生成embedding数据库 :param images_dir: :param out_face_dir: :return: ''' # image_list=file_processing.get_files_list(images_dir, postfix='jpg') image_list, names_list = file_processing.gen_files_labels(images_dir, postfix='jpg') face_detect = face_recognition.Facedetection() for image_path, name in zip(image_list, names_list): image = image_processing.read_image(image_path, resize_height=0, resize_width=0, normalization=False) # 获取 判断标识 bounding_box crop_image bounding_box, points = face_detect.detect_face(image) bounding_box = bounding_box[:, 0:4].astype(int) bounding_box = bounding_box[0, :] print("face box:{}".format(bounding_box)) face_image = image_processing.crop_image(image, bounding_box) # image_processing.show_image("face", face_image) # image_processing.show_image_box("face",image,bounding_box) out_path = os.path.join(out_face_dir, name) face_image = image_processing.resize_image(face_image, resize_height, resize_width) if not os.path.exists(out_path): os.mkdir(out_path) basename = os.path.basename(image_path) out_path = os.path.join(out_path, basename) image_processing.save_image(out_path, face_image)
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_face(images_dir, out_face_dir): print(images_dir) ''' 生成人脸数据图库,保存在out_face_dir中,这些数据库将用于生成embedding数据库 :param images_dir: :param out_face_dir: :return: ''' # image_list=file_processing.get_files_list(images_dir, postfix='jpg') image_list, names_list = file_processing.gen_files_labels(images_dir) face_detect = face_recognition.Facedetection() for image_path, name in zip(image_list, names_list): print("图片的路径", str(image_path)) if image_path == 'dataset/images/.DS_Store': continue if image_path == 'dataset/images/face1/.DS_Store': continue if image_path == 'dataset/images/face2/.DS_Store': continue if image_path == 'dataset/images/face3/.DS_Store': continue if image_path == 'dataset/images/face4/.DS_Store': continue if image_path == './dataset/emb_face/.DS_Store': continue image = image_processing.read_image(image_path, resize_height=0, resize_width=0, normalization=False) # 获取 判断标识 bounding_box crop_image bounding_box, points = face_detect.detect_face(image) bounding_box = bounding_box[:, 0:4].astype(int) if (len(bounding_box) < 1): continue bounding_box = bounding_box[0, :] print("face box:{}".format(bounding_box)) face_image = image_processing.crop_image(image, bounding_box) # image_processing.show_image("face", face_image) # image_processing.show_image_box("face",image,bounding_box) out_path = os.path.join(out_face_dir, name) if (len(face_image) > 0): face_image = image_processing.resize_image(face_image, resize_height, resize_width) else: continue if not os.path.exists(out_path): os.mkdir(out_path) basename = os.path.basename(image_path) out_path = os.path.join(out_path, basename) image_processing.save_image(out_path, face_image)
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_path='E:/Face/dataset/bzl/subjectphoto_with_name/谢伟林_179_180.jpg' 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 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) # 获得人脸特征 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_face(images_dir, out_face_dir): ''' 生成人脸数据图库,保存在out_face_dir中,这些数据库将用于生成embedding数据库 ''' #此处引用自定义函数 : file_processing.gen_files_labels(images_dir,postfix='jpg') #返回值: 图像数组: images_dir下所有文件(是的,你没想错!包括子目录下的所有文件)的路径(Path)会被存储到image_list # 标签数组: names_list就是相应的的"文件名或子目录名",在本项目中,子目录名 将作为 作为样本的标签(即ID,e.g.姓名 或 学号之类的) print('#2nd--Mtcnn人脸库') image_list,names_list=file_processing.gen_files_labels(images_dir,postfix='jpg') face_detect=face_recognition.Facedetection() for image_path ,name in zip(image_list,names_list): # try: image=image_processing.read_image(image_path, resize_height=0, resize_width=0, normalization=False) # 获取 判断标识 bounding_box crop_image bounding_box, points = face_detect.detect_face(image) bounding_box = bounding_box[:,0:4].astype(int) # try: bounding_box=bounding_box[0,:] # except: # print("跳过当前图片") # continue print("矩形框坐标为:{}".format(bounding_box)) face_image = image_processing.crop_image(image,bounding_box) # except: # print("当前图像帧格式非法,将被忽略") out_path=os.path.join(out_face_dir,name) face_image=image_processing.resize_image(face_image, resize_height, resize_width) if not os.path.exists(out_path): os.mkdir(out_path) basename=os.path.basename(image_path) out_path=os.path.join(out_path,basename) image_processing.save_image(out_path,face_image)
import copy import time from tkinter.messagebox import askquestion, showinfo import threading # 后端申明 resize_width = 160 resize_height = 160 # 存放facenet预训练模型的路径 model_path = 'models/20180408-102900' # 存放人脸特征数据库的路径 npy_dataset_path = 'dataset/emb/faceEmbedding.npy' filename = 'dataset/emb/name.txt' # 加载facenet face_net = face_recognition.facenetEmbedding(model_path) face_detect = face_recognition.Facedetection() # 数据库声明 model_path = 'models/20180408-102900' # 存放facenet预训练模型的路径 dataset_path = 'dataset/images/photo_database' # 用于建立特征库的照片 out_emb_path = 'dataset/emb/faceEmbedding.npy' # 特征库输出地址 out_filename = 'dataset/emb/name.txt' # 标签保存地址 # 前端声明 camera_switch = False count = 1 # 临时变量 tempimagepath = r"dataset\images\photo_database\\" # 摄像机设置
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() #杀死窗口进程
#-*- coding: UTF-8 -*- import cv2 as cv import face_recognition import numpy as np import time import pickle name = ['1', '2', '3', '4'] #预测的标签样本 model_path = 'models/20180408-102900' #face_net的路径 mtcnn = face_recognition.Facedetection() #导入mtcnn face_net = face_recognition.facenetEmbedding(model_path) with open('face_svm.pkl', 'rb') as infile: (classifymodel1, class_names1) = pickle.load(infile) with open('face_xg.pkl', 'rb') as infile: (classifymodel2, class_names2) = pickle.load(infile) ''' 以下三个函数为人脸的裁剪和图像归一化处理 ''' def prewhiten(x): mean = np.mean(x) std = np.std(x) std_adj = np.maximum(std, 1.0 / np.sqrt(x.size)) y = np.multiply(np.subtract(x, mean), 1 / std_adj) return y def crop_image(image, box): crop_img = image[box[1]:box[3], box[0]:box[2]] return crop_img