使用python?AI快速比對兩張人臉圖像及遇到的坑
本篇文章的代碼塊的實現(xiàn)主要是為了能夠快速的通過python第三方非標(biāo)準(zhǔn)庫對比出兩張人臉是否一樣。
實現(xiàn)過程比較簡單,但是第三方python依賴的安裝過程較為曲折,下面是通過實踐對比總結(jié)出來的能夠支持的幾個版本,避免大家踩坑。
python版本:3.6.8 dlib版本:19.7.0 face-recognition版本:0.1.10
開始之前,我們選擇使用pip的方式對第三方的非標(biāo)準(zhǔn)庫進(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兩個模塊導(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
新建一個python函數(shù)get_face_encodings,用來獲取人臉部分的編碼,后面可以根據(jù)這個編碼來進(jìn)行人臉比對。
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ù)中注釋都是通過Pycharm插件自動生成的,接下來我們直接調(diào)用get_face_encodings函數(shù)分別獲取兩個人臉的編碼。
# 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)獲取到了對應(yīng)的人臉編碼。接著使用compare_faces函數(shù)進(jìn)行人臉比對。
# 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('人臉比對結(jié)果:{}'.format(is_same))
人臉比對結(jié)果:False
這個時候人臉比對結(jié)果已經(jīng)出來了,F(xiàn)alse代表不一樣。這里compare_faces有一個比較重要的參數(shù)就是tolerance=0.3,默認(rèn)情況下是0.6。
tolerance參數(shù)的值越小的時候代表比對要求更加嚴(yán)格,因此這個參數(shù)的大小需要根據(jù)實際情況設(shè)置,它會直接影響整個比對過程的結(jié)果。
到此這篇關(guān)于如何使用python AI快速比對兩張人臉圖像?的文章就介紹到這了,更多相關(guān)python AI快速比對兩張人臉圖像內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Django怎么在admin后臺注冊數(shù)據(jù)庫表
這篇文章主要介紹了Django怎么在admin后臺注冊數(shù)據(jù)庫表,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-11-11Python自動化運維之Ansible定義主機(jī)與組規(guī)則操作詳解
這篇文章主要介紹了Python自動化運維之Ansible定義主機(jī)與組規(guī)則操作,結(jié)合實例形式分析了自動化運維工具Ansible定義主機(jī)與組規(guī)則相關(guān)配置操作與注意事項,需要的朋友可以參考下2019-06-06淺談OpenCV中的新函數(shù)connectedComponentsWithStats用法
這篇文章主要介紹了淺談OpenCV中的新函數(shù)connectedComponentsWithStats用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07python 實現(xiàn)非極大值抑制算法(Non-maximum suppression, NMS)
這篇文章主要介紹了python 如何實現(xiàn)非極大值抑制算法(Non-maximum suppression, NMS),幫助大家更好的進(jìn)行機(jī)器學(xué)習(xí),感興趣的朋友可以了解下2020-10-10Python基于easygui實現(xiàn)pdf和word轉(zhuǎn)換小程序
這篇文章主要為大家詳細(xì)介紹了Python如何基于easygui實現(xiàn)pdf和word轉(zhuǎn)換小程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-04-04