speech_recognition_90.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:xiaokai-bot 作者: CCZU-DEV 项目源码 文件源码
def _filter(ctx_msg):
    if ctx_msg.get('format') == 'media' and ctx_msg['raw_ctx'].get('media_type') == 'voice':
        m = re.match('\[??\]\(([/_A-Za-z0-9]+\.mp3)\)', ctx_msg.get('content'))
        if m:
            core.echo('????????????……', ctx_msg)
            mp3_path = m.group(1)
            wav_path = os.path.splitext(mp3_path)[0] + '.wav'
            voice = AudioSegment.from_mp3(mp3_path)
            voice.export(wav_path, format='wav')

            service = os.environ.get('SPEECH_RECOGNITION_SERVICE', '').lower()
            text = None
            service_full_name = None
            if service == 'baidu':
                service_full_name = '??????'
                text = _recognize_baidu(
                    wav_path,
                    get_source(ctx_msg),
                    os.environ.get('BAIDU_SPEECH_API_KEY'),
                    os.environ.get('BAIDU_SPEECH_SECRET_KEY'),
                    language='zh'
                )
            elif service == 'bing':
                service_full_name = '??????'
                text = _recognize_bing(
                    wav_path,
                    os.environ.get('BING_SPEECH_API_KEY'),
                    language='zh-CN'
                )
            else:
                print('Unknown speech recognition service name.', file=sys.stderr)

            if text:
                reply = '?????' + service_full_name + '??\n%s\n\n??????????????????……' % text
                ctx_msg['text'] = text
                ctx_msg['from_voice'] = True
            else:
                reply = '???????????????'
            core.echo(reply, ctx_msg)
            os.remove(wav_path)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号