赞
踩
save_image1 = None
save_image2 = None
image_label1 = None
image_label2 = None
def compare_faces():
if save_image1 and save_image2:
img1 = face_recognition.load_image_file(save_image1)
img2 = face_recognition.load_image_file(save_image2)
face1_encodings = face_recognition.face_encodings(img1)
face2_encodings = face_recognition.face_encodings(img2)
if not face1_encodings or not face2_encodings:
return 0.0
face1_encoding = face1_encodings[0]
face2_encoding = face2_encodings[0]
similarity = face_recognition.compare_faces([face1_encoding], face2_encoding)[0]
# 注意:compare_faces返回的是布尔值,我们需要计算相似度分数(如果需要的话)
# 这里使用compare_faces的默认行为,即返回是否相似的布尔值
# 若要计算具体相似度分数,则需要使用其他方法(如余弦相似度等)
if similarity:
b5.config(text="人脸相似")
else:
b5.config(text="人脸不相似")
else:
b5.config(text="请先选择两张图片!")
def display_image(image_path, label_var):
global image_label1, image_label2
if label_var == 1:
current_label = image_label1
else:
current_label = image_label2
if current_label:
current_label.destroy()
image = Image.open(image_path)
image = image.resize((250, 300), Image.Resampling.LANCZOS)
photo = ImageTk.PhotoImage(image)
current_label = Label(win, image=photo)
current_label.image = photo # 保持对PhotoImage对象的引用,防止被垃圾回收
current_label.place(x=60 if label_var == 1 else 360, y=150)
if label_var == 1:
image_label1 = current_label
else:
image_label2 = current_label
def s1(label_var): def select_image(): #定义选择图片的函数 #指明label_var不是一个局部变量也不是全局变量,而是外部函数s1的参数。这样,select_image函数就可以修改s1函数的参数 nonlocal label_var #弹出选择文件的对话框,以及被选择文件的类型 file_path = filedialog.askopenfilename(title="选择图片", filetypes=(("图片文件", "*.png;*.jpg;*.jpeg;*.bmp"), ("所有文件", "*.*"))) #如果选择了文件 if file_path: global save_image1, save_image2 #这是一个条件判断,根据label_var的值来决定将用户选择的文件路径赋值给哪一个全局变量。如果label_var等于1,则将文件路径赋值给save_image1;否则,赋值给save_image2 if label_var == 1: save_image1 = file_path else: save_image2 = file_path display_image(file_path, label_var) return select_image
运行结果:
这是对不同人脸进行作比较
这是同一张人脸,不同角度的对比结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。