logo

【OpenMV4 H7 固件4.8.1】加载SD卡BMP图片后卡死,无法显示图像

2026-01-27 11:48
P
pflh369ho0g6

【OpenMV4 H7 固件4.8.1】加载SD卡BMP图片后卡死,无法显示图像

用户使用 OpenMV4 H7(固件 v4.8.1)运行一段从 SD 卡加载并显示 BMP 图片的代码,程序能成功加载图片(打印 ✅ 成功加载图片),但后续卡死,无法在 IDE 预览窗口中显示 test.bmp。代码在 draw_image() 后陷入停滞,无报错退出,IDE 画面冻结。

  • 故障类型:代码报错(实际表现为运行卡死 / 无响应)
  • 硬件型号:OpenMV4 H7
  • 固件版本:4.8.1
  • IDE 环境:OpenMV IDE v4.8.1;MicroPython v1.26.0-77
  • 关键现象:控制台打印 '✅ 成功加载图片',但之后无图像渲染,IDE 预览窗口黑屏或残留上一帧,程序不继续执行循环
import sensor, image, time, os


# 配置参数

SD_IMAGE_PATH = "/sdcard/test.bmp"  # SD卡中BMP图片的路径


# 初始化摄像头

sensor.reset()

sensor.set_pixformat(sensor.RGB565)

sensor.set_framesize(sensor.QQVGA)

sensor.skip_frames(time=2000)


# 加载SD卡中的BMP图片

try:

    bmp_img = image.Image(SD_IMAGE_PATH, sensor.RGB565)

    print("✅ 成功加载图片: %s" % SD_IMAGE_PATH)

except Exception as e:

    print("❌ 加载图片失败: %s" % e)

    while True:

        img = sensor.snapshot()

        img.draw_string(10, 10, "加载图片失败!", color=(255,0,0), scale=2)

        time.sleep(1)


# 显示图片到IDE缓冲区

while True:

    # 获取摄像头帧缓冲区

    frame = sensor.snapshot()

    # 清除帧缓冲区内容(避免叠加摄像头画面)

    frame.draw_rectangle(0, 0, frame.width(), frame.height(), color=(0,0,0), fill=True)

    # 计算缩放比例,保持图片比例并适配窗口

    x_scale = float(frame.width()) / bmp_img.width()

    y_scale = float(frame.height()) / bmp_img.height()

    scale = min(x_scale, y_scale)

    # 居中绘制图片

    frame.draw_image(bmp_img, (frame.width() - int(bmp_img.width() * scale))//2,

                     (frame.height() - int(bmp_img.height() * scale))//2,

                     x_scale=scale, y_scale=scale)

    # 等待刷新

    time.sleep(100)
2026-01-27 15:19
Z
星小瞳xs

time.sleep(num)是直接睡眠num秒,你提供的代码,一个睡1s,一个睡100s,基本上没有刷新率了,表现上就是卡死,实际上并没有卡死

Z
星小瞳xs
import sensor, image, time, os



# 初始化摄像头

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time=2000)


# 加载SD卡中的BMP图片

SD_IMAGE_PATH = "test.bmp"  # SD卡中BMP图片的路径

try:
    bmp_img = image.Image(SD_IMAGE_PATH, sensor.RGB565)
    print("✅ 成功加载图片: %s" % SD_IMAGE_PATH)

except Exception as e:
    print("❌ 加载图片失败: %s" % e)
    while True:
        img = sensor.snapshot()
        img.draw_string(10, 10, "加载图片失败!",color=(255,0,0), scale=2)
        # time.sleep(1)


# 显示图片到IDE缓冲区

while True:

    # 获取摄像头帧缓冲区

    frame = sensor.snapshot()

    # 清除帧缓冲区内容(避免叠加摄像头画面)

    frame.draw_rectangle(0, 0, frame.width(), frame.height(), color=(0,0,0), fill=True)

    # 计算缩放比例,保持图片比例并适配窗口

    x_scale = float(frame.width()) / bmp_img.width()

    y_scale = float(frame.height()) / bmp_img.height()

    scale = min(x_scale, y_scale)

    # 居中绘制图片

    frame.draw_image(bmp_img, (frame.width() - int(bmp_img.width() * scale))//2,

                     (frame.height() - int(bmp_img.height() * scale))//2,

                     x_scale=scale, y_scale=scale)

    # 等待刷新

    # time.sleep(100)