超级版主
 
- 积分
- 5173
- 金钱
- 5173
- 注册时间
- 2019-5-8
- 在线时间
- 1295 小时
|
|
第六十三章 运动侦测实验
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
乐鑫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 程序流程图
程序流程图能帮助我们更好的理解一个工程的功能和实现的过程,对学习和设计工程有很好的主导作用。下面看看本实验的程序流程图:
图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函数的工作原理。
- <font size="3">TaskHandle_t camera_task_handle;</font>
- <font size="3">TaskHandle_t ai_task_handle;</font>
- <font size="3">QueueHandle_t xQueueFrameO = NULL;</font>
- <font size="3">QueueHandle_t xQueueAIFrameO = NULL;</font>
- <font size="3">/**</font>
- <font size="3"> * @brief 摄像头图像数据获取任务</font>
- <font size="3"> * [url=home.php?mod=space&uid=271674]@param[/url] arg:未使用</font>
- <font size="3"> * @retval 无</font>
- <font size="3"> */</font>
- <font size="3">static void esp_camera_process_handler(void *arg)</font>
- <font size="3">{</font>
- <font size="3"> arg = arg;</font>
- <font size="3"> camera_fb_t *camera_frame = NULL;</font>
- <font size="3"> while (1)</font>
- <font size="3"> {</font>
- <font size="3"> /* 获取摄像头图像 */</font>
- <font size="3"> camera_frame = esp_camera_fb_get();</font>
- <font size="3"> if (camera_frame)</font>
- <font size="3"> {</font>
- <font size="3"> /* 以队列的形式发送 */</font>
- <font size="3"> xQueueSend(xQueueFrameO, &camera_frame, portMAX_DELAY);</font>
- <font size="3"> }</font>
- <font size="3"> }</font>
- <font size="3">}</font>
- <font size="3">/**</font>
- <font size="3"> * @brief 摄像头图像数据传入AI处理任务</font>
- <font size="3"> * @param arg:未使用</font>
- <font size="3"> * @retval 无</font>
- <font size="3"> */</font>
- <font size="3">static void esp_ai_process_handler(void *arg)</font>
- <font size="3">{</font>
- <font size="3"> arg = arg;</font>
- <font size="3"> camera_fb_t *face_ai_frameI = NULL;</font>
- <font size="3"> camera_fb_t *face_ai_frameI2 = NULL;</font>
- <font size="3"> while(1)</font>
- <font size="3"> {</font>
- <font size="3"> /* 以队列的形式获取摄像头图像数据 */</font>
- <font size="3"> if (xQueueReceive(xQueueFrameO, &face_ai_frameI, portMAX_DELAY))</font>
- <font size="3"> {</font>
- <font size="3"> if (xQueueReceive(xQueueFrameO, &face_ai_frameI2, portMAX_DELAY))</font>
- <font size="3"> {</font>
- <font size="3"> /* 判断图像是否出现运动 */</font>
- <font size="3"> uint32_t moving_point_number = dl::image::</font>
- <font size="3">get_moving_point_number(</font>
- <font size="3">(uint16_t *)face_ai_frameI->buf,</font>
- <font size="3">(uint16_t *)face_ai_frameI2->buf,</font>
- <font size="3">face_ai_frameI->height,</font>
- <font size="3">face_ai_frameI->width, 8, 15);</font>
- <font size="3"> if (moving_point_number > 50)</font>
- <font size="3"> {</font>
- <font size="3"> printf("Something moved\r\n");</font>
- <font size="3"> /* 此处是在图像中绘画检测效果 */</font>
- <font size="3"> dl::image::draw_filled_rectangle(</font>
- <font size="3">(uint16_t *)face_ai_frameI2->buf, </font>
- <font size="3">face_ai_frameI2->height, </font>
- <font size="3">face_ai_frameI2->width, 0, 0, 40, 40);</font>
- <font size="3"> }</font>
- <font size="3"> else</font>
- <font size="3"> {</font>
- <font size="3"> printf("Something not moved\r\n");</font>
- <font size="3"> }</font>
- <font size="3"> </font>
- <font size="3"> esp_camera_fb_return(face_ai_frameI);</font>
- <font size="3"> /* 以队列的形式发送AI处理的图像 */</font>
- <font size="3"> xQueueSend(xQueueAIFrameO, &face_ai_frameI2, portMAX_DELAY);</font>
- <font size="3"> }</font>
- <font size="3"> }</font>
- <font size="3"> }</font>
- <font size="3">}</font>
- <font size="3">/**</font>
- <font size="3"> * @brief AI图像数据开启</font>
- <font size="3"> * @param 无</font>
- <font size="3"> * @retval 1:创建任务及队列失败;0:创建任务及对了成功</font>
- <font size="3"> */</font>
- <font size="3">uint8_t esp_motion_detection_ai_strat(void)</font>
- <font size="3">{</font>
- <font size="3"> /* 创建队列及任务 */</font>
- <font size="3"> xQueueFrameO = xQueueCreate(5, sizeof(camera_fb_t *));</font>
- <font size="3"> xQueueAIFrameO = xQueueCreate(5, sizeof(camera_fb_t *));</font>
- <font size="3">xTaskCreatePinnedToCore(esp_camera_process_handler,</font>
- <font size="3"> "esp_camera_process_handler", 4 * 1024, NULL, </font>
- <font size="3">5, &camera_task_handle, 1);</font>
- <font size="3">xTaskCreatePinnedToCore(esp_ai_process_handler, "esp_ai_process_handler", </font>
- <font size="3">6 * 1024, NULL, 5, &ai_task_handle, 1);</font>
- <font size="3"> if (xQueueFrameO != NULL </font>
- <font size="3"> || xQueueAIFrameO != NULL </font>
- <font size="3"> || camera_task_handle != NULL </font>
- <font size="3"> || ai_task_handle != NULL)</font>
- <font size="3"> {</font>
- <font size="3"> return 0;</font>
- <font size="3"> }</font>
- <font size="3"> </font>
- <font size="3"> return 1;</font>
- <font size="3">}</font>
复制代码 上述原理非常简单:只需要在ai_task_handle任务下获取两张图像数据,然后通过AI计算判断这两个图像是否匹配。如果图像不匹配,则说明当前处于运动状态;如果图像匹配,则说明当前图像处于相对静止状态,最后,我们使用消息队列将当前图像数据传输至LCD进行显示。
63.3 下载验证
程序下载成功后,当检测到图像变化时,图像左上角有蓝色块闪烁。
|
|