当前位置:网站首页>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.cn
pip install paddlehub -i https://pypi.tuna.tsinghua.edu.cn/simple  --trusted-host https://pypi.tuna.tsinghua.edu.cn
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple  --trusted-host https://pypi.tuna.tsinghua.edu.cn

     Version 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 ).

 

原网站

版权声明
本文为[Color Space]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250640474377.html