Politrees commited on
Commit
f3b050a
·
verified ·
1 Parent(s): 1d8b7f1

Update steganography.py

Browse files
Files changed (1) hide show
  1. steganography.py +35 -20
steganography.py CHANGED
@@ -5,17 +5,23 @@ import librosa
5
  import librosa.display
6
  import gradio as gr
7
  import soundfile as sf
 
 
 
8
 
9
  # Constants
10
  DEFAULT_FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
11
  DEFAULT_SAMPLE_RATE = 22050
12
 
 
 
 
13
  # Function for creating a spectrogram image with text
14
  def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80, margin=10, letter_spacing=5):
15
  try:
16
  font = ImageFont.truetype(DEFAULT_FONT_PATH, max_font_size)
17
  except IOError:
18
- print(f"Font not found at {DEFAULT_FONT_PATH}. Using default font.")
19
  font = ImageFont.load_default()
20
 
21
  image = Image.new('L', (base_width, height), 'black')
@@ -55,8 +61,10 @@ def spectrogram_image_to_audio(image, sr=DEFAULT_SAMPLE_RATE):
55
  def create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing):
56
  spec_image = text_to_spectrogram_image(text, base_width, height, max_font_size, margin, letter_spacing)
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)
@@ -65,8 +73,10 @@ def create_audio_with_spectrogram(text, base_width, height, max_font_size, margi
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
@@ -82,8 +92,9 @@ def display_audio_spectrogram(audio_path):
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
 
@@ -92,11 +103,26 @@ def image_to_spectrogram_audio(image_path, sr=DEFAULT_SAMPLE_RATE):
92
  image = Image.open(image_path).convert('L')
93
  image = np.array(image)
94
  y = spectrogram_image_to_audio(image, sr)
95
- img2audio_path = 'image_to_audio_output.wav'
96
- sf.write(img2audio_path, y, sr)
 
 
97
  return img2audio_path
98
 
99
  # Gradio interface
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg")) as txt2spec:
101
  with gr.Tab("Text to Spectrogram"):
102
  with gr.Group():
@@ -114,11 +140,6 @@ with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="gr
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
- audio_path, spectrogram_path = create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
120
- return audio_path, spectrogram_path
121
-
122
  generate_button.click(gradio_interface_fn, inputs=[text, base_width, height, max_font_size, margin, letter_spacing], outputs=[output_audio, output_spectrogram])
123
 
124
  with gr.Tab("Image to Spectrogram"):
@@ -130,9 +151,6 @@ with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="gr
130
  with gr.Column(variant='panel'):
131
  output_audio_from_image = gr.Audio(type="filepath", label="Generated audio")
132
 
133
- def gradio_image_to_audio_fn(upload_image):
134
- return image_to_spectrogram_audio(upload_image)
135
-
136
  convert_button.click(gradio_image_to_audio_fn, inputs=[upload_image], outputs=[output_audio_from_image])
137
 
138
  with gr.Tab("Audio Spectrogram"):
@@ -144,9 +162,6 @@ with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="gr
144
  with gr.Column(variant='panel'):
145
  decoded_image = gr.Image(type="filepath", label="Audio Spectrogram")
146
 
147
- def gradio_decode_fn(upload_audio):
148
- return display_audio_spectrogram(upload_audio)
149
-
150
  decode_button.click(gradio_decode_fn, inputs=[upload_audio], outputs=[decoded_image])
151
 
152
  txt2spec.launch(share=True)
 
5
  import librosa.display
6
  import gradio as gr
7
  import soundfile as sf
8
+ import os
9
+ import logging
10
+ import tempfile
11
 
12
  # Constants
13
  DEFAULT_FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
14
  DEFAULT_SAMPLE_RATE = 22050
15
 
16
+ # Setup logging
17
+ logging.basicConfig(level=logging.INFO)
18
+
19
  # Function for creating a spectrogram image with text
20
  def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80, margin=10, letter_spacing=5):
21
  try:
22
  font = ImageFont.truetype(DEFAULT_FONT_PATH, max_font_size)
23
  except IOError:
24
+ logging.warning(f"Font not found at {DEFAULT_FONT_PATH}. Using default font.")
25
  font = ImageFont.load_default()
26
 
27
  image = Image.new('L', (base_width, height), 'black')
 
61
  def create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing):
62
  spec_image = text_to_spectrogram_image(text, base_width, height, max_font_size, margin, letter_spacing)
63
  y = spectrogram_image_to_audio(spec_image)
64
+
65
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_audio:
66
+ audio_path = temp_audio.name
67
+ sf.write(audio_path, y, DEFAULT_SAMPLE_RATE)
68
 
69
  # Create spectrogram from audio
70
  S = librosa.feature.melspectrogram(y=y, sr=DEFAULT_SAMPLE_RATE)
 
73
  librosa.display.specshow(S_dB, sr=DEFAULT_SAMPLE_RATE, x_axis='time', y_axis='mel')
74
  plt.axis('off')
75
  plt.tight_layout(pad=0)
76
+
77
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_spectrogram:
78
+ spectrogram_path = temp_spectrogram.name
79
+ plt.savefig(spectrogram_path, bbox_inches='tight', pad_inches=0, transparent=True)
80
  plt.close()
81
 
82
  return audio_path, spectrogram_path
 
92
  plt.axis('off')
93
  plt.tight_layout(pad=0)
94
 
95
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_spectrogram:
96
+ spectrogram_path = temp_spectrogram.name
97
+ plt.savefig(spectrogram_path, bbox_inches='tight', pad_inches=0, transparent=True)
98
  plt.close()
99
  return spectrogram_path
100
 
 
103
  image = Image.open(image_path).convert('L')
104
  image = np.array(image)
105
  y = spectrogram_image_to_audio(image, sr)
106
+
107
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.wav') as temp_audio:
108
+ img2audio_path = temp_audio.name
109
+ sf.write(img2audio_path, y, sr)
110
  return img2audio_path
111
 
112
  # Gradio interface
113
+ def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
114
+ logging.info(f"Generating audio and spectrogram for text: {text}")
115
+ audio_path, spectrogram_path = create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
116
+ return audio_path, spectrogram_path
117
+
118
+ def gradio_image_to_audio_fn(upload_image):
119
+ logging.info(f"Converting image to audio: {upload_image}")
120
+ return image_to_spectrogram_audio(upload_image)
121
+
122
+ def gradio_decode_fn(upload_audio):
123
+ logging.info(f"Generating spectrogram for audio: {upload_audio}")
124
+ return display_audio_spectrogram(upload_audio)
125
+
126
  with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg")) as txt2spec:
127
  with gr.Tab("Text to Spectrogram"):
128
  with gr.Group():
 
140
  output_audio = gr.Audio(type="filepath", label="Generated audio")
141
  output_spectrogram = gr.Image(type="filepath", label="Spectrogram")
142
 
 
 
 
 
 
143
  generate_button.click(gradio_interface_fn, inputs=[text, base_width, height, max_font_size, margin, letter_spacing], outputs=[output_audio, output_spectrogram])
144
 
145
  with gr.Tab("Image to Spectrogram"):
 
151
  with gr.Column(variant='panel'):
152
  output_audio_from_image = gr.Audio(type="filepath", label="Generated audio")
153
 
 
 
 
154
  convert_button.click(gradio_image_to_audio_fn, inputs=[upload_image], outputs=[output_audio_from_image])
155
 
156
  with gr.Tab("Audio Spectrogram"):
 
162
  with gr.Column(variant='panel'):
163
  decoded_image = gr.Image(type="filepath", label="Audio Spectrogram")
164
 
 
 
 
165
  decode_button.click(gradio_decode_fn, inputs=[upload_audio], outputs=[decoded_image])
166
 
167
  txt2spec.launch(share=True)