lep1 commited on
Commit
de08eb3
1 Parent(s): d7c820e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -31
app.py CHANGED
@@ -103,39 +103,34 @@ try:
103
  result += str_left_to_right + "\n"
104
  st.write(str_left_to_right)
105
 
106
- def text_to_speech_gtts(text, lang='en'):
107
- # 将文本转换为语音
108
- tts = gTTS(text=text, lang=lang)
109
- # 保存音频文件
110
- tts.save("./output_audio/output.mp3")
111
-
112
- audio_file_path = "./output_audio/output.mp3"
113
-
114
- text_to_speech_gtts(result)
115
 
116
- def play_mp3(file_path):
117
- # 初始化 pygame
118
- pygame.mixer.init()
119
- # 加载 MP3 文件
120
- pygame.mixer.music.load(file_path)
121
- # 播放 MP3 文件
122
- pygame.mixer.music.play()
123
 
124
- # 等待音频播放完毕
125
- while pygame.mixer.music.get_busy():
126
- continue
127
 
128
- # 调用函数,播放 MP3 文件
129
- play_mp3("./output_audio/output.mp3")
 
 
 
 
 
 
 
 
 
 
 
 
 
130
 
131
-
132
  except Exception as ex:
133
- st.write("Please try again with images with types of JPG, JPEG, PNG ...")
134
-
135
- with open(audio_file_path, "rb") as fl:
136
- st.download_button(
137
- "Download Braille Audio",
138
- data=fl,
139
- file_name="detected_braille.mp3",
140
- mime="audio/mp3",
141
- )
 
103
  result += str_left_to_right + "\n"
104
  st.write(str_left_to_right)
105
 
106
+ AUDIO_OUTPUT_FOLDER = "./output_audio"
107
+ os.makedirs(AUDIO_OUTPUT_FOLDER, exist_ok=True)
 
 
 
 
 
 
 
108
 
109
+ def text_to_speech_gtts(text, lang='en', filename="output.mp3"):
110
+ """将文本转换为语音并保存音频文件"""
111
+ tts = gTTS(text=text, lang=lang)
112
+ audio_path = os.path.join(AUDIO_OUTPUT_FOLDER, filename)
113
+ tts.save(audio_path)
114
+ return audio_path
 
115
 
116
+ # 示例检测结果文本
117
+ detected_text = "This is a sample Braille detection result."
 
118
 
119
+ try:
120
+ # 生成语音文件
121
+ audio_file_path = text_to_speech_gtts(detected_text)
122
+
123
+ # 在 Streamlit 中播放音频
124
+ st.audio(audio_file_path, format="audio/mp3")
125
+
126
+ # 提供下载按钮
127
+ with open(audio_file_path, "rb") as audio_file:
128
+ st.download_button(
129
+ label="Download Braille Audio",
130
+ data=audio_file,
131
+ file_name="detected_braille.mp3",
132
+ mime="audio/mp3",
133
+ )
134
 
 
135
  except Exception as ex:
136
+ st.write("An error occurred while processing the audio.")