lep1 commited on
Commit
d67b468
1 Parent(s): a2d2c17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -9
app.py CHANGED
@@ -14,6 +14,7 @@ from ultralyticsplus import YOLO, render_result
14
 
15
  from gtts import gTTS
16
  import os
 
17
 
18
  from convert import convert_to_braille_unicode, parse_xywh_and_class
19
 
@@ -29,13 +30,6 @@ def load_image(image_path):
29
  image = PIL.Image.open(image_path)
30
  return image
31
 
32
- def generate_audio(text, filename="detected_braille.mp3"):
33
- """Convert text to speech and save audio as file"""
34
- tts = gTTS(text=text, lang='en')
35
- audio_path = f"./output_audio/detected_braille.mp3"
36
- tts.save(audio_path)
37
- return audio_path
38
-
39
  # title
40
  st.title("Braille Pattern Detection")
41
 
@@ -106,9 +100,34 @@ try:
106
  box_classes = box_line[:, -1]
107
  for each_class in box_classes:
108
  str_left_to_right += convert_to_braille_unicode(model.names[int(each_class)])
109
- st.write(str_left_to_right)
110
  result += str_left_to_right + "\n"
111
- audio_file_path = generate_audio(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
 
113
  except Exception as ex:
114
  st.write("Please try again with images with types of JPG, JPEG, PNG ...")
 
14
 
15
  from gtts import gTTS
16
  import os
17
+ import pygame
18
 
19
  from convert import convert_to_braille_unicode, parse_xywh_and_class
20
 
 
30
  image = PIL.Image.open(image_path)
31
  return image
32
 
 
 
 
 
 
 
 
33
  # title
34
  st.title("Braille Pattern Detection")
35
 
 
100
  box_classes = box_line[:, -1]
101
  for each_class in box_classes:
102
  str_left_to_right += convert_to_braille_unicode(model.names[int(each_class)])
 
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(f"./output_audio/output.mp3")
111
+
112
+ audio_file_path = f"./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("f./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 ...")