7 Mask detection7.1 experimental goals7.2 Preparation before the experiment7.3 experimental procedure7.4 experimental results7.5 experiment summary
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
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.
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.
ximport sensor, image, time, lcd
from maix import KPU
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 1000)
clock = time.clock()
xxxxxxxxxx
od_img = image.Image(size=(320,256), copy_to_fb=False)
anchor = (0.156250, 0.222548, 0.361328, 0.489583, 0.781250, 0.983133, 1.621094, 1.964286, 3.574219, 3.94000)
kpu = KPU()
print("ready load model")
kpu.load_kmodel("/sd/KPU/face_mask_detect/detect_5.kmodel")
kpu.init_yolo2(anchor, anchor_num=5, img_w=320, img_h=240, net_w=320 , net_h=256 ,layer_w=10 ,layer_h=8, threshold=0.7, nms_value=0.4, classes=2)
xxxxxxxxxx
while True:
clock.tick()
img = sensor.snapshot()
od_img.draw_image(img, 0,0)
od_img.pix_to_ai()
kpu.run_with_output(od_img)
dect = kpu.regionlayer_yolo2()
fps = clock.fps()
if len(dect) > 0:
print("dect:", dect)
for l in dect :
if l[4] :
img.draw_rectangle(l[0],l[1],l[2],l[3], color=(0, 255, 0))
img.draw_string(l[0],l[1]-24, "with mask", color=(0, 255, 0), scale=2)
else:
img.draw_rectangle(l[0],l[1],l[2],l[3], color=(255, 0, 0))
img.draw_string(l[0],l[1]-24, "without mask", color=(255, 0, 0), scale=2)
img.draw_string(0, 0, "%2.1ffps" %(fps), color=(0, 60, 128), scale=2.0)
lcd.display(img)
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.
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.