def __init__(self): # 创建mtcnn对象,建立一个mtcnn模型 # 检测图片中的人脸 self.mtcnn_model = mtcnn() # 门限函数 self.threshold = [0.5, 0.8, 0.9] # 载入facenet模型 # 将检测到的人脸转化为128维的向量 self.facenet_model = InceptionResNetV1() # model.summary() model_path = './model_data/facenet_keras.h5' self.facenet_model.load_weights(model_path) #-----------------------------------------------# # 对数据库中的人脸进行编码 # known_face_encodings中存储的是编码后的人脸 # known_face_names为人脸的名字 #-----------------------------------------------# face_list = os.listdir("face_dataset") self.known_face_encodings = [] self.known_face_names = [] # 所有的人脸进行遍历,得到仓库里所有人脸的128维特征向量和对应名称 for face in face_list: # obama.jpg进行'.'分割,取前一段字符串 name = face.split(".")[0] # 人脸读取出来 img = cv2.imread("./face_dataset/" + face) # 图像由 BGR -> RGB img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 检测人脸 rectangles = self.mtcnn_model.detectFace(img, self.threshold) # 转化成正方形 rectangles = utils.rect2square(np.array(rectangles)) # facenet要传入一个160x160的图片 # 进行读取 rectangle = rectangles[0] # 记下他们的landmark,5个标记点,进行人脸对齐 landmark = (np.reshape(rectangle[5:15], (5, 2)) - np.array( [int(rectangle[0]), int(rectangle[1])])) / (rectangle[3] - rectangle[1]) * 160 #人脸截取 crop_img = img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])] crop_img = cv2.resize(crop_img, (160, 160)) # 进行对齐操作 new_img, _ = utils.Alignment_1(crop_img, landmark) # 增加一个维度 new_img = np.expand_dims(new_img, 0) # 将检测到的人脸传入到facenet的模型中,实现128维特征向量的提取 face_encoding = utils.calc_128_vec(self.facenet_model, new_img) self.known_face_encodings.append(face_encoding) self.known_face_names.append(name)
def __init__(self): # 创建mtcnn对象用于检测图片中的人脸 self.mtcnn_model = mtcnn() # 门限函数 self.threshold = [0.5, 0.8, 0.9] # 载入facenet用于将检测到的人脸转化为128维的向量 self.facenet_model = InceptionResNetV1() # model.summary() #模型summary信息 model_path = './model_data/facenet_keras.h5' # 模型文件 self.facenet_model.load_weights(model_path) #-----------------------------------------------# # 对数据库中的人脸进行编码 # known_face_encodings中存储的是编码后的人脸 # known_face_names为人脸的名字 # face_dataset文件中的图片为人脸数据库 #-----------------------------------------------# face_list = os.listdir("face_dataset") # 列出文件夹中的人脸图片 self.known_face_encodings = [] # 存储编码后的人脸 self.known_face_names = [] # 人脸的名字及图片的名字 # 遍历人脸 for face in face_list: # 图片文件名 name = face.split(".")[0] # 读取图片是BGR格式数据 img = cv2.imread("./face_dataset/" + face) # 颜色空间转换 # cv2.COLOR_BGR2RGB 将BGR格式转换成RGB格式 # cv2.COLOR_BGR2GRAY 将BGR格式转换成灰度图片 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 检测图版中的人脸 rectangles = self.mtcnn_model.detectFace(img, self.threshold) # 长方形转化成正方形 rectangles = utils.rect2square(np.array(rectangles)) # facenet要传入一个160x160的图片 rectangle = rectangles[0] # 记下他们的landmark landmark = (np.reshape(rectangle[5:15], (5, 2)) - np.array( [int(rectangle[0]), int(rectangle[1])])) / (rectangle[3] - rectangle[1]) * 160 crop_img = img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])] # Resize to 160x160 crop_img = cv2.resize(crop_img, (160, 160)) new_img, _ = utils.Alignment_1(crop_img, landmark) new_img = np.expand_dims(new_img, 0) # 将检测到的人脸传入到facenet的模型中,实现128维特征向量的提取 face_encoding = utils.calc_128_vec(self.facenet_model, new_img) # print(face_encoding) # 记录128维护特征向量到数组 self.known_face_encodings.append(face_encoding) # 记录文件名到数组 self.known_face_names.append(name)
def __init__(self): # 创建mtcnn对象 # 检测图片中的人脸 self.mtcnn_model = mtcnn() # 门限函数 self.threshold = [0.5, 0.8, 0.9] # 载入facenet # 将检测到的人脸转化为128维的向量 self.facenet_model = InceptionResNetV1() # model.summary() model_path = './model_data/facenet_keras.h5' self.facenet_model.load_weights(model_path) # -----------------------------------------------# # 对数据库中的人脸进行编码 # known_face_encodings中存储的是编码后的人脸 # known_face_names为人脸的名字 # -----------------------------------------------# face_list = os.listdir("face_dataset") # 存放编码后的人脸 self.known_face_encodings = [] # 存放编码后人脸的名字 self.known_face_names = [] for face in face_list: name = face.split(".")[0] img = cv2.imread("./face_dataset/" + face) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # 检测人脸 rectangles = self.mtcnn_model.detectFace(img, self.threshold) # 转化成正方形 rectangles = utils.rect2square(np.array(rectangles)) # facenet要传入一个160x160的图片 rectangle = rectangles[0] # 记下他们的landmark landmark = (np.reshape(rectangle[5:15], (5, 2)) - np.array( [int(rectangle[0]), int(rectangle[1])])) / (rectangle[3] - rectangle[1]) * 160 crop_img = img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])] crop_img = cv2.resize(crop_img, (160, 160)) new_img, _ = utils.Alignment_1(crop_img, landmark) new_img = np.expand_dims(new_img, 0) # 将检测到的人脸传入到facenet的模型中,实现128维特征向量的提取 face_encoding = utils.calc_128_vec(self.facenet_model, new_img) self.known_face_encodings.append(face_encoding) self.known_face_names.append(name)
def __init__(self): #-------------------------# # 创建mtcnn的模型 # 用于检测人脸 #-------------------------# self.mtcnn_model = mtcnn() self.threshold = [0.5, 0.6, 0.8] #-----------------------------------# # 载入facenet # 将检测到的人脸转化为128维的向量 #-----------------------------------# self.facenet_model = InceptionResNetV1() model_path = './model_data/facenet_keras.h5' self.facenet_model.load_weights(model_path) #-----------------------------------------------# # 对数据库中的人脸进行编码 # known_face_encodings中存储的是编码后的人脸 # known_face_names为人脸的名字 #-----------------------------------------------# face_list = os.listdir("face_dataset") self.known_face_encodings = [] self.known_face_names = [] for face in face_list: name = face.split(".")[0] img = cv2.imread("./face_dataset/" + face) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #---------------------# # 检测人脸 #---------------------# rectangles = self.mtcnn_model.detectFace(img, self.threshold) #---------------------# # 转化成正方形 #---------------------# rectangles = utils.rect2square(np.array(rectangles)) #-----------------------------------------------# # facenet要传入一个160x160的图片 # 利用landmark对人脸进行矫正 #-----------------------------------------------# rectangle = rectangles[0] landmark = np.reshape(rectangle[5:15], (5, 2)) - np.array( [int(rectangle[0]), int(rectangle[1])]) crop_img = img[int(rectangle[1]):int(rectangle[3]), int(rectangle[0]):int(rectangle[2])] crop_img, _ = utils.Alignment_1(crop_img, landmark) crop_img = np.expand_dims(cv2.resize(crop_img, (160, 160)), 0) #--------------------------------------------------------------------# # 将检测到的人脸传入到facenet的模型中,实现128维特征向量的提取 #--------------------------------------------------------------------# face_encoding = utils.calc_128_vec(self.facenet_model, crop_img) self.known_face_encodings.append(face_encoding) self.known_face_names.append(name)
def __init__(self): # 创建mtcnn对象 # 检测图片中的人脸 self.mtcnn_model = mtcnn() # 门限函数 self.threshold = [0.5,0.6,0.8] self.Crop_HEIGHT = 160 self.Crop_WIDTH = 160 self.classes_path = "model_data/classes.txt" self.NUM_CLASSES = 2 self.mask_model = MobileNet(input_shape=[self.Crop_HEIGHT,self.Crop_WIDTH,3],classes=self.NUM_CLASSES) self.mask_model.load_weights("./logs/last_one.h5") self.class_names = self._get_class()
def __init__(self): # create a mtcnn object # detect the face in img self.mtcnn_model = mtcnn() # threshold self.threshold = [0.5, 0.6, 0.8] self.Crop_HEIGHT = 160 self.Crop_WIDTH = 160 self.classes_path = "model_data/classes.txt" self.NUM_CLASSES = 2 self.mask_model = MobileNet( input_shape=[self.Crop_HEIGHT, self.Crop_WIDTH, 3], classes=self.NUM_CLASSES) self.mask_model.load_weights("./logs/last_one.h5") self.class_names = self._get_class()
def __init__(self): #-------------------------# # 创建mtcnn的模型 # 用于检测人脸 #-------------------------# self.mtcnn_model = mtcnn() self.threshold = [0.5, 0.6, 0.8] #-------------------------# # 创建mobilenet的模型 # 用于判断是否佩戴口罩 #-------------------------# self.classes_path = "model_data/classes.txt" self.class_names = self._get_class() self.Crop_HEIGHT = 224 self.Crop_WIDTH = 224 self.NUM_CLASSES = len(self.class_names) self.mask_model = MobileNet( input_shape=[self.Crop_HEIGHT, self.Crop_WIDTH, 3], classes=self.NUM_CLASSES) self.mask_model.load_weights("./logs/last_one.h5")
import cv2 import numpy as np from net.mtcnn import mtcnn import utils.utils as utils from net.inception import InceptionResNetV1 img = cv2.imread('face_dataset/timg.jpg') # 创建mtcnn对象 mtcnn_model = mtcnn() # 门限函数 threshold = [0.5, 0.7, 0.9] # 检测人脸 rectangles = mtcnn_model.detectFace(img, threshold) draw = img.copy() # 转化成正方形 # 人脸框 & 五个特征点的位置 rectangles = utils.rect2square(np.array(rectangles)) # 载入facenet facenet_model = InceptionResNetV1() # model.summary() model_path = './model_data/facenet_keras.h5' facenet_model.load_weights(model_path) for rectangle in rectangles: if rectangle is not None: landmark = (np.reshape(rectangle[5:15], (5, 2)) - np.array( [int(rectangle[0]), int(rectangle[1])])) / (rectangle[3] - rectangle[1]) * 160
def __init__(self): # 创建mtcnn对象 # 检测图片中的人脸 self.mtcnn_model = mtcnn() # 门限函数 self.threshold = [0.5, 0.8, 0.9]