当前位置:网站首页>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 ).
边栏推荐
- 現在通過開戶經理發的開戶鏈接股票開戶安全嗎?
- 电子学:第014课——实验 15:防入侵报警器(第一部分)
- 共话云原生数据库的未来
- Sword finger offer (simple level)
- Stm32cubemx learning (5) input capture experiment
- Not afraid of losing a hundred battles, but afraid of losing heart
- Electronics: Lesson 014 - Experiment 15: intrusion alarm (Part I)
- Luogu p2839 [national training team]middle (two points + chairman tree + interval merging)
- 电子学:第008课——实验 6:非常简单的开关
- June training (day 25) - tree array
猜你喜欢

Matlab代码格式一键美化神器

c#搭建ftp服务器并实现文件上传和下载

To understand the difference between Gram-positive and Gram-negative bacteria and the difference in pathogenicity

Can bus working condition and signal quality "physical examination"

面试前准备好这些,Offer拿到手软,将军不打无准备的仗

Stm32cubemx learning (5) input capture experiment

Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test

电子学:第010课——实验 8:继电振荡器

Opencv daily function structure analysis and shape descriptor (8) Fitline function fitting line

剑指offer刷题(中等等级)
随机推荐
云计算考试版本1.0
How do I install the software using the apt get command?
Force buckle 272 Closest binary search tree value II recursion
Luogu p2839 [national training team]middle (two points + chairman tree + interval merging)
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)
The first game of 2021 ICPC online game
想转行学软件测试担心哪些问题?
力扣 272. 最接近的二叉搜索树值 II 递归
Not afraid of losing a hundred battles, but afraid of losing heart
电子学:第008课——实验 6:非常简单的开关
Use Adobe Acrobat pro to resize PDF pages
FFT [template]
Websocket understanding and application scenarios
[Mobius inversion]
【补题】2021牛客暑期多校训练营4-n
[red flag Cup] Supplementary questions
[supplementary question] 2021 Niuke summer multi school training camp 9-N
Solving some interesting problems with recurrence of function
电子学:第010课——实验 8:继电振荡器
五分钟快速搭建一个实时人脸口罩检测系统(OpenCV+PaddleHub 含源码)