最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

基于Python實現(xiàn)人臉自動戴口罩系統(tǒng)

 更新時間:2020年02月06日 11:39:48   作者:不脫發(fā)的程序猿  
2019年新型冠狀病毒感染的肺炎疫情發(fā)生以來,牽動人心,舉國哀痛,口罩、酒精、消毒液奇貨可居。這篇文章主要介紹了基于Python的人臉自動戴口罩系統(tǒng),需要的朋友可以參考下

1、項目背景

2019年新型冠狀病毒感染的肺炎疫情發(fā)生以來,牽動人心,舉國哀痛,口罩、酒精、消毒液奇貨可居。

搶不到口罩,怎么辦?作為技術(shù)人今天分享如何使用Python實現(xiàn)自動戴口罩系統(tǒng),來安慰自己,系統(tǒng)效果如下所示:

本系統(tǒng)的實現(xiàn)原理是借助 Dlib模塊的Landmark人臉68個關(guān)鍵點檢測庫輕松識別出人臉五官數(shù)據(jù),根據(jù)這些數(shù)據(jù),確定嘴唇部分的位置數(shù)據(jù)(48點~67點位置),根據(jù)檢測到嘴部的尺寸和方向,借助PLL模塊調(diào)整口罩的尺寸和方向,實現(xiàn)將口罩放在圖像的適當位置。

2、頁面設(shè)計

 基于tkinter模塊實現(xiàn)GUI設(shè)計,可載入人物圖像,選擇四種類型口罩(這里的口罩是處理好的圖片),展示佩戴好口罩的效果,操作完成退出系統(tǒng),效果如下所示:


頁面布局實現(xiàn)代碼如下所示:

def __init__(self):
 self.root = tk.Tk()
 self.root.title('基于Pyhon的人臉自動戴口罩系統(tǒng)')
 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()

載入人物圖像,實現(xiàn)代碼如下所示:

 # 原圖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) # 調(diào)整圖片大小至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)

人臉戴口罩展示,實現(xiàn)代碼如下所示

# 人臉戴口罩效果展示
 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格式
 # 在合適位置添加頭發(fā)圖片
 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) # 調(diào)整圖片大小至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

導入四種口罩圖像,實現(xiàn)代碼如下所示:   

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、器官識別

頁面功能實現(xiàn)后就是依托Dlib庫實現(xiàn)人臉器官關(guān)鍵點的識別,分析出嘴部位置及尺寸,這里為了方便各位直觀了解,寫了一個測試Demo,將人物臉部關(guān)鍵點都顯示出來,代碼如下所示:

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

效果如下所示: 

在本系統(tǒng)中這些關(guān)鍵點無需繪制顯示,直接使用就可以,實現(xiàn)代碼如下所示:

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)
 # 根據(jù)人臉的大小擴大嘴唇對應(yīng)口罩的區(qū)域
 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、退出系統(tǒng)

退出系統(tǒng)非常簡單,一行Demo即可實現(xiàn),如下所示:

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

總結(jié)

以上所述是小編給大家介紹的基于Python實現(xiàn)人臉自動戴口罩系統(tǒng),希望對大家有所幫助!

相關(guān)文章

  • python中的IO流使用解讀

    python中的IO流使用解讀

    這篇文章主要介紹了python中的IO流使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • Pytorch 如何實現(xiàn)常用正則化

    Pytorch 如何實現(xiàn)常用正則化

    這篇文章主要介紹了Pytorch 實現(xiàn)常用正則化的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • python實現(xiàn)音樂下載的統(tǒng)計

    python實現(xiàn)音樂下載的統(tǒng)計

    這篇文章主要為大家詳細介紹了python實現(xiàn)音樂下載的統(tǒng)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 教大家玩轉(zhuǎn)Python字符串處理的七種技巧

    教大家玩轉(zhuǎn)Python字符串處理的七種技巧

    這篇文章主要給大家介紹了關(guān)于學會Python字符串處理的七種技巧,其中包括字符串的連接和合并、字符串的切片和相乘、字符串的分割、字符串的開頭和結(jié)尾的處理、字符串的查找和匹配、字符串的替換以及字符串中去掉一些字符等操作,需要的朋友可以參考。
    2017-03-03
  • Python PyMySQL操作MySQL數(shù)據(jù)庫的方法詳解

    Python PyMySQL操作MySQL數(shù)據(jù)庫的方法詳解

    PyMySQL是一個用于Python編程語言的純Python MySQL客戶端庫,它遵循Python標準DB API接口,并提供了許多方便的功能,本文就來和大家簡單介紹一下吧
    2023-05-05
  • python?OpenCV計算圖片相似度的5種算法

    python?OpenCV計算圖片相似度的5種算法

    本文主要介紹了python?OpenCV計算圖片相似度的5種算法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • Python讀取pdf表格寫入excel的方法

    Python讀取pdf表格寫入excel的方法

    這篇文章主要介紹了Python讀取pdf表格寫入excel的方法,幫助大家更好的利用python處理excel表格,感興趣的朋友可以了解下
    2021-01-01
  • TensorFlow實現(xiàn)checkpoint文件轉(zhuǎn)換為pb文件

    TensorFlow實現(xiàn)checkpoint文件轉(zhuǎn)換為pb文件

    今天小編就為大家分享一篇TensorFlow實現(xiàn)checkpoint文件轉(zhuǎn)換為pb文件,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python如何實現(xiàn)動態(tài)數(shù)組

    Python如何實現(xiàn)動態(tài)數(shù)組

    這篇文章主要介紹了Python如何實現(xiàn)動態(tài)數(shù)組,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • python中的sys模塊詳解

    python中的sys模塊詳解

    sys模塊是與python解釋器交互的一個接口,sys 模塊提供了許多函數(shù)和變量來處理 Python 運行時環(huán)境的不同部分,這篇文章主要介紹了python之sys模塊詳解,需要的朋友可以參考下
    2022-11-11

最新評論

莎车县| 瑞昌市| 龙南县| 满城县| 湖南省| 永仁县| 隆化县| 惠州市| 中西区| 津市市| 米泉市| 台北市| 富锦市| 张家界市| 石阡县| 广宗县| 莱州市| 安顺市| 南投县| 津南区| 邳州市| 通州市| 黔南| 隆回县| 肃北| 永靖县| 湟源县| 当阳市| 平湖市| 商都县| 库尔勒市| 伊吾县| 衢州市| 普宁市| 肥东县| 普格县| 固镇县| 县级市| 德令哈市| 依兰县| 金寨县|