做网站带源码软件,怀化订水网站,博客网站的建设流程,济南个人网站建设一、人脸对比
1.定义全局变量来存储选择的图片路径和标签
save_image1 None
save_image2 None
image_label1 None
image_label2 None2.定义了一个名为compare_faces的函数#xff0c;用于比较两张图片中的人脸是否相似
def compare_faces():if save_image1 and save_im…一、人脸对比
1.定义全局变量来存储选择的图片路径和标签
save_image1 None
save_image2 None
image_label1 None
image_label2 None2.定义了一个名为compare_faces的函数用于比较两张图片中的人脸是否相似
def compare_faces():if save_image1 and save_image2:2.1加载图片并转换为RGB
img1 face_recognition.load_image_file(save_image1)img2 face_recognition.load_image_file(save_image2)2.2提取人脸编码
face1_encodings face_recognition.face_encodings(img1)face2_encodings face_recognition.face_encodings(img2)2.3如果没有检测到人脸返回相似度为0
if not face1_encodings or not face2_encodings:return 0.02.4比较人脸编码
face1_encoding face1_encodings[0]face2_encoding face2_encodings[0]2.5计算相似度
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请先选择两张图片)3.选择文件和单人脸提取中的选择文件部分一致
4.定义了一个名为display_image的函数用于在图形用户界面GUI上显示图像
def display_image(image_path, label_var):global image_label1, image_label24.1根据传入的label_var来设置正确的标签
if label_var 1:current_label image_label1
else:current_label image_label24.2如果之前已经有一个标签先销毁它
if current_label:current_label.destroy()image Image.open(image_path)
image image.resize((250, 300), Image.Resampling.LANCZOS)
photo ImageTk.PhotoImage(image)4.3创建新的标签并显示图片
current_label Label(win, imagephoto)
current_label.image photo # 保持对PhotoImage对象的引用防止被垃圾回收
current_label.place(x60 if label_var 1 else 360, y150)4.4更新全局变量
if label_var 1:image_label1 current_label
else:image_label2 current_label5.定义选择文件函数前几篇文章有做过详细介绍
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_image2if label_var 1:save_image1 file_pathelse:save_image2 file_pathdisplay_image(file_path, label_var)return select_image运行结果 这是对不同人脸进行作比较 这是同一张人脸不同角度的对比结果