logo

OpenMV LCD显示与蜂鸣器同时使用时蜂鸣器仅响一次的问题

2026-01-18 16:34
4
4pqqadtr4n2v

OpenMV LCD显示与蜂鸣器同时使用时蜂鸣器仅响一次的问题

用户在使用OpenMV Cam连接官方LCD屏时,发现当同时启用蜂鸣器报警和LCD实时画面显示功能时,蜂鸣器只能成功响一次,之后便无法再次触发。而打印日志显示检测逻辑正常运行,说明问题出在硬件控制环节。

具体现象如下:

  • 口罩识别功能基于官方示例代码和模型实现,运行正常
  • 蜂鸣器通过P1引脚输出高电平驱动,使用pyb.Pin.OUT_PP模式
  • 调用beep_once(1000)函数可使蜂鸣器响一次,但后续调用无效
  • 注释掉lcd.write(img_128)后,蜂鸣器可重复正常工作
  • 固件版本为4.5.4




连的官方LCD屏的,功能是LCD屏显示实时画面,口罩识别后蜂鸣器响,用的高电平响蜂鸣器, 不知道为什么这两句beep_once(1000) 和 lcd.write(img_128)冲突,蜂鸣器只能响一次,后面就不响了, print("检测到目标,触发蜂鸣器!")是正常运作的,lcd的第二句注释掉就能正常响了,不知道为什么,固件库是4.5.4,
口罩识别用的官方演示的代码和模型

# Untitled - By: Demo - Thu Jan 15 20264.5.4



import sensor

import time

import display

import image

import tf

import  os

import  pyb



sensor.reset()                         # Reset and initialize the sensor.

sensor.set_pixformat(sensor.RGB565)    # Set pixel format to RGB565 (or GRAYSCALE)

sensor.set_framesize(sensor.QVGA)      # Set frame size to QVGA (320x240)

sensor.set_windowing((240, 240))       # Set 240x240 window.

sensor.skip_frames(time=2000)          # Let the camera adjust.



net = "trained.tflite"

labels = [line.rstrip('\n') for line in open("labels.txt")]



lcd = display.SPIDisplay()



clock = time.clock()





def process_frame(img_320):

    """处理单帧图像:320x240 -> 128x160"""

    # 创建目标图像

    img_128 = image.Image(128, 160, img_320.format())



    # 计算缩放比例(填充式)

    scale_w = 128 / 240

    scale_h = 160 / 240



    # 根据第二张图片的说明,使用AREA算法进行缩小

    img_128.draw_image(img_320, 0, 0,

                      x_scale=scale_w,

                      y_scale=scale_h,

                      hint=image.AREA)  # AREA算法适合缩小图像



    return img_128







buzzer = pyb.Pin("P1", pyb.Pin.OUT_PP)

# 蜂鸣器鸣叫函数

def beep_once(duration_ms=500):

    buzzer.high()           # 设置 P1 为高电平,启动蜂鸣器

    pyb.delay(duration_ms)  # 维持高电平一段时间(毫秒)

    buzzer.low()            # 设置 P1 为低电平,关闭蜂鸣器





while True:

    clock.tick()



    img = sensor.snapshot()



    #beep_once()



    #pyb.delay(2000)

    # default settings just do one detection... change them to search the image...





    for obj in tf.classify(net, img, min_scale=1.0, scale_mul=0.8, x_overlap=0.5, y_overlap=0.5):

        print("**********\nPredictions at [x=%d,y=%d,w=%d,h=%d]" % obj.rect())

        img.draw_rectangle(obj.rect())

        # This combines the labels and confidence values into a list of tuples

        predictions_list = list(zip(labels, obj.output()))



        for i in range(len(predictions_list)):

            print("%s = %f" % (predictions_list[i][0], predictions_list[i][1]))





            if predictions_list[i][0]== 'face' and  predictions_list[i][1]>0.5:

              print("检测到目标,触发蜂鸣器!")

              beep_once(1000)  # 蜂鸣器响 500 毫秒(0.5 秒)



            # 等待 2 秒后再次鸣叫

    img_320 = sensor.snapshot()

    img_128 = process_frame(img_320)

    lcd.write(img_128)
2026-01-19 17:13
Z
星小瞳xs

更新到最新固件后重新尝试