Example #1
0
    def get_tts_audio(self, message, language, options=None):
        """Load TTS from BaiduTTS."""
        from aip import AipSpeech
        aip_speech = AipSpeech(
            self._app_data['appid'],
            self._app_data['apikey'],
            self._app_data['secretkey']
        )

        if options is None:
            result = aip_speech.synthesis(
                message, language, 1, self._speech_conf_data
            )
        else:
            speech_data = self._speech_conf_data.copy()
            for key, value in options.items():
                speech_data[_OPTIONS[key]] = value

            result = aip_speech.synthesis(
                message, language, 1, speech_data
            )

        if isinstance(result, dict):
            _LOGGER.error(
                "Baidu TTS error-- err_no:%d; err_msg:%s; err_detail:%s",
                result['err_no'],
                result['err_msg'],
                result['err_detail']
            )
            return None, None

        return self._codec, result
def hecheng(name,rank,text,type):
	if type == 'sayhello':
		path = 'Hello-audio/sayhello/%s/hello%s.mp3'%(name,rank)#这里是存入sayhello的语音,需要传入name和等级两个参数
	if type == 'opendoor':
		path = 'Hello-audio/opendoor/%s%s.mp3'%(name,rank)  #开门的语音设置,只需要传入name:授权开门,或者不开这两个参数
	if type == 'equipment':
		path = 'Hello-audio/equipment/%s%s.mp3'%(name,rank)		
	client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

	result = client.synthesis('%s'%(text),'zh',1,{
		'vol':6,
		'per':4,

		})
	if not isinstance(result, dict):
	    with open(path, 'wb') as f:
	        f.write(result)
	return path
Example #3
0
def init_aip():
    global aip_client
    global aip_client_ocr

    APP_ID = '11758766'
    API_KEY = 'I5yAyCHCEw5eQhjG0nuXkgHr'
    SECRET_KEY = '8OxzRjt1dMrRgtsmTHTLpH1SxWBywoju' 

    aip_client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

    #连接超时时间
    aip_client.setConnectionTimeoutInMillis(1000)
    #数据传输超时时间
    aip_client.setSocketTimeoutInMillis(3000)





    APP_ID = '14462175'
    API_KEY = 'KG8eaIU0ouFCaMLnQZ2u3IX1'
    SECRET_KEY = 'waX99HpVyaukCX6aruuazGNo6zaVOcVu' 
    aip_client_ocr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
Example #4
0
class Recognition:
    def __init__(self, config_info):
        self.baidu = config_info['baidu']
        self.speech = AipSpeech(self.baidu['appId'], self.baidu['apiKey'], self.baidu['secretKey'])

    def get_wave_file(self, wave_file):
        if os.path.exists(wave_file):
            fp=wave.open(wave_file,'rb')
            nf = fp.getnframes()  # 获取文件的采样点数量
            print('sampwidth:', fp.getsampwidth(), 'framerate:', fp.getframerate(), 'channels:', fp.getnchannels())
            f_len = nf * fp._sampwidth  # 文件长度计算,每个采样2个字节
            audio_data = fp.readframes(nf)
            frame_rate = fp._framerate
            return (audio_data, frame_rate, f_len)
        else:
            raise FileNotFoundError

    def recognize(self, audio_file):
        # 格式支持:pcm(不压缩)、wav(不压缩,pcm编码)、amr(压缩格式)。
        # 推荐pcm 采样率 :16000 固定值。 编码:16bit 位深的单声道。
        # 百度服务端会将非pcm格式,转为pcm格式,因此使用wav、amr会有额外的转换耗时。
        (wave_content, frame_rate, len) = self.get_wave_file(audio_file)
        result = self.speech.asr(wave_content, 'pcm', frame_rate, {'lan': 'zh'})
        return result
Example #5
0
from xugu import *
import os
import signal
from aip import AipSpeech
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

audio_file='auido.mp3'
AppID = "15469649"
access_token = "" #定义sccess_token变量
API_KEY = "3vZgLINSnGGEafPflkTLzkGh" #定义API_KEY变量
SECRET_KEY = "8cUXtkMed2z86kqfyrV606ylnCmfcc48" #定义SECRET_KEY变量
frame = None #定义frame变量
now_time = 0 #定义now_time变量
animal_info = None #定义animal_info变量
client = AipSpeech(AppID, API_KEY, SECRET_KEY)

def save_audio(number):
    result  = client.synthesis(number, 'zh', 1, {
        'vol': 5,
        'per': 2
        })
    if not isinstance(result, dict):
        with open(audio_file, 'wb') as f:
            f.write(result)
        os.popen("play *mp3")

def cvimg_to_b64(img):
    """
    图片转换函数,将二进制图片转换为base64加密格式
    """
Example #6
0
    word_to_voice1('请欣赏')
    pygame.mixer.music.load(music_file)
    pygame.mixer.music.set_volume(volume)
    pygame.mixer.music.play(loops=0)


def do_it_once(p):  # 一次成功的执行
    get_voice(p)
    songname = voice_to_word(client)
    parse(songname, 1)
    play_music(download(songname))


if __name__ == '__main__':
    p = pyaudio.PyAudio()  # 初始化
    client = AipSpeech(ID, KEY, KEY2)
    pygame.mixer.init()

    while 1:  # 循环
        do_it_once(p)
        while pygame.mixer.music.get_busy() == True:
            print('''
*****************************
* 1.新的曲目   2.退出程序    *
* 3.暂停播放   4.取消暂停    *
* 5.音量调大   6.音量调小    *
*****************************
            ''')
            s = input("请选择您要操作的序号:")
            if s == '1':
                print("新的曲目")
Example #7
0
 def __init__(self,app_id,api_key,secret_ket):
     """  APPID AK SK """
     APP_ID = app_id
     API_KEY = api_key
     SECRET_KEY = secret_ket
     self.cliten = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
Example #8
0
# 音频文件转文字:采用百度的语音识别python-SDK
# 百度语音识别API配置参数
from aip import AipSpeech

APP_ID = '23732074'
API_KEY = 'yFqNBRp7oDYqPDl6G83ViuOM'
SECRET_KEY = 'rDKmCvQ4shBLPg3WZektLIxsOEjdH0B8'
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
path = 'voices/myvoices.wav'


# 将语音转文本STT
def listen():
    # 读取录音文件
    with open(path, 'rb') as fp:
        voices = fp.read()
    try:
        # 参数dev_pid:1536普通话(支持简单的英文识别)、1537普通话(纯中文识别)、1737英语、1637粤语、1837四川话、1936普通话远场
        result = client.asr(voices, 'wav', 16000, {
            'dev_pid': 1537,
        })
        result_text = result["result"][0]
        print("you said: " + result_text)
        return result_text
    except KeyError:
        print("KeyError")


# 与机器人对话:调用的是图灵机器人
import requests
import json
Example #9
0
from aip import AipSpeech

app_id = "14975947"
api_key = "X9f3qewZCohppMHxlunznUbi"
secret_key = "LupWgIIFzZ9kTVNZSH5G0guNGZIqqTom"

client = AipSpeech(app_id, api_key, secret_key)
result = client.synthesis(
    "声音测试,Good morning!",
    "zh",
    4,
    {
        "vol": 9,  # 音量
        "spd": 5,  # 语速
        "pit": 7,  # 语调
        "per": 4,  # 音色,0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女
    })

with open("Test.mp3", "wb") as f:
    f.write(result)
Example #10
0
        0 > p && (p = (2147483647 & p) + 2147483648),
        p %= 1e6,
        p.toString() + "." + (p ^ m)
    }
"""
import execjs
from aip import AipSpeech
import time
import requests
import json
""" 你的 APPID AK SK """
APP_ID = '你的appID'
API_KEY = '你的key'
SECRET_KEY = '你的secret'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)


# 第一步:读取文件
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


# 识别本地文件
respone = client.asr(get_file_content('16k.pcm'), 'pcm', 16000, {
    'dev_pid': 1536,
})

str = respone["result"][0]
print(str)
Example #11
0
        elif system == 'Darwin' and platform.machine() in [
                "i386", "x86", "x86_64", "AMD64"
        ]:
            player = os.path.join(path, "player", "mpg123-mac")
        else:
            raise OSError(
                "MP3 player utility not available - consider installing the MPG123 command line application using `brew install mpg123` or your operating system's equivalent"
            )

    try:
        stat_info = os.stat(player)
        os.chmod(player, stat_info.st_mode | stat.S_IEXEC)
    except OSError:
        pass

    process = subprocess.Popen("\"%s\" -q -" % player,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               shell=True)
    play_info, stderr = process.communicate(mp3_data)
    return play_info


client = AipSpeech('14922410', 'NSChZHWWVwa1BSwZ36Oaya4C',
                   '1dd0sxs2LXYRWETZ4gZSSDYDQvM6aROv')

result = client.synthesis('请问是李文斌先生吗?')

play_mp3(result)

print('hello world!')
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

from aip import AipSpeech
""" 你的 APPID AK SK """
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()


# 识别本地文件
#1737   英文,有标点
#1536	普通话(支持简单的英文识别)
#1537	普通话(纯中文识别)
a = client.asr(get_file_content('output1.wav'), 'wav', 16000, {
    'dev_pid': 1737,
})
print(a['result'])
Example #13
0
"""
@Author : zhudong
@Email  : [email protected]
@Time   : 2019/4/2 上午8:55
@File   : Baidu_speech2text.py
desc:
"""

import os
from tqdm import tqdm
from aip import AipSpeech

APP_ID = ""
API_KEY = ""
SECRET_KEY = ""
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)


# 读取文件
def get_file_content(filePath):
    tmp = 'tmp'

    if not os.path.exists(tmp):
        os.makedirs(tmp)

    tmp_filename = 'tmp/__tmp__.wav'
    print(filePath)

    os.system('sox {} -c 1 -r 16000 -b 16 {}'.format(filePath, tmp_filename))
    with open(tmp_filename, 'rb') as fp:
        signal = fp.read()
Example #14
0
from aip import AipSpeech
app_id = "15544234"
api_key = "GWjmFRoTNT3azlTXNblqAzM9"
secret_key = "XZXUb08jPAftrojw6cchEf518eeLkUQ9"
client = AipSpeech(app_id,api_key,secret_key)#将所需要的参数放入AipSpeech里面,形成一个对象
#第一个参数是文本信息,第二个参数是语音  #"zh"表示中文,引文为"en",1代表使用的平台为PC
texts = ["周夫拥"]
texts[0].replace("”","")
result = client.synthesis(texts,"zh",1,{
    "vol":5,#音量
    "spd":4,#语速
    "pit":9,#语调
    "per":4,# 0:女,1:男,3:逍遥音,4:小萝莉 等等

})
with open("auido5.mp3","wb") as f:
    f.write(result)
Example #15
0
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
    * @par Copyright (C): 2010-2019, hunan CLB Tech
    * @file        speech-recognition
    * @version      V1.0
    * @details
    * @par History
    
    @author: zhulin
"""
from aip import AipSpeech
#这里需要填你自己的id和密钥
APP_ID='16226519'
API_KEY='5KVxQVES4LSja0u2G4y8m1O9'
SECRET_KEY='KhaXYwGLSmQYgnwHkuXKpV9MO2ta0bQ8'
#初始化
aipSpeech=AipSpeech(APP_ID,API_KEY,SECRET_KEY)
#读文件
def get_file_content(filePath):
    with open(filePath,'rb') as fp:
        return fp.read()
#参数请查阅技术文档,格式是amr,语言是中文
result=aipSpeech.asr(get_file_content('/home/pi/CLBDEMO/_8_speech/8k.amr'),'amr',8000,{
        'lan':'zh',
        })
print(result['result'][0])
Example #16
0
from pyaudio import PyAudio, paInt16
from aip import AipSpeech
import win32api

framerate = 16000
NUM_SAMPLES = 2000
channels = 1
sampwidth = 2
TIME = 5
chunk = 2014

APP_ID = "10838516"
API_KEY = "KQskXIDQaZ8L8kiqgpnKutrg"
SECRET_KEY = "msFNNbMZs88FdGSjRR06Lu4rj16ya983"

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)


def save_wave_file(filename, data):
    '''save the date to the wavfile'''
    wf = wave.open(filename, 'wb')
    wf.setnchannels(channels)
    wf.setsampwidth(sampwidth)
    wf.setframerate(framerate)
    wf.writeframes(b"".join(data))
    wf.close()


def my_record():
    pa = PyAudio()
    stream = pa.open(format=paInt16,
Example #17
0
 def __init__(self, appid, api_key, secret_key, per=1, lan='zh', **args):
     super(self.__class__, self).__init__()
     self.client = AipSpeech(appid, api_key, secret_key)
     self.per, self.lan = str(per), lan
Example #18
0
class parse_zh(object):
    def __init__(self):
        self._APP_ID = '10852898'
        self._API_KEY = '7GsURiUfKWzPz0Ppl4tjipTg'
        self._SECRET_KEY = '9d2a195cc287eaef7e96572e0370b67b'
        self._client = AipSpeech(self._APP_ID, self._API_KEY, self._SECRET_KEY)

        self._chuck = 4096
        self._sample_rate = 44100
        self._channels = 1
        self._samplewidth = 2
        self._sample_time = 4  # s

        self._record_speech_name = 'recorded_speech.wav'

    # 读取文件
    def get_file_content(self, filePath):
        with open(filePath, 'rb') as fp:
            return fp.read()

    def get_result(self):
        print('parsing speech...')
        self._req = self._client.asr(
            self.get_file_content("recorded_speech.pcm"), 'pcm', 16000, {
                'lan': 'zh',
            })
        print(self._req)
        if self._req['err_no'] == 0:
            return self._req['result'][0], self._req['err_no']
        else:
            return None, self._req['err_no']

    def get_voice(self, msg):
        self._ret_voice = self._client.synthesis(msg, 'zh', 1, {
            'vol': 5,
        })
        if not isinstance(self._ret_voice, dict):
            with open('audio.mp3', 'wb') as f:
                f.write(self._ret_voice)
            os.system('omxplayer audio.mp3')
        return self._ret_voice

    def save_wave_file(self, filename, data):
        '''save the date to the wav file'''
        wf = wave.open(filename, 'wb')
        wf.setnchannels(self._channels)
        wf.setsampwidth(self._samplewidth)
        wf.setframerate(self._sample_rate)
        wf.writeframes(b"".join(data))
        wf.close()

    def record_voice(self):
        #open the input of wave
        pa = PyAudio()
        stream = pa.open(format=paInt16,
                         channels=self._channels,
                         rate=self._sample_rate,
                         input=True,
                         input_device_index=2,
                         frames_per_buffer=self._chuck)
        save_buffer = []
        print('recording...')
        for i in range(
                0, int(self._sample_rate / self._chuck * self._sample_time)):
            data = stream.read(self._chuck, exception_on_overflow=False)
            save_buffer.append(data)

        stream.stop_stream()
        stream.close()
        pa.terminate()

        self.save_wave_file(self._record_speech_name, save_buffer)
        save_buffer = []
        print(self._record_speech_name, "saved")
        print('speech record OK!')
        os.system(
            "ffmpeg -y -f s16le -ac 1 -ar 44100 -i recorded_speech.wav -acodec pcm_s16le -f s16le -ac 1 -ar 16000 recorded_speech.pcm -loglevel -8"
        )
Example #19
0
#             with request.urlopen(req) as f:
#                 content = f.read()
#                 imageName = os.path.join("G:/images", "{}_{}.jpg".format(i,n) )
#                 with open(imageName, 'wb') as fp:
#                     fp.write(content)
#                 n+=1

from aip import AipSpeech
# 定义常量
APP_ID = '9902583'

API_KEY = 'R7h09AYY3dNMQdIdz1zaWiWo'

SECRET_KEY = '3245afb910ba976d21bbaad591803899'
# 初始化AipSpeech对象
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

autoDict = {'Caroline': True, 'tiankongzhicheng': True}
autoUserNames = {}


# 读取文件
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


# 识别本地文件
result = aipSpeech.asr(get_file_content('170718-132442.mp3'), 'pcm', 16000, {
    'lan': 'zh',
})
Example #20
0
from aip import AipSpeech
""" 你的 APPID AK SK """
APP_ID = '16981704'
API_KEY = 'CeLs5zCuQwWXBhHbrnDGQhc3'
SECRET_KEY = 'HIOyvsDRcXKlP95NOY72CAUznUIC6OKZ'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

text = "先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。\
宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。\
侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必能裨补阙漏,有所广益。"

result = client.synthesis(text, 'zh', 1, {
    'vol': 5,
})

# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
    with open('auido.mp3', 'wb') as f:
        f.write(result)
Example #21
0
import sys, os

reload(sys)
sys.setdefaultencoding("utf-8")

# 引入Speech SDK
from aip import AipSpeech

# 定义常量
APP_ID = "xxxx"
API_KEY = "xxxx"
SECRET_KEY = "xxxx"

# 初始化AipSpeech对象
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

while 1:
    text = raw_input("您想说什么?0 => EXIT :")
    if text == '0':
        sys.exit(0)

    result = aipSpeech.synthesis(text, 'zh', 1, {
        'vol': 5,
        'per': 0,
        'pit': 5,
        'spd': 5
    })
    #vol:语速,取值0-15,默认为5中语速
    #per:发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女
    #pit:音调,取值0-9,默认为5中语调
Example #22
0
    params = {
        'key': 'free',
        'appid': 0,
        'msg': info
    }
    res = requests.get(url, params=params)
    if res.status_code == 200:
        return res.json()['content']
    else:
        return '网络连接错误!'

APP_ID='16574992'
API_KEY = 'Se1pQXlRIVxiNCiwC4YPezgg'
SECRET_KEY = 'aoxVXTChFGhEe4A0NSkvkwTPqtSLuOP3'

client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

result  = {
    'vol': 5, 'per':'4','pit':'5'
}
def text2audio(worlds,role_param,out_putfile,lang='zh'):
    result = client.synthesis(worlds,lang,1,role_param)

    if not isinstance(result, dict):
        with open(out_putfile, 'wb') as f:
            f.write(result)
        return 'ok'

def anything2pcm(input_file):
    """将一切格式的音频转为 pcm 格式,需将ffmpeg 添加到环境变量"""
    # input_file = os.path.join(os.getcwd(), input_file)
Example #23
0
# python VoiceTest.py

from aip import AipSpeech

App_ID = '10252241'
API_Key = 'ZfPzLAEjqsiOExjSEImlD8gb'
Secret_Key = 'd94ebabd13a1c5f33f216e9af5577fbb'

aip_Speech = AipSpeech(App_ID, API_Key, Secret_Key)

result = aip_Speech.synthesis(
    '主人,下面为您进行今天的播报。连日来,世界各大天文台预警宣布的重大新发现,在10月16日晚10点如期兑现,全球多国科学家10月16日同步举行新闻发布会,宣布人类第一次从约1.3亿光年外探测到来自双中子星合并的引力波,并同时看到这一壮观宇宙事件发出的电磁信号。',
    'zh', 1, {
        'vol': 5,
        'per': 4,
    })
# 识别正确返回语音二进制,错误则返回dict 参照下面错误码
if not isinstance(result, dict):
    with open('auido_test.mp3', 'wb') as f:
        f.write(result)
Example #24
0
 def __init__(self):
     self.client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
Example #25
0
            if temp <= 2000:
                if flag == True:
                    flag = False
                    wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
                    wf.setnchannels(CHANNELS)
                    wf.setsampwidth(p.get_sample_size(FORMAT))
                    wf.setframerate(RATE)
                    wf.writeframes(b''.join(rec))
                    wf.close()
                    rec = []
                    self.tuling()

        stream.stop_stream()
        stream.close()
        p.terminate()


if __name__ == '__main__':

    pygame.mixer.init()

    # create admin user
    with open('./usrs_info.pickle', 'wb') as fp:
        usrs_info = {'admin': 'admin'}
        pickle.dump(obj=usrs_info, file=fp)

    aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

    iting = Iting()
Example #26
0
 def __init__(self, config_info):
     self.baidu = config_info['baidu']
     self.speech = AipSpeech(self.baidu['appId'], self.baidu['apiKey'], self.baidu['secretKey'])
Example #27
0
    def recoder(self):
        pa = PyAudio()
        stream = pa.open(format=paInt16,
                         channels=1,
                         rate=self.SAMPLING_RATE,
                         input=True,
                         frames_per_buffer=self.NUM_SAMPLES)
        save_count = 0
        save_buffer = []
        time_count = self.TIME_COUNT

        while True:
            time_count -= 1
            # print time_count
            # 读入NUM_SAMPLES个取样
            # print('又回来了')
            string_audio_data = stream.read(self.NUM_SAMPLES)
            # 将读入的数据转换为数组
            audio_data = np.fromstring(string_audio_data, dtype=np.short)
            # 计算大于LEVEL的取样的个数
            large_sample_count = np.sum(audio_data > self.LEVEL)
            print(np.max(audio_data))
            # 如果个数大于COUNT_NUM,则至少保存SAVE_LENGTH个块
            if large_sample_count > self.COUNT_NUM:
                save_count = self.SAVE_LENGTH
            else:
                save_count -= 1

            if save_count < 0:
                save_count = 0

            if save_count > 0:
                # 将要保存的数据存放到save_buffer中
                #print  save_count > 0 and time_count >0
                save_buffer.append(string_audio_data)
            else:
                #print save_buffer
                # 将save_buffer中的数据写入WAV文件,WAV文件的文件名是保存的时刻
                #print "debug"
                if len(save_buffer) > 0:
                    self.Voice_String = save_buffer
                    save_buffer = []
                    # modified by jxc 20180912
                    client = AipSpeech(self.BAIDU_APP_ID, self.BAIDU_API_KEY,
                                       self.BAIDU_SECRET_KEY)
                    voice_buffer = np.array(self.Voice_String).tostring()
                    result = client.asr(voice_buffer, 'wav', 16000, {
                        'dev_pid': 1936,
                    })
                    '''
                    1536    普通话(支持简单的英文识别)  搜索模型    无标点 支持自定义词库
                    1537    普通话(纯中文识别)  输入法模型   有标点 不支持自定义词库
                    1737    英语      有标点 不支持自定义词库
                    1637    粤语      有标点 不支持自定义词库
                    1837    四川话     有标点 不支持自定义词库
                    1936    普通话远场   远场模型    有标点 不支持
                    '''
                    print("hear : ")
                    # print(result['result'])
                    ppt_send = ''
                    # 需要判断是否为空!
                    # ppt 区域,专门设置ppt需要的内容
                    if (result.get('result') == None):
                        print(' get none ')
                        break
                    for item in self.key_words:
                        if item in result.get('result')[0]:
                            ppt_send = item
                            '''
                            if(item == '手作'):
                                ppt_send = 'diy'
                            elif(item == '订阅'):
                                ppt_send = 'RSS'
                            '''
                            ws = create_connection("ws://127.0.0.1:9001")
                            ws.send(ppt_send)
                            ws.close()
                        '''
                        # server.send_message_to_all('北京欢迎你')
                        ws = create_connection("ws://127.0.0.1:9001")
                        ws.send("Hello, PeKing! ")
                        ws.close()
                        '''
                    # modified end by jxc
                    print("Recode a piece of  voice successfully! 1 ")
                    time_count = self.TIME_COUNT
                    return True  # modified by jxc 20180912 for loop again and again
                    # return True
            if time_count == 0:
                if len(save_buffer) > 0:
                    self.Voice_String = save_buffer
                    save_buffer = []
                    # added by jxc 20180913
                    client = AipSpeech(self.BAIDU_APP_ID, self.BAIDU_API_KEY,
                                       self.BAIDU_SECRET_KEY)
                    voice_buffer = np.array(self.Voice_String).tostring()
                    result = client.asr(voice_buffer, 'wav', 16000, {
                        'dev_pid': 1936,
                    })
                    '''
                    1536    普通话(支持简单的英文识别)  搜索模型    无标点 支持自定义词库
                    1537    普通话(纯中文识别)  输入法模型   有标点 不支持自定义词库
                    1737    英语      有标点 不支持自定义词库
                    1637    粤语      有标点 不支持自定义词库
                    1837    四川话     有标点 不支持自定义词库
                    1936    普通话远场   远场模型    有标点 不支持
                    '''
                    print("hear : ")
                    # print(result['result'])
                    ppt_send = ''
                    # 需要判断是否为空!
                    # ppt 区域,专门设置ppt需要的内容
                    if (result.get('result') == None):
                        print(' get none ')
                        break
                    for item in self.key_words:
                        if item in result.get('result')[0]:
                            ppt_send = item
                            '''
                            if(item == '手作'):
                                ppt_send = 'diy'
                            elif(item == '订阅'):
                                ppt_send = 'RSS'
                            '''
                            ws = create_connection("ws://127.0.0.1:9001")
                            ws.send(ppt_send)
                            ws.close()
                        '''
                        # server.send_message_to_all('北京欢迎你')
                        ws = create_connection("ws://127.0.0.1:9001")
                        ws.send("Hello, PeKing! ")
                        ws.close()
                        '''

                    time_count = self.TIME_COUNT
                    # add end by jxc
                    print("Recode a piece of  voice successfully! 2 ")
                    time_count = self.TIME_COUNT
                    return True
                else:
                    return True
Example #28
0
	def __init__(self, config):
		APP_ID = config[SLUG]['app_id']
		API_KEY = config[SLUG]['api_key']
		SECRET_KEY = config[SLUG]['secret_key']
		self._client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
Example #29
0
class BaiduTTS(object):
    def __init__(self):
        self.__tools = Tools()
        self.__AppID = self.__get_AppID()  # 百度AppID
        self.__APIKey = self.__get_APIKey()  # 百度APIKey
        self.__SecretKey = self.__get_SecretKey()  # 百度SecretKey
        self.__lang = self.__get_lang()  # 语言选择,填写zh
        self.__ctp = self.__get_ctp()  # 客户端类型选择,web端填写1
        self.__cuid = self.__get_cuid(
        )  # 用户唯一标识,用来区分用户,填写机器 MAC 地址或 IMEI 码,长度为60以内
        self.__spd = self.__get_spd()  # 语速,取值0-9,默认为5中语速
        self.__pit = self.__get_pit()  # 音调,取值0-9,默认为5中语调
        self.__vol = self.__get_vol()  # 音量,取值0-15,默认为5中音量
        self.__per = self.__get_per(
        )  # 发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女
        self.__client = AipSpeech(self.__AppID, self.__APIKey,
                                  self.__SecretKey)

    def __get_AppID(self):  # 百度AppID
        return '14998128'

    def __get_APIKey(self):  # 百度APIKey
        return 'maUfjhtuP3LBrfXmBsfyhtIF'

    def __get_SecretKey(self):  # 百度SecretKey
        return '6xhMVKhpI7rSzHu7koO8H6GVr0y17wK5'

    def __get_lang(self):  # 语言选择,填写zh
        return 'zh'

    def __get_ctp(self):  # 客户端类型选择,web端填写1
        return '1'

    def __get_cuid(self):  # 用户唯一标识,用来区分用户,填写机器 MAC 地址或 IMEI 码,长度为60以内
        return self.__tools.get_cuid()

    def __get_spd(self):  # 语速,取值0-9,默认为5中语速
        return '5'

    def __get_pit(self):  # 音调,取值0-9,默认为5中语调
        return '5'

    def __get_vol(self):  # 音量,取值0-15,默认为5中音量
        return '5'

    def __get_per(self):  # 发音人选择, 0为女声,1为男声,3为情感合成-度逍遥,4为情感合成-度丫丫,默认为普通女
        return '0'

    def TTS(self, msg):  #输入文字,输出语音文件(MP3格式)
        speechFile = 'speech.mp3'
        if os.path.exists(speechFile):
            os.remove(speechFile)
        try:
            result = self.__client.synthesis(
                msg, self.__lang, self.__ctp, {
                    'cuid': self.__cuid,
                    'spd': self.__spd,
                    'pit': self.__pit,
                    'vol': self.__vol,
                    'per': self.__per
                })
            if not isinstance(result, dict):
                with open(speechFile, 'wb') as f:
                    f.write(result)
                return speechFile
            else:
                return ''
        except:
            return ''
Example #30
0
    def record_voice(self):
        APP_ID = '24142986'
        API_KEY = 'lz8wrZPBovwoWXqpL2FRBtDX'
        SECRET_KEY = '34kKxkbMKB8VaqWZRQxV1y4QbPNW0xkG'

        client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)

        CHUNK = 1024
        FORMAT = pyaudio.paInt16  # 16bit编码格式
        CHANNELS = 1  # 单声道
        RATE = 16000  # 16000采样频率

        while True:
            if self.record == 1:
                p = pyaudio.PyAudio()
                # 创建音频流
                stream = p.open(
                    format=FORMAT,  # 音频流wav格式
                    channels=CHANNELS,  # 单声道
                    rate=RATE,  # 采样率16000
                    input=True,
                    frames_per_buffer=CHUNK)
                print("Start Recording...")
                frames = []  # 录制的音频流
                # 录制音频数据
                while True:
                    # print("begin")
                    for i in range(0, 2):
                        data1 = stream.read(CHUNK)
                        frames.append(data1)
                    audio_data1 = np.fromstring(data1, dtype=np.short)
                    temp1 = np.max(audio_data1)
                    if temp1 > 550:
                        # print("检测到信号")
                        # print('当前阈值:', temp1)
                        less = 0
                        while True:
                            # print("recording")
                            for i in range(0, 5):
                                data2 = stream.read(CHUNK)
                                frames.append(data2)
                            audio_data2 = np.fromstring(data2, dtype=np.short)
                            temp2 = np.max(audio_data2)
                            if temp2 < 550:
                                less = less + 1
                                # print("below threshold, counting: ", less, '当前阈值:', temp2)
                                # 如果有连续15个循环的点,都不是声音信号,就认为音频结束了
                                if less == 2:
                                    break
                            else:
                                less = 0
                                # print('当前阈值:', temp2)
                        break
                    else:
                        frames = []
                # 录制完成
                stream.stop_stream()
                stream.close()
                p.terminate()
                print("Recording Done...")
                # 保存音频文件
                with wave.open("./1.wav", 'wb') as wf:
                    wf.setnchannels(CHANNELS)
                    wf.setsampwidth(p.get_sample_size(FORMAT))
                    wf.setframerate(RATE)
                    wf.writeframes(b''.join(frames))
                    wf.close()
                result = client.asr(self.get_file_content('1.wav'), 'wav',
                                    16000, {
                                        'dev_pid': 1537,
                                    })
                if result.__contains__('result') == False:
                    continue
                elif result.__contains__('result') == True:
                    result = ''.join(result["result"])
                    print(result)
                    if result == '退出。':
                        self.signal.emit()
                    elif result == '象棋单机游戏。':
                        os.system('python ./ChineseChess/main.py')
                    elif result == '象棋联机游戏。':
                        os.system('python ./ChineseChess/client.py')
                    elif result == '连连看。':
                        os.system('python ./link_game/main.py')
                    elif result == '扫雷。':
                        os.system('python ./Minesweeper/main.py')
                    elif result == '拼图。':
                        os.system('python ./Puzzle/game.py')
                    elif result == '推箱子。':
                        os.system('python ./Sokoban/game.py')
                    elif result == '五子棋。':
                        os.system('python ./Gobang/main.py')
Example #31
0
 def __init__(self):
     self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY)
Example #32
0
 def __init__(self, appid, api_key, secret_key, **args):
     super(self.__class__, self).__init__()
     self.client = AipSpeech(appid, api_key, secret_key)
Example #33
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from aip import AipSpeech

app_id = "14975947"
api_key = "X9f3qewZCohppMHxlunznUbi"
secret_key = "LupWgIIFzZ9kTVNZSH5GOguNGZIqqTom"

client = AipSpeech(app_id, api_key, secret_key)
result = client.synthesis("哈哈,你好!", "zh", 1, {
    "vol": 9,
    "spd": 6,
    "pit": 7,
    "per": 4,
})

with open("audio.mp3", "wb") as f:
    f.write(result)
Example #34
0
# python VoiceTest.py
import requests
import json
from aip import AipSpeech
import time
import os

App_ID='10252241'
API_Key='ZfPzLAEjqsiOExjSEImlD8gb'
Secret_Key='d94ebabd13a1c5f33f216e9af5577fbb'

aip_Speech=AipSpeech(App_ID, API_Key, Secret_Key)

# 新闻抓取函数
def crawler():
	headers = {
	'Host':'36kr.com',
	'User-Agent':'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0',
	'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
	'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
	'Accept-Encoding':'gzip, deflate',
	'Referer':'http://36kr.com/',
	'Cookie':'aliyungf_tc=AQAAAFGIRiN1AwEAcFGi0/aLqHNa5GWU; device-uid=e840e1d0-c912-11e7-8319-89834efd4d75; ktm_ab_test=t.21_v.deploy; krnewsfrontss=4bfe287abfa729f568b69f2f103174b4; M-XSRF-TOKEN=a8c79e2e8f3d6e6d672a57d6b2e7aad5534a6fb8c2bfe776c775f7c4b4d8ddd4; kr_stat_uuid=9EKQn25177447; TY_SESSION_ID=0b83317b-1234-4696-b364-ebd23fd922f7',
	'Connection':'keep-alive',
	'Upgrade-Insecure-Requests':1,
	'Cache-Control':'max-age=0',
	}

	json_data=''
	url = 'http://36kr.com/newsflashes'
	page = requests.get(url,headers=headers)
# -*- coding: utf-8 -*-
"""
Created on Sun Aug  5 17:28:28 2018

@author: jukuo
"""
#-*- coding: utf-8 -*-                    
from aip import AipSpeech
import make_wav

""" 你的 APPID AK SK """
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()

# 识别本地文件
r = GenAudio()
r.num_samples = 2000    #pyaudio内置缓冲大小
r.sampling_rate = 16000  #取样频率
r.level = 1500          #声音保存的阈值
r.count_num = 20        #count_num个取样之内出现COUNT_NUM个大于LEVEL的取样则记录声音
r.save_length = 8       #声音记录的最小长度:save_length * num_samples 个取样
r.time_count = 10        #录音时间,单位s
r.read_audio()
r.save_wav("./test.wav")
Example #36
0
readOffLine: 离线识别
readOnLine: 在线识别

@author: funyoo
"""

from aip import AipSpeech
import speech_recognition as sr

# 百度需要的参数
APP_ID = 'xxxxxxxxxx'
API_KEY = 'xxxxxxxxxxxxxxxxxx'
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxx'

CLIENT = AipSpeech(APP_ID, API_KEY, SECRET_KEY)


# 离线识别
def readOffLine():
    r = sr.Recognizer()  #调用识别器
    test = sr.AudioFile("command.wav")  #导入语音文件
    with test as source:
        audio = r.record(source)
        type(audio)
        c = r.recognize_sphinx(audio, language='zh-cn')  #识别输出
        return c


# 在线识别
def readOnLine():