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

 更新时间:2023年02月24日 08:34:31   作者:Python 集中营  
这篇文章主要介绍了如何使用python AI快速比对两张人脸图像?实现过程比较简单,但是第三方python依赖的安装过程较为曲折,下面是通过实践对比总结出来的能够支持的几个版本,避免大家踩坑,需要的朋友可以参考下

本篇文章的代码块的实现主要是为了能够快速的通过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快速比对两张人脸图像内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python3.7中安装openCV库的方法

    Python3.7中安装openCV库的方法

    这篇文章主要介绍了Python3.7中安装openCV库的方法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-07-07
  • Python如何为图片添加水印

    Python如何为图片添加水印

    这篇文章主要介绍了Python如何使用Python-Pillow库给图片添加水印的方法,非常的简单实用,有需要的小伙伴可以参考下
    2016-11-11
  • python 接口实现 供第三方调用的例子

    python 接口实现 供第三方调用的例子

    今天小编就为大家分享一篇python 接口实现 供第三方调用的例子,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-08-08
  • Python机器学习之SVM支持向量机

    Python机器学习之SVM支持向量机

    这篇文章主要为大家详细介绍了Python机器学习之SVM支持向量机,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • python flask中静态文件的管理方法

    python flask中静态文件的管理方法

    下面小编就为大家分享一篇python flask中静态文件的管理方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • 解决pygal.style的LightColorizedStyle参数问题

    解决pygal.style的LightColorizedStyle参数问题

    这篇文章主要介绍了解决pygal.style的LightColorizedStyle参数问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-07-07
  • 深入解析Python中filter函数的使用

    深入解析Python中filter函数的使用

    在Python中,filter函数是一种内置的高阶函数,它能够接受一个函数和一个迭代器,然后返回一个新的迭代器,本文主要来介绍一下Python中filter函数的具体用法,需要的可以参考一下
    2023-07-07
  • 详解Python连接MySQL数据库的多种方式

    详解Python连接MySQL数据库的多种方式

    这篇文章主要介绍了Python连接MySQL数据库方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • 以tensorflow库为例讲解Pycharm中如何更新第三方库

    以tensorflow库为例讲解Pycharm中如何更新第三方库

    这篇文章主要介绍了以tensorflow库为例讲解Pycharm中如何更新第三方库,文章介绍有详细流程,需要的小伙伴可以参考一下,希望对你的学习工作有所帮助
    2022-03-03
  • python如何判断IP地址合法性

    python如何判断IP地址合法性

    这篇文章主要为大家详细介绍了python如何判断IP地址合法性,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04

最新评论