脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Python - 使用python AI快速比对两张人脸图像及遇到的坑

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

2023-03-04 13:47Python 集中营 Python

这篇文章主要介绍了如何使用python AI快速比对两张人脸图像?实现过程比较简单,但是第三方python依赖的安装过程较为曲折,下面是通过实践对比总结出来的能够支持的几个版本,避免大家踩坑,需要的朋友可以参考下

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

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

?
1
2
3
python版本:3.6.8
dlib版本:19.7.0
face-recognition版本:0.1.10

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

?
1
2
3
4
5
6
7
pip install cmake
 
pip install dlib==19.7.0
 
pip install face-recognition==0.1.10
 
pip install opencv-python

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

?
1
2
3
4
5
# 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,用来获取人脸部分的编码,后面可以根据这个编码来进行人脸比对。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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函数分别获取两个人脸的编码。

?
1
2
3
4
5
6
7
8
9
10
11
# 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函数进行人脸比对。

?
1
2
3
4
# 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快速比对两张人脸图像内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/chengxuyuan_110/article/details/129169424

延伸 · 阅读

精彩推荐
  • PythonPython实现小黑屋游戏的完整实例

    Python实现小黑屋游戏的完整实例

    这篇文章主要给大家介绍了关于Python实现小黑屋游戏的完整实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需...

    M.G~12782021-08-22
  • Python写好Python代码的几条重要技巧

    写好Python代码的几条重要技巧

    好的代码具有易理解、可扩展、易维护的特点,简直是人见人爱。本文就将介绍写好python代码的多个技巧...

    Python开发者12232021-11-10
  • PythonPython进阶_关于命名空间与作用域(详解)

    Python进阶_关于命名空间与作用域(详解)

    下面小编就为大家带来一篇Python进阶_关于命名空间与作用域(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    脚本之家2882020-11-13
  • Pythonpython3.5的包存放的具体路径

    python3.5的包存放的具体路径

    在本篇内容里小编给大家分享了一篇关于python3.5的包存放的具体路径相关内容,有需要的朋友们可以参考下。 ...

    FXL2042020-08-17
  • PythonPython及Django框架生成二维码的方法分析

    Python及Django框架生成二维码的方法分析

    这篇文章主要介绍了Python及Django框架生成二维码的方法,结合实例形式分析了Python及Django框架使用qrcode包实现二维码生成功能的相关操作技巧,需要的朋友可...

    刘元涛8042021-01-10
  • Pythonpython如何对实例属性进行类型检查

    python如何对实例属性进行类型检查

    这篇文章主要为大家详细介绍了python如何对实例属性进行类型检查,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    北门吹雪5142021-01-23
  • Pythonpandas.DataFrame 根据条件新建列并赋值的方法

    pandas.DataFrame 根据条件新建列并赋值的方法

    下面小编就为大家分享一篇pandas.DataFrame 根据条件新建列并赋值的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    魏之燕16372021-01-29
  • Pythonpytorch--之halfTensor的使用详解

    pytorch--之halfTensor的使用详解

    这篇文章主要介绍了pytorch--之halfTensor的使用详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...

    zxyhhjs201711522021-11-11