def feature_extraction(self, feat_ext, images, rects, img): if feat_ext == 'facenet': return facenet_feature_extraction.feature_extraction(images) elif feat_ext == 'vgg16': return vgg16_feature_extraction.feature_extraction(images) elif feat_ext == 'dlib_68': return dlib_feature_extraction.feature_extraction(img, rects, "68") else: return []
def batch_feature_extraction(self, face_dst_path, batch_size=64): batch = [] self.labels = [] self.src_features = None dlib_src_img = [] dlib_src_rect = [] # 所有人脸文件的循环 for ii, file in enumerate(self.filenames, 1): # 保存人脸文件名,不包含后缀,例:000001_2 self.labels.append(file.split("/")[-1].split(".")[0]) if self.feat_ext == 'dlib_68': # dlib比较特殊,需要提供人脸所在帧的图片和人脸框 # 读取人脸所在帧的图片并保存到dlib_src_img中,例:000001.jpg dlib_img = misc.imread(os.path.expanduser(os.path.join(self.dstpath, file.split("_")[0] + ".jpg")), mode='RGB') dlib_src_img.append(dlib_img) # 保存人脸框 dlib_src_rect.append(self.dst_rects_lst[ii - 1]) else: # 读取人脸图片并保存到batch中 img = misc.imread(os.path.expanduser(os.path.join(face_dst_path, file)), mode='RGB') if self.feat_ext == 'facenet': batch.append(img.reshape((160, 160, 3))) else: img = misc.imresize(img, (224, 224, 3), interp='bilinear') batch.append(img.reshape((1, 224, 224, 3))) # 如果读取的人脸数达到一个batch批次了,或者所有人脸都加载进来了,开始批量提取人脸特征 if ii % batch_size == 0 or ii == len(self.filenames): if self.feat_ext == 'facenet': images = np.stack(batch) codes_batch = facenet_feature_extraction.feature_extraction(images) elif self.feat_ext == 'vgg16': images = np.concatenate(batch) codes_batch = vgg16_feature_extraction.feature_extraction(images) elif self.feat_ext == 'dlib_68': images = np.stack(dlib_src_img) # concatenate if images.ndim > 3: codes_batch = [] for i in range(images.shape[0]): feature = dlib_feature_extraction.feature_extraction_single(images[i], dlib_src_rect[i], "68") codes_batch.append(feature) else: codes_batch = dlib_feature_extraction.feature_extraction_single(images, dlib_src_rect[0], "68") # 保存人脸特征 if self.src_features is None: self.src_features = codes_batch else: self.src_features = np.concatenate((self.src_features, codes_batch)) # 重置变量,开始下一批次的循环 batch = [] dlib_src_img = [] dlib_src_rect = [] print('{} images processed'.format(ii))