Python实现⼈脸⼝罩检测!这玩意太强⼤了啊!
由于疫情的影响,⼈脸⼝罩检测系统的开发成为很多⼈争相开发的⼀种算法。很多公司或者个⼈都开源了他们很多的代码或者SDK。⼤家在GitHub或者各种平台上都能够找到很多的资源。 前段时间在博客上看到⼏个有趣的开源项⽬,它能检测我们是否有戴⼝罩,跑起程序测试后,发现识别率挺⾼的,也适应不同环境;于是分享给⼤家。我⽤了其中两种并整合在⼀个GUI下⾯,⼀种是基于深度学习的,⼀种是基于opencv训练的。
测试环境
windows10 系统; 软件:pyCharm; 使⽤模型:tenforflow1.15.0 ; python3.7
先学后教
下⾯先看⼀下整个系统的GUI界⾯:
GUI程序:
哈根达斯价格from tkinter import *
from tkinter.filedialog import askdirectory
ssagebox import showinfo
import cv2
import numpy as np
from PIL import Image, ImageTk
from tkinter import ttk
import pygame
import time
import tensorflow_infer as flow
file_slogan = r'video/slogan.mp3'
file_slogan_short = r'video/slogan_short.mp3'
pygame.mixer.init(frequency=16000, size=-16, channels=2, buffer=4096)
detector = cv2.CascadeClassifier('haarcascades\\haarcascade_l')
mask_detector = cv2.CascadeClassifier('xml\\l')
class GUI:
def __init__(lf):
lf.camera = None # 摄像头
< = Tk()
mainloop()
def createFirstPage(lf):
lf.page1 = )
lf.page1.pack()
Label(lf.page1, text='欢迎使⽤⼝罩检测跟踪系统', font=('粗体', 20)).pack()
image = Image.open("demo2.jpg") # 随便使⽤⼀张图⽚做背景界⾯不要太⼤
photo = ImageTk.PhotoImage(image = image)
lf.data1 = Label(lf.page1, width=780,image = photo)
lf.data1.image = photo
lf.data1.pack(padx=5, pady=5)
lf.button11 = Button(lf.page1, width=18, height=2, text="深度学习算法", bg='red', font=("宋", 12),
relief='rai',command = lf.createSecondPage1)
lf.button11.pack(side=LEFT, padx=25, pady = 10)
lf.button12 = Button(lf.page1, width=18, height=2, text="传统算法", bg='green', font=("宋", 12),
relief='rai', command = lf.createSecondPage)
lf.button12.pack(side=LEFT, padx=25, pady = 10)
lf.button13 = Button(lf.page1, width=18, height=2, text="打开本地视频", bg='white', font=("宋", 12), relief='rai', command = lf.lect_path)
lf.button13.pack(side=LEFT, padx=25, pady = 10)
lf.button14 = Button(lf.page1, width=18, height=2, text="退出系统", bg='gray', font=("宋", 12),
relief='rai',command = lf.quitMain)
lf.button14.pack(side=LEFT, padx=25, pady = 10)
def createSecondPage1(lf):
lf.camera = cv2.VideoCapture(0)
lf.page1.pack_forget()
lf.page2 = )
lf.page2.pack()
Label(lf.page2, text='欢迎使⽤⼝罩检测跟踪系统', font=('粗体', 20)).pack()
lf.data2 = Label(lf.page2)冬季菜谱
lf.data2.pack(padx=5, pady=5)
lf.button21 = Button(lf.page2, width=18, height=2, text="返回", bg='gray', font=("宋", 12),
relief='rai',command = lf.backFirst)
lf.button21.pack(padx=25,pady = 10)
lf.video_loop1(lf.data2)
def video_loop1(lf, panela):
def slogan_short():
timeplay = 1.5
global playflag_short
playflag_short = 1
while playflag_short:
track = pygame.mixer.music.load(file_slogan_short)
print("------------请您戴好⼝罩")
pygame.mixer.music.play()
time.sleep(timeplay)
playflag_short = 0
time.sleep(0)
success, img = ad() # 从摄像头读取照⽚
if success:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
num,c,img = flow.inference(img, conf_thresh=0.5, iou_thresh=0.4, target_shape=(260, 260), draw_result=True, show_result=Fal)
# 语⾳提⽰
# if(isinstance(num/5,int)& (c=='NoMask')):
# slogan_short()
# cv2.imshow('image', img)
# img = flow.inference(img, show_result=True, target_shape=(260, 260))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2image = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA) # 转换颜⾊从BGR到RGBA
current_image = Image.fromarray(cv2image) # 将图像转换成Image对象
cpu占用率高怎么解决
imgtk = ImageTk.PhotoImage(image=current_image)
panela.imgtk = imgtk
def lect_path(lf):
lf.pash_= askdirectory()
path = StringVar()
path.t(lf.pash_)
def createSecondPage(lf):
lf.camera = cv2.VideoCapture(0)
lf.page1.pack_forget()冯晓达
lf.page2 = )
lf.page2.pack()
Label(lf.page2, text='欢迎使⽤⼝罩检测跟踪系统', font=('粗体', 20)).pack()
詈是什么意思
lf.data2 = Label(lf.page2)
lf.data2.pack(padx=5, pady=5)
lf.button21 = Button(lf.page2, width=18, height=2, text="返回", bg='gray', font=("宋", 12), relief='rai',command = lf.backFirst)
lf.button21.pack(padx=25,pady = 10)
lf.video_loop(lf.data2)
def video_loop(lf, panela):
success, img = ad() # 从摄像头读取照⽚
if success:
faces = detector.detectMultiScale(img, 1.1, 3)
for (x, y, w, h) in faces:
# 参数分别为图⽚、左上⾓坐标,右下⾓坐标,颜⾊,厚度
face = img[y:y + h, x:x + w] # 裁剪坐标为[y0:y1, x0:x1]
mask_face = mask_detector.detectMultiScale(img, 1.1, 5)
三年级名言警句
for (x2, y2, w2, h2) in mask_face:
cv2.putText(img, 'mask', (x2 - 2, y2 - 2),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 255))
#img = mask.facesdetecter(img)
cv2image = cv2.cvtColor(img, cv2.COLOR_BGR2RGBA) # 转换颜⾊从BGR到RGBA
#faces = detector.detectMultiScale(cv2image, 1.1, 3)
current_image = Image.fromarray(cv2image) # 将图像转换成Image对象
imgtk = ImageTk.PhotoImage(image=current_image)
panela.imgtk = imgtk
def backFirst(lf):
lf.page2.pack_forget()
lf.page1.pack()
# 释放摄像头资源
lea()
cv2.destroyAllWindows()
def backMain(lf):
lf.page3.pack_forget()
lf.page1.pack()
def quitMain(lf):
if __name__ == '__main__':
demo = GUI()
1.深度学习的效果:
检测出胡歌没有带⼝罩。红⾊框框是圈出⼈脸部分,上⽅的字体:NoMask ,准确率 1 (即有100%把握认为没带⼝罩)如果在多⼈的情况下,能检测出来吗?
这个模型能同时检测多⼈的,并且准确⾼。以上是基于照⽚的。
下⾯是基于GUI打开摄像头的,有⽆⼝罩的对⽐:
这个深度学习的项⽬完全开源,可以⾃⼰先到⽹页上去体验⼀波:
深度学习开源项⽬简单介绍:
1)⽀持5⼤主流深度学习框架(PyTorch、TensorFlow、MXNet、Keras和Caffe),已经写好接⼝了;可以根据⾃⾝的环境选择合适的框架,⽐如:TensorFlow;所有模型都在models⽂件夹下。
八月十五日夜湓亭望月2)公开了进8000张的⼈脸⼝罩数据和模型,数据集来⾃于WIDER Face和MAFA数据集, 重新修改了标注并进⾏了校验(主要是 MAFA和WIDER Face的⼈脸位置定义不⼀样,所以进⾏了修改标注)并将其开源出来。
2.基于Opencv的⼝罩佩戴识别
注:这个⽅法只能检测有⼝罩的,⽆⼝罩就没法检测,不给任何提⽰或者标注。
看看有⼝罩的效果: