351浏览
查看: 351|回复: 0

[Micropython] Micropython玩转ESP32P4:绑定AI相关模块

[复制链接]
本帖最后由 PY学习笔记 于 2025-8-10 14:48 编辑

近期,dfrobot出了一款新的开发板——FireBeetle 2 ESP32-P4,板载ESP32P4,虽然没有内置的WIFI和BLE,但是它的性能十分的高,所以很有幸能体验到这款开发板
1.开发板介绍
FireBeetle 2 ESP32-P4有很多种外设:
  • Type-C USB CDC:Type-C USB烧录、调试接口
  • IO3/LED:板载LED引脚
  • Power LED:主板电源指示灯
  • RST:复位按键
  • IO35/BOOT:IO引脚/BOOT按键
  • MIC: MEMS PDM麦克风
  • HIGH-SPEED USB OTG 2.0: Type-C高速USB OTG 2.0
  • ESP32-P4:ESP32-P4芯片
  • MIPI-DSI: 两通道MIPI-DSI屏幕(兼容树莓派4B DSI屏幕线序)
  • MIPI-CSI: 两通道MIPI-DSI屏幕(兼容树莓派4B CSI摄像头线序)
  • TF Card: TF卡插槽
  • 16MB FLASH: 16MB Flash存储
  • ESP32-C6:ESP32-C6-MINI-1模组,通过SDIO与ESP32-P4连接,用于扩展WiFi、蓝牙

2.绑定AI相关模块
在github上面,有一个大佬分享了一个将EspDL绑定到micropython中,但是还少了一个猫咪识别的模型,所以自己做了一个,仓库地址如下:
接着需要将驱动也绑定进来,修改~/esp/micropython.cmake如下:
  1. include(${CMAKE_CURRENT_LIST_DIR}/micropython_csi_camera/micropython.cmake)
  2. include(${CMAKE_CURRENT_LIST_DIR}/mp_jpeg/src/micropython.cmake)
  3. include(${CMAKE_CURRENT_LIST_DIR}/mp_esp_dl_models/src/micropython.cmake)
复制代码
再次编译即可使用。
3.体验AI相关模块
首先是人脸检测:
  1. from espdl import FaceDetector
  2. from jpeg import Decoder, Encoder
  3. decoder = Decoder()
  4. # 捕获并处理图像
  5. img = open("human_face.jpg", "rb").read()  # 捕获原始图像(通常是JPEG格式)
  6. wh = decoder.get_img_info(img)# 获取图像的宽度和高度
  7. # 获取图像的宽度和高度
  8. width, height = wh
  9. encoder = Encoder(width=width, height=height, pixel_format="RGB888")
  10. face_detector = FaceDetector(width=width, height=height)
  11. framebuffer = decoder.decode(img)  # 转换为RGB888格式
  12. # 将memoryview转换为bytearray以便修改
  13. framebuffer = bytearray(framebuffer)
  14. # 运行人脸检测
  15. results = face_detector.run(framebuffer)
  16. # 绘制边框
  17. def draw_rectangle(buffer, width, height, x, y, w, h, list1, color=(255, 0, 0)):
  18.     """
  19.     在RGB888格式的图像缓冲区上绘制矩形边框
  20.     :param buffer: 图像缓冲区
  21.     :param width: 图像宽度
  22.     :param height: 图像高度
  23.     :param x: 边框左上角的x坐标
  24.     :param y: 边框左上角的y坐标
  25.     :param w: 边框宽度
  26.     :param h: 边框高度
  27.     :param color: 边框颜色(RGB格式)
  28.     """
  29.     # 辅助函数:设置单个像素的颜色
  30.     def set_pixel(buffer, width, x, y, color):
  31.         offset = (y * width + x) * 3
  32.         buffer[offset] = color[0]  # R
  33.         buffer[offset + 1] = color[1]  # G
  34.         buffer[offset + 2] = color[2]  # B
  35.     # 辅助函数:绘制更大的点
  36.     def draw_large_dot(buffer, width, x, y, color, size=3):
  37.         for i in range(x - size, x + size + 1):
  38.             for j in range(y - size, y + size + 1):
  39.                 if 0 <= i < width and 0 <= j < height:
  40.                     set_pixel(buffer, width, i, j, color)
  41.     # 绘制上边框
  42.     for i in range(x, x + w):
  43.         if 0 <= i < width and 0 <= y < height:
  44.             set_pixel(buffer, width, i, y, color)
  45.     # 绘制下边框
  46.     for i in range(x, x + w):
  47.         if 0 <= i < width and 0 <= y + h < height:
  48.             set_pixel(buffer, width, i, y + h, color)
  49.     # 绘制左边框
  50.     for j in range(y, y + h):
  51.         if 0 <= j < height and 0 <= x < width:
  52.             set_pixel(buffer, width, x, j, color)
  53.     # 绘制右边框
  54.     for j in range(y, y + h):
  55.         if 0 <= j < height and 0 <= x + w < width:
  56.             set_pixel(buffer, width, x + w, j, color)
  57.     # 绘制特征点
  58.     if list1:
  59.         draw_large_dot(buffer, width, list1[0], list1[1], (0, 0, 255), size=2)
  60.         draw_large_dot(buffer, width, list1[2], list1[3], (0, 0, 255), size=2)
  61.         draw_large_dot(buffer, width, list1[4], list1[5], (0, 255, 0), size=2)
  62.         draw_large_dot(buffer, width, list1[6], list1[7], (255, 0, 0), size=2)
  63.         draw_large_dot(buffer, width, list1[8], list1[9], (255, 0, 0), size=2)
  64. if results:
  65.     # 在图像上绘制人脸边框
  66.     for face in results:
  67.         print(face)
  68.         x1, y1, x2, y2 = face['box']
  69.         draw_rectangle(framebuffer, width, height, x1, y1, x2 - x1, y2 - y1, face['features'], color=(255, 0, 0))  # 使用红色边框
  70. # 将带有边框的图像重新编码为JPEG格式并保存
  71. marked_img = encoder.encode(framebuffer)
  72. with open("marked_image.jpg", "wb") as f:
  73.     f.write(marked_img)</span>
复制代码

Micropython玩转ESP32P4:绑定AI相关模块图1
行人检测:
  1. from espdl import HumanDetector
  2. from jpeg import Decoder, Encoder
  3. decoder = Decoder()
  4. encoder = Encoder(width=640, height=480,pixel_format="RGB888")
  5. human_detector = HumanDetector(width=640, height=480)
  6. # 捕获并处理图像
  7. img = open("pedestrian.jpg", "rb").read()  # 捕获原始图像(通常是JPEG格式)
  8. framebuffer = decoder.decode(img)  # 转换为RGB888格式
  9. # 将memoryview转换为bytearray以便修改
  10. framebuffer = bytearray(framebuffer)
  11. # 运行行人检测
  12. results = human_detector.run(framebuffer)
  13. # 绘制边框
  14. def draw_rectangle(buffer, width, height, x, y, w, h, color=(255, 0, 0)):
  15.     """
  16.     在RGB888格式的图像缓冲区上绘制矩形边框
  17.     :param buffer: 图像缓冲区
  18.     :param width: 图像宽度
  19.     :param height: 图像高度
  20.     :param x: 边框左上角的x坐标
  21.     :param y: 边框左上角的y坐标
  22.     :param w: 边框宽度
  23.     :param h: 边框高度
  24.     :param color: 边框颜色(RGB格式)
  25.     """
  26.     # 辅助函数:设置单个像素的颜色
  27.     def set_pixel(buffer, width, x, y, color):
  28.         offset = (y * width + x) * 3
  29.         buffer[offset] = color[0]  # R
  30.         buffer[offset + 1] = color[1]  # G
  31.         buffer[offset + 2] = color[2]  # B
  32.     # 绘制上边框
  33.     for i in range(x, x + w):
  34.         if 0 <= i < width and 0 <= y < height:
  35.             set_pixel(buffer, width, i, y, color)
  36.     # 绘制下边框
  37.     for i in range(x, x + w):
  38.         if 0 <= i < width and 0 <= y + h < height:
  39.             set_pixel(buffer, width, i, y + h, color)
  40.     # 绘制左边框
  41.     for j in range(y, y + h):
  42.         if 0 <= j < height and 0 <= x < width:
  43.             set_pixel(buffer, width, x, j, color)
  44.     # 绘制右边框
  45.     for j in range(y, y + h):
  46.         if 0 <= j < height and 0 <= x + w < width:
  47.             set_pixel(buffer, width, x + w, j, color)
  48. # 在图像上绘制边框
  49. for face in results:
  50.     print(face)
  51.     x1, y1, x2, y2 = face['box']
  52.     draw_rectangle(framebuffer, 640, 480, x1, y1, x2-x1, y2-y1, color=(255, 0, 0))  # 使用红色边框
  53.    
  54. # 将带有边框的图像重新编码为JPEG格式并保存
  55. marked_img = encoder.encode(framebuffer)
  56. with open("marked_image1.jpg", "wb") as f:
  57.     f.write(marked_img)
复制代码

Micropython玩转ESP32P4:绑定AI相关模块图2
猫咪检测:
  1. from espdl import CatDetector
  2. from jpeg import Decoder, Encoder
  3. decoder = Decoder()
  4. # 捕获并处理图像
  5. img = open("cat.jpg", "rb").read()  # 捕获原始图像(通常是JPEG格式)
  6. wh = decoder.get_img_info(img)# 获取图像的宽度和高度
  7. # 获取图像的宽度和高度
  8. width, height = wh
  9. encoder = Encoder(width=width, height=height, pixel_format="RGB888")
  10. cat_detector = CatDetector(width=width, height=height)
  11. framebuffer = decoder.decode(img)  # 转换为RGB888格式
  12. # 将memoryview转换为bytearray以便修改
  13. framebuffer = bytearray(framebuffer)
  14. # 运行猫咪检测
  15. results = cat_detector.run(framebuffer)
  16. # 绘制边框
  17. def draw_rectangle(buffer, width, height, x, y, w, h, list1, color=(255, 0, 0)):
  18.     """
  19.     在RGB888格式的图像缓冲区上绘制矩形边框
  20.     :param buffer: 图像缓冲区
  21.     :param width: 图像宽度
  22.     :param height: 图像高度
  23.     :param x: 边框左上角的x坐标
  24.     :param y: 边框左上角的y坐标
  25.     :param w: 边框宽度
  26.     :param h: 边框高度
  27.     :param color: 边框颜色(RGB格式)
  28.     """
  29.     # 辅助函数:设置单个像素的颜色
  30.     def set_pixel(buffer, width, x, y, color):
  31.         offset = (y * width + x) * 3
  32.         buffer[offset] = color[0]  # R
  33.         buffer[offset + 1] = color[1]  # G
  34.         buffer[offset + 2] = color[2]  # B
  35.     # 辅助函数:绘制更大的点
  36.     def draw_large_dot(buffer, width, x, y, color, size=3):
  37.         for i in range(x - size, x + size + 1):
  38.             for j in range(y - size, y + size + 1):
  39.                 if 0 <= i < width and 0 <= j < height:
  40.                     set_pixel(buffer, width, i, j, color)
  41.     # 绘制上边框
  42.     for i in range(x, x + w):
  43.         if 0 <= i < width and 0 <= y < height:
  44.             set_pixel(buffer, width, i, y, color)
  45.     # 绘制下边框
  46.     for i in range(x, x + w):
  47.         if 0 <= i < width and 0 <= y + h < height:
  48.             set_pixel(buffer, width, i, y + h, color)
  49.     # 绘制左边框
  50.     for j in range(y, y + h):
  51.         if 0 <= j < height and 0 <= x < width:
  52.             set_pixel(buffer, width, x, j, color)
  53.     # 绘制右边框
  54.     for j in range(y, y + h):
  55.         if 0 <= j < height and 0 <= x + w < width:
  56.             set_pixel(buffer, width, x + w, j, color)
  57.     # 绘制特征点
  58.     if list1:
  59.         draw_large_dot(buffer, width, list1[0], list1[1], (0, 0, 255), size=2)
  60.         draw_large_dot(buffer, width, list1[2], list1[3], (0, 0, 255), size=2)
  61.         draw_large_dot(buffer, width, list1[4], list1[5], (0, 255, 0), size=2)
  62.         draw_large_dot(buffer, width, list1[6], list1[7], (255, 0, 0), size=2)
  63.         draw_large_dot(buffer, width, list1[8], list1[9], (255, 0, 0), size=2)
  64. if results:
  65.     # 在图像上绘制边框
  66.     for face in results:
  67.         print(face)
  68.         x1, y1, x2, y2 = face['box']
  69.         draw_rectangle(framebuffer, width, height, x1, y1, x2 - x1, y2 - y1, None, color=(255, 0, 0))  # 使用红色边框
  70. # 将带有边框的图像重新编码为JPEG格式并保存
  71. marked_img = encoder.encode(framebuffer)
  72. with open("marked_image4.jpg", "wb") as f:
  73.     f.write(marked_img)
复制代码

Micropython玩转ESP32P4:绑定AI相关模块图3
yolo11分类:
  1. from espdl import CocoDetector
  2. from jpeg import Decoder, Encoder
  3. from myufont import CustomBMFont
  4. from machine import Pin,SDCard
  5. import os
  6. sd = SDCard(slot=0,width=4, sck=43, cmd=44, data=(39, 40, 41, 42))
  7. os.mount(sd, '/sd')
  8. decoder = Decoder()
  9. encoder = Encoder(width=405, height=540,pixel_format="RGB888")
  10. face_detector = CocoDetector(width=405, height=540)
  11. MSCOCO_CLASSES = [
  12.     "人", "自行车", "汽车", "摩托车", "飞机", "公共汽车", "火车", "卡车", "船", "交通灯",
  13.     "消防栓", "消防水带", "停车计时器", "长椅", "鸟", "猫", "狗", "马", "羊", "牛",
  14.     "大象", "熊", "斑马", "长颈鹿", "背包", "伞", "手提包", "领带", "行李箱", "飞盘",
  15.     "滑雪板", "滑雪杖", "滑板", "冲浪板", "网球拍", "瓶子", "酒杯", "杯子", "刀叉", "碗",
  16.     "香蕉", "苹果", "三明治", "橙子", "西兰花", "胡萝卜", "热狗", "披萨", "甜甜圈", "蛋糕",
  17.     "椅子", "沙发", "盆栽", "床", "餐桌", "马桶", "电视", "笔记本电脑", "鼠标", "遥控器",
  18.     "键盘", "手机", "微波炉", "烤箱", "烤面包机", "水槽", "冰箱", "书", "时钟", "花瓶",
  19.     "剪刀", "泰迪熊", "吹风机", "牙刷"
  20. ]
  21. font = CustomBMFont('/sd/text_full_16px_2312.v3.bmf')
  22. # 捕获并处理图像
  23. img = open("bus.jpg", "rb").read()  # 捕获原始图像(通常是JPEG格式)
  24. framebuffer = decoder.decode(img)  # 转换为RGB888格式
  25. # 将memoryview转换为bytearray以便修改
  26. framebuffer = bytearray(framebuffer)
  27. # 运行人脸检测
  28. results = face_detector.run(framebuffer)
  29. # 绘制边框
  30. def draw_rectangle(buffer, width, height, x, y, w, h,font,label, color=(255, 0, 0)):
  31.     """
  32.     在RGB888格式的图像缓冲区上绘制矩形边框
  33.     :param buffer: 图像缓冲区
  34.     :param width: 图像宽度
  35.     :param height: 图像高度
  36.     :param x: 边框左上角的x坐标
  37.     :param y: 边框左上角的y坐标
  38.     :param w: 边框宽度
  39.     :param h: 边框高度
  40.     :param color: 边框颜色(RGB格式)
  41.     """
  42.     # 辅助函数:设置单个像素的颜色
  43.     def set_pixel(buffer, width, x, y, color):
  44.         offset = (y * width + x) * 3
  45.         buffer[offset] = color[0]  # R
  46.         buffer[offset + 1] = color[1]  # G
  47.         buffer[offset + 2] = color[2]  # B
  48.     def is_chinese(ch):
  49.         """判断一个字符是否为中文字符"""
  50.         if '\u4e00' <= ch <= '\u9fff' or \
  51.            '\u3400' <= ch <= '\u4dbf' or \
  52.            '\u20000' <= ch <= '\u2a6df':
  53.             return True
  54.         return False
  55.     def text(font, text, x_start, y_start, color,spacing=0, line_spacing=0, max_width=width):
  56.         font_size = font.font_size
  57.         bytes_per_row = (font_size + 7) // 8  # 每行占用的字节数
  58.         x, y = x_start, y_start
  59.         
  60.         for char in text:
  61.             # 处理换行符
  62.             if char == '\n':
  63.                 y += font_size + line_spacing
  64.                 x = x_start
  65.                 continue
  66.             if char == '\r':
  67.                 x += 2*font_size
  68.                 continue
  69.             # 获取字符宽度(中文字符全宽,ASCII字符半宽)
  70.             char_width = font_size if is_chinese(char) else font_size // 2
  71.             
  72.             # 检查是否需要换行
  73.             if max_width is not None and x + char_width > x_start + max_width:
  74.                 y += font_size + line_spacing
  75.                 x = x_start
  76.             
  77.             # 获取字符位图
  78.             bitmap = font.get_char_bitmap(char)
  79.             
  80.             # 绘制字符
  81.             for row in range(font_size):
  82.                 for col in range(char_width if not is_chinese(char) else font_size):
  83.                     byte_idx = row * bytes_per_row + col // 8
  84.                     bit_mask = 0x80 >> (col % 8)
  85.                     
  86.                     if byte_idx < len(bitmap) and (bitmap[byte_idx] & bit_mask):
  87.                         set_pixel(framebuffer,max_width,x + col, y + row, color)
  88.             
  89.             # 移动到下一个字符位置
  90.             x += char_width + spacing
  91.     # 绘制上边框
  92.     for i in range(x, x + w):
  93.         if 0 <= i < width and 0 <= y < height:
  94.             set_pixel(buffer, width, i, y, color)
  95.     # 绘制下边框
  96.     for i in range(x, x + w):
  97.         if 0 <= i < width and 0 <= y + h < height:
  98.             set_pixel(buffer, width, i, y + h, color)
  99.     # 绘制左边框
  100.     for j in range(y, y + h):
  101.         if 0 <= j < height and 0 <= x < width:
  102.             set_pixel(buffer, width, x, j, color)
  103.     # 绘制右边框
  104.     for j in range(y, y + h):
  105.         if 0 <= j < height and 0 <= x + w < width:
  106.             set_pixel(buffer, width, x + w, j, color)
  107.     text(font,label, x, y-20, color)
  108. # 在图像上绘制人脸边框
  109. for face in results:
  110.     #print(face)
  111.     x1, y1, x2, y2 = face['box']
  112.     label = MSCOCO_CLASSES[face['category']]+":"+str(int(face['score']*100))+"%"
  113.     draw_rectangle(framebuffer, 405, 540, x1, y1, x2-x1, y2-y1,font,label)  # 使用红色边框
  114.     print(label)
  115. # 将带有边框的图像重新编码为JPEG格式并保存
  116. marked_img = encoder.encode(framebuffer)
  117. with open("marked_image2.jpg", "wb") as f:
  118.     f.write(marked_img)
复制代码

Micropython玩转ESP32P4:绑定AI相关模块图4
Micropython玩转ESP32P4:绑定AI相关模块图5

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

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail