身体检测

本节例程的位置在 百度云盘资料\野火K210 AI视觉相机\1-教程文档_例程源码\例程\10-KPU\head_body_detect\body_detect.py

介绍

可用于人身体检测,下图为实机演示

野火logo 野火logo

例程

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sensor, image, time, lcd
from maix import KPU
import gc

lcd.init()
sensor.reset(dual_buff=True)        # Reset and initialize the sensor. It will
                                    # run automatically, call sensor.run(0) to stop
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 1000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.

od_img = image.Image(size=(320,256))

anchor_body_detect = (0.0978, 0.1758, 0.1842, 0.3834, 0.3532, 0.5982, 0.4855, 1.1146, 0.8869, 1.6407, 1.2388, 3.4157, 2.0942, 2.1114, 2.7138, 5.0008, 6.0293, 6.4540)
body_kpu = KPU()
print("ready load model")
body_kpu.load_kmodel("/sd/KPU/head_body_detect/uint8_person_detect_v1_old.kmodel")
body_kpu.init_yolo2(anchor_body_detect, anchor_num=9, img_w=320, img_h=240, net_w=320 , net_h=256 ,layer_w=10 ,layer_h=8, threshold=0.7, nms_value=0.2, classes=1)

while True:
    gc.collect()
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()
    a = od_img.draw_image(img, 0,0)
    od_img.pix_to_ai()

    body_kpu.run_with_output(od_img)
    body_boxes = body_kpu.regionlayer_yolo2()
    if len(body_boxes) > 0:
        for l in body_boxes :
            a = img.draw_rectangle(l[0],l[1],l[2],l[3], color=(255, 0, 0))

    fps = clock.fps()
    a = img.draw_string(0, 0, "%2.1ffps" %(fps), color=(0, 60, 128), scale=2.0)
    lcd.display(img)

body_kpu.deinit()

例程解析

1
2
3
import sensor, image, time, lcd
from maix import KPU
import gc
  • 这些库提供了对摄像头、图像处理、时间、LCD显示和内存管理等的支持。

1
2
3
4
5
6
7
lcd.init()
sensor.reset(dual_buff=True)        # Reset and initialize the sensor. It will
                                    # run automatically, call sensor.run(0) to stop
sensor.set_pixformat(sensor.RGB565) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.QVGA)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 1000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.
  • 初始化LCD显示和摄像头设置,包括双缓冲、像素格式、帧大小,并跳过一些帧以确保设置生效。同时创建一个时钟对象来跟踪帧率(FPS)

1
2
3
4
5
6
7
od_img = image.Image(size=(320,256))

anchor_body_detect = (0.0978, 0.1758, 0.1842, 0.3834, 0.3532, 0.5982, 0.4855, 1.1146, 0.8869, 1.6407, 1.2388, 3.4157, 2.0942, 2.1114, 2.7138, 5.0008, 6.0293, 6.4540)
body_kpu = KPU()
print("ready load model")
body_kpu.load_kmodel("/sd/KPU/head_body_detect/uint8_person_detect_v1_old.kmodel")
body_kpu.init_yolo2(anchor_body_detect, anchor_num=9, img_w=320, img_h=240, net_w=320 , net_h=256 ,layer_w=10 ,layer_h=8, threshold=0.7, nms_value=0.2, classes=1)
  • 创建一个用于神经网络输入的图像对象od_img,加载一个预训练的KPU模型用于人体检测,并初始化YOLO v2神经网络。anchor_body_detect是用于YOLO算法的锚点 boxes,anchor_num是锚点的数量,img_w和img_h是输入图像的宽度和高度,net_w和net_h是神经网络输入层的宽度和高度,layer_w和layer_h是神经网络输出层的宽度和高度,threshold是置信度阈值,nms_value是非极大值抑制的阈值,classes是检测的类别数量。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
while True:
    gc.collect()
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()
    a = od_img.draw_image(img, 0,0)
    od_img.pix_to_ai()

    body_kpu.run_with_output(od_img)
    body_boxes = body_kpu.regionlayer_yolo2()
    if len(body_boxes) > 0:
        for l in body_boxes :
            a = img.draw_rectangle(l[0],l[1],l[2],l[3], color=(255, 0, 0))

    fps = clock.fps()
    a = img.draw_string(0, 0, "%2.1ffps" %(fps), color=(0, 60, 128), scale=2.0)
    lcd.display(img)
  • 手动调用垃圾回收以释放内存。

  • 更新FPS时钟。

  • 捕获一帧图像。

  • 将捕获的图像绘制到od_img上,并转换为神经网络输入格式。

  • 运行KPU模型进行人体检测。

  • 如果检测到人体,则在原始图像上绘制矩形框。

  • 在图像上显示当前的FPS。

  • 将图像显示在LCD上。

1
body_kpu.deinit()
  • 在循环结束后,清理KPU资源