docker的教程

阿里云语音合成对接接口

2018-12-11  本文已影响0人  逍遥_yjz

1.第一种方式-阿里云语音合成,录音文件识别,自然语言分析,rest 调用 python实现

自然语言分析官网文档:
https://help.aliyun.com/document_detail/61378.html?spm=a2c4g.11186623.6.547.9q3U1C
智能语音官网文档:
https://help.aliyun.com/product/30413.html?spm=a2c4g.11186623.3.1.yS0DIK
决定采用阿里云提供的restful api接口,主要利用python urllib库来实现调用,整合里官方给出的demo,总结出调用代码。话不多说,贴出代码:

python环境

python3.5.2

代码结构所示:


结构图

http请求代理工具类

在工程目录中建立utils目录,在目录中建立init.py文件代码如下

# -*- coding:utf-8 -*-
import hashlib
import urllib.request
import hmac
import base64
import datetime
import ssl
import uuid
from urllib.error import HTTPError


class http_proxy:
    """
    Http工具类,封装了鉴权
    """

    def __init__(self, ak_id, ak_secret):
        self.__ak_id = ak_id
        self.__ak_secret = ak_secret

    def __current_gmt_time(self):
        date = datetime.datetime.strftime(datetime.datetime.utcnow(), "%a, %d %b %Y %H:%M:%S GMT")
        return date

    def __md5_base64(self, strbody):
        hash = hashlib.md5()
        hash.update(strbody.encode('utf-8'))
        print(hash.digest())
        return base64.b64encode(hash.digest()).decode('utf-8')

    def __sha1_base64(self, str_to_sign, secret):
        hmacsha1 = hmac.new(secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha1)
        return base64.b64encode(hmacsha1.digest()).decode('utf-8')

    def send_request(self, url, body):
        gmtnow = self.__current_gmt_time()
        print(gmtnow)
        body_md5 = self.__md5_base64(body)
        print(body_md5)
        str_to_sign = "POST\napplication/json\n" + body_md5 + "\napplication/json\n" + gmtnow
        print(str_to_sign)
        signature = self.__sha1_base64(str_to_sign, self.__ak_secret)
        print(signature)
        auth_header = "Dataplus " + self.__ak_id + ":" + signature
        print(auth_header)
        ssl._create_default_https_context = ssl._create_unverified_context
        headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
        req = urllib.request.Request(url, headers=headers)
        req.add_header("Accept", "application/json")
        req.add_header("Content-Type", "application/json")
        req.add_header("Date", gmtnow)
        req.add_header("Authorization", auth_header)
        data = body.encode('utf-8')
        f = urllib.request.urlopen(req, data)
        return f.read().decode('utf-8')

    def send_get(self, url, task_id):
        gmtnow = self.__current_gmt_time()
        print(gmtnow)
        accept = "application/json"
        content_type = "application/json"
        str_to_sign = "GET\n" + accept + "\n" + "" + "\n" + content_type + "\n" + gmtnow
        print(str_to_sign)
        signature = self.__sha1_base64(str_to_sign, self.__ak_secret)
        print(signature)
        auth_header = "Dataplus " + self.__ak_id + ":" + signature
        print(auth_header)
        ssl._create_default_https_context = ssl._create_unverified_context
        url += "/" + task_id
        req = urllib.request.Request(url)
        req.add_header("Accept", "application/json")
        req.add_header("Content-Type", "application/json")
        req.add_header("Date", gmtnow)
        req.add_header("Authorization", auth_header)
        f = urllib.request.urlopen(req)
        return f.read().decode('utf-8')
        # try:
        #     f = urllib.request.urlopen(req)
        # except HTTPError as e:
        #     print(e)
        #     return None
        # else:
        #     return f.read().decode('utf-8')


    def send_requestForNlp(self, path, content):

        method = "POST"
        content_type = "application/json;chrset=utf-8"
        accept = "application/json"
        host = "nlp.cn-shanghai.aliyuncs.com"
        gmtnow = self.__current_gmt_time()
        print(gmtnow)
        body_md5 = self.__md5_base64(content)
        print(body_md5)
        uuidstr =  uuid.uuid4().hex
        str_to_sign = method+"\n"+accept+"\n" + body_md5 + "\n"+content_type+"\n" + gmtnow+ "\nx-acs-signature-method:HMAC-SHA1\n" + "x-acs-signature-nonce:" + uuidstr + "\n" + path;
        print(str_to_sign)
        signature = self.__sha1_base64(str_to_sign, self.__ak_secret)
        print(signature)
        auth_header = "acs " + self.__ak_id + ":" + signature
        print(auth_header)
        ssl._create_default_https_context = ssl._create_unverified_context
        req = urllib.request.Request("http://"+host+path)
        req.add_header("Accept",accept)
        req.add_header("Content-Type", content_type)
        req.add_header("Content-MD5", body_md5)
        req.add_header("Date", gmtnow)
        req.add_header("Host", host)
        req.add_header("x-acs-signature-nonce", uuidstr)
        req.add_header("x-acs-signature-method", "HMAC-SHA1")
        req.add_header("Authorization", auth_header)
        data = content.encode('utf-8')
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'}

        f = urllib.request.urlopen(req, data, headers=headers)
        return f.read().decode('utf-8')


    def sendTtsPost(self, textData, ttsRequestParam,fileRootPath):
        url = 'http://nlsapi.aliyun.com/speak?'#语音合成接口
        #拼接
        paramArray = []
        for key in ttsRequestParam:
            paramArray.append(key+"="+ttsRequestParam[key])

        url+=url+'&'.join(paramArray)


        method = "POST"
        content_type = "text/plain"
        accept = "audio/" + ttsRequestParam['encode_type'] + ",application/json"
        gmtnow = self.__current_gmt_time()

        body_md5 = self.__md5_base64(textData)
        print(body_md5)
        str_to_sign = method + "\n" + accept + "\n" + body_md5 + "\n" + content_type + "\n" + gmtnow
        print(str_to_sign)
        signature = self.__sha1_base64(str_to_sign, self.__ak_secret)
        print(signature)
        auth_header = "Dataplus " + self.__ak_id + ":" + signature
        print(auth_header)
        ssl._create_default_https_context = ssl._create_unverified_context
        req = urllib.request.Request(url)
        req.add_header("accept", accept)
        req.add_header("content-Type", content_type)
        req.add_header("date", gmtnow)
        req.add_header("Authorization", auth_header)
        req.add_header("Content-Length", len(textData))
        data = textData.encode('utf-8')
        f = urllib.request.urlopen(req, data)
        if f.status ==200:
            file = 'D:\PythonFiles\\2018second\\testTTS\demo\\' + uuid.uuid4().hex + ".wav"
            content = f.read()
            with open(file, 'wb') as f:
                f.write(content)
            print("success"+file)
        else:
            print('失败!')

调用demo

在目录中建立demo.py ,注意和上面的utils目录同级

# -*- coding:utf-8 -*-

import sys
import utils
import json
# app_key   语音数据格式  领域
# nls-service-realtime-8k   8kHz采样率 智能客服服务领域,比如电话客服等
# nls-service-multi-domain  16kHz采样率    汉语通用识别
# nls-service   16kHz采样率    输入法,社交聊天
# nls-service-tv    16kHz采样率    家庭娱乐
# nls-service-shopping  16kHz采样率    电商购物
# nls-realtime-fangyan  16kHz采样率    支持东北、河南、四川等方言
# nls-service-yue-streaming 16kHz采样率    粤语
# nls-service-en    16kHz采样率    英语

ak_id = "";  ##数加管控台获得的accessId
ak_secret = ""  ## 数加管控台获得的accessSecret
url = "https://nlsapi.aliyun.com/transcriptions"


# 录音文件提交
def request():
    body = {
        'app_key': 'nls-service-multi-domain',
        'oss_link': 'http://网址/audio/zl4.mp3',
    }
    bodyStr = json.dumps(body)
    httpProxy = utils.http_proxy(ak_id, ak_secret)
    result = httpProxy.send_request(url, bodyStr)
    return result


# 录音文件识别结果查询
def query(id):
    httpProxy = utils.http_proxy(ak_id, ak_secret)
    result = httpProxy.send_get(url, id)
    return result


# 自然语音分析 分词

def nlpTest():
    path = '/nlp/api/wordpos/general'

    postBody = {
        'text': '为什么世界是这个样子,人们都不诚实,我要努力,获得成功,让别人尊敬',
        'lang': 'ZH',
    }
    bodyStr = json.dumps(postBody)
    httpProxy = utils.http_proxy(ak_id, ak_secret)
    result = httpProxy.send_requestForNlp(path,bodyStr)
    return result

#语音合成
def ttsTest():
    text = '阿彪阿彪阿彪阿彪阿彪阿彪阿彪阿彪阿彪阿彪阿彪阿彪,'
    fileRootPath='g:audio/'
    ttsRequestParam ={
        'encode_type':'wav',#合成语音的编码格式,支持pcm/wav/mp3/alaw
        'voice_name':'ruoxi',#xiaogang - 男,xiaoyun - 女
        'volume':'33',#0~100
        'sample_rate':'16000',#抽样频率率 8000/16000
        'speech_rate':'0',#语速 -500~500
        'pitch_rate':'0',#语调 -500~500
        'tts_nus':'1',#0 - 通过参数合成语音,1 - 拼接原始录音
        'background_music_id':'1',#播放语音时可选背景音乐,0,1
        'background_music_offset':'0',#背景音乐播放偏移时长,毫秒。当启用背景音乐时生效
        'background_music_volume':'100'#背景音乐音量,当启用背景音乐时生效,0~100
    }
    httpProxy = utils.http_proxy(ak_id, ak_secret)
    filepath = httpProxy.sendTtsPost(text,ttsRequestParam,fileRootPath)
    print(filepath)



if __name__ == '__main__':
    #print(request())
    #print(query('2324ec1ed63549318b9477f1bf3eaf8a'))
    #print(nlpTest())
    print(ttsTest())

转发来源于:https://blog.csdn.net/huangmingleiluo/article/details/81301209

2. 第二种方式

定义speechSynthesis.py

# -*- coding:utf-8 -*-
import hashlib
import requests
import hmac
import base64
import datetime
import tempfile


class Alibaba:
    def __init__(self):
        self.__ak_id = ""  # 替换个人用户 id
        self.__ak_secret = ""  # 替换个人用户 secret

    def __current_gmt_time(self):
        date = datetime.datetime.strftime(datetime.datetime.utcnow(), "%a, %d %b %Y %H:%M:%S GMT")
        return date

    def __md5_base64(self, body):
        hash = hashlib.md5()
        hash.update(body)
        str_hex = hash.digest()
        return base64.b64encode(str_hex)

    def __sha1_base64(self, str_to_sign, secret):
        hmacsha1 = hmac.new(secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha1)
        return base64.b64encode(hmacsha1.digest()).decode('utf-8')

    def send_request(self, phrase):
        options = {'method': 'POST',
                   'url': 'http://nlsapi.aliyun.com/speak?encode_type=wav&voice_name=xiaoyun&volume=33&volume=50',
                   'body': phrase.encode('utf-8')
                   }
        headers = {
            'Authorization': '',
            'Content-type': 'text/plain',
            'Accept': 'audio/mp3,application/json',
            'Date': self.__current_gmt_time()
        }
        bodyMd5 = self.__md5_base64(options['body']).decode('utf-8')

        feature = options['method'] + '\n' + headers['Accept'] + '\n' + bodyMd5 + '\n' \
                  + headers['Content-type'] + '\n' + headers['Date']
        signature = self.__sha1_base64(feature, self.__ak_secret)

        headers['Authorization'] = "Dataplus " + self.__ak_id + ":" + signature

        req = requests.post(options['url'], data=options['body'], headers=headers)
        #print(req.json())
        with tempfile.NamedTemporaryFile(suffix='.mp3', delete=False) as f:
            f.write(req.content)
            tmpfile = f.name
            return tmpfile


if __name__ == '__main__':
    a = Alibaba()
    a.send_request('今天天气怎天天气怎么样?')  # 输入要合成语音的文字


3. 第三种

定义successsfulExample.py
涉及到文件的转移和文件的重命名

# -*- coding:utf-8 -*-
import hashlib
import requests
import hmac
import base64
import datetime
import tempfile
import shutil, os


class aliyun:

    def __init__(self):
        self.__aliyun_id = ""  # 个人用户 id
        self.__aliyun_secret = ""  # 个人用户 secret


    def __time(self):
        time = datetime.datetime.strftime(datetime.datetime.utcnow(), "%a, %d %b %Y %H:%M:%S GMT")

        return time


    def __md5_base64(self, body):
        hash = hashlib.md5()

        hash.update(body)
        str_hex = hash.digest()
        return base64.b64encode(str_hex)


    def __sha1_base64(self, str_to_sign, secret):
        hmacsha1 = hmac.new(secret.encode('utf-8'), str_to_sign.encode('utf-8'), hashlib.sha1)

        return base64.b64encode(hmacsha1.digest()).decode('utf-8')


    def send_request(self, phrase):
        options = {'method': 'POST',
            'url': 'http://nlsapi.aliyun.com/speak?encode_type=wav&voice_name=ruoxi&volume=35',
            'body': phrase.encode('utf-8')}
        headers = {'Authorization': '', 'voice_name': 'xiaogang', 'Content-type': 'text/plain', 'Accept': 'audio/mp3,application/json',
                           'Date': self.__time()}

        MD5 = self.__md5_base64(options['body']).decode('utf-8')

        feature = options['method'] + '\n' + headers['Accept'] + '\n' + MD5 + '\n' \
                + headers['Content-type'] + '\n' + headers['Date']
        signa = self.__sha1_base64(feature, self.__aliyun_secret)


        headers['Authorization'] = "Dataplus " + self.__aliyun_id + ":" + signa


        request = requests.post(options['url'], data=options['body'], headers=headers)
        print(type(request.content))
        path = r'D:\PythonFiles\2018second\testTTS\successsfulExample'
        with tempfile.NamedTemporaryFile(suffix='.wav', prefix='3333', dir=path, delete=False) as file:
            file.write(request.content)
            tmpfile = file.name
            return tmpfile

if __name__ == '__main__':
    a = aliyun()
    text = "李先生吗"
    file = a.send_request(text)
    #time = datetime.datetime.strftime(datetime.datetime.utcnow(), "%a, %d %b %Y-%b-%d %H:%M:%S GMT")
    slash = '\\'
    time = datetime.datetime.strftime(datetime.datetime.utcnow(), "%Y-%m-%d-%H%M%S")
    print(time)
    print(type(time))
    time1 = "123454.wav"
    main_path = r'D:\PythonFiles\2018second\testTTS\successsfulExample'
    print(file)
    video_name = os.path.join(main_path + slash, str(time) + '.wav')
    #shutil.move(file, video_name)  # 文件位置
    os.rename(file, os.path.join(main_path + slash, time + '.wav'))

参考资料:
中文转拼音和文件重命名
Python标准模块——tempfile

上一篇下一篇

猜你喜欢

热点阅读