lep1 commited on
Commit
6a12e25
·
verified ·
1 Parent(s): 6d3d75e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -14
app.py CHANGED
@@ -29,15 +29,11 @@ def load_image(image_path):
29
  image = PIL.Image.open(image_path)
30
  return image
31
 
32
- def play_audio(text):
33
- """Convert text to speech and play audio in Streamlit"""
34
  tts = gTTS(text=text, lang='en')
35
- tts.save("detected_braille.mp3")
36
- audio_file = open("detected_braille.mp3", "rb")
37
- audio_bytes = audio_file.read()
38
- st.audio(audio_bytes, format="audio/mp3")
39
- audio_file.close()
40
- os.remove("detected_braille.mp3") # 删除文件,节省空间
41
 
42
  # title
43
  st.title("Braille Pattern Detection")
@@ -114,16 +110,16 @@ try:
114
  st.write(str_left_to_right)
115
 
116
  # Convert detected Braille text to audio and play
117
- if detected_text:
118
- play_audio(detected_text)
119
 
120
  except Exception as ex:
121
  st.write("Please try again with images with types of JPG, JPEG, PNG ...")
122
 
123
- with open(IMAGE_DOWNLOAD_PATH, "rb") as fl:
124
  st.download_button(
125
- "Download object-detected image",
126
  data=fl,
127
- file_name="image0.jpg",
128
- mime="image/jpg",
129
  )
 
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
+ tts.save(filename)
36
+ return filename
 
 
 
 
37
 
38
  # title
39
  st.title("Braille Pattern Detection")
 
110
  st.write(str_left_to_right)
111
 
112
  # Convert detected Braille text to audio and play
113
+ if detected_text:
114
+ audio_file_path = generate_audio(detected_text)
115
 
116
  except Exception as ex:
117
  st.write("Please try again with images with types of JPG, JPEG, PNG ...")
118
 
119
+ with open(audio_file_path, "rb") as fl:
120
  st.download_button(
121
+ "Download Braille Audio",
122
  data=fl,
123
+ file_name="detected_braille.mp3",
124
+ mime="audio/mp3",
125
  )