Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to detect whether a face is wearing a mask by Python code

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about Python code how to detect whether a face is wearing a mask. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Recently, masks have become an absolutely hot topic. Under the epidemic, going out without a mask is not only irresponsible to yourself, but also a potential threat to others. So many neighborhoods have security guards at the door, and anyone who doesn't wear a mask will yell back (scared me to death).

In spite of this, manual testing is always possible to miss people, and stare at all the time, the security uncle is also very tired. Today, we will try the feasibility of using a computer to automatically detect whether a face is wearing a mask. If feasible, then according to our previous push: face detection is automatically turned on, we can make a real-time camera. If you find someone who is not wearing a mask, send a warning to the security guard to improve the efficiency of the security guard.

Of course, it takes a lot of training data to know whether other people's faces are wearing masks, but Baidu recently opened up their face mask recognition model PaddleHub mask test:

Https://github.com/PaddlePaddle/PaddleHub

Let's try the effect of this model today.

1. Prepare for

In order to achieve this experiment, Python is essential, if you have not already installed Python, it is recommended to read our article: ultra-detailed Python installation Guide.

Then, we need to install Baidu's paddlepaddle, and there are detailed instructions when we go to their official website:

Https://www.paddlepaddle.org.cn/install/quick

Choose these options according to your own situation, the last version of CUDA, because this experiment does not need training data, and does not need too much calculation, so you can directly choose the CPU version. After the selection, the installation instructions will appear below. I have to say that Paddlepaddle is still relatively intimate in these aspects (that is, the name is not good).

Note that if the name of the program in your Python3 environment variable is Python, remember to change the python3 xxx statement to Python xxx to install as follows:

Python-m pip install paddlepaddle-I https://mirror.baidu.com/pypi/simple2. Write code

As a deep learning platform, Paddlehub is quite convenient to use, especially for non-research applications like me. There are only four steps:

1. Introduce modules and pictures

two。 Load model

3. Classification and prediction

4. Result display

Save the picture we need to test in the same directory as the code file and name it 3.jpg:

Want to see this little sister take off her mask.

The code is as follows:

Yes, you read it right. It's as simple as that. Get rid of blank lines and comments, there are only 12 lines of code, a little harder, get rid of all the matplot display, only 6 lines of code. A little harder, combine the test_img_path and input_path variables with the module.face_detection statement, and you'll find only three lines of code:

The classification results after execution can be viewed in the detection_result directory in the same directory. So, 20 lines of code is modest, 3 lines of code is enough.

3. Result display

The picture we tested, the results are as follows:

MASK:97.64% represents the person wearing a mask with a credibility of 97.64%.

NO MASK: 97.41% means the person is not wearing a mask, and the credibility is 97.41%.

The last uncle is also MASK, but its credibility is only 54.31%, so it is likely to be misjudged. In our practical application, we only need to raise the warning threshold (for example, 80%) to filter out these unreliable categories.

However, one drawback of this model is that it is not very accurate for people from a long distance:

The person closest to the camera may be because of the side face, the facial features are not obvious and cannot be recognized. But in the distance, those wearing masks are recognized as not wearing masks, which is the pot of this model. However, this model can still be used in real life by threshold filtering and shortening the distance of camera photography.

This is how the Python code shared by the editor detects whether a face is wearing a mask. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report