dual_core_vision_and_speech.py

# 本作品采用MIT许可证授权。
# 版权所有 (c) 2013-2026 OpenMV LLC。保留所有权利。
# https://github.com/openmv/openmv/blob/master/LICENSE
#
# 双核视觉与语音示例
#
# 此示例在HP核上运行BlazeFace人脸检测,同时在HE核上运行
# MicroSpeech关键词检测器。最近听到的
# 关键词会绘制在相机图像上。
#
# 内置的MicroSpeech模型只能识别关键词“Yes”和“No”。

import csi
import time
import openamp
import ml
from ml.postprocessing.mediapipe import BlazeFace


label = None
label_ticks = 0
LABEL_HOLD_MS = 2000


def task_callback(src_addr, data):
    global label, label_ticks
    label = data.decode()
    label_ticks = time.ticks_ms()


# 这个异步函数在HE核上运行。
@openamp.async_remote(task_callback)
async def task1(ept):
    from ml.apps import MicroSpeech
    speech = MicroSpeech(gain_db=24)
    while True:
        l, scores = speech.listen(timeout=0, threshold=0.70)
        if l:
            ept.send(l)


# 在HP核上初始化相机之前先启动HE核。
rproc = openamp.RemoteProc(0x80320000)
rproc.start()

csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.VGA)

# BlazeFace需要方形图像以获得最佳效果。
csi0.window((400, 400))

model = ml.Model("/rom/blazeface_front_128.tflite", postprocess=BlazeFace(threshold=0.4))
print(model)

clock = time.clock()
while True:
    clock.tick()
    img = csi0.snapshot()

    # faces是由((x, y, w, h), score, keypoints)元组组成的列表
    for r, score, keypoints in model.predict([img]):
        ml.utils.draw_predictions(img, [r], ("face",), ((0, 0, 255),), format=None)
        ml.utils.draw_keypoints(img, keypoints, color=(255, 0, 0))

    if label is not None:
        if time.ticks_diff(time.ticks_ms(), label_ticks) < LABEL_HOLD_MS:
            img.draw_string((4, 4), f"Heard: {label}", color=(255, 0, 0), scale=2)
        else:
            label = None

    print(clock.fps(), "fps")

results matching ""

    No results matching ""