Python text to speech, so that your text will “speak”, pick the foot of the big man second to become coquettish cute girl

Time:2019-11-26

Preface

The text and pictures of the article are from the Internet, only for learning and communication, and do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.

Author: PK Ge

PS: if you need Python learning materials, you can click the link below to get them by yourself

http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef

App also has the function of converting text to speech. Although it sounds awkward, it can basically solve the problem that elders can’t see the text or their eyes are tired. They can get information by converting text to speech.

Can we use Python to convert text to speech? Yes, Baidu has a voice interface, which can be called directly in Python, and even provides a variety of voice options, of course, you can choose the voice of cute girls.

Install Baidu interface

Install directly through PIP command (- I is followed by Douban image, so the download and installation speed is faster)

pip3 install baidu-aip -i https://pypi.douban.com/simple/

Create application

Baidu voice synthesis address is as follows:

https://ai.baidu.com/tech/speech/tts

Click to use immediately. If you don’t log in, you will be required to log in first. You can log in with your own baidu account. If you don’t have a new baidu account, you can sign up.

After login, enter the application page. Speech recognition includes input method, search, English, Cantonese, Sichuan dialect, Yuanyang, and speech synthesis library. As we can see, most API calls are free and unlimited. 在这里插入图片描述

We create an application and check the required interface permissions. My requirements are relatively simple, just simple text to voice, so there are fewer checked interfaces. Of course, this interface can also be used for face recognition, image recognition, natural language processing, etc. you can choose it on demand. 在这里插入图片描述

After creation, we can see three important values of the application in the application list: appid, API key, secret key, which are used when calling the interface.

在这里插入图片描述

In the document center, let’s take a look at the basic upload parameters.

在这里插入图片描述

Code debugging

According to the interface document, we can write a simple code that can communicate the interface.

We need to fill in the values of app? ID, API? Key and secret? Key that you created the app.

Among them, the value of per indicates the male and female voices, 0 and 1 are common female voices and male voices, which sounds a bit awkward, 3 and 4 are emotional, obviously more emotional than 0 and 1, you can try.

from aip import AipSpeech
 ​
 APP_ID = '17657240'
 API_KEY = 'YRG9NQ6lVQYOXeazzK9rAovG'
 SECRET_KEY = 'TlG8tchxR3YFGg0hFuC3ODy2QvRafc2v'
 ​
 client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
 ​
 #English: zh Cantonese: CT English: en
 ​
 Result = client. Synthesis ('hahahaha ',' zh ', 1{
     'vol': 5, 'per': 4
 })
 ​
 #If the recognition is correct and the binary error of voice is returned, then the dict will be returned with reference to the following error code
 if not isinstance(result, dict):
     with open('audio.mp3', 'wb') as f:
         f.write(result)
 ​

 

After executing the code, an audio.mp3 file will be generated in the same level directory.

Second change coquettish cute sister

We add the interface of Kingsoft ICIBA and return beautiful English and Chinese translation sentences every day. We ask Du Yaya to read her out and make her voice become charming and cute.

Jinshan Ciba’s interface is very simple. We call directly requests and return JSON statements. We return English and Chinese statements.

def get_msg():
     Url ='http://open.iciba.com/dsapi / '(daily API link of Kingsoft ICIBA)
     html = requests.get(url)
     Content = HTML. Json() ['content '] (get a daily English statement)
     Note = HTML. Json() ['note '] (get a translation statement in English every day)
     return content, note

 

Give the values of app? ID, API? Key and secret? Key.

The next step is the same as the above one. It’s just to replace the fixed text above with the statement of Kingsoft ICIBA returned by the interface.

在这里插入图片描述

When we open the file, we can still hear some coquettish voices, don’t we believe it? You open it!

When I sent this article, the Chinese quotations returned by Kingsoft are: things are rarely impossible; the reason why people fail is not so much the lack of conditions as the lack of determination.

Next, you can send this cute girl’s audio to your friends via wechat or free SMS