import cv2
import os
import sys

import face_recognition

def load_image(image_path):
“”“加载图像”“”
return face_recognition.load_image_file(image_path)

def detect_faces(image):
“”“检测图像中的人脸位置和编码”“”
face_locations = face_recognition.face_locations(image)
face_encodings = face_recognition.face_encodings(image, face_locations)
return face_locations, face_encodings

def compare_faces(original_face_encodings, target_face_encodings):
“”“比较人脸编码,判断是否存在匹配”“”
for original_encoding in original_face_encodings:
matches = face_recognition.compare_faces(target_face_encodings, original_encoding)
if any(matches):
return True
return False

def main():
current_directory = os.getcwd()

# 原始图像路径
# original_image_path = current_directory+"\\22.png"
original_image_path = current_directory+"\\视频截图.jpg"
# 目标图像路径
target_image_path = current_directory+"\\头像.png"

if len(sys.argv) >= 2:
    original_image_path =""
    target_image_path =""
    original_image_path = sys.argv[1]
    target_image_path = sys.argv[2]

# 加载图像
original_image = load_image(original_image_path)
target_image = load_image(target_image_path)

# 检测人脸
_, original_face_encodings = detect_faces(original_image)
_, target_face_encodings = detect_faces(target_image)

# 比较人脸
if compare_faces(original_face_encodings, target_face_encodings):
    #"原图片中的人脸存在于目标图片中。"
    return 1
else:
    #原图片中的人脸不存在于目标图片中
    return 0

if name == “main”:
result = main()
print(result)

更多推荐