python: Text-to-Speech and Speech-to-Text

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
python.exe -m pip install --upgrade pip
pip install pyttsx3
pip install comtypes
pip install Pillow
pip install requests
 
pip install PocketSphinx
pip install SpeechRecognition
 
python: 3.11
 
"""
 
 
import sys
import os
import pyttsx3 as pyttsx
from win32com.client import Dispatch
from comtypes.client import CreateObject
#from comtypes.tools import SpeechLib
import speech_recognition as sr
engine = CreateObject("SAPI.SpVoice")
stream = CreateObject("SAPI.SpFileStream")
from comtypes.gen import SpeechLib
 
 
class ttsHepler(object):
    """
 
    """
 
 
 
    def ttsstrx(self, word:str):
        """
        文本转换为语音
        :param word:
        :return:
        """
        engine = pyttsx.init()
        engine.say(word)
        engine.runAndWait()
 
 
    def ttswin(self,word:str):
        """
        文本转换为语音
        :param word:
        :return:
        """
        #msg = "Python由荷兰数学和计算机科学研究学会的吉多·范罗苏姆于1990年代初设计,作为一门叫做ABC语言的替代品。"
        speaker = Dispatch("SAPI.SpVoice")
        speaker.Speak(word)
        del speaker
 
    def ttslib(self,word:str,dy=False):
        """
     文本转语音
        :param word:
        :return:
        """
        engine = CreateObject("SAPI.SpVoice",dynamic=dy)
        stream = CreateObject("SAPI.SpFileStream",dynamic=dy)
        from comtypes.gen import SpeechLib
        infile = 'fileText.txt'
        f = open(infile, 'r',encoding='utf-8')
        theText = f.read()
        f.close()
 
        outfile = 'demo_audio.wav'
 
        stream.Open(outfile, SpeechLib.SSFMCreateForWrite)
        engine.AudioOutputStream = stream
        engine.speak(theText)
        stream.close()
 
    def sttspeech(self):
        """
        语音转换为文本
        下载普通话识别文件
        下载路径:https://sourceforge.net/projects/cmusphinx/files/Acoustic%20and%20Language%20Models/Mandarin/
        解压之后,修改文件名称,cmusphinx-zh-cn-5.2 改为 zh-CN,
        zh_cn.cd_cont_5000文件夹改为acoustic-model,
        zh_cn.dic改为pronounciation-dictionary.dict,
        zh_cn.lm.bin改为language-model.lm.bin。
        然后移动zn-CN文件夹到python3\Lib\site-packages\speech_recognition\pocketsphinx-data下。
        :return:
        """
        r = sr.Recognizer()
        audio_file = 'demo_audio.wav'
        with sr.AudioFile(audio_file) as source:
            audio = r.record(source)
        try:
            print("文本内容:", r.recognize_sphinx(audio, language='zh-CN'))
            # 默认会识别为英文,如果要识别中文,需要下载普通话识别文件
        except Exception as e:
            print(e)

  

posted @   ®Geovin Du Dream Park™  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
历史上的今天:
2020-08-05 csharp:Validate email address using C#
2020-08-05 Sql:SQL Server CREATE SEQUENCE statement
2016-08-05 C++: read access data using ADOX.DLL and System::Data::OleDb
2016-08-05 C++: DataGridView::DataSource
2016-08-05 Software license key and activation
2013-08-05 Css:Conditional comments 条件注释
2013-08-05 SQL Server迭代求和
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示