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

如何使用python AI快速比对两张人脸图像?

本篇文章的代码块的实现主要是为了能够快速的通过python第三方非标准库对比出两张人脸是否一样。

实现过程比较简单,但是第三方python依赖的安装过程较为曲折,下面是通过实践对比总结出来的能够支持的几个版本,避免大家踩坑。

python版本:3.6.8
dlib版本:19.7.0
face-recognition版本:0.1.10

开始之前,我们选择使用pip的方式对第三方的非标准库进行安装。

pip install cmakepip install dlib==19.7.0pip install face-recognition==0.1.10pip install opencv-python

然后,将使用到的模块cv2/face-recognition两个模块导入到代码块中即可。

# OpenCV is a library of programming functions mainly aimed at real-time computer vision.
import cv2# It's loading a pre-trained model that can detect faces in images.
import face_recognition

新建一个python函数get_face_encodings,用来获取人脸部分的编码,后面可以根据这个编码来进行人脸比对。

def get_face_encodings(image_path):"""It takes an image path, loads the image, finds the faces in the image, and returns the 128-d face encodings for eachface:param image_path: The path to the image to be processed"""# It's loading a pre-trained model that can detect faces in images.image = cv2.imread(image_path)# It's converting the image from BGR to RGB.image_RGB = image[:, :, ::-1]image_face = face_recognition.face_locations(image_RGB)# It's taking the image and the face locations and returning the face encodings.face_env = face_recognition.face_encodings(image_RGB, image_face)# It's returning the first face encoding in the list.return face_env[0]

上述函数中注释都是通过Pycharm插件自动生成的,接下来我们直接调用get_face_encodings函数分别获取两个人脸的编码。

# It's taking the image and the face locations and returning the face encodings.
ima1 = get_face_encodings('03.jpg')# It's taking the image and the face locations and returning the face encodings.
ima2 = get_face_encodings('05.jpg')

上面我们选择了两张附有人脸的图片,并且已经获取到了对应的人脸编码。接着使用compare_faces函数进行人脸比对。

# It's comparing the two face encodings and returning True if they match.
is_same = face_recognition.compare_faces([ima1], ima2, tolerance=0.3)[0]print('人脸比对结果:{}'.format(is_same))

人脸比对结果:False

这个时候人脸比对结果已经出来了,False代表不一样。这里compare_faces有一个比较重要的参数就是tolerance=0.3,默认情况下是0.6。

tolerance参数的值越小的时候代表比对要求更加严格,因此这个参数的大小需要根据实际情况设置,它会直接影响整个比对过程的结果。

往期精彩

python自制PDF转换.PNG格式图片(按每页生成图片完整源码)小工具!

python如何使用最简单的方式将PDF转换成Word?

python项目环境迁移时如何生成第三方库文件requirements.txt并安装?

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

相关文章:

  • (2)C#传智:变量基础(第二天)
  • 02-mysql高级-
  • windows 使用everything 查看文件(夹)存储空间占用
  • 2023该好好赚钱了,推荐三个下班就能做的副业
  • vue3如何进行数据监听watch/watchEffect
  • Wgcloud安装和使用(性能监控)
  • 前端如何实现本地图片上传?
  • 【基础算法】差分的应用(一维差分和二维差分)
  • 第49章 API统一集中管理
  • carla0.9.13-UE4添加4轮车模型(Linux系统)
  • 对比yolov4和yolov3
  • Android ServiceManager
  • 数据挖掘,计算机网络、操作系统刷题笔记53
  • 地球板块运动vr交互模拟体验教学提高学生的学习兴趣
  • 【Android玩机】跟大家聊聊面具Magisk的使用(安装、隐藏)
  • DACS: Domain Adaptation via Cross-domain Mixed Sampling 学习笔记
  • python并发编程(并发与并行,同步和异步,阻塞与非阻塞)
  • 【项目】DTO、VO以及PO之间的关系和区别
  • Nginx介绍
  • 你什么档次?敢和我用一样的即时通讯平台WorkPlus?
  • 学习资源 - 深度学习
  • C语言数据结构初阶(1)----时空复杂度
  • vscode SSH 保存密码自动登录服务器
  • VR全景多种玩法打破传统宣传,打造全新云端视界
  • Git 教程
  • 一种全新的图像滤波理论的实验(二)
  • Boost库文档搜索引擎
  • Linux中安装JDK
  • 宝塔面板公网ip非80端口非443端口部署ssl
  • 手撕八大排序(上)