超级版主
 
- 积分
- 5165
- 金钱
- 5165
- 注册时间
- 2019-5-8
- 在线时间
- 1291 小时
|
|
第六十一章 二维码识别实验
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
乐鑫ESP-WHO二维码识别是乐鑫公司推出的二维码识别技术。该技术通过特定的算法和程序,可以快速、准确地识别二维码,读取其中的数据并进行相应的处理。本章,我们使用乐鑫AI库来实现二维码识别功能。
本章分为如下几个部分:
61.1 硬件设计
61.2 软件设计
61.3 下载验证
61.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的内部资源,因此并没有相应的连接原理图。
61.2 软件设计
61.2.1 程序流程图
程序流程图能帮助我们更好的理解一个工程的功能和实现的过程,对学习和设计工程有很好的主导作用。下面看看本实验的程序流程图:
图61.2.1.1 程序流程图
61.2.2 程序解析
在本章节中,我们将重点关注两个文件:esp_qr_detection.cpp和esp_qr_detection.hpp。其中,esp_qr_detection.hpp主要声明了esp_qr_detection函数,其内容相对简单,因此我们暂时不作详细解释。本章节的核心关注点是esp_qr_detection.cpp文件中的函数。
接下来,我们将详细解析esp_qr_detection_ai_strat函数的工作原理。
- <font size="3">TaskHandle_t camera_task_handle;</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] camera_frame:图像指针</font>
- <font size="3"> * @retval 无</font>
- <font size="3"> */</font>
- <font size="3">extern "C" void esp_qr_scanner(camera_fb_t *camera_frame)</font>
- <font size="3">{</font>
- <font size="3"> esp_image_scanner_t *esp_scn = esp_code_scanner_create();</font>
- <font size="3">esp_code_scanner_config_t config = {ESP_CODE_SCANNER_MODE_FAST,</font>
- <font size="3"> ESP_CODE_SCANNER_IMAGE_RGB565,</font>
- <font size="3"> camera_frame->width, camera_frame->height};</font>
- <font size="3"> esp_code_scanner_set_config(esp_scn, config);</font>
- <font size="3"> int decoded_num = esp_code_scanner_scan_image(esp_scn, camera_frame->buf);</font>
- <font size="3"> if (decoded_num)</font>
- <font size="3"> {</font>
- <font size="3"> dl::image::draw_filled_rectangle((uint16_t *)camera_frame->buf,</font>
- <font size="3"> camera_frame->height, </font>
- <font size="3">camera_frame->width, 0, 0, 20, 20);</font>
- <font size="3"> esp_code_scanner_symbol_t result = esp_code_scanner_result(esp_scn);</font>
- <font size="3"> printf("Decoded %s symbol "%s"\n", result.type_name, result.data);</font>
- <font size="3"> }</font>
- <font size="3"> </font>
- <font size="3"> esp_code_scanner_destroy(esp_scn);</font>
- <font size="3">}</font>
- <font size="3">/**</font>
- <font size="3"> * @brief 摄像头图像数据获取任务</font>
- <font size="3"> * @param 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"> esp_qr_scanner(camera_frame);</font>
- <font size="3"> /* 以队列的形式发送 */</font>
- <font size="3"> xQueueSend(xQueueAIFrameO, &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 无</font>
- <font size="3"> * @retval 1:创建任务及队列失败;0:创建任务及对了成功</font>
- <font size="3"> */</font>
- <font size="3">uint8_t esp_qr_detection_ai_strat(void)</font>
- <font size="3">{</font>
- <font size="3"> /* 创建队列及任务 */</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, </font>
- <font size="3">NULL, 5, &camera_task_handle, 1);</font>
- <font size="3"> </font>
- <font size="3"> if (xQueueAIFrameO != NULL </font>
- <font size="3"> || camera_task_handle != NULL)</font>
- <font size="3"> {</font>
- <font size="3"> return 0;</font>
- <font size="3"> }</font>
- <font size="3"> return 1;</font>
- <font size="3">}</font>
复制代码 在上述源码中,我们首先创建了一个消息队列和一个AI处理任务。消息队列用于传输图像数据,而AI处理任务则负责获取图像数据并进行二维码识别。如果识别成功,串口将打印成功的内容;如果识别失败,串口将打印失败的内容。最后,我们使用消息队列将当前图像数据传输至LCD进行显示。
61.3 下载验证
程序下载成功后,如果在检测过程中发现二维码,该系统会对当前二维码进行解码,并把解码内容以串口形式输出。另外,当检测二维码时,图像左上角显示蓝色矩形弹出,如下图。
图61.3.1 二维码识别效果图 |
|