当前位置: 首页 > news >正文

【深度学习】人脸识别工程化落地

文章目录

  • 前言
  • 1、facenet
  • 2、使用
    • 2.1.其它blog
    • 2.2 实践
  • 总结


前言

老早以前就希望能写一篇关于人脸识别的工程化落地的案例,一年前做疲劳驾驶时使用的dlib插件,它封装好了,人脸检测、对齐、相似度计算三个部分,就是插件比较难装,但同时也少了很多细节.
今天我们来做一些高级一点的, facenet网络


1、facenet

谷歌人脸识别算法,发表于 CVPR 2015,利用相同人脸在不同角度等姿态的照片下有高内聚性,不同人脸有低耦合性,提出使用 cnn + triplet mining 方法,在 LFW 数据集上准确度达到 99.63%。
测试时只需要计算人脸特征EMBEDDING,然后计算距离使用阈值即可判定两张人脸照片是否属于相同的个体。
在这里插入图片描述
简单来讲,在使用阶段,facenet即是:
1、输入一张人脸图片
2、通过深度卷积网络提取特征
3、L2标准化
4、得到一个长度为128特征向量。

2、使用

2.1.其它blog

其它人在使用中一般是这样的

    #---------------------------------------------------##   检测图片#---------------------------------------------------#def detect_image(self, image_1, image_2):#---------------------------------------------------##   图片预处理,归一化#---------------------------------------------------#with torch.no_grad():image_1 = resize_image(image_1, [self.input_shape[1], self.input_shape[0]], letterbox_image=self.letterbox_image)image_2 = resize_image(image_2, [self.input_shape[1], self.input_shape[0]], letterbox_image=self.letterbox_image)photo_1 = torch.from_numpy(np.expand_dims(np.transpose(preprocess_input(np.array(image_1, np.float32)), (2, 0, 1)), 0))photo_2 = torch.from_numpy(np.expand_dims(np.transpose(preprocess_input(np.array(image_2, np.float32)), (2, 0, 1)), 0))if self.cuda:photo_1 = photo_1.cuda()photo_2 = photo_2.cuda()#---------------------------------------------------##   图片传入网络进行预测#---------------------------------------------------#output1 = self.net(photo_1).cpu().numpy()output2 = self.net(photo_2).cpu().numpy()#---------------------------------------------------##   计算二者之间的距离#---------------------------------------------------#l1 = np.linalg.norm(output1 - output2, axis=1)# plt.subplot(1, 2, 1)# plt.imshow(np.array(image_1))# plt.subplot(1, 2, 2)# plt.imshow(np.array(image_2))# plt.text(-12, -12, 'Distance:%.3f' % l1, ha='center', va= 'bottom',fontsize=11)# plt.show()return l1

核心思想是:传入两张图片,计算距离,设置阈值判断是否是同一个人.
可是,这种放在工程上,比如人脸开门是不行的,所谓人脸识别,必须有一个人脸库,送入一张图片和人脸库的所有图片进行遍历比对,挑出相似度最高的一张图片,和阈值比对,决定是否开门,并留存记录.

那肯定不能次次遍历人脸库啊,所以人脸库的图片要先转出特征向量的collection,存起来, 送入一张图片后,用模型抽取向量特征,和collections里面的向量比较.
如果人脸库太大,比如几万张特征,我们可以分batch存,每个batch找出最合适的,有点像桶排序,并且并行起来计算,是不是比较有意思.

2.2 实践

    #---------------------------------------------------##   保存人脸库特征#---------------------------------------------------#def save_to_tensor(self):#---------------------------------------------------##   图片预处理,归一化#---------------------------------------------------#path_dir = "./img"file_name_list = os.listdir(path_dir)img_feature = {}with torch.no_grad():for file_name in file_name_list:image_1 = Image.open(os.path.join(path_dir,file_name))image_1 = resize_image(image_1, [self.input_shape[1], self.input_shape[0]], letterbox_image=self.letterbox_image)photo_1 = torch.from_numpy(np.expand_dims(np.transpose(preprocess_input(np.array(image_1, np.float32)), (2, 0, 1)), 0))if self.cuda:photo_1 = photo_1.cuda()                   #---------------------------------------------------##   图片传入网络进行预测#---------------------------------------------------#output1 = self.net(photo_1).cpu().numpy()img_feature[file_name] = output1print(img_feature)with open("img_feature.txt", "wb") as file:pickle.dump(img_feature, file)return None

经过这些代码,把./img/ 下的人脸库向量都以二进制的方式存入了文件中.
送入一张图片,只需要,将图片送入模型,获取当前人的人脸信息,然后和人脸库的向量比较,

    def get_from_face_collection(self):input_file = "img/1_001.jpg"image_1 = Image.open(input_file)image_1 = resize_image(image_1, [self.input_shape[1], self.input_shape[0]], letterbox_image=self.letterbox_image)with torch.no_grad():photo_1 = torch.from_numpy(np.expand_dims(np.transpose(preprocess_input(np.array(image_1, np.float32)), (2, 0, 1)), 0))if self.cuda:photo_1 = photo_1.cuda()            #---------------------------------------------------##   图片传入网络进行预测#---------------------------------------------------#output1 = self.net(photo_1).cpu().numpy()f = open('img_feature.txt','rb')img_feature_json = pickle.load(f)distance_map = {}for img_name, featrue_map in img_feature_json.items():l1 = np.linalg.norm(output1 - featrue_map, axis=1)distance_map[img_name] = l1print(distance_map)

distance_map 就是你说要的结果集,然后做个排序,找到top1 和阈值比对即可
git:https://github.com/justinge/facenet_pytorch.git

总结

还有用milivus向量库的,先挖个坑,最终工程化一个到位的.

http://www.lryc.cn/news/3085.html

相关文章:

  • AOP面向切面编程思想。
  • 实验7-变治技术及动态规划初步
  • JVM垃圾回收机制GC理解
  • C++中的容器
  • 2023备战金三银四,Python自动化软件测试面试宝典合集(五)
  • SpringDI自动装配BeanSpring注解配置和Java配置类
  • 2月面经:真可惜...拿了小米的offer,字节却惨挂在三面
  • 磐云PY-B8 网页注入
  • 多传感器融合定位十-基于滤波的融合方法Ⅰ其二
  • Java集合面试题:HashMap源码分析
  • 华为OD机试 - 数组合并(Python),真题含思路
  • Vue2创建移动端项目
  • PorterDuffXfermode与圆角图片
  • 如何准备大学生电子设计竞赛
  • 【Java容器(jdk17)】ArrayList深入源码,就是这么简单
  • 【Java 面试合集】简述下Java的三个特性 以及项目中的应用
  • git基本概念图示【学习】
  • 微前端qiankun架构 (基于vue2实现)使用教程
  • 记录robosense RS-LIDAR-16使用过程3
  • 【博学谷学习记录】大数据课程-学习第七周总结
  • 154、【动态规划】leetcode ——494. 目标和:回溯法+动态规划(C++版本)
  • MySQL-窗口函数
  • 【C++设计模式】学习笔记(1):面向对象设计原则
  • [测开篇]设计测试用例的方法如何正确描述Bug
  • 设计模式学习笔记--单例、建造者、适配器、装饰、外观、组合
  • English Learning - Day5 L1考前复习 2023.2.10 周五
  • C. Prepend and Append
  • javassm超市在线配送管理系统
  • Scratch少儿编程案例-多模式贪吃蛇(无尽和计时)
  • 谷歌蜘蛛池怎么搭建?Google蜘蛛池可以帮助谷歌排名吗?