程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

荐 基于Python的人脸自动戴口罩系统

发布于2020-02-10 17:24     阅读(1234)     评论(0)     点赞(19)     收藏(2)


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

项目描述参见:https://handsome-man.blog.csdn.net/article/details/104174562

立即下载

目录

1、项目背景

2、页面设计

3、器官识别

4、退出系统


1、项目背景

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

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

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

2、页面设计

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

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

  1. def __init__(self):
  2. self.root = tk.Tk()
  3. self.root.title('基于Pyhon的人脸自动戴口罩系统')
  4. self.root.geometry('1200x500')
  5. self.path1_ = None
  6. self.path2_ = None
  7. self.seg_img_path = None
  8. self.mask = None
  9. self.label_Img_seg = None
  10. decoration = PIL.Image.open('./pic/bg.png').resize((1200, 500))
  11. render = ImageTk.PhotoImage(decoration)
  12. img = tk.Label(image=render)
  13. img.image = render
  14. img.place(x=0, y=0)
  15. # 原图1的展示
  16. tk.Button(self.root, text="打开头像", command=self.show_original1_pic).place(x=50, y=120)
  17. tk.Button(self.root, text="退出软件", command=quit).place(x=900, y=40)
  18. tk.Label(self.root, text="头像", font=10).place(x=280, y=120)
  19. self.cv_orinial1 = tk.Canvas(self.root, bg='white', width=270, height=270)
  20. self.cv_orinial1.create_rectangle(8, 8, 260, 260, width=1, outline='red')
  21. self.cv_orinial1.place(x=180, y=150)
  22. self.label_Img_original1 = tk.Label(self.root)
  23. self.label_Img_original1.place(x=180, y=150)
  24. tk.Label(self.root,text="选择口罩",font=10).place(x=600,y=120)
  25. first_pic = Image.open("./pic/Mask.png")
  26. first_pic = first_pic.resize((60, 60), Image.ANTIALIAS)
  27. first_pic = ImageTk.PhotoImage(first_pic)
  28. self.first = tk.Label(self.root, image=first_pic)
  29. self.first.place(x=600,y=160, width=60, height=60)
  30. self.first.bind("<Button-1>", self.mask0)
  31. second_pic = Image.open("./pic/Mask1.png")
  32. second_pic = second_pic.resize((60, 60), Image.ANTIALIAS)
  33. second_pic = ImageTk.PhotoImage(second_pic)
  34. self.second_pic = tk.Label(self.root, image=second_pic)
  35. self.second_pic.place(x=600, y=230, width=60, height=60)
  36. self.second_pic.bind("<Button-1>", self.mask1)
  37. third_pic = Image.open("./pic/Mask3.png")
  38. third_pic = third_pic.resize((60, 60), Image.ANTIALIAS)
  39. third_pic = ImageTk.PhotoImage(third_pic)
  40. self.third_pic = tk.Label(self.root, image=third_pic)
  41. self.third_pic.place(x=600, y=300, width=60, height=60)
  42. self.third_pic.bind("<Button-1>", self.mask3)
  43. forth_pic = Image.open("./pic/Mask4.png")
  44. forth_pic = forth_pic.resize((60, 60), Image.ANTIALIAS)
  45. forth_pic = ImageTk.PhotoImage(forth_pic)
  46. self.forth_pic = tk.Label(self.root, image=forth_pic)
  47. self.forth_pic.place(x=600, y=370, width=60, height=60)
  48. self.forth_pic.bind("<Button-1>", self.mask4)
  49. tk.Label(self.root, text="佩戴效果", font=10).place(x=920, y=120)
  50. self.cv_seg = tk.Canvas(self.root, bg='white', width=270, height=270)
  51. self.cv_seg.create_rectangle(8, 8, 260, 260, width=1, outline='red')
  52. self.cv_seg.place(x=820, y=150)
  53. self.label_Img_seg = tk.Label(self.root)
  54. self.label_Img_seg.place(x=820, y=150)
  55. self.root.mainloop()

载入人物图像,实现代码如下所示:

  1. # 原图1展示
  2. def show_original1_pic(self):
  3. self.path1_ = askopenfilename(title='选择文件')
  4. print(self.path1_)
  5. self.Img = PIL.Image.open(r'{}'.format(self.path1_))
  6. Img = self.Img.resize((270,270),PIL.Image.ANTIALIAS) # 调整图片大小至256x256
  7. img_png_original = ImageTk.PhotoImage(Img)
  8. self.label_Img_original1.config(image=img_png_original)
  9. self.label_Img_original1.image = img_png_original # keep a reference
  10. self.cv_orinial1.create_image(5, 5,anchor='nw', image=img_png_original)

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

  1. # 人脸戴口罩效果展示
  2. def show_morpher_pic(self):
  3. img1 = cv2.imread(self.path1_)
  4. x_min, x_max, y_min, y_max, size = self.get_mouth(img1)
  5. adding = self.mask.resize(size)
  6. im = Image.fromarray(img1[:, :, ::-1]) # 切换RGB格式
  7. # 在合适位置添加头发图片
  8. im.paste(adding, (int(x_min), int(y_min)), adding)
  9. # im.show()
  10. save_path = self.path1_.split('.')[0]+'_result.jpg'
  11. im.save(save_path)
  12. Img = im.resize((270, 270), PIL.Image.ANTIALIAS) # 调整图片大小至270x270
  13. img_png_seg = ImageTk.PhotoImage(Img)
  14. self.label_Img_seg.config(image=img_png_seg)
  15. self.label_Img_seg.image = img_png_seg # keep a reference

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

  1. def mask0(self, event):
  2. self.mask = Image.open('pic/mask.png')
  3. self.show_morpher_pic()
  4. def mask1(self, event):
  5. self.mask = Image.open('pic/mask1.png')
  6. self.show_morpher_pic()
  7. def mask3(self, event):
  8. self.mask = Image.open('pic/mask3.png')
  9. self.show_morpher_pic()
  10. def mask4(self, event):
  11. self.mask = Image.open('pic/mask4.png')
  12. self.show_morpher_pic()

3、器官识别

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

  1. #coding=utf-8
  2. #图片检测 - Dlib版本
  3. import cv2
  4. import dlib
  5. import time
  6. t=time.time()
  7. path = "./pic/im.jpg"
  8. img = cv2.imread(path)
  9. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  10. #人脸分类器
  11. detector = dlib.get_frontal_face_detector()
  12. # 获取人脸检测器
  13. predictor = dlib.shape_predictor(
  14. "./shape_predictor_68_face_landmarks.dat"
  15. )
  16. dets = detector(gray, 1)
  17. for face in dets:
  18. shape = predictor(img, face) # 寻找人脸的68个标定点
  19. # 遍历所有点,打印出其坐标,并圈出来
  20. for pt in shape.parts():
  21. pt_pos = (pt.x, pt.y)
  22. cv2.circle(img, pt_pos, 1, (0, 255, 0), 2)
  23. cv2.imshow("image", img)
  24. print('所用时间为{}'.format(time.time()-t))
  25. cv2.waitKey(0)
  26. #cv2.destroyAllWindows()
  27. time.sleep(5)

效果如下所示: 

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

  1. def get_mouth(self, img):
  2. img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  3. detector = dlib.get_frontal_face_detector()
  4. predictor = dlib.shape_predictor('./shape_predictor_68_face_landmarks.dat')
  5. faces = detector(img_gray, 0)
  6. for k, d in enumerate(faces):
  7. x = []
  8. y = []
  9. # 人脸大小的高度
  10. height = d.bottom() - d.top()
  11. # 人脸大小的宽度
  12. width = d.right() - d.left()
  13. shape = predictor(img_gray, d)
  14. # 48-67 为嘴唇部分
  15. for i in range(48, 68):
  16. x.append(shape.part(i).x)
  17. y.append(shape.part(i).y)
  18. # 根据人脸的大小扩大嘴唇对应口罩的区域
  19. y_max = (int)(max(y) + height / 3)
  20. y_min = (int)(min(y) - height / 3)
  21. x_max = (int)(max(x) + width / 3)
  22. x_min = (int)(min(x) - width / 3)
  23. size = ((x_max - x_min), (y_max - y_min))
  24. return x_min, x_max, y_min, y_max, size

4、退出系统

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

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

关注微信公众号,回复关键字:基于Python的人脸自动戴口罩系统,获取项目资源。

发布了149 篇原创文章 · 获赞 8172 · 访问量 74万+


所属网站分类: 技术文章 > 博客

作者:我下面给你吃

链接:https://www.pythonheidong.com/blog/article/231007/322f466b1f03162a8147/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

19 0
收藏该文
已收藏

评论内容:(最多支持255个字符)