[Huawei] cloud technology sharing system based on automatic face wearing masks Python people

Project Background

Since 2019 novel coronavirus pneumonia epidemic occurrence of infections, affecting the people, the whole nation mourning, masks, alcohol, disinfectant valuable commodity.

Grab a mask, how to do? As the technology today to share how to use Python automatically wear a mask system, to comfort themselves, the effect of the system is as follows:

The principle of the system is by Dlib module Landmark face 68 keypoint detection library to easily identify the face features data, based on these data, the position data (48 o'clock to 67 o'clock position) of the lip portion, according to the detected mouth size and orientation of the portion, by adjusting the size and direction of the PLL module masks, to achieve the proper position of the image on the mask.

2, page design

: Tkinter module implements GUI design, can be loaded character image, choose from four types of masks (masks here is the deal good picture), wearing face masks to show the effect of the operation is complete exit the system, the effect is based on the following

Page Layout implementation code is shown below:

    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()

Loading the character image, the implementation shown in the following code:

    # 原图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)

Face wearing masks show, the implementation code as follows:

    # 人脸戴口罩效果展示
    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

Introduction of the four mask image, the implementation code as follows:

    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, organ recognition

After the page is relying on to achieve recognition Dlib function library for face organ key points, analysis of the position and size of the mouth, here for the convenience of our intuitive understanding, write a test Demo, will face key figures are displayed, as follows below:

#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)

Results as shown below:

These keys need not shown in this drawing system, can be used directly, to achieve code as follows:

    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, exit the system

Exit system is very simple, line Demo can be realized as follows:

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

Author: Huawei cloud special feeds the developer does not ape hair loss program

 

Guess you like

Origin www.cnblogs.com/huaweicloud/p/12383667.html