Politrees commited on
Commit
4aade4a
·
verified ·
1 Parent(s): fca0fa2

Update steganography.py

Browse files
Files changed (1) hide show
  1. steganography.py +18 -8
steganography.py CHANGED
@@ -57,9 +57,19 @@ def create_audio_with_spectrogram(text, base_width, height, max_font_size, margi
57
  y = spectrogram_image_to_audio(spec_image)
58
  audio_path = 'output.wav'
59
  sf.write(audio_path, y, DEFAULT_SAMPLE_RATE)
60
- image_path = 'spectrogram.png'
61
- plt.imsave(image_path, spec_image, cmap='gray')
62
- return audio_path, image_path
 
 
 
 
 
 
 
 
 
 
63
 
64
  # Function for displaying the spectrogram of an audio file
65
  def display_audio_spectrogram(audio_path):
@@ -69,11 +79,11 @@ def display_audio_spectrogram(audio_path):
69
 
70
  plt.figure(figsize=(10, 4))
71
  librosa.display.specshow(S_dB, sr=sr, x_axis='time', y_axis='mel')
72
- plt.axis('off') # Отключаем оси и надписи
73
- plt.tight_layout(pad=0) # Убираем отступы
74
 
75
  spectrogram_path = 'uploaded_spectrogram.png'
76
- plt.savefig(spectrogram_path, bbox_inches='tight', pad_inches=0, transparent=True) # Сохраняем без фона
77
  plt.close()
78
  return spectrogram_path
79
 
@@ -102,13 +112,13 @@ with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="gr
102
  with gr.Column(variant='panel'):
103
  with gr.Group():
104
  output_audio = gr.Audio(type="filepath", label="Generated audio")
105
- output_image = gr.Image(type="filepath", label="Spectrogram")
106
 
107
  def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
108
  print("\n", text)
109
  return create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
110
 
111
- generate_button.click(gradio_interface_fn, inputs=[text, base_width, height, max_font_size, margin, letter_spacing], outputs=[output_audio, output_image])
112
 
113
  with gr.Tab("Image to Spectrogram"):
114
  with gr.Group():
 
57
  y = spectrogram_image_to_audio(spec_image)
58
  audio_path = 'output.wav'
59
  sf.write(audio_path, y, DEFAULT_SAMPLE_RATE)
60
+
61
+ # Create spectrogram from audio
62
+ S = librosa.feature.melspectrogram(y=y, sr=DEFAULT_SAMPLE_RATE)
63
+ S_dB = librosa.power_to_db(S, ref=np.max)
64
+ plt.figure(figsize=(10, 4))
65
+ librosa.display.specshow(S_dB, sr=DEFAULT_SAMPLE_RATE, x_axis='time', y_axis='mel')
66
+ plt.axis('off')
67
+ plt.tight_layout(pad=0)
68
+ spectrogram_path = 'spectrogram.png'
69
+ plt.savefig(spectrogram_path, bbox_inches='tight', pad_inches=0, transparent=True)
70
+ plt.close()
71
+
72
+ return audio_path, spectrogram_path
73
 
74
  # Function for displaying the spectrogram of an audio file
75
  def display_audio_spectrogram(audio_path):
 
79
 
80
  plt.figure(figsize=(10, 4))
81
  librosa.display.specshow(S_dB, sr=sr, x_axis='time', y_axis='mel')
82
+ plt.axis('off')
83
+ plt.tight_layout(pad=0)
84
 
85
  spectrogram_path = 'uploaded_spectrogram.png'
86
+ plt.savefig(spectrogram_path, bbox_inches='tight', pad_inches=0, transparent=True)
87
  plt.close()
88
  return spectrogram_path
89
 
 
112
  with gr.Column(variant='panel'):
113
  with gr.Group():
114
  output_audio = gr.Audio(type="filepath", label="Generated audio")
115
+ output_spectrogram = gr.Image(type="filepath", label="Spectrogram")
116
 
117
  def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
118
  print("\n", text)
119
  return create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
120
 
121
+ generate_button.click(gradio_interface_fn, inputs=[text, base_width, height, max_font_size, margin, letter_spacing], outputs=[output_audio, output_spectrogram])
122
 
123
  with gr.Tab("Image to Spectrogram"):
124
  with gr.Group():