Yolo手掌检测

本节例程的位置在 百度云盘资料\野火K210 AI视觉相机\1-教程文档_例程源码\例程\10-KPU\yolo_hand_detect\yolo_hand_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 = (0.8125, 0.4556, 1.1328, 1.2667, 1.8594, 1.4889, 1.4844, 2.2000, 2.6484, 2.9333)
kpu = KPU()
print("ready load model")
kpu.load_kmodel("/sd/KPU/yolo_hand_detect/hand_detect.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.3, 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()
    kpu.run_with_output(od_img)
    dect = kpu.regionlayer_yolo2()
    fps = clock.fps()
    if len(dect) > 0:
        print("dect:",dect)
        for l in dect :
            a = img.draw_rectangle(l[0],l[1],l[2],l[3], color=(0, 255, 0))

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

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 = (0.8125, 0.4556, 1.1328, 1.2667, 1.8594, 1.4889, 1.4844, 2.2000, 2.6484, 2.9333)
kpu = KPU()
print("ready load model")
kpu.load_kmodel("/sd/KPU/yolo_hand_detect/hand_detect.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.3, classes=1)
  • 创建一个用于神经网络输入的图像对象od_img,加载一个预训练的KPU模型用于手势检测,并初始化YOLO v2神经网络。anchor是用于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()
    kpu.run_with_output(od_img)
    dect = kpu.regionlayer_yolo2()
    fps = clock.fps()
    if len(dect) > 0:
        print("dect:",dect)
        for l in dect :
            a = img.draw_rectangle(l[0],l[1],l[2],l[3], color=(0, 255, 0))

    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
kpu.deinit()
  • 在循环结束后,清理KPU资源。