当前位置:网站首页>Quickly build a real-time face mask detection system in five minutes (opencv+paddlehub with source code)
Quickly build a real-time face mask detection system in five minutes (opencv+paddlehub with source code)
2022-06-25 08:14:00 【Color Space】
Reading guide
This article mainly introduces how to use OpenCV and PaddleHub Realize a real-time face mask detection system .( official account :OpenCV And AI Deep learning )
Background introduction
from 19 The epidemic broke out in and now , It is normal for everyone to wear masks . There are many related applications , Such as virus development prediction 、 Mask wearing detection and face recognition with mask .
The face mask wearing detection system introduced today mainly uses OpenCV And Baidu feijiang (PaddlePaddle) Of PaddleHub Detection model provided .PaddleHub It provides many practical models , Including image processing 、 word processing 、 Audio processing 、 Video processing and industrial applications .github Address :https://github.com/PaddlePaddle/PaddleHub

Face mask detection
The model of face detection part is as follows :

The two models in the red box support face mask detection , Choose here pyramidbox_lite_server_mask, Detailed implementation steps :
【1】 install PaddlePaddle、PaddleHub and OpenCV(opencv-python)
pip install paddlepaddle -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host https://pypi.tuna.tsinghua.edu.cnpip install paddlehub -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host https://pypi.tuna.tsinghua.edu.cnpip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host https://pypi.tuna.tsinghua.edu.cnVersion used in this article :
PaddlePaddle---2.3.0
PaddleHun---2.2.0
opencv-python---4.6.0.66
Be careful : install PaddlePaddle There may be some problems , Lead to import paddle Failure , You can search for solutions according to the error information .
【2】 Picture face mask detection
Prepare the map to be measured , Run the following code , Modify the image path :
import paddlehub as hub
import cv2
mask_detector = hub.Module(name="pyramidbox_lite_server_mask")
img_path = './imgs/A0.png'
img = cv2.imread(img_path)
input_dict = {"data": [img]}
result = mask_detector.face_detection(data=input_dict)
count = len(result[0]['data'])
if count < 1:
print('There is no face detected!')
else:
for i in range(0,count):
#print(result[0]['data'][i])
label = result[0]['data'][i].get('label')
score = float(result[0]['data'][i].get('confidence'))
x1 = int(result[0]['data'][i].get('left'))
y1 = int(result[0]['data'][i].get('top'))
x2 = int(result[0]['data'][i].get('right'))
y2 = int(result[0]['data'][i].get('bottom'))
cv2.rectangle(img,(x1,y1),(x2,y2),(255,200,0),2)
if label == 'NO MASK':
cv2.putText(img,label,(x1,y1),0,0.8,(0,0,255),2)
else:
cv2.putText(img,label,(x1,y1),0,0.8,(0,255,0),2)
cv2.imwrite('result.jpg',img)
cv2.imshow('mask-detection', img)
cv2.waitKey()
cv2.destroyAllWindows()
print('Done!')
The first time the code starts, the corresponding model will be downloaded to the following location :
C:\Users\xxx\.paddlehub\modules, No more downloads in the future

Test chart 1:

Running results :

Test chart 2:

Running results :

Test chart 3:

Running results :

Test chart 4:

Running results :

From the above test results , It's not bad !
【3】 Video or camera real-time face mask detection
Prepare to test the video or directly open the camera to detect , Select the corresponding code :
cap = cv2.VideoCapture('2.mp4') # Video file detection# cap = cv2.VideoCapture(0) # Camera detection
Complete code :
import paddlehub as hub
import cv2
mask_detector = hub.Module(name="pyramidbox_lite_server_mask")
def mask_detecion(img):
input_dict = {"data": [img]}
result = mask_detector.face_detection(data=input_dict)
count = len(result[0]['data'])
if count < 1:
#print('There is no face detected!')
pass
else:
for i in range(0,count):
#print(result[0]['data'][i])
label = result[0]['data'][i].get('label')
score = float(result[0]['data'][i].get('confidence'))
x1 = int(result[0]['data'][i].get('left'))
y1 = int(result[0]['data'][i].get('top'))
x2 = int(result[0]['data'][i].get('right'))
y2 = int(result[0]['data'][i].get('bottom'))
cv2.rectangle(img,(x1,y1),(x2,y2),(255,200,0),2)
if label == 'NO MASK':
cv2.putText(img,label,(x1,y1),0,0.8,(0,0,255),2)
else:
cv2.putText(img,label,(x1,y1),0,0.8,(0,255,0),2)
return img
if __name__ == '__main__':
cap = cv2.VideoCapture('2.mp4') # Video file detection
#cap = cv2.VideoCapture(0) # Camera detection
if(cap.isOpened()): # Video opened successfully
while(True):
ret,frame = cap.read()# Read a frame
result = mask_detecion(frame)
cv2.imshow('mask_detection',result)
if cv2.waitKey(1)&0xFF ==27: # Press down Esc Key to exit
break
else:
print ('open video/camera failed!')
cap.release()
cv2.destroyAllWindows()test result :

download 1:Pytoch Common function manual
stay 「OpenCV And AI depth 」 Back office reply No :Pytorch Function manual , Can learn to download the first copy of the whole network Pytorch Function common manual , Include Tensors Introduce 、 Introduction to basic functions 、 Data processing functions 、 Optimization function 、CUDA Programming 、 Deal with more Wait for Chapter 14 .
download 2:145 individual OpenCV Example application code
stay 「OpenCV And AI thorough 」 Official account back office reply :OpenCV145, Can learn to download 145 individual OpenCV Example application code (Python and C++ Dual language implementation ).
边栏推荐
- TCP stuff
- 使用apt-get命令如何安装软件?
- Opencv daily function structure analysis and shape descriptor (8) Fitline function fitting line
- Log in to MySQL 5.7 under ubuntu18 and set the root password
- Electronics: Lesson 009 - Experiment 7: study relays
- 想转行学软件测试担心哪些问题?
- c#中设置lable控件的TextAlign属性控制文字居中的方法
- 現在通過開戶經理發的開戶鏈接股票開戶安全嗎?
- TCP与UDP
- Electronics: Lesson 012 - Experiment 13: barbecue LED
猜你喜欢

剑指offer刷题(简单等级)

Matlab code format one click beautification artifact

唐老师讲运算放大器(第七讲)——运放的应用

电子学:第008课——实验 6:非常简单的开关

Looking for b-end product manager after years? I almost ruined myself

Drawing of clock dial

c#ColorDialog更改文本颜色和FontDialog更改文本字体的使用示例

深度学习系列48:DeepFaker

Ph中和过程建模

Sword finger offer (medium level)
随机推荐
417 sequence traversal of binary tree 1 (102. sequence traversal of binary tree, 107. level traversal of binary tree II, 199. right view of binary tree, 637. layer average of binary tree)
Free SSL certificate acquisition tutorial
TCP stuff
TCP 加速小记
牛客:飞行路线(分层图+最短路)
Stm32cubemx learning (5) input capture experiment
socket问题记录
Luogu p3313 [sdoi2014] travel (tree chain + edge weight transfer point weight)
Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system
洛谷P5994 [PA2014]Kuglarz(异或思维+MST)
Luogu p1073 [noip2009 improvement group] optimal trade (layered diagram + shortest path)
Mr. Tang's lecture on operational amplifier (Lecture 7) -- Application of operational amplifier
Black dot = = white dot (MST)
Luogu p2048 [noi2010] super Piano (rmq+ priority queue)
图像超分综述:超长文一网打尽图像超分的前世今生 (附核心代码)
Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test
How to create a new branch with SVN
C examples of using colordialog to change text color and fontdialog to change text font
CVPR 2022 oral 2D images become realistic 3D objects in seconds
Basic record of getting started with PHP