欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用python?AI快速比對兩張人臉圖像及遇到的坑

 更新時間:2023年02月24日 08:34:31   作者:Python?集中營  
這篇文章主要介紹了如何使用python?AI快速比對兩張人臉圖像?實(shí)現(xiàn)過程比較簡單,但是第三方python依賴的安裝過程較為曲折,下面是通過實(shí)踐對比總結(jié)出來的能夠支持的幾個版本,避免大家踩坑,需要的朋友可以參考下

本篇文章的代碼塊的實(shí)現(xiàn)主要是為了能夠快速的通過python第三方非標(biāo)準(zhǔn)庫對比出兩張人臉是否一樣。

實(shí)現(xiàn)過程比較簡單,但是第三方python依賴的安裝過程較為曲折,下面是通過實(shí)踐對比總結(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í)際情況設(shè)置,它會直接影響整個比對過程的結(jié)果。

到此這篇關(guān)于如何使用python AI快速比對兩張人臉圖像?的文章就介紹到這了,更多相關(guān)python AI快速比對兩張人臉圖像內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論