• python 语音识别(百度api)



    前言

    现在语音助手已经成为我们生活中ai的代表,下面让我们看一下语音助手的语音输入功能


    准备

    1. 让我们搜索一下百度的开放平台

    在这里插入图片描述

    1. 进入百度开放平台后选择短语音识别
      在这里插入图片描述

    2. 然后去领取免费的资源
      在这里插入图片描述

    3. 创建应用
      在这里插入图片描述

    4. 记住相关的key
      在这里插入图片描述

    下载库

    本次用到的库(以及引入方法):

    import speech_recognition as sr
    from aip import AipSpeech
    import pyttsx3
    
    • 1
    • 2
    • 3

    代码实现以及

    编写我的key

    APP_ID = '27468776'
    API_KEY = 'ygQgtR4DIh14lDi8gwSMCm7o'
    SECRET_KEY = '7flGlZba42ZYxHudy3dSQ8YW4n89W5hc'
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    
    • 1
    • 2
    • 3
    • 4

    语音的录入:

    try:
        def rec(rate=16000):
            r = sr.Recognizer()
            with sr.Microphone(sample_rate=rate) as source:
                print("请说话")
                audio = r.listen(source)
    
            with open("recording.wav", "wb") as f:
                f.write(audio.get_wav_data())
    except:
        print("语音录入发生错误")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    使用百度语音作为STT引擎并通过pyttsx3函数将返回的内容读出来形成简单的对话

        with open('recording.wav', 'rb') as f:
            audio_data = f.read()
    
            result = client.asr(audio_data, 'wav', 16000, {
            'dev_pid': 1536,
        })
            try:
                result_text = result["result"][0]
                print("你说: " + result_text)
    
    
                if result_text =="于金龙":
                    engine = pyttsx3.init()
                    engine.say('真帅')
                    engine.runAndWait()
                elif result_text=="张新凯":
                    engine = pyttsx3.init()
                    engine.say('真难看')
                    engine.runAndWait()
            except:
                print("没有识别到语音")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    完整代码

    import pyttsx3
    
    import speech_recognition as sr
    from aip import AipSpeech
    
    # Baidu Speech API, replace with your personal key
    APP_ID = '27468776'
    API_KEY = 'ygQgtR4DIh14lDi8gwSMCm7o'
    SECRET_KEY = '7flGlZba42ZYxHudy3dSQ8YW4n89W5hc'
    
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
    
    # Use SpeechRecognition to record
    try:
        def rec(rate=16000):
            r = sr.Recognizer()
            with sr.Microphone(sample_rate=rate) as source:
                print("请说话")
                audio = r.listen(source)
    
            with open("recording.wav", "wb") as f:
                f.write(audio.get_wav_data())
    except:
        print("语音录入发生错误")
    
    
    # 使用百度语音作为STT引擎
    def listen():
        with open('recording.wav', 'rb') as f:
            audio_data = f.read()
    
            result = client.asr(audio_data, 'wav', 16000, {
            'dev_pid': 1536,
        })
            try:
                result_text = result["result"][0]
                print("你说: " + result_text)
    
    
                if result_text =="阿龙":
                    engine = pyttsx3.init()
                    engine.say('真帅')
                    engine.runAndWait()
                elif result_text=="其他人":
                    engine = pyttsx3.init()
                    engine.say('真难看')
                    engine.runAndWait()
            except:
                print("没有识别到语音")
    
    
    while True:
        rec()
        request = listen()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    运行结果

    在这里插入图片描述

  • 相关阅读:
    JAVA内存模型与JVM内存结构
    Redis源码与设计剖析 -- 13.有序集合对象
    UE4插件软链接(关联)
    【Pandas总结】第八节 Pandas 合并数据集_pd.merge()
    Springboot项目整合jieba分词,实现语句最精确的切分
    MQTT,JSON,VSCODE(C语言编程环境)心得
    ffmpeg的pcm、yuv小知识点
    more than one ‘primary‘ bean found among candidates: xxxTransactionManager
    React-18(组件化开发)--ref与react过渡动画
    Pr 视频效果:过时
  • 原文地址:https://blog.csdn.net/yujinlong2002/article/details/126864522