使用python?AI快速比對(duì)兩張人臉圖像及遇到的坑
本篇文章的代碼塊的實(shí)現(xiàn)主要是為了能夠快速的通過(guò)python第三方非標(biāo)準(zhǔn)庫(kù)對(duì)比出兩張人臉是否一樣。
實(shí)現(xiàn)過(guò)程比較簡(jiǎn)單,但是第三方python依賴的安裝過(guò)程較為曲折,下面是通過(guò)實(shí)踐對(duì)比總結(jié)出來(lái)的能夠支持的幾個(gè)版本,避免大家踩坑。
python版本:3.6.8 dlib版本:19.7.0 face-recognition版本:0.1.10
開(kāi)始之前,我們選擇使用pip的方式對(duì)第三方的非標(biāo)準(zhǔn)庫(kù)進(jìn)行安裝。
pip install cmake pip install dlib==19.7.0 pip install face-recognition==0.1.10 pip install opencv-python
然后,將使用到的模塊cv2/face-recognition兩個(gè)模塊導(dǎo)入到代碼塊中即可。
# 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
新建一個(gè)python函數(shù)get_face_encodings,用來(lái)獲取人臉部分的編碼,后面可以根據(jù)這個(gè)編碼來(lái)進(jìn)行人臉比對(duì)。
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]上述函數(shù)中注釋都是通過(guò)Pycharm插件自動(dòng)生成的,接下來(lái)我們直接調(diào)用get_face_encodings函數(shù)分別獲取兩個(gè)人臉的編碼。
# 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')上面我們選擇了兩張附有人臉的圖片,并且已經(jīng)獲取到了對(duì)應(yīng)的人臉編碼。接著使用compare_faces函數(shù)進(jìn)行人臉比對(duì)。
# 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('人臉比對(duì)結(jié)果:{}'.format(is_same))人臉比對(duì)結(jié)果:False
這個(gè)時(shí)候人臉比對(duì)結(jié)果已經(jīng)出來(lái)了,F(xiàn)alse代表不一樣。這里compare_faces有一個(gè)比較重要的參數(shù)就是tolerance=0.3,默認(rèn)情況下是0.6。
tolerance參數(shù)的值越小的時(shí)候代表比對(duì)要求更加嚴(yán)格,因此這個(gè)參數(shù)的大小需要根據(jù)實(shí)際情況設(shè)置,它會(huì)直接影響整個(gè)比對(duì)過(guò)程的結(jié)果。
到此這篇關(guān)于如何使用python AI快速比對(duì)兩張人臉圖像?的文章就介紹到這了,更多相關(guān)python AI快速比對(duì)兩張人臉圖像內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 接口實(shí)現(xiàn) 供第三方調(diào)用的例子
今天小編就為大家分享一篇python 接口實(shí)現(xiàn) 供第三方調(diào)用的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08
Python機(jī)器學(xué)習(xí)之SVM支持向量機(jī)
這篇文章主要為大家詳細(xì)介紹了Python機(jī)器學(xué)習(xí)之SVM支持向量機(jī),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
解決pygal.style的LightColorizedStyle參數(shù)問(wèn)題
這篇文章主要介紹了解決pygal.style的LightColorizedStyle參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
詳解Python連接MySQL數(shù)據(jù)庫(kù)的多種方式
這篇文章主要介紹了Python連接MySQL數(shù)據(jù)庫(kù)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
以tensorflow庫(kù)為例講解Pycharm中如何更新第三方庫(kù)
這篇文章主要介紹了以tensorflow庫(kù)為例講解Pycharm中如何更新第三方庫(kù),文章介紹有詳細(xì)流程,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)工作有所幫助2022-03-03

