ros下基於百度語音的,語音識別和語音合成


代碼地址如下:
http://www.demodashi.com/demo/13153.html

概述:

本demo是ros下基於百度語音的,語音識別和語音合成,能夠實現文字轉語音,語音轉文字的功能。

詳細:

1. 安裝庫與環境

首先確保已經安裝了以下兩個庫文件。

1.1 Python 音頻處理庫 PyAudio

python -m pip install pyaudio

1.2 Python 音頻處理庫 vlc

pip install python-vlc

1.3 ROS

確保安裝了ROS
http://wiki.ros.org/indigo

2. 實時語音識別與語音合成

2.1 運行

Speech Recognition(語音識別):

roslaunch simple_voice simple_voice.launch

Text To Speech(語音合成):

roslaunch simple_voice simple_speaker.launch

2.2 概述

在運行前先確保安裝了python的pyaudio 以及 vlc 庫文件.

百度語音識別為開發者提供業界優質且免費的語音服務,通過場景識別優化,,准確率達到90%以上,讓您的應用繪“聲”繪色。

本文中的語音識別功能:采用百度語音識別庫,實現語音轉化為文字的功能,並且輸出為ros話題。
本文中的語音合成功能:采用百度語音識別庫,實現將文字轉化為語音並且存儲為mp3/wav文件。

2.3 Node

包中一共有3個節點:

  • node_main.py
  • simple_speek.py
  • voice_node.py.

node_main.py 是TTS(Text To Speech)的demo節點, 該demo是和laser scanner一起運行的,當laser檢測到一個障礙物,node_main將會觸發simple_speek.py讓機器說出英語或者漢語 'excuse me', 'make a way for me pls'或者'請讓一下',等話語。

simple_speek.py 將會訂閱 std_msgs/String 消息類型的話題,並且將該話題中輸入的文字轉化為語音

voice_node.py 將會識別您在5秒內說出的話語並且輸出到終端上。

2.4 訂閱的 Topic

TTS(Text To Speech - simple_speek.py):

/speak_string(std_msgs/String)

語音合成節點中機器將會說出的文字。

Demo(node_main.py):

/SpeakerSubTopic (std_msgs/String )

這個是也是一個語音合成的demo節點,是用來觸發Text To Speech - simple_speek.py節點的,您可以隨意更改SpeakerSubTopic中的文本。

當您給該節點發布stop將會立刻出發Text To Speech - simple_speek.py節點開始說話了。

2.5 發布的 Topic

Demo (node_main.py):

/speak_string(std_msgs/String )

該話題將會定義機器的語音合成(TTS)說什么。

Speech Recognition(voice_node.py):

/Rog_result(std_msgs/String )

這個是語音識別程序,功能是將語音轉化為文字。
觸發是在終點中輸入ENTER

3. 實現過程的部分代碼展示

simple_speek.py中播放合成語音部分:

def play_video(self,file_):
	#print '\n start speaking ', "file://%s"%file_
	rospy.loginfo('start speaking ')
	player = vlc.MediaPlayer("file://%s"%file_)
	player.play()
	rospy.sleep(1)
	while player.is_playing():
		pass
		#self.pub.publish('PENDING')
	rospy.loginfo('done\n')

simple_speek.py中訂閱消息部分:

class speeker():
	def __init__(self):
		self.define()
		rospy.Subscriber('speak_string', String, self.SpeedCB, queue_size=1)
		rospy.Timer(rospy.Duration(self.ResponseSensitivity), self.TimerCB)
		rospy.spin()

simple_speek.py中語音合成部分:

def SpeedCB(self, data):
	with self.locker:
		speak_string = data.data
		if self.TalkNow:
			self.WavName = speak_string
			self.TalkNow = False
			self.mp3file = '%s'%self.path + self.WavName + '.%s'%self.FORMAT
			if os.path.exists(r'%s'%self.mp3file):
				self.play_video(self.mp3file)
			else:
				self.speek(speak_string)

voice_node.py 觸發部分

def __init__(self):
if_continue=''
while not rospy.is_shutdown() and if_continue == '':
	 self.define()
	self.recode()
	words = self.reg()
	reg = rospy.Publisher('Rog_result', String, queue_size=1)
	 reg.publish(words)
	#self.savewav("testing")#testing
	if_continue = raw_input('pls input ENTER to continue')

voice_node.py 語音識別部分

while True and NO_WORDS:
	time_out -= 1
	print 'time_out in', time_out # 讀入NUM_SAMPLES個取樣
	string_audio_data = stream.read(self.NUM_SAMPLES) # 將讀入的數據轉換為數組
	audio_data = np.fromstring(string_audio_data, dtype=np.short) 

	# 查看是否沒有語音輸入
	NO_WORDS -= 1
	if np.max(audio_data) > self.UPPER_LEVEL:
		NO_WORDS=self.NO_WORDS
	print 'self.NO_WORDS ', NO_WORDS
	print 'np.max(audio_data) ', np.max(audio_data)

	# 計算大於LOWER_LEVEL的取樣的個數
	large_sample_count = np.sum( audio_data > self.LOWER_LEVEL )

	# 如果個數大於COUNT_NUM,則至少保存SAVE_LENGTH個塊
	if large_sample_count > self.COUNT_NUM:
		save_count = self.SAVE_LENGTH 
	else: 
		save_count -= 1

	# 將要保存的數據存放到save_buffer中
	if save_count < 0:
		save_count = 0 
	elif save_count > 0 : 
		save_buffer.append( string_audio_data ) 
	else:
		pass

	# 將save_buffer中的數據寫入WAV文件,WAV文件的文件名是保存的時刻
	if len(save_buffer) > 0 and NO_WORDS==0: 
		self.Voice_String = save_buffer
		save_buffer = [] 
		rospy.loginfo( "Recode a piece of voice successfully!")
	elif len(save_buffer) > 0 and time_out==0: 
		self.Voice_String = save_buffer
		save_buffer = [] 
		rospy.loginfo( "Recode a piece of voice successfully!")
	else: 
		pass

4. 項目文件結構

ros下基於百度語音的,語音識別和語音合成

代碼地址如下:
http://www.demodashi.com/demo/13153.html

注:本文著作權歸作者,由demo大師代發,拒絕轉載,轉載需要作者授權


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM