F
人脸检测+口罩识别:
使用impluse edge 和haar,需要添加到U盘里生成的两个文件,可以直接复制运行。
以下代码可以成功运行,矩形框标出人脸,并显示出mask yes 或者mask no,并实现了在lcd上显示。欢迎大家参考,有问题可以留言。
import sensor, image, time, os, tf, uos, gc, lcd
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((240, 240))
sensor.skip_frames(time=2000)
face_cascade = image.HaarCascade("frontalface", stage=25)
net = tf.load("trained.tflite", load_to_fb=uos.stat('trained.tflite')[6] > (gc.mem_free() - (64*1024)))
labels = [line.rstrip('\n') for line in open("labels.txt")]
clock = time.clock()
lcd.init()
lcd.clear()
while True:
clock.tick()
img = sensor.snapshot()
objects = img.find_features(face_cascade, threshold=0.75, scale_factor=1.25)
for r in objects:
img.draw_rectangle(r)
for r in objects:
face_img = img.crop(r)
mask = net.classify(face_img)[0].output()[0]
if mask > 0.1:
img.draw_rectangle(r, color=(0, 255, 0))
img.draw_string(r[0], r[1], "Mask", color=(0, 255, 0))
else:
img.draw_rectangle(r, color=(255, 0, 0))
img.draw_string(r[0], r[1], "No Mask", color=(255, 0, 0))
lcd.display(img)
print(clock.fps())