搜索

查看: 3089|回复: 11

[Python] 使用python AI快速比对两张人脸图像及遇到的坑

[复制链接]
发表于 2023-5-4 17:18:05 | 显示全部楼层 |阅读模式
Editor 2023-5-4 17:18:05 3089 11 看全部
本篇文章的代码块的实现主要是为了能够快速的通过python第三方非标准库对比出两张人脸是否一样。
实现过程比较简单,但是第三方python依赖的安装过程较为曲折,下面是通过实践对比总结出来的能够支持的几个版本,避免大家踩坑。
python版本:3.6.8
dlib版本:19.7.0
face-recognition版本:0.1.10
开始之前,我们选择使用pip的方式对第三方的非标准库进行安装。
pip install cmake
pip install dlib==19.7.0
pip install face-recognition==0.1.10
pip 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 each
    face
    :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')
# 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 AI快速比对两张人脸图像?的文章就介绍到这了,更多相关python AI快速比对两张人脸图像内容请搜索知鸟论坛以前的文章或继续浏览下面的相关文章希望大家以后多多支持知鸟论坛
回复

使用道具 举报

发表于 2023-6-29 04:34:07 | 显示全部楼层
麻辣鸡翅 2023-6-29 04:34:07 看全部
楼主太厉害了!楼主,I*老*虎*U!我觉得知鸟论坛真是个好地方!
回复

使用道具 举报

发表于 2023-6-29 10:53:28 | 显示全部楼层
462710480 2023-6-29 10:53:28 看全部
其实我一直觉得楼主的品味不错!呵呵!知鸟论坛太棒了!
回复

使用道具 举报

发表于 2023-6-29 14:18:00 | 显示全部楼层
李志敏 2023-6-29 14:18:00 看全部
感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
回复

使用道具 举报

发表于 2023-6-29 14:25:17 | 显示全部楼层
胡37 2023-6-29 14:25:17 看全部
楼主,大恩不言谢了!知鸟论坛是最棒的!
回复

使用道具 举报

发表于 2023-6-29 18:36:15 | 显示全部楼层
无人岛屿颈 2023-6-29 18:36:15 看全部
我看不错噢 谢谢楼主!知鸟论坛越来越好!
回复

使用道具 举报

发表于 2023-6-29 19:18:37 | 显示全部楼层
我是的十八簿 2023-6-29 19:18:37 看全部
楼主发贴辛苦了,谢谢楼主分享!我觉得知鸟论坛是注册对了!
回复

使用道具 举报

发表于 2023-6-29 21:03:29 | 显示全部楼层
掌舵的鱼1987 2023-6-29 21:03:29 看全部
感谢楼主的无私分享!要想知鸟论坛好 就靠你我他
回复

使用道具 举报

发表于 2023-6-29 21:54:49 | 显示全部楼层
123456819 2023-6-29 21:54:49 看全部
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
回复

使用道具 举报

发表于 2023-6-29 22:20:24 | 显示全部楼层
执着等待等wc 2023-6-29 22:20:24 看全部
既然你诚信诚意的推荐了,那我就勉为其难的看看吧!知鸟论坛不走平凡路。
回复

使用道具 举报

  • 您可能感兴趣
点击右侧快捷回复 【请勿灌水】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 返回列表

RSS订阅| SiteMap| 小黑屋| 知鸟论坛
联系邮箱E-mail:zniao@foxmail.com
快速回复 返回顶部 返回列表