基于Python实现人脸自动戴口罩系统


Posted in Python onFebruary 06, 2020

1、项目背景

2019年新型冠状病毒感染的肺炎疫情发生以来,牵动人心,举国哀痛,口罩、酒精、消毒液奇货可居。

基于Python实现人脸自动戴口罩系统

抢不到口罩,怎么办?作为技术人今天分享如何使用Python实现自动戴口罩系统,来安慰自己,系统效果如下所示:

基于Python实现人脸自动戴口罩系统

本系统的实现原理是借助 Dlib模块的Landmark人脸68个关键点检测库轻松识别出人脸五官数据,根据这些数据,确定嘴唇部分的位置数据(48点~67点位置),根据检测到嘴部的尺寸和方向,借助PLL模块调整口罩的尺寸和方向,实现将口罩放在图像的适当位置。

基于Python实现人脸自动戴口罩系统

2、页面设计

 基于tkinter模块实现GUI设计,可载入人物图像,选择四种类型口罩(这里的口罩是处理好的图片),展示佩戴好口罩的效果,操作完成退出系统,效果如下所示:

基于Python实现人脸自动戴口罩系统

页面布局实现代码如下所示:

def __init__(self):
 self.root = tk.Tk()
 self.root.title('基于Pyhon的人脸自动戴口罩系统')
 self.root.geometry('1200x500')
 self.path1_ = None
 self.path2_ = None
 self.seg_img_path = None
 self.mask = None
 self.label_Img_seg = None
 decoration = PIL.Image.open('./pic/bg.png').resize((1200, 500))
 render = ImageTk.PhotoImage(decoration)
 img = tk.Label(image=render)
 img.image = render
 img.place(x=0, y=0)
 # 原图1的展示
 tk.Button(self.root, text="打开头像", 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="头像", 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.Label(self.root)
 self.label_Img_original1.place(x=180, y=150)
 tk.Label(self.root,text="选择口罩",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("<Button-1>", 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, width=60, height=60)
 self.second_pic.bind("<Button-1>", 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, width=60, height=60)
 self.third_pic.bind("<Button-1>", self.mask3)
 forth_pic = Image.open("./pic/Mask4.png")
 forth_pic = forth_pic.resize((60, 60), Image.ANTIALIAS)
 forth_pic = ImageTk.PhotoImage(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("<Button-1>", 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展示
 def show_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) # 调整图片大小至256x256
 img_png_original = ImageTk.PhotoImage(Img)
 self.label_Img_original1.config(image=img_png_original)
 self.label_Img_original1.image = img_png_original # keep a reference
 self.cv_orinial1.create_image(5, 5,anchor='nw', image=img_png_original)

人脸戴口罩展示,实现代码如下所示

# 人脸戴口罩效果展示
 def show_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) # 调整图片大小至270x270
 img_png_seg = ImageTk.PhotoImage(Img)
 self.label_Img_seg.config(image=img_png_seg)
 self.label_Img_seg.image = img_png_seg # keep a reference

导入四种口罩图像,实现代码如下所示:   

def mask0(self, event):
 self.mask = Image.open('pic/mask.png')
 self.show_morpher_pic()
 def mask1(self, event):
 self.mask = Image.open('pic/mask1.png')
 self.show_morpher_pic()
 def mask3(self, event):
 self.mask = Image.open('pic/mask3.png')
 self.show_morpher_pic()
 def mask4(self, event):
 self.mask = Image.open('pic/mask4.png')
 self.show_morpher_pic()

3、器官识别

页面功能实现后就是依托Dlib库实现人脸器官关键点的识别,分析出嘴部位置及尺寸,这里为了方便各位直观了解,写了一个测试Demo,将人物脸部关键点都显示出来,代码如下所示:

#coding=utf-8
#图片检测 - Dlib版本
import cv2
import dlib
import time
t=time.time()
path = "./pic/im.jpg"
img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 
#人脸分类器
detector = dlib.get_frontal_face_detector()
# 获取人脸检测器
predictor = dlib.shape_predictor(
 "./shape_predictor_68_face_landmarks.dat"
)
dets = detector(gray, 1)
for face in dets:
 shape = predictor(img, face) # 寻找人脸的68个标定点
 # 遍历所有点,打印出其坐标,并圈出来
 for pt in 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)

效果如下所示: 

基于Python实现人脸自动戴口罩系统

在本系统中这些关键点无需绘制显示,直接使用就可以,实现代码如下所示:

def get_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)
 for k, d in enumerate(faces):
 x = []
 y = []
 # 人脸大小的高度
 height = d.bottom() - d.top()
 # 人脸大小的宽度
 width = d.right() - d.left()
 shape = predictor(img_gray, d)
 # 48-67 为嘴唇部分
 for i in range(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))
 return x_min, x_max, y_min, y_max, size

4、退出系统

退出系统非常简单,一行Demo即可实现,如下所示:

def quit(self):
 self.root.destroy()

总结

以上所述是小编给大家介绍的基于Python实现人脸自动戴口罩系统,希望对大家有所帮助!

Python 相关文章推荐
python之matplotlib学习绘制动态更新图实例代码
Jan 23 Python
对numpy中的数组条件筛选功能详解
Jul 02 Python
Django框架用户注销功能实现方法分析
May 28 Python
聊聊python里如何用Borg pattern实现的单例模式
Jun 06 Python
flask框架路由常用定义方式总结
Jul 23 Python
tensorflow 模型权重导出实例
Jan 24 Python
Python 序列化和反序列化库 MarshMallow 的用法实例代码
Feb 25 Python
解决Python 异常TypeError: cannot concatenate 'str' and 'int' objects
Apr 08 Python
keras 如何保存最佳的训练模型
May 25 Python
python进行OpenCV实战之画图(直线、矩形、圆形)
Aug 27 Python
Python调用SMTP服务自动发送Email的实现步骤
Feb 07 Python
python FTP编程基础入门
Feb 27 Python
python时间日期操作方法实例小结
Feb 06 #Python
python通过matplotlib生成复合饼图
Feb 06 #Python
tensorflow 初始化未初始化的变量实例
Feb 06 #Python
python中count函数简单的实例讲解
Feb 06 #Python
tensorflow之变量初始化(tf.Variable)使用详解
Feb 06 #Python
使用TensorFlow搭建一个全连接神经网络教程
Feb 06 #Python
详解python 降级到3.6终极解决方案
Feb 06 #Python
You might like
PHP 选项及相关信息函数库
2006/12/04 PHP
php将mysql数据库整库导出生成sql文件的具体实现
2014/01/08 PHP
php实现每天自动变换随机问候语的方法
2015/05/12 PHP
joomla组件开发入门教程
2016/05/04 PHP
两个SUBMIT按钮,如何区分处理
2006/08/22 Javascript
分享20款好玩的jQuery游戏
2011/04/17 Javascript
轻松创建nodejs服务器(9):实现非阻塞操作
2014/12/18 NodeJs
jQuery Validate表单验证插件 添加class属性形式的校验
2016/01/18 Javascript
JavaScript数组方法总结分析
2016/05/06 Javascript
Vue CLI3搭建的项目中路径相关问题的解决
2018/09/17 Javascript
小程序点击图片实现自动播放视频
2020/05/29 Javascript
JS页面获取 session 值,作用域和闭包学习笔记
2019/10/16 Javascript
[00:37]DOTA2上海特级锦标赛 OG战队宣传片
2016/03/03 DOTA
[35:55]完美世界DOTA2联赛PWL S3 Rebirth vs CPG 第一场 12.11
2020/12/13 DOTA
python魔法方法-属性访问控制详解
2016/07/25 Python
Python正则表达式教程之一:基础篇
2017/03/02 Python
python web.py开发httpserver解决跨域问题实例解析
2018/02/12 Python
python绘制立方体的方法
2018/07/02 Python
Python3对称加密算法AES、DES3实例详解
2018/12/06 Python
Python中类似于jquery的pyquery库用法分析
2019/12/02 Python
python装饰器的特性原理详解
2019/12/25 Python
python pandas.DataFrame.loc函数使用详解
2020/03/26 Python
对python pandas中 inplace 参数的理解
2020/06/27 Python
Python读取ini配置文件传参的简单示例
2021/01/05 Python
CSS3悬停效果案例应用
2012/11/21 HTML / CSS
button在IE6/7下的黑边去除方案
2012/12/24 HTML / CSS
CSS3绘制圆角矩形的简单示例
2015/09/28 HTML / CSS
HTML5自定义mp3播放器源码
2020/01/06 HTML / CSS
Bealls Florida百货商店:生活服饰、家居装饰和鞋子
2018/02/23 全球购物
Holland & Barrett爱尔兰:英国领先的健康零售商
2019/03/31 全球购物
俄罗斯外国汽车和国产汽车配件网上商店:Движком
2020/04/19 全球购物
介绍一下.net和Java的特点和区别
2012/09/26 面试题
什么是测试驱动开发(TDD)
2012/02/15 面试题
应届大学生求职信
2013/12/01 职场文书
警察思想汇报
2014/01/04 职场文书
公务员爱岗敬业演讲稿
2014/08/26 职场文书