您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“如何使用python AI快速比對(duì)兩張人臉圖像”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“如何使用python AI快速比對(duì)兩張人臉圖像”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
實(shí)現(xiàn)過程比較簡單,但是第三方python依賴的安裝過程較為曲折,下面是通過實(shí)踐對(duì)比總結(jié)出來的能夠支持的幾個(gè)版本,避免大家踩坑。
python版本:3.6.8 dlib版本:19.7.0 face-recognition版本:0.1.10
開始之前,我們選擇使用pip的方式對(duì)第三方的非標(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兩個(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,用來獲取人臉部分的編碼,后面可以根據(jù)這個(gè)編碼來進(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ù)中注釋都是通過Pycharm插件自動(dòng)生成的,接下來我們直接調(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)出來了,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ì)過程的結(jié)果。
讀到這里,這篇“如何使用python AI快速比對(duì)兩張人臉圖像”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。