python语音识别

文本转换为语音

1、使用pyttsx

pip install pyttsx3

import pyttsx3 as pyttsx

engine = pyttsx.init()
engine.say('你好pyttsx')
engine.say('好好学习')
engine.runAndWait()

2、使用SAPI

from win32com.client import Dispatch

spearker = Dispatch('SAPI.SpVoice')
spearker.Speak('大家好')
del spearker

3、使用SpeechLib

可以从文本文件中获取输入,再将其转换为语音

from comtypes.client import CreateObject
from comtypes.gen import SpeechLib

engine = CreateObject('SAPI.SpVoice')
stream = CreateObject('SAPI.SpFileStream')

infile = 'demo.txt'
outfile = 'demo_audio.wav'

stream.Open(outfile, SpeechLib.SSFMCreateForWrite)
engine.AudioOutputStream = stream
#读取文本内容
f = open(infile,'r',encoding='utf-8')
theText = f.read()
f.close()
engine.speak(theText)
stream.close()

 

语音转换为文本

       PocketSphinx是一个用于语音转换文本的开源API。它是一个轻量级的语音识别引擎,尽管在桌面端也能很好地工作,它还专门为手机和移动设备做过调优。

     pip install PocketSphinx

import speech_recognition as sr


audio_file = 'demo_audio.wav'
r = sr.Recognizer()
#打开语音文件
with sr.AudioFile(audio_file) as source:
	audio = r.record(source)


#print('文本内容: ',r.recognize_sphinx(audio,language='zh_CN'))  #汉语
print('文本内容: ',r.recognize_sphinx(audio))  #英语

 

 

你可能感兴趣的:(python,语音识别)