正点原子 发表于 6 天前

《DNK210使用指南 -CanMV版 V1.0》第四十三章 人脸属性分析实验


1)实验平台:正点原子DNK210开发板
2)购买链接:https://detail.tmall.com/item.htm?id=782801398750
3)全套实验源码+手册+视频下载地址:http://openedv.com/thread-348335-1-1.html
4)正点原子官方B站:https://space.bilibili.com/394620890
5)正点原子手把手教你学DNK210快速入门视频教程:https://www.bilibili.com/video/BV1kD421G7fu
6)正点原子FPGA交流群:132780729


第四十三章 人脸属性分析实验

       在上一章节中,介绍了利用maix.KPU模块实现了人脸口罩佩戴检测,本章将继续介绍利用maix.KPU模块实现的人脸属性分析。通过本章的学习,读者将学习到人脸属性分析应用在CanMV上的实现。
       本章分为如下几个小节:
       43.1 maix.KPU模块介绍
       43.2 硬件设计
       43.3 程序设计
       43.4 运行验证

       43.1 maix.KPU模块介绍
       有关maix.KPU模块的介绍,请见第39.1小节《maix.KPU模块介绍》。

       43.2 硬件设计

       43.2.1 例程功能
       1. 获取摄像头输出的图像,并送入KPU进行人脸检测,接着对检测到的人脸分别进行人脸五关键点检测和属性分析,最后将所有的检测结果和原始图像一同在LCD上进行显示。

       43.2.2 硬件资源
       本章实验内容,主要讲解maix.KPU模块的使用,无需关注硬件资源。

       43.2.3 原理图
       本章实验内容,主要讲解maix.KPU模块的使用,无需关注原理图。

       43.3 程序设计

       43.3.1 maix.KPU模块介绍
       有关maix.KPU模块的介绍,请见第43.1小节《maix.KPU模块介绍》。

       43.3.2 程序流程图

图43.3.2.1 人脸属性分析实验流程图
       43.3.3 main.py代码
       main.py中的脚本代码如下所示:
import lcd
import sensor
import gc
from maix import KPU

lcd.init()
sensor.reset()
sensor.set_framesize(sensor.QVGA)
sensor.set_pixformat(sensor.RGB565)
sensor.set_hmirror(False)

anchor = (0.1075, 0.126875, 0.126875, 0.175, 0.1465625, 0.2246875, 0.1953125, 0.25375, 0.2440625, 0.351875, 0.341875, 0.4721875, 0.5078125, 0.6696875, 0.8984375, 1.099687, 2.129062, 2.425937)
names = ['face']

# 构造并初始化人脸检测KPU对象
face_detecter = KPU()
face_detecter.load_kmodel("/sd/KPU/face_detect_320x240.kmodel")
face_detecter.init_yolo2(anchor, anchor_num=len(anchor) // 2, img_w=320, img_h=240, net_w=320, net_h=240, layer_w=10, layer_h=8, threshold=0.5, nms_value=0.2, classes=len(names))

# 构造并初始化人脸五关键点检测KPU对象
ld5_kpu = KPU()
ld5_kpu.load_kmodel("/sd/KPU/ld5.kmodel")

pos_face_attr = ["Male ", "Mouth Open ", "Smiling ", "Glasses"]
neg_face_attr = ["Female ", "Mouth Closed", "No Smile", "No Glasses"]

# 构造并初始化人脸属性分析KPU对象
fac_kpu = KPU()
fac_kpu.load_kmodel("/sd/KPU/fac.kmodel")

# 按指定比例扩展矩形框
def extend_box(x, y, w, h, scale):
    x1 = int(x - scale * w)
    x2 = int(x + w - 1 + scale * w)
    y1 = int(y - scale * h)
    y2 = int(y + h - 1 + scale * h)
    x1 = x1 if x1 > 0 else 0
    x2 = x2 if x2 < (320 - 1) else (320 - 1)
    y1 = y1 if y1 > 0 else 0
    y2 = y2 if y2 < (240 - 1) else (240 - 1)
    return x1, y1, x2 - x1 + 1, y2 - y1 + 1

while True:
    img = sensor.snapshot()
    face_detecter.run_with_output(img)
    faces = face_detecter.regionlayer_yolo2()
    for face in faces:
      # 框出人脸位置
      x, y, w, h = extend_box(face, face, face, face, 0.08)
      img.draw_rectangle(x, y, w, h, color=(0, 255, 0))
      # 计算人脸五关键点
      face_img = img.cut(x, y, w, h)
      resize_img = face_img.resize(128, 128)
      resize_img.pix_to_ai()
      output = ld5_kpu.run_with_output(resize_img, getlist=True)
      for i in range(len(output) // 2):
            point_x = int(KPU.sigmoid(output) * w + x)
            point_y = int(KPU.sigmoid(output) * h + y)
            img.draw_cross(point_x, point_y, size=5, color=(0, 0, 255))
      # 计算人脸属性
      output = fac_kpu.run_with_output(resize_img, getlist=True)
      for i in range(len(output)):
            if KPU.sigmoid(output) > 0.5:
<span style="font-style: italic;"><span style="font-style: normal;">                img.draw_string(x + w + 2, y + i * 16 + 2, "%s" % (pos_face_attr), color=(255, 0, 0), scale=1.5)
            else:
                img.draw_string(x + w + 2, y + i * 16 + 2, "%s" % (neg_face_attr</span><span style="font-style: normal;">), color=(0, 0, 255), scale=1.5)
      del face_img
      del resize_img
    lcd.display(img)
    gc.collect()</span></span>       可以看到一开始是先初始化了LCD和摄像头,并分别构造并初始化了用于人脸检测、人脸五关键点、人脸属性分析的KPU对象。
       然后便是在一个循环中不断地获取摄像头输出的图像,首先将图像进行人脸检测,检测图像中存在的人脸,接着对人脸图像进行五关键点检测,分析出人脸五关键点的位置,接着是对人脸图像进行属性分析,分析人脸的性别、嘴巴开合、是否微笑、是否佩戴眼镜等属性,最后将以上所有的分析检测结果在图像上进行绘制,然后在LCD上显示图像。

       43.4 运行验证
       将DNK210开发板连接CanMV IDE,点击CanMV IDE上的“开始(运行脚本)”按钮后,将摄像头对准人脸,让其采集到人脸图像,随后便能在LCD上看到摄像头输出的图像,同时能看到图像上标注了人脸位置、人脸五关键点位置、人脸属性等信息,如下图所示:

图43.4.1 LCD显示人脸属性分析实验结果
页: [1]
查看完整版本: 《DNK210使用指南 -CanMV版 V1.0》第四十三章 人脸属性分析实验