Giter Club home page Giter Club logo

kws_demo's Introduction

Key Word Spotting

使用方法

1. 初始化kws, 并配置好唤醒词

from stream_kws_ctc import KeyWordSpotter

kws = KeyWordSpotter(
                     ckpt_path='model/avg_30.pt',
                     config_path='model/config.yaml',
                     token_path='model/tokens.txt',
                     lexicon_path='model/lexicon.txt',
                     threshold=0.02,
                     min_frames=5,
                     max_frames=250,
                     interval_frames=50,
                     score_beam=3,
                     path_beam=20,
                     gpu=-1,
                     is_jit_model=False,)

kws.set_keywords("你好小镜")

其中各参数意义如下:

ckpt_path: 模型路径
config_path: 模型对应配置文件路径
token_path: 字典路径, 
  实际模型可以检测的字符集合, 不在此集合中的字会被检测成此集合同音字
lexicon_path: 词典路径, 
  给出了每个词到字典中基本单元的映射关系, 可以看到一些字被映射成了同音字
threshold: 唤醒词检测阈值, 
  为大于零的一个数值, 数值越大则越不容易检测出来唤醒词(同时越不容易误检)
min_frames: 唤醒词最小的帧数, 
  1帧对应0.01s, 如果检出唤醒词时长低于此帧数, 会直接忽略视为没有唤醒词
max_frames: 唤醒词最大的帧数, 
  1帧对应0.01s, 如果检出唤醒词时长高于此帧数, 会直接忽略视为没有唤醒词
interval_frames: 连续两个唤醒词之间间隔帧数, 
  1帧对应0.01s, 如果第二个唤醒词检出时间距离上一个的时间间隔低于此帧数, 第二个唤醒词被忽略
score_beam: 唤醒词解码时, 每帧候选结果数量限制值, 
  每一帧只留下有限个与唤醒词发音相同的概率值参与解码搜索
path_beam: 唤醒词解码时, 所有时刻总的候选解码路径数量限制值, 
  任何时刻最多保留一定数量的解码路径
gpu: 使用GPU的序号, 
  -1表示不使用GPU, 大于等于0时表示选用机器上存在的某一块GPU
is_jit_model: 是否是JITScript模型, 
  默认提供的都是torch的checkpoint模型

2. 输入音频

注意,算法只支持16k采样率,16bit的单通道音频输入

import librosa

wav_path = '/path/to/your/wave'

y, _ = librosa.load(wav_path, sr=16000, mono=True)
 # NOTE: model supports 16k sample_rate
wav = (y * (1 << 15)).astype("int16").tobytes()

3. 流式检测

# We inference every 0.3 seconds, in streaming fashion.
interval = int(0.3 * 16000) * 2
for i in range(0, len(wav), interval):
    chunk_wav = wav[i: min(i + interval, len(wav))]
    result = kws.forward(chunk_wav)
    print(result)

4. 输出格式

输出格式为dict,包含state, keyword, start, end, score五个元素

"state": 关键词检测状态, 1表示检出,0表示未检出
"keyword": 检测出的关键词
"start": 关键词起始时间,以此次连接为起始时刻
"end": 关键词结束时间
"score": 关键词的分数,越高则表示关键词置信度越高

如果检测出了关键词,那么结果格式如下
{
    "state": 1,
    "keyword": keyword,
    "start": start,
    "end": end,
    "score": score
}
否则,结果格式如下:
{
    "state": 0,
    "keyword": None,
    "start": None,
    "end": None,
    "score": None
}

kws_demo's People

Contributors

duj12 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

demingcheng

kws_demo's Issues

wekws-ctc训练问题

hi Jean Du,
我尝试在你开源的dctcn预训练模型(mobvoi_kws_transcription/23.pt)基础上使用CTC Loss训练“你好问问/hi小问”唤醒词,目前训练5个epoch,发现cv_acc特别低,且loss下降缓慢。

INFO Epoch 0 CV info cv_loss 60.06750510101584 cv_acc 32.3473716624177
INFO Epoch 5 CV info cv_loss 59.136570353712315 cv_acc 33.24977346669095

请问是否正常?

大神,请教一下哈,为什么时间久了就唤醒不了了?

这是我的代码:
`def detection(audio, kw):
if kw=='nihaoxiaojing':
kws=kws_xiaojing
elif kw=='hixiaowen' or kw=='nihaowenwen':
kws=kws_xiaowen

else:  # for other input data, we recommend xiaowen model
   kws=kws_xiaowen

kws.reset_all()
if audio is None:
    return "Input Error! Please enter one audio!"

# with wave.open(audio, 'rb') as fin:
#     assert fin.getnchannels() == 1
#     wav = fin.readframes(fin.getnframes())

y, _ = librosa.load(audio, sr=16000)
# NOTE: model supports 16k sample_rate
wav = (y * (1 << 15)).astype("int16").tobytes()

# We inference every 0.3 seconds, in streaming fashion.
interval = int(0.3 * 16000) * 2
for i in range(0, len(wav), interval):
    chunk_wav = wav[i: min(i + interval, len(wav))]
    result = kws.forward(chunk_wav)
    if 'state' in result and result['state'] == 1:
        keyword = result['keyword']
        start = result['start']
        end = result['end']
        txt = f'Activated: Detect {keyword} from {start} to {end} second.'
        kws.reset_all()
        print(txt)
return "Deactivated."`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.