Build your own voice dialogue robot

"Zero basis-  build a robot of your own "

 

First, imagine that when you come home, there is only one person at home, but no one is chatting, and then you send a command, the computer will automatically start a conversation with you, and you don’t need to type or look at the screen, because she will Make your own voice, respond to your questions, and greetings.

01—Programming ideas are very important

Have a good idea and implement it in code. How to implement it is more important.

First of all, think about how you would implement an automatic reply robot if you did not read this article, and then look at my solution, because mine is not the best solution.

 

Ideas:

1. The first thing that comes to mind is macro. I want to speak, and then let the machine respond immediately and answer us through the speaker. This is our need.

2. We split him into some small demands.

(1) If I want to speak, sound will be produced, and the system cannot translate the sound, so we need to record the sound we make.

(2) Convert sound into text.

(3) Send the text to the robot that you trained, but it is more difficult to train the robot yourself. At this time, use a third-party interface to automatically reply, just like the Xiaoling robot in the public background.

3. Then our specific process will come out.

Sound---->Audio file----->Call the third-party interface (speech recognition)------->Text------->Send to Turing Robot------ ->The robot makes a reply------->Return text------->Text-to-speech---->output and make a sound.

Suddenly feel dizzy , how to adjust it.

 

02—Speech generates audio files

To generate a voice file, we need to record and save it to a file. How can python start recording and save the file?

Think about it!

Here you need to import a module, as the saying goes, there will not be any guide there! Hey, python is so strong.

Import the module, here you need to install a pip install pyaudio

import wave
from pyaudio import PyAudio,paInt16

Those who are interested can learn about it and continue to see the implementation code

def save_wave_file(filename,data):#Save audio file 
    wf=wave.open(filename,'wb') 
    wf.setnchannels(1) 
    wf.setsampwidth(2) 
    wf.setframerate(8000) 
    wf.writeframes(b"". join(data)) 
    wf.close() 

def my_record():#Realize recording 
    pa=PyAudio() 
    stream=pa.open(format = paInt16,channels=1, 
                   rate=8000,input=True, 
                   frames_per_buffer=2000) 
    my_buf =[] 
    count=0 
    print("Recording") 
    while count<2*15: 
        #Control the recording time, 15 seconds audio = stream.read(2000) 
        my_buf.append(audio) 
        count+=1 
        # print('.' )
    save_wave_file('01.wav',my_buf)#Call the save audio file function 
    stream.close() 
    print("Recording completed!")

Voice generation audio file is done

 

03—Audio file to text

 

We have already obtained the audio file above, how do we convert the audio file into text?

We continue:

A third-party speech recognition interface can be used here. Here I am using Baidu’s interface, because it is relatively simple, so you can study the relevant API yourself.

Import module: pip install baidu_aip

from aip import AipSpeech

Import the module name we need, then send the audio file and return the text.

The three parameters here are left to everyone to obtain.

def audio_word(): 
    APP_ID ='' 
    API_KEY ='' 
    SECRET_KEY ='' 
    client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) 
    def get_file_content(filePath): 
        with open(filePath,'rb') as fp: 
            return fp.read( ) 
    ret = client.asr(get_file_content('01.wav'),'wav', 16000, {'dev_pid': 1537, }) 
    print(ret) #Get the recognized text

Here I did not do exception handling, to test your abilities, and give you a link for you to try it yourself.

This is Baidu's document and open platform. If you need to use it, you can apply for a test of your own application:

Related documents http://ai.baidu.com/docs#/ASR-Online-Python-SDK/top

Open platform https://console.bce.baidu.com/ai/#/ai/speech/app/detail~appId=608501

 

04—Talk to the robot

Okay, here we are, our voice is successfully transformed into words, and then?

Call the third-party interface again this time to make an automatic response.

Here I am calling Turing Robot, you can also find out.

For some reason, here I directly use the chatbot of the platform dialog box,

As follows (the requests module needs to be imported here):

def tu_ling(text):

    url = "http://www.tuling123.com/robot-chat/robot/chat/227960/jwt7"
    data = {"perception": {"inputText": {"text": text}}, "userInfo": {"userId": "demo123"}}
    header = {
        "Referer": "http://www.tuling123.com/member/robot/1140264/center/frame.jhtml?page=0&child=0",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4094.1 Safari/537.36"}
    tuling = requests.post(url, json=data, headers=header)
    conent = tuling.json()

I won’t tell you, I’m using crawlers here

 

05—Text to speech

Okay, (knocking on the blackboard) the key point, I didn’t take the exam the year before, and I didn’t take the exam last year.

We need to convert text to speech and use an output device to output it. How to do it?

How to do? Import another module!

pip install Pywin32

After the import was successful, when I installed it, pywin32 seemed to be the code of python2. I needed to modify several places to make it support python3.

Then call

import win32com.client 
speaker = win32com.client.Dispatch("SAPI.SpVoice") 
speaker.Speak("I am a voice assistant, Xiaoling!")

Well, our voice partner is done.

Break it down, it feels very simple! What are you waiting for, make one yourself!

 

related suggestion:

Automatic operation of the browser - no interface selenium reptiles

Automatically open the browser, automatic operation

Python implements blessing bullet frame

WeChat add friends automatically

Python crawler tutorial

 

 

Reply in the background of the official account: " Voice Robot " to get the complete code and related plug-ins.

 

Welcome to follow us!

 

 

Guess you like

Origin blog.csdn.net/qq_39046854/article/details/83834628