BaiduYuyin.py 文件源码

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

项目:myRobot 作者: jesson20121020 项目源码 文件源码
def voice_to_text(self, voice_file, callback):
        # print 'xdc::::::::voice:::::', voice_file, callback
        fp = wave.open(voice_file, 'rb')
        nf = fp.getnframes()
        f_len = nf * 2
        audio_data = fp.readframes(nf)

        cuid = "xxxxxxxxxx" #my xiaomi phone MAC
        srv_url = 'http://vop.baidu.com/server_api' + '?cuid=' + cuid + '&token=' + self.get_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 be list, not dict
        c.setopt(c.POST, 1)
        c.setopt(c.CONNECTTIMEOUT, 30)
        c.setopt(c.TIMEOUT, 30)
        c.setopt(c.WRITEFUNCTION, callback)
        c.setopt(c.POSTFIELDS, audio_data)
        c.setopt(c.POSTFIELDSIZE, f_len)
        c.perform() #pycurl.perform() has no return val
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号