先安装依赖库dlib、face_recognition、cv2
下载wheel文件:
python3.6:
dlib-19.7.0-cp36-cp36m-win_amd64.whl: https://drfs.ctcontents.com/file/1445568/768652503/68cb5d/Python/dlib-19.7.0-cp36-cp36m-win_amd64.whl
python3.7:
dlib-19.17.99-cp37-cp37m-win_amd64.whl: https://drfs.ctcontents.com/file/1445568/768652504/b726a5/Python/dlib-19.17.99-cp37-cp37m-win_amd64.whl
python3.8:
dlib-19.19.0-cp38-cp38-win_amd64.whl.whl: https://drfs.ctcontents.com/file/1445568/768652508/77e657/Python/dlib-19.19.0-cp38-cp38-win_amd64.whl.whl
再使用pip安装face_recognition、cv2
pip install opencv-python
pip install face-recognition
比较两张图片
![](https://i-blog.csdnimg.cn/blog_migrate/08f3d6b6b837834a9a23296a0a09eec9.png)
import cv2
import face_recognition
def find_face_encodings(image_path):
# reading image
image = cv2.imread(image_path)
# get face encodings from the image
face_enc = face_recognition.face_encodings(image)
# return face encodings
return face_enc[0]
# getting face encodings for first image
image_1 = find_face_encodings("image_1.png")
# getting face encodings for second image
image_2 = find_face_encodings("image_2.png")
# checking both images are same
is_same = face_recognition.compare_faces([image_1], image_2)[0]
print(f"Is Same: {is_same}")
if is_same:
# finding the distance level between images
distance = face_recognition.face_distance([image_1], image_2)
distance = round(distance[0] * 100)
# calcuating accuracy level between images
accuracy = 100 - round(distance)
print("The images are same")
print(f"Accuracy Level: {accuracy}%")
else:
print("The images are not same")
输出:
Is Same: True
The images are same
Accuracy Level: 70%