7 Mask detection

7.1 experimental goals

This lesson mainly learns the mask detection function, analyzes the picture collected by the camera, compares the model, analyzes whether to wear a mask, and prints out the status of wearing a mask.

The reference code path for this experiment is :CanMV\05-AI\face_mask_detect.py

 

7.2 Preparation before the experiment

Please first import the model file into the memory card, and then insert the memory card into the memory card slot of the K210 module.

7.3 experimental procedure

The factory firmware of the module has integrated the AI vision algorithm module. If you have downloaded other firmware, please burn it back to the factory firmware before doing the experiment.

  1. Import the libraries and initialize the camera and LCD display.
  1. To initialize the parameters related to KPU, kpu needs to load the kmodel file. The path of the model file required for this experiment is:/sd/KPU/face_mask_detect/detect_5.kmodel,And use yolo2 to calculate conformity to the model requirements. od_img is the image of the neural network, the size is 320*256, which is used to store the camera image and pass it to KPU for calculation.
  1. Create a new while loop, pass the image to the KPU for calculation, use the yolo2 neural network algorithm to solve, if it is a face wearing a mask buttoned, it is framed in green and displayed "with mask", if it is a face without a mask, it is framed in red and displayed "without mask".

 

7.4 experimental results

Connect the K210 module to the computer through the microUSB data cable, CanMV IDE click the connect button, after the connection is completed click the Run button to run the routine code. You can also download the code as main.py and run it in the K210 module.

Wait for the system initialization to complete, the LCD displays the camera screen, shoots the face with the camera, displays a green box and "with mask" when there is a mask, and displays a red box and "without mask" when no mask is worn. At the same time, the serial terminal at the bottom of the IDE will print information about the face.

image-20230220104144290

image-20230220104211330

image-20230220104235456

7.5 experiment summary

The memory card required for mask detection needs to load the model file, so you need to import the model file into the memory card in advance, and then insert the memory card into the memory card slot of the K 210 module, if the model file in the memory card cannot be read, an error will be reported.

At present, the threshold of mask detection is threshold=0.7, and if you need to detect faces more accurately, you can adjust the threshold appropriately.