Python如何使用face_recognition实现AI识别图片中的人物(ai,python,开发技术)

时间:2024-04-29 05:44:48 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

环境安装

首先我们看看官方给出的人脸识别效果图

Python如何使用face_recognition实现AI识别图片中的人物

我们看一下README关于安装环境的信息

Python如何使用face_recognition实现AI识别图片中的人物

官方给出的可安装操作系统是Mac和Linux,但是我想在windows安装,继续往下看。

Python如何使用face_recognition实现AI识别图片中的人物

windows虽然不是官方支持,但是也能装,不就是个dlib吗?好的,那就开始装。

我们直接安装requirements_dev.txt,这里要注意,把pip去掉。

Python如何使用face_recognition实现AI识别图片中的人物

注意一点安装dlib的时候会报错,需要先安装cmake,安装命令如下:

pipinstallcmake-ihttps://pypi.douban.com/simple

除此之外,项目还需要安装opencv-python,安装命令如下:

pipinstallopencv-python-ihttps://pypi.douban.com/simple

代码使用

先做一下说明,在使用face_recognition运行的时候,可以选择安装face_recognition命令进行运行的模式,也可以使用face_recognition模块构建代码运行。为了二次开发,我还是先试试代码的方式,主要试试人脸识别模块。

官方代码如下:

importface_recognition#Loadthejpgfilesintonumpyarraysbiden_image=face_recognition.load_image_file("biden.jpg")obama_image=face_recognition.load_image_file("obama.jpg")unknown_image=face_recognition.load_image_file("obama2.jpg")#Getthefaceencodingsforeachfaceineachimagefile#Sincetherecouldbemorethanonefaceineachimage,itreturnsalistofencodings.#ButsinceIknoweachimageonlyhasoneface,Ionlycareaboutthefirstencodingineachimage,soIgrabindex0.try:biden_face_encoding=face_recognition.face_encodings(biden_image)[0]obama_face_encoding=face_recognition.face_encodings(obama_image)[0]unknown_face_encoding=face_recognition.face_encodings(unknown_image)[0]exceptIndexError:print("Iwasn'tabletolocateanyfacesinatleastoneoftheimages.Checktheimagefiles.Aborting...")quit()known_faces=[biden_face_encoding,obama_face_encoding]#resultsisanarrayofTrue/Falsetellingiftheunknownfacematchedanyoneintheknown_facesarrayresults=face_recognition.compare_faces(known_faces,unknown_face_encoding)print("IstheunknownfaceapictureofBiden?{}".format(results[0]))print("IstheunknownfaceapictureofObama?{}".format(results[1]))print("Istheunknownfaceanewpersonthatwe'veneverseenbefore?{}".format(notTrueinresults))

代码说明:

1、首先可以看到将两个人脸的数据加到了known_faces列表内。

2、然后用未知图数据进行识别判断。

看一下加入到known_faces的照片

Python如何使用face_recognition实现AI识别图片中的人物

看一下需要识别的照片

Python如何使用face_recognition实现AI识别图片中的人物

看一下执行结果

Python如何使用face_recognition实现AI识别图片中的人物

我们可以看到在拜登的识别中提示false,在奥巴马识别中提示true。这里要注意一点,我们看一下compare_faces方法参数。

Python如何使用face_recognition实现AI识别图片中的人物

参数tolerance最佳为0.6,越低越严格,所以可以按照自己的需求调整。

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Python如何使用face_recognition实现AI识别图片中的人物的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:如何使用Python实现识别图像中人物下一篇:

7 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18