OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 24|回复: 0

《ESP32-S3使用指南—IDF版 V1.6》第六十三章 运动侦测实验

[复制链接]

1200

主题

1214

帖子

2

精华

超级版主

Rank: 8Rank: 8

积分
5173
金钱
5173
注册时间
2019-5-8
在线时间
1295 小时
发表于 14 小时前 | 显示全部楼层 |阅读模式
第六十三章 运动侦测实验

1)实验平台:正点原子DNESP32S3开发板

2)章节摘自【正点原子】ESP32-S3使用指南—IDF版 V1.6

3)购买链接:https://detail.tmall.com/item.htm?&id=768499342659

4)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/esp32/ATK-DNESP32S3.html

5)正点原子官方B站:https://space.bilibili.com/394620890

6)正点原子DNESP32S3开发板技术交流群:132780729


2.jpg

3.png

乐鑫AI库中提供了一种名为运动侦测API接口的功能。该功能的原理非常简单:只需要获取两张图像数据,然后通过AI计算判断这两个图像是否匹配。如果图像不匹配,则说明当前处于运动状态;如果图像匹配,则说明当前图像处于相对静止状态。本章,我们调用乐鑫AI库的运动侦测API接口来实现运动侦测功能。
本章分为如下几个部分:
63.1 硬件设计
63.2 软件设计
63.3 下载验证


63.1 硬件设计

1. 例程功能
本章实验功能简介:使用乐鑫官方的ESP32-WHO AI库对OV2640和OV5640摄像头输出的数据进行运动侦测。

2. 硬件资源
1)LED灯
        LED-IO1
2)XL9555
        IIC_INT-IO0(需在P5连接IO0)
        IIC_SDA-IO41
        IIC_SCL-IO42
3)SPILCD
        CS-IO21
        SCK-IO12
        SDA-IO11
        DC-IO40(在P5端口,使用跳线帽将IO_SET和LCD_DC相连)
        PWR- IO1_3(XL9555)
        RST- IO1_2(XL9555)
4)CAMERA
        OV_SCL-IO38
        OV_SDA- IO39
        VSYNC- IO47
        HREF- IO48
        PCLK- IO45
        D0- IO4
        D1- IO5
        D2- IO6
        D3- IO7
        D4- IO15
        D5- IO16
        D6- IO17
        D7- IO18
        RESET-IO0_5(XL9555)
        PWDN-IO0_4(XL9555)

3. 原理图
本章实验使用的KPU为ESP32-S3的内部资源,因此并没有相应的连接原理图。

63.2 软件设计

63.2.1 程序流程图
程序流程图能帮助我们更好的理解一个工程的功能和实现的过程,对学习和设计工程有很好的主导作用。下面看看本实验的程序流程图:

第六十三章 运动侦测实验825.png
图63.2.1.1 程序流程图

63.2.2 程序解析
在本章节中,我们将重点关注两个文件:esp_motion_detection.cpp和esp_motion_detection.hpp。其中,esp_motion_detection.hpp主要声明了esp_motion_detection函数,其内容相对简单,因此我们暂时不作详细解释。本章节的核心关注点是esp_motion_detection.cpp文件中的函数。
接下来,我们将详细解析esp_motion_detection_ai_strat函数的工作原理。
  1. <font size="3">TaskHandle_t camera_task_handle;</font>
  2. <font size="3">TaskHandle_t ai_task_handle;</font>
  3. <font size="3">QueueHandle_t xQueueFrameO = NULL;</font>
  4. <font size="3">QueueHandle_t xQueueAIFrameO = NULL;</font>


  5. <font size="3">/**</font>
  6. <font size="3"> * @brief       摄像头图像数据获取任务</font>
  7. <font size="3"> * [url=home.php?mod=space&uid=271674]@param[/url]       arg:未使用</font>
  8. <font size="3"> * @retval      无</font>
  9. <font size="3"> */</font>
  10. <font size="3">static void esp_camera_process_handler(void *arg)</font>
  11. <font size="3">{</font>
  12. <font size="3">    arg = arg;</font>
  13. <font size="3">    camera_fb_t *camera_frame = NULL;</font>

  14. <font size="3">    while (1)</font>
  15. <font size="3">    {</font>
  16. <font size="3">        /* 获取摄像头图像 */</font>
  17. <font size="3">        camera_frame = esp_camera_fb_get();</font>

  18. <font size="3">        if (camera_frame)</font>
  19. <font size="3">        {</font>
  20. <font size="3">            /* 以队列的形式发送 */</font>
  21. <font size="3">            xQueueSend(xQueueFrameO, &camera_frame, portMAX_DELAY);</font>
  22. <font size="3">        }</font>
  23. <font size="3">    }</font>
  24. <font size="3">}</font>

  25. <font size="3">/**</font>
  26. <font size="3"> * @brief       摄像头图像数据传入AI处理任务</font>
  27. <font size="3"> * @param       arg:未使用</font>
  28. <font size="3"> * @retval      无</font>
  29. <font size="3"> */</font>
  30. <font size="3">static void esp_ai_process_handler(void *arg)</font>
  31. <font size="3">{</font>
  32. <font size="3">    arg = arg;</font>
  33. <font size="3">    camera_fb_t *face_ai_frameI = NULL;</font>
  34. <font size="3">    camera_fb_t *face_ai_frameI2 = NULL;</font>

  35. <font size="3">    while(1)</font>
  36. <font size="3">    {</font>
  37. <font size="3">        /* 以队列的形式获取摄像头图像数据 */</font>
  38. <font size="3">        if (xQueueReceive(xQueueFrameO, &face_ai_frameI, portMAX_DELAY))</font>
  39. <font size="3">        {</font>
  40. <font size="3">            if (xQueueReceive(xQueueFrameO, &face_ai_frameI2, portMAX_DELAY))</font>
  41. <font size="3">            {</font>
  42. <font size="3">                /* 判断图像是否出现运动 */</font>
  43. <font size="3">                uint32_t moving_point_number = dl::image::</font>
  44. <font size="3">get_moving_point_number(</font>
  45. <font size="3">(uint16_t *)face_ai_frameI->buf,</font>
  46. <font size="3">(uint16_t *)face_ai_frameI2->buf,</font>
  47. <font size="3">face_ai_frameI->height,</font>
  48. <font size="3">face_ai_frameI->width, 8, 15);</font>

  49. <font size="3">                if (moving_point_number > 50)</font>
  50. <font size="3">                {</font>
  51. <font size="3">                    printf("Something moved\r\n");</font>
  52. <font size="3">                    /* 此处是在图像中绘画检测效果 */</font>
  53. <font size="3">                    dl::image::draw_filled_rectangle(</font>
  54. <font size="3">(uint16_t *)face_ai_frameI2->buf, </font>
  55. <font size="3">face_ai_frameI2->height, </font>
  56. <font size="3">face_ai_frameI2->width, 0, 0, 40, 40);</font>
  57. <font size="3">                }</font>
  58. <font size="3">                else</font>
  59. <font size="3">                {</font>
  60. <font size="3">                    printf("Something not moved\r\n");</font>
  61. <font size="3">                }</font>
  62. <font size="3">                </font>
  63. <font size="3">                esp_camera_fb_return(face_ai_frameI);</font>
  64. <font size="3">                /* 以队列的形式发送AI处理的图像 */</font>
  65. <font size="3">                xQueueSend(xQueueAIFrameO, &face_ai_frameI2, portMAX_DELAY);</font>
  66. <font size="3">            }</font>
  67. <font size="3">        }</font>
  68. <font size="3">    }</font>
  69. <font size="3">}</font>

  70. <font size="3">/**</font>
  71. <font size="3"> * @brief       AI图像数据开启</font>
  72. <font size="3"> * @param       无</font>
  73. <font size="3"> * @retval      1:创建任务及队列失败;0:创建任务及对了成功</font>
  74. <font size="3"> */</font>
  75. <font size="3">uint8_t esp_motion_detection_ai_strat(void)</font>
  76. <font size="3">{</font>
  77. <font size="3">    /* 创建队列及任务 */</font>
  78. <font size="3">    xQueueFrameO = xQueueCreate(5, sizeof(camera_fb_t *));</font>
  79. <font size="3">    xQueueAIFrameO = xQueueCreate(5, sizeof(camera_fb_t *));</font>
  80. <font size="3">xTaskCreatePinnedToCore(esp_camera_process_handler,</font>
  81. <font size="3">                       "esp_camera_process_handler", 4 * 1024, NULL, </font>
  82. <font size="3">5, &camera_task_handle, 1);</font>
  83. <font size="3">xTaskCreatePinnedToCore(esp_ai_process_handler, "esp_ai_process_handler", </font>
  84. <font size="3">6 * 1024, NULL, 5, &ai_task_handle, 1);</font>

  85. <font size="3">    if (xQueueFrameO != NULL </font>
  86. <font size="3">        || xQueueAIFrameO != NULL </font>
  87. <font size="3">        || camera_task_handle != NULL </font>
  88. <font size="3">        || ai_task_handle != NULL)</font>
  89. <font size="3">    {</font>
  90. <font size="3">        return 0;</font>
  91. <font size="3">    }</font>
  92. <font size="3">    </font>
  93. <font size="3">    return 1;</font>
  94. <font size="3">}</font>
复制代码
上述原理非常简单:只需要在ai_task_handle任务下获取两张图像数据,然后通过AI计算判断这两个图像是否匹配。如果图像不匹配,则说明当前处于运动状态;如果图像匹配,则说明当前图像处于相对静止状态,最后,我们使用消息队列将当前图像数据传输至LCD进行显示。

63.3 下载验证
程序下载成功后,当检测到图像变化时,图像左上角有蓝色块闪烁。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


关闭

原子哥极力推荐上一条 /1 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-11-27 23:07

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表