页面布局实现代码如下:def__init__(self):self.root=tk.Tk()self.root.title('基于Pyhon的人脸自动佩戴口罩系统')self.root.geometry('1200x500')self.path1_=Noneself.path2_=Noneself.seg_img_path=Noneself.mask=Noneself.label_Img_seg=Nonedecoration=PIL.Image.open('./pic/bg.png').resize((1200,500))render=ImageTk.PhotoImage(decoration)img=tk.Label(image=render)img.image=renderimg.place(x=0,y=0)#显示原图1tk.Button(self.root,text="openAvatar",command=self.show_original1_pic).place(x=50,y=120)tk.Button(self.root,text="退出软件",command=quit).place(x=900,y=40)tk.Label(self.root,text="Avatar",font=10).place(x=280,y=120)self.cv_orinial1=tk.Canvas(self.root,bg='white',width=270,height=270)self.cv_orinial1.create_rectangle(8,8,260,260,width=1,outline='red')self.cv_orinial1.place(x=180,y=150)self.label_Img_original1=tk.标签(self.root)self.label_Img_original1.place(x=180,y=150)tk.Label(self.root,text="selectmask",font=10).place(x=600,y=120)first_pic=Image.open("./pic/Mask.png")first_pic=first_pic.resize((60,60),Image.ANTIALIAS)first_pic=ImageTk.PhotoImage(first_pic)self.first=tk.Label(self.root,image=first_pic)self.first.place(x=600,y=160,width=60,height=60)self.first.bind("",self.mask0)second_pic=Image.open("./pic/Mask1.png")second_pic=second_pic.resize((60,60),Image.ANTIALIAS)second_pic=ImageTk.PhotoImage(second_pic)self.second_pic=tk.Label(self.root,image=second_pic)self.second_pic.place(x=600,y=230,宽度=60,高度=60)self.second_pic.bind("",self.mask1)third_pic=Image.open("./pic/Mask3.png")third_pic=third_pic.resize((60,60),Image.ANTIALIAS)third_pic=ImageTk.PhotoImage(third_pic)self.third_pic=tk.Label(self.root,image=third_pic)self.third_pic.place(x=600,y=300,宽度=60,高度=60)self.third_pic.bind("",self.mask3)forth_pic=Image.open("./pic/Mask4.png")forth_pic=forth_pic.resize((60,60),Image.ANTIALIAS)forth_pic=ImageTk.照片图像(forth_pic)self.forth_pic=tk.Label(self.root,image=forth_pic)self.forth_pic.place(x=600,y=370,width=60,height=60)self.forth_pic.bind("",self.mask4)tk.Label(self.root,text="佩戴效果",font=10).place(x=920,y=120)self.cv_seg=tk.Canvas(self.root,bg='white',width=270,height=270)self.cv_seg.create_rectangle(8,8,260,260,width=1,outline='red')self.cv_seg.place(x=820,y=150)self.label_Img_seg=tk.Label(self.root)self.label_Img_seg.place(x=820,y=150)self.root.mainloop()加载角色图片,实现代码如下:#原图1显示defshow_original1_pic(self):self.path1_=askopenfilename(title='选择文件')print(self.path1_)self.Img=PIL.Image.open(r'{}'.format(self.path1_))Img=self.Img.resize((270,270),PIL.Image.ANTIALIAS)#调整图像大小为256x256img_png_original=ImageTk.PhotoImage(Img)self.label_Img_original1.config(image=img_png_original)self.label_Img_original1.image=img_png_original#保持参考self.cv_orinial1,create_anchor='nw',image=img_png_original)戴口罩的人脸显示,实现代码如下:#显示效果戴着面具defshow_morpher_pic(self):img1=cv2.imread(self.path1_)x_min,x_max,y_min,y_max,size=self.get_mouth(img1)adding=self.mask.resize(size)im=Image.fromarray(img1[:,:,::-1])#切换RGB格式#在合适位置添加头发图片im.paste(adding,(int(x_min),int(y_min)),adding)#im.show()save_path=self.path1_.split('.')[0]+'_result.jpg'im.save(save_path)Img=im.resize((270,270),PIL.Image.ANTIALIAS)#调整图片大小为270x270img_png_seg=ImageTk.PhotoImage(Img)self.label_Img_seg.config(image=img_png_seg)self.label_Img_seg.image=img_png_seg#保持引用导入四种mask图片,实现代码如下:defmask0(self,event):self。mask=Image.open('pic/mask.png')self.show_morpher_pic()defmask1(self,event):self.mask=Image.open('pic/mask1.png')self.show_morpher_pic()defmask3(自我,事件):self.mask=Image.open('pic/mask3.png')self.show_morpher_pic()defmask4(自我,事件):self.mask=Image.open('pic/mask4.png')自己。show_morpher_pic()器官识别页面的功能实现后,依托Dlib库实现了人脸器官关键点的识别,并分析出嘴巴的位置和大小。这里为了方便大家直观的理解,写了一个测试demo,里面使用的是人脸显示所有关键点,代码如下:coding=utf-8图片检测-dlib版本importcv2importdlibimporttimet=time.time()path="./pic/im.jpg"img=cv2.imread(path)gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)人脸分类器detector=dlib.get_frontal_face_detector()#getfacedetectorpredictor=dlib.shape_predictor("./shape_predictor_68_face_landmarks.dat")dets=detector(gray,1)forfaceindets:shape=predictor(img,face)#寻找人脸智汇代理应用的68个标定点http://www.kaifx.cn/broker/th...#遍历所有点并打印绘制其坐标和圆它们在shape.parts():pt_pos=(pt.x,pt.y)cv2.circle(img,pt_pos,1,(0,255,0),2)cv2.imshow("image",img)print('使用的时间是{}'.format(time.time()-t))cv2.waitKey(0)cv2.destroyAllWindows()time.sleep(5)在本系统中这些关键点不需要绘制显示,直接使用即可。实现代码如下:defget_mouth(self,img):img_gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)detector=dlib.get_frontal_face_detector()predictor=dlib.shape_predictor('./shape_predictor_68_face_landmarks.dat')faces=detector(img_gray,0)fork,dinenumerate(faces):x=[]y=[]#heightoffacesize=d.bottom()-d.top()#脸的宽度sizewidth=d.right()-d.left()shape=predictor(img_gray,d)#48-67是唇部foriinrange(48,68):x.append(shape.part(i).x)y.append(shape.part(i).y)#根据嘴唇的大小扩大对应嘴唇的蒙版面积面对y_max=(int)(max(y)+height/3)y_min=(int)(min(y)-height/3)x_max=(int)(max(x)+width/3)x_min=(int)(min(x)-width/3)size=((x_max-x_min),(y_max-y_min))returnx_min,x_max,y_min,y_max,size退出系统退出系统很简单,一行demo可以实现,如下图:defquit(self):self.root.destroy()
