今日は、テキストから音声に変換する装置を作ってみましょう。
さあ、はじめましょう
プロジェクトの紹介
Text to Speech は、テキストを人間が理解できる音声に変換することを意味します。このアプリケーションは、文章や単語を正しく読むのが苦手な人にとって、非常に便利なものです。
このアプリケーションでは、ユーザーが入力ボックスにテキストを入力すると、ボタンを1回クリックするだけで、アプリケーションがテキストを音声に変換します。
1. ライブラリのインポート
まず、このアプリケーションに必要な以下のライブラリをインポートします。
- tkinter
- gTTS
- playsound
|
1
2
3
|
from tkinter import *
from gtts import gTTS
from playsound import playsound
|
2. Tkinterの初期ウィンドウを作成する
まず、tkinterのウィンドウを初期化し、ジオメトリや背景色、タイトルなどの設定を追加します。
|
1
2
3
4
|
window = Tk()
window.geometry("350x300")
window.configure(bg='#FAD7A0')
window.title("TEXT TO SPEECH")
|
3. ウィンドウにウィジェットを追加する
次のステップでは、画面上にラベル、入力ボックス、ボタンなどを追加します。そのためのコードを以下に示します。ウィジェットの宣言は、便宜上、ハイライトされています。
このアプリケーションでは、3つのボタンを使用します。ひとつはテキストを再生するボタン、もうひとつはアプリケーションをリセットするボタン、そして最後のひとつはアプリケーションを終了するボタンです。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
Label(window, text = " TEXT TO SPEECH ", font = "arial 20 bold", bg='black',fg="white").pack()
Msg = StringVar()
Label(window,text ="Enter Your Text Here: ", font = 'arial 20 bold', fg ='darkgreen').place(x=5,y=60)
entry_field = Entry(window, textvariable = Msg ,width ='30',font = 'arial 15 bold',bg="lightgreen")
entry_field.place(x=5,y=100)
Button(window, text = "PLAY TEXT", font = 'arial 15 bold' , width = '20',bg = 'orchid',fg="white").place(x=35,y=140)
Button(window, font = 'arial 15 bold',text = 'RESET APPLICATION', width = '20',bg = 'darkgreen',fg="white").place(x=35 , y = 190)
Button(window, font = 'arial 15 bold',text = 'EXIT APPLICATION', width = '20' , bg = 'red',fg="white").place(x=35 , y = 240)
|
4. テキストを音声に変換するボタン用の関数を作成する
アプリケーションの終了ボタンは、ウィンドウを閉じるだけのシンプルなものです。
次の関数はリセットボタンで、入力ボックスに空白の文字列をセットして内容を削除します。最後の機能は、テキストを音声に変換するために必要なものです。
-
-
get: エントリーボックスに入力されたテキストを取得し、変数に格納します。
-
-
-
gTTS: 関数に渡されたメッセージを音声に変換します。
-
-
-
save: スピーチを mp3 フォーマットで保存します。
-
- playsound` : 前のステップで保存した音声を再生します。
|
1
2
3
4
5
6
7
8
9
10
11
|
def Text_to_speech():
Message = entry_field.get()
speech = gTTS(text = Message)
speech.save('data.mp3')
playsound('data.mp3')
def Exit():
window.destroy()
def Reset():
Msg.set("")
|
次のステップでは、ボタン宣言に command プロパティを追加して、各ボタンに機能を結びつけます。
テキストを音声に変換する最終コード
このプロジェクトの最終的なコードは以下のとおりです。
|
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
|
from tkinter import *
from gtts import gTTS
from playsound import playsound
def Text_to_speech():
Message = entry_field.get()
speech = gTTS(text = Message)
speech.save('data.mp3')
playsound('data.mp3')
def Exit():
window.destroy()
def Reset():
Msg.set("")
window = Tk()
window.geometry("350x300")
window.configure(bg='#FAD7A0')
window.title("TEXT TO SPEECH")
Label(window, text = " TEXT TO SPEECH ", font = "arial 20 bold", bg='black',fg="white").pack()
Msg = StringVar()
Label(window,text ="Enter Your Text Here: ", font = 'arial 20 bold', fg ='darkgreen').place(x=5,y=60)
entry_field = Entry(window, textvariable = Msg ,width ='30',font = 'arial 15 bold',bg="lightgreen")
entry_field.place(x=5,y=100)
Button(window, text = "PLAY TEXT", font = 'arial 15 bold' , command = Text_to_speech ,width = '20',bg = 'orchid',fg="white").place(x=35,y=140)
Button(window, font = 'arial 15 bold',text = 'RESET APPLICATION', width = '20' , command = Reset,bg = 'darkgreen',fg="white").place(x=35 , y = 190)
Button(window, font = 'arial 15 bold',text = 'EXIT APPLICATION', width = '20' , command = Exit, bg = 'red',fg="white").place(x=35 , y = 240)
window.mainloop() |
サンプル出力映像
以下のビデオでは、アプリケーションの動作が確認できます。ぜひご覧ください。
まとめ
Python tkinterの音声合成プロジェクトが完成しました。気に入っていただけたでしょうか?
お読みいただきありがとうございました。