Politrees commited on
Commit
76f1d4e
·
verified ·
1 Parent(s): 0151e44

Update steganography.py

Browse files
Files changed (1) hide show
  1. steganography.py +17 -76
steganography.py CHANGED
@@ -1,8 +1,5 @@
1
  import logging
2
  import tempfile
3
- import os
4
- from io import BytesIO
5
-
6
  import gradio as gr
7
  import librosa
8
  import librosa.display
@@ -11,18 +8,12 @@ import numpy as np
11
  import soundfile as sf
12
  from PIL import Image, ImageDraw, ImageFont
13
 
14
- # Constants
15
  DEFAULT_FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
16
  DEFAULT_SAMPLE_RATE = 22050
17
 
18
- # Setup logging
19
  logging.basicConfig(level=logging.INFO)
20
 
21
-
22
- # Function for creating a spectrogram image with text
23
- def text_to_spectrogram_image(
24
- text, base_width=512, height=256, max_font_size=80, margin=10, letter_spacing=5
25
- ):
26
  try:
27
  font = ImageFont.truetype(DEFAULT_FONT_PATH, max_font_size)
28
  except IOError:
@@ -44,7 +35,6 @@ def text_to_spectrogram_image(
44
  - draw.textbbox((0, 0), text[0], font=font)[1]
45
  )
46
 
47
- # Adjust width and height based on text size
48
  width = max(base_width, text_width + margin * 2)
49
  height = max(height, text_height + margin * 2)
50
 
@@ -63,29 +53,20 @@ def text_to_spectrogram_image(
63
  image = np.where(image > 0, 255, image)
64
  return image
65
 
66
-
67
- # Converting an image to audio
68
  def spectrogram_image_to_audio(image, sr=DEFAULT_SAMPLE_RATE):
69
  flipped_image = np.flipud(image)
70
  S = flipped_image.astype(np.float32) / 255.0 * 100.0
71
  y = librosa.griffinlim(S)
72
  return y
73
 
74
-
75
- # Function for creating an audio file and spectrogram from text
76
- def create_audio_with_spectrogram(
77
- text, base_width, height, max_font_size, margin, letter_spacing
78
- ):
79
- spec_image = text_to_spectrogram_image(
80
- text, base_width, height, max_font_size, margin, letter_spacing
81
- )
82
  y = spectrogram_image_to_audio(spec_image)
83
 
84
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
85
  audio_path = temp_audio.name
86
  sf.write(audio_path, y, DEFAULT_SAMPLE_RATE)
87
 
88
- # Create spectrogram from audio
89
  S = librosa.feature.melspectrogram(y=y, sr=DEFAULT_SAMPLE_RATE)
90
  S_dB = librosa.power_to_db(S, ref=np.max)
91
  plt.figure(figsize=(10, 4))
@@ -95,15 +76,11 @@ def create_audio_with_spectrogram(
95
 
96
  with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_spectrogram:
97
  spectrogram_path = temp_spectrogram.name
98
- plt.savefig(
99
- spectrogram_path, bbox_inches="tight", pad_inches=0, transparent=True
100
- )
101
  plt.close()
102
 
103
  return audio_path, spectrogram_path
104
 
105
-
106
- # Function for displaying the spectrogram of an audio file
107
  def display_audio_spectrogram(audio_path):
108
  y, sr = librosa.load(audio_path, sr=None)
109
  S = librosa.feature.melspectrogram(y=y, sr=sr)
@@ -114,14 +91,12 @@ def display_audio_spectrogram(audio_path):
114
  plt.axis("off")
115
  plt.tight_layout(pad=0)
116
 
117
- buf = BytesIO()
118
- plt.savefig(buf, format='png', bbox_inches='tight', pad_inches=0, transparent=True)
 
119
  plt.close()
120
- buf.seek(0)
121
- return buf
122
-
123
 
124
- # Converting a downloaded image to an audio spectrogram
125
  def image_to_spectrogram_audio(image_path, sr=DEFAULT_SAMPLE_RATE):
126
  image = Image.open(image_path).convert("L")
127
  image = np.array(image)
@@ -132,46 +107,26 @@ def image_to_spectrogram_audio(image_path, sr=DEFAULT_SAMPLE_RATE):
132
  sf.write(img2audio_path, y, sr)
133
  return img2audio_path
134
 
135
-
136
- # Gradio interface
137
  def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
138
- logging.info(f"Generating audio and spectrogram for text:\n{text}\n")
139
  audio_path, spectrogram_path = create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
140
  return audio_path, spectrogram_path
141
 
142
-
143
  def gradio_image_to_audio_fn(upload_image):
144
- logging.info(f"Converting image to audio:\n{upload_image}\n")
145
  return image_to_spectrogram_audio(upload_image)
146
 
147
-
148
  def gradio_decode_fn(upload_audio):
149
- logging.info(f"Generating spectrogram for audio:\n{upload_audio}\n")
150
  return display_audio_spectrogram(upload_audio)
151
 
152
-
153
- with gr.Blocks(
154
- title="Audio Steganography",
155
- css="footer{display:none !important}",
156
- theme=gr.themes.Soft(
157
- primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg"
158
- ),
159
- ) as txt2spec:
160
  with gr.Tab("Text to Spectrogram"):
161
  with gr.Group():
162
  text = gr.Textbox(lines=2, placeholder="Enter your text:", label="Text", info="Enter the text you want to convert to audio.")
163
  with gr.Row(variant="panel"):
164
  base_width = gr.Slider(value=512, label="Image Width", visible=False)
165
  height = gr.Slider(value=256, label="Image Height", visible=False)
166
- max_font_size = gr.Slider(
167
- minimum=10, maximum=130, step=5, value=80, label="Font size"
168
- )
169
- margin = gr.Slider(
170
- minimum=0, maximum=50, step=1, value=10, label="Indent"
171
- )
172
- letter_spacing = gr.Slider(
173
- minimum=0, maximum=50, step=1, value=5, label="Letter spacing"
174
- )
175
  generate_button = gr.Button("Generate", variant="primary", size="lg")
176
 
177
  with gr.Column(variant="panel"):
@@ -179,42 +134,28 @@ with gr.Blocks(
179
  output_audio = gr.Audio(type="filepath", label="Generated audio")
180
  output_spectrogram = gr.Image(type="filepath", label="Spectrogram")
181
 
182
- generate_button.click(
183
- gradio_interface_fn,
184
- inputs=[text, base_width, height, max_font_size, margin, letter_spacing],
185
- outputs=[output_audio, output_spectrogram],
186
- )
187
 
188
  with gr.Tab("Image to Spectrogram"):
189
  with gr.Group():
190
  with gr.Column():
191
  upload_image = gr.Image(type="filepath", label="Upload image")
192
- convert_button = gr.Button(
193
- "Convert to audio", variant="primary", size="lg"
194
- )
195
 
196
  with gr.Column(variant="panel"):
197
  output_audio_from_image = gr.Audio(type="filepath", label="Generated audio")
198
 
199
- convert_button.click(
200
- gradio_image_to_audio_fn,
201
- inputs=[upload_image],
202
- outputs=[output_audio_from_image],
203
- )
204
 
205
  with gr.Tab("Audio Spectrogram"):
206
  with gr.Group():
207
  with gr.Column():
208
  upload_audio = gr.Audio(type="filepath", label="Upload audio", scale=3)
209
- decode_button = gr.Button(
210
- "Show spectrogram", variant="primary", size="lg"
211
- )
212
 
213
  with gr.Column(variant="panel"):
214
  decoded_image = gr.Image(type="filepath", label="Audio Spectrogram")
215
 
216
- decode_button.click(
217
- gradio_decode_fn, inputs=[upload_audio], outputs=[decoded_image]
218
- )
219
 
220
  txt2spec.launch(share=True)
 
1
  import logging
2
  import tempfile
 
 
 
3
  import gradio as gr
4
  import librosa
5
  import librosa.display
 
8
  import soundfile as sf
9
  from PIL import Image, ImageDraw, ImageFont
10
 
 
11
  DEFAULT_FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
12
  DEFAULT_SAMPLE_RATE = 22050
13
 
 
14
  logging.basicConfig(level=logging.INFO)
15
 
16
+ def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80, margin=10, letter_spacing=5):
 
 
 
 
17
  try:
18
  font = ImageFont.truetype(DEFAULT_FONT_PATH, max_font_size)
19
  except IOError:
 
35
  - draw.textbbox((0, 0), text[0], font=font)[1]
36
  )
37
 
 
38
  width = max(base_width, text_width + margin * 2)
39
  height = max(height, text_height + margin * 2)
40
 
 
53
  image = np.where(image > 0, 255, image)
54
  return image
55
 
 
 
56
  def spectrogram_image_to_audio(image, sr=DEFAULT_SAMPLE_RATE):
57
  flipped_image = np.flipud(image)
58
  S = flipped_image.astype(np.float32) / 255.0 * 100.0
59
  y = librosa.griffinlim(S)
60
  return y
61
 
62
+ def create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing):
63
+ spec_image = text_to_spectrogram_image(text, base_width, height, max_font_size, margin, letter_spacing)
 
 
 
 
 
 
64
  y = spectrogram_image_to_audio(spec_image)
65
 
66
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio:
67
  audio_path = temp_audio.name
68
  sf.write(audio_path, y, DEFAULT_SAMPLE_RATE)
69
 
 
70
  S = librosa.feature.melspectrogram(y=y, sr=DEFAULT_SAMPLE_RATE)
71
  S_dB = librosa.power_to_db(S, ref=np.max)
72
  plt.figure(figsize=(10, 4))
 
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
83
 
 
 
84
  def display_audio_spectrogram(audio_path):
85
  y, sr = librosa.load(audio_path, sr=None)
86
  S = librosa.feature.melspectrogram(y=y, sr=sr)
 
91
  plt.axis("off")
92
  plt.tight_layout(pad=0)
93
 
94
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_spectrogram:
95
+ spectrogram_path = temp_spectrogram.name
96
+ plt.savefig(spectrogram_path, bbox_inches="tight", pad_inches=0, transparent=True)
97
  plt.close()
98
+ return spectrogram_path
 
 
99
 
 
100
  def image_to_spectrogram_audio(image_path, sr=DEFAULT_SAMPLE_RATE):
101
  image = Image.open(image_path).convert("L")
102
  image = np.array(image)
 
107
  sf.write(img2audio_path, y, sr)
108
  return img2audio_path
109
 
 
 
110
  def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
 
111
  audio_path, spectrogram_path = create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
112
  return audio_path, spectrogram_path
113
 
 
114
  def gradio_image_to_audio_fn(upload_image):
 
115
  return image_to_spectrogram_audio(upload_image)
116
 
 
117
  def gradio_decode_fn(upload_audio):
 
118
  return display_audio_spectrogram(upload_audio)
119
 
120
+ with gr.Blocks(title="Audio Steganography", css="footer{display:none !important}", theme=gr.themes.Soft(primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg")) as txt2spec:
 
 
 
 
 
 
 
121
  with gr.Tab("Text to Spectrogram"):
122
  with gr.Group():
123
  text = gr.Textbox(lines=2, placeholder="Enter your text:", label="Text", info="Enter the text you want to convert to audio.")
124
  with gr.Row(variant="panel"):
125
  base_width = gr.Slider(value=512, label="Image Width", visible=False)
126
  height = gr.Slider(value=256, label="Image Height", visible=False)
127
+ max_font_size = gr.Slider(minimum=10, maximum=130, step=5, value=80, label="Font size")
128
+ margin = gr.Slider(minimum=0, maximum=50, step=1, value=10, label="Indent")
129
+ letter_spacing = gr.Slider(minimum=0, maximum=50, step=1, value=5, label="Letter spacing")
 
 
 
 
 
 
130
  generate_button = gr.Button("Generate", variant="primary", size="lg")
131
 
132
  with gr.Column(variant="panel"):
 
134
  output_audio = gr.Audio(type="filepath", label="Generated audio")
135
  output_spectrogram = gr.Image(type="filepath", label="Spectrogram")
136
 
137
+ generate_button.click(gradio_interface_fn, inputs=[text, base_width, height, max_font_size, margin, letter_spacing], outputs=[output_audio, output_spectrogram])
 
 
 
 
138
 
139
  with gr.Tab("Image to Spectrogram"):
140
  with gr.Group():
141
  with gr.Column():
142
  upload_image = gr.Image(type="filepath", label="Upload image")
143
+ convert_button = gr.Button("Convert to audio", variant="primary", size="lg")
 
 
144
 
145
  with gr.Column(variant="panel"):
146
  output_audio_from_image = gr.Audio(type="filepath", label="Generated audio")
147
 
148
+ convert_button.click(gradio_image_to_audio_fn, inputs=[upload_image], outputs=[output_audio_from_image])
 
 
 
 
149
 
150
  with gr.Tab("Audio Spectrogram"):
151
  with gr.Group():
152
  with gr.Column():
153
  upload_audio = gr.Audio(type="filepath", label="Upload audio", scale=3)
154
+ decode_button = gr.Button("Show spectrogram", variant="primary", size="lg")
 
 
155
 
156
  with gr.Column(variant="panel"):
157
  decoded_image = gr.Image(type="filepath", label="Audio Spectrogram")
158
 
159
+ decode_button.click(gradio_decode_fn, inputs=[upload_audio], outputs=[decoded_image])
 
 
160
 
161
  txt2spec.launch(share=True)