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

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

[复制链接]
本帖最后由 PY学习笔记 于 2025-8-9 19:07 编辑

近期,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. decoder = Decoder()
  4. encoder = Encoder(width=405, height=540,pixel_format="RGB888")
  5. coco_detector = CocoDetector(width=405, height=540)
  6. MSCOCO_CLASSES = [
  7.     "人", "自行车", "汽车", "摩托车", "飞机", "公共汽车", "火车", "卡车", "船", "交通灯",
  8.     "消防栓", "消防水带", "停车计时器", "长椅", "鸟", "猫", "狗", "马", "羊", "牛",
  9.     "大象", "熊", "斑马", "长颈鹿", "背包", "伞", "手提包", "领带", "行李箱", "飞盘",
  10.     "滑雪板", "滑雪杖", "滑板", "冲浪板", "网球拍", "瓶子", "酒杯", "杯子", "刀叉", "碗",
  11.     "香蕉", "苹果", "三明治", "橙子", "西兰花", "胡萝卜", "热狗", "披萨", "甜甜圈", "蛋糕",
  12.     "椅子", "沙发", "盆栽", "床", "餐桌", "马桶", "电视", "笔记本电脑", "鼠标", "遥控器",
  13.     "键盘", "手机", "微波炉", "烤箱", "烤面包机", "水槽", "冰箱", "书", "时钟", "花瓶",
  14.     "剪刀", "泰迪熊", "吹风机", "牙刷"
  15. ]
  16. # 捕获并处理图像
  17. img = open("bus.jpg", "rb").read()  # 捕获原始图像(通常是JPEG格式)
  18. framebuffer = decoder.decode(img)  # 转换为RGB888格式
  19. # 将memoryview转换为bytearray以便修改
  20. framebuffer = bytearray(framebuffer)
  21. # 运行yolo11分类
  22. results = coco_detector.run(framebuffer)
  23. # 绘制边框
  24. def draw_rectangle(buffer, width, height, x, y, w, h, color=(255, 0, 0)):
  25.     """
  26.     在RGB888格式的图像缓冲区上绘制矩形边框
  27.     :param buffer: 图像缓冲区
  28.     :param width: 图像宽度
  29.     :param height: 图像高度
  30.     :param x: 边框左上角的x坐标
  31.     :param y: 边框左上角的y坐标
  32.     :param w: 边框宽度
  33.     :param h: 边框高度
  34.     :param color: 边框颜色(RGB格式)
  35.     """
  36.     # 辅助函数:设置单个像素的颜色
  37.     def set_pixel(buffer, width, x, y, color):
  38.         offset = (y * width + x) * 3
  39.         buffer[offset] = color[0]  # R
  40.         buffer[offset + 1] = color[1]  # G
  41.         buffer[offset + 2] = color[2]  # B
  42.     # 绘制上边框
  43.     for i in range(x, x + w):
  44.         if 0 <= i < width and 0 <= y < height:
  45.             set_pixel(buffer, width, i, y, color)
  46.     # 绘制下边框
  47.     for i in range(x, x + w):
  48.         if 0 <= i < width and 0 <= y + h < height:
  49.             set_pixel(buffer, width, i, y + h, color)
  50.     # 绘制左边框
  51.     for j in range(y, y + h):
  52.         if 0 <= j < height and 0 <= x < width:
  53.             set_pixel(buffer, width, x, j, color)
  54.     # 绘制右边框
  55.     for j in range(y, y + h):
  56.         if 0 <= j < height and 0 <= x + w < width:
  57.             set_pixel(buffer, width, x + w, j, color)
  58. # 在图像上绘制人脸边框
  59. for face in results:
  60.     #print(face)
  61.     x1, y1, x2, y2 = face['box']
  62.     draw_rectangle(framebuffer, 405, 540, x1, y1, x2-x1, y2-y1, color=(255, 0, 0))  # 使用红色边框
  63.     print(MSCOCO_CLASSES[face['category']],end=":")
  64.     print(str(int(face['score']*100))+"%")
  65. # 将带有边框的图像重新编码为JPEG格式并保存
  66. marked_img = encoder.encode(framebuffer)
  67. with open("marked_image2.jpg", "wb") as f:
  68.     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