The Python language realizes the example of Baidu speech recognition function

Source: Internet
Author: User
In the future, Ai occupies a heavy position in the market, Python language is the best programming language to study artificial intelligence, below, let us feel the charm of it!

Baidu gives the sample program, whether C or Java version, are divided into method1 and method2 two kinds, the former is called implicit (post is the JSON string, audio data encoded into the JSON), The latter is called explicit (post is the audio data) This article mainly introduces the Python language implementation of the Baidu Speech recognition API use examples, has a certain value for reference, the need for friends can refer to, hope to help everyone.

The first consideration is that the Pythonwave package deals with "strings", worrying about inconsistent with the C language array, so choose the inefficient but insured method1,

That is, the audio data base64 encoded, plus the sample rate, the number of channels and other information gathered into the dict, and finally the overall encoding into a JSON string

The results are always reported:

3300 Incorrect input parameters

Have tried urllib2 and Pycurl bag, are the above situation

Forced swap with METHOD2, success (it seems that the wave packet storage of audio is not a "string")

#encoding =utf-8 Import Wave import urllib, urllib2, Pycurl import base64 import JSON # # Get access token by API Key & Secret key Def Get_token (): ApiKey = "xxxxxxxx" Secretkey = "xxxxxxxxx" Auth_url = "Https://openapi.baidu.com/oauth   /2.0/token?grant_type=client_credentials&client_id= "+ ApiKey +" &client_secret= "+ secretkey; res = Urllib2.urlopen (auth_url) Json_data = Res.read () return json.loads (json_data) [' Access_token '] def dump_res (BUF) : Print BUF # # post audio to server Def use_cloud (token): fp = Wave.open (' vad_0.wav ', ' RB ') NF = Fp.getnframes () f _len = NF * 2 Audio_data = fp.readframes (nf) cuid = "xxxxxxxxxx" #my Xiaomi phone MAC srv_url = ' http://vop.baidu.c Om/server_api ' + '? cuid= ' + cuid + ' &token= ' + token http_header = [' content-type:audio/pcm; rate=8000 ', ' Content-length:%d '% f_len] c = pycurl. Curl () c.setopt (Pycurl. URL, str (srv_url)) #curl doesn ' t support Unicode #c. setopt (C.returntransfer, 1) c.setopt (C.httpheader, Http_header) #must is list, not dict c.setopt (c.post, 1) c.setopt (C.connecttimeout, a) c.setopt (c.ti   Meout, C.setopt (c.writefunction, Dump_res) c.setopt (C.postfields, Audio_data) c.setopt (c.postfieldsize, F_len) C.perform () #pycurl. Perform () has no return val if __name__ = = "__main__": token = Get_token () use_cloud (token)

Run results

{"Corpus_no": "6150045491002357923", "err_msg": "Success.", "Err_no": 0, "result": ["Play small Apple,"], "SN": " 243903724071431919050 "}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.