Spaces:
Running
Running
Update steganography.py
Browse files- steganography.py +34 -71
steganography.py
CHANGED
@@ -5,23 +5,24 @@ import librosa
|
|
5 |
import librosa.display
|
6 |
import gradio as gr
|
7 |
import soundfile as sf
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
# Function for creating a spectrogram image with text
|
11 |
def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80, margin=10, letter_spacing=5):
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
font = ImageFont.load_default()
|
17 |
|
18 |
image = Image.new('L', (base_width, height), 'black')
|
19 |
draw = ImageDraw.Draw(image)
|
20 |
-
|
21 |
-
for char in text
|
22 |
-
|
23 |
-
text_width += text_bbox[2] - text_bbox[0] + letter_spacing
|
24 |
-
text_width -= letter_spacing
|
25 |
|
26 |
if text_width + margin * 2 > base_width:
|
27 |
width = text_width + margin * 2
|
@@ -30,20 +31,21 @@ def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80
|
|
30 |
|
31 |
image = Image.new('L', (width, height), 'black')
|
32 |
draw = ImageDraw.Draw(image)
|
33 |
-
|
34 |
text_x = (width - text_width) // 2
|
35 |
-
text_y = (height -
|
|
|
36 |
for char in text:
|
37 |
draw.text((text_x, text_y), char, font=font, fill='white')
|
38 |
char_bbox = draw.textbbox((0, 0), char, font=font)
|
39 |
text_x += char_bbox[2] - char_bbox[0] + letter_spacing
|
40 |
-
|
41 |
image = np.array(image)
|
42 |
image = np.where(image > 0, 255, image)
|
43 |
return image
|
44 |
|
45 |
# Converting an image to audio
|
46 |
-
def spectrogram_image_to_audio(image, sr=
|
47 |
flipped_image = np.flipud(image)
|
48 |
S = flipped_image.astype(np.float32) / 255.0 * 100.0
|
49 |
y = librosa.griffinlim(S)
|
@@ -54,19 +56,21 @@ def create_audio_with_spectrogram(text, base_width, height, max_font_size, margi
|
|
54 |
spec_image = text_to_spectrogram_image(text, base_width, height, max_font_size, margin, letter_spacing)
|
55 |
y = spectrogram_image_to_audio(spec_image)
|
56 |
audio_path = 'output.wav'
|
57 |
-
sf.write(audio_path, y,
|
58 |
image_path = 'spectrogram.png'
|
59 |
plt.imsave(image_path, spec_image, cmap='gray')
|
60 |
return audio_path, image_path
|
61 |
|
62 |
# Function for displaying the spectrogram of an audio file
|
63 |
def display_audio_spectrogram(audio_path):
|
64 |
-
y, sr = librosa.load(audio_path)
|
65 |
S = librosa.feature.melspectrogram(y=y, sr=sr)
|
66 |
S_dB = librosa.power_to_db(S, ref=np.max)
|
67 |
|
68 |
plt.figure(figsize=(10, 4))
|
69 |
-
librosa.display.specshow(S_dB)
|
|
|
|
|
70 |
plt.tight_layout()
|
71 |
|
72 |
spectrogram_path = 'uploaded_spectrogram.png'
|
@@ -75,7 +79,7 @@ def display_audio_spectrogram(audio_path):
|
|
75 |
return spectrogram_path
|
76 |
|
77 |
# Converting a downloaded image to an audio spectrogram
|
78 |
-
def image_to_spectrogram_audio(image_path, sr=
|
79 |
image = Image.open(image_path).convert('L')
|
80 |
image = np.array(image)
|
81 |
y = spectrogram_image_to_audio(image, sr)
|
@@ -84,36 +88,8 @@ def image_to_spectrogram_audio(image_path, sr=22050):
|
|
84 |
return img2audio_path
|
85 |
|
86 |
# Gradio interface
|
87 |
-
with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg")) as
|
88 |
-
|
89 |
-
with gr.Group():
|
90 |
-
with gr.Row(variant='panel'):
|
91 |
-
with gr.Column():
|
92 |
-
gr.HTML("<center><h2><a href='https://t.me/pol1trees'>Telegram Channel</a></h2></center>")
|
93 |
-
with gr.Column():
|
94 |
-
gr.HTML("<center><h2><a href='https://t.me/+GMTP7hZqY0E4OGRi'>Telegram Chat</a></h2></center>")
|
95 |
-
with gr.Column():
|
96 |
-
gr.HTML("<center><h2><a href='https://www.youtube.com/channel/UCHb3fZEVxUisnqLqCrEM8ZA'>YouTube</a></h2></center>")
|
97 |
-
with gr.Column():
|
98 |
-
gr.HTML("<center><h2><a href='https://github.com/Bebra777228/Audio-Steganography'>GitHub</a></h2></center>")
|
99 |
-
|
100 |
-
with gr.Tab("INFO"):
|
101 |
-
with gr.Column(variant='panel'):
|
102 |
-
with gr.Group():
|
103 |
-
gr.HTML("<center><h2>Ha-Ha-Ha, I'm laughing at you.</h2></center>")
|
104 |
-
gr.HTML("<center><h2>People, before using this interface, read about what Steganography is.</h2></center>")
|
105 |
-
gr.HTML("<center><h2><b>Steganography is not Stable Diffusion and not Suno, why are you trying to generate images and music here?</b></h2></center>")
|
106 |
-
with gr.Column(variant='panel'):
|
107 |
-
with gr.Group():
|
108 |
-
gr.HTML("<center><h1><b>Steganography is a method of hiding information within other information or a physical object in such a way that it cannot be detected. Using steganography, you can hide almost any digital content, including texts, images, audio, and video files.</b></h2></center>")
|
109 |
-
gr.HTML("<center><h1><b>In this interface, steganography is used to hide text or an image in the spectrogram of an sound.</b></h1></center>")
|
110 |
-
with gr.Column(variant='panel'):
|
111 |
-
with gr.Group():
|
112 |
-
gr.Image(value="https://huggingface.co/spaces/Politrees/Audio-Steganography/resolve/main/content/1.jpg", show_label=False, show_share_button=False, show_download_button=False)
|
113 |
-
gr.Image(value="https://huggingface.co/spaces/Politrees/Audio-Steganography/resolve/main/content/2.jpg", show_label=False, show_share_button=False, show_download_button=False)
|
114 |
-
|
115 |
with gr.Tab("Text to Spectrogram"):
|
116 |
-
gr.HTML("<center><h2>Oh my god people, learn to read. Go to the “INFO” tab, it says what this interface is and what it is for, don't be idiots.</h2></center>")
|
117 |
with gr.Group():
|
118 |
text = gr.Textbox(lines=2, placeholder="Enter your text:", label="Text")
|
119 |
with gr.Row(variant='panel'):
|
@@ -123,7 +99,7 @@ with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="gr
|
|
123 |
margin = gr.Slider(minimum=0, maximum=50, step=1, value=10, label="Indent")
|
124 |
letter_spacing = gr.Slider(minimum=0, maximum=50, step=1, value=5, label="Letter spacing")
|
125 |
generate_button = gr.Button("Generate")
|
126 |
-
|
127 |
with gr.Column(variant='panel'):
|
128 |
with gr.Group():
|
129 |
output_audio = gr.Audio(type="filepath", label="Generated audio")
|
@@ -132,48 +108,35 @@ with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="gr
|
|
132 |
def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
|
133 |
print("\n", text)
|
134 |
return create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
|
135 |
-
|
136 |
-
generate_button.click(
|
137 |
-
gradio_interface_fn,
|
138 |
-
inputs=[text, base_width, height, max_font_size, margin, letter_spacing],
|
139 |
-
outputs=[output_audio, output_image]
|
140 |
-
)
|
141 |
|
142 |
with gr.Tab("Image to Spectrogram"):
|
143 |
-
gr.HTML("<center><h2>Oh my god people, learn to read. Go to the “INFO” tab, it says what this interface is and what it is for, don't be idiots.</h2></center>")
|
144 |
with gr.Group():
|
145 |
with gr.Row(variant='panel'):
|
146 |
upload_image = gr.Image(type="filepath", label="Upload image")
|
147 |
convert_button = gr.Button("Convert to audio")
|
148 |
-
|
149 |
with gr.Column(variant='panel'):
|
150 |
output_audio_from_image = gr.Audio(type="filepath", label="Generated audio")
|
151 |
|
152 |
def gradio_image_to_audio_fn(upload_image):
|
153 |
return image_to_spectrogram_audio(upload_image)
|
154 |
-
|
155 |
-
convert_button.click(
|
156 |
-
gradio_image_to_audio_fn,
|
157 |
-
inputs=[upload_image],
|
158 |
-
outputs=[output_audio_from_image]
|
159 |
-
)
|
160 |
|
161 |
with gr.Tab("Audio Spectrogram"):
|
162 |
with gr.Group():
|
163 |
with gr.Row(variant='panel'):
|
164 |
upload_audio = gr.Audio(type="filepath", label="Upload audio", scale=3)
|
165 |
decode_button = gr.Button("Show spectrogram", scale=2)
|
166 |
-
|
167 |
with gr.Column(variant='panel'):
|
168 |
decoded_image = gr.Image(type="filepath", label="Audio Spectrogram")
|
169 |
|
170 |
def gradio_decode_fn(upload_audio):
|
171 |
return display_audio_spectrogram(upload_audio)
|
172 |
-
|
173 |
-
decode_button.click(
|
174 |
-
|
175 |
-
|
176 |
-
outputs=[decoded_image]
|
177 |
-
)
|
178 |
-
|
179 |
-
iface.launch(share=True)
|
|
|
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')
|
22 |
draw = ImageDraw.Draw(image)
|
23 |
+
|
24 |
+
text_width = sum(draw.textbbox((0, 0), char, font=font)[2] - draw.textbbox((0, 0), char, font=font)[0] + letter_spacing for char in text) - letter_spacing
|
25 |
+
text_height = draw.textbbox((0, 0), text[0], font=font)[3] - draw.textbbox((0, 0), text[0], font=font)[1]
|
|
|
|
|
26 |
|
27 |
if text_width + margin * 2 > base_width:
|
28 |
width = text_width + margin * 2
|
|
|
31 |
|
32 |
image = Image.new('L', (width, height), 'black')
|
33 |
draw = ImageDraw.Draw(image)
|
34 |
+
|
35 |
text_x = (width - text_width) // 2
|
36 |
+
text_y = (height - text_height) // 2
|
37 |
+
|
38 |
for char in text:
|
39 |
draw.text((text_x, text_y), char, font=font, fill='white')
|
40 |
char_bbox = draw.textbbox((0, 0), char, font=font)
|
41 |
text_x += char_bbox[2] - char_bbox[0] + letter_spacing
|
42 |
+
|
43 |
image = np.array(image)
|
44 |
image = np.where(image > 0, 255, image)
|
45 |
return image
|
46 |
|
47 |
# Converting an image to audio
|
48 |
+
def spectrogram_image_to_audio(image, sr=DEFAULT_SAMPLE_RATE):
|
49 |
flipped_image = np.flipud(image)
|
50 |
S = flipped_image.astype(np.float32) / 255.0 * 100.0
|
51 |
y = librosa.griffinlim(S)
|
|
|
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 |
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):
|
66 |
+
y, sr = librosa.load(audio_path, sr=None)
|
67 |
S = librosa.feature.melspectrogram(y=y, sr=sr)
|
68 |
S_dB = librosa.power_to_db(S, ref=np.max)
|
69 |
|
70 |
plt.figure(figsize=(10, 4))
|
71 |
+
librosa.display.specshow(S_dB, sr=sr, x_axis='time', y_axis='mel')
|
72 |
+
plt.colorbar(format='%+2.0f dB')
|
73 |
+
plt.title('Mel-frequency spectrogram')
|
74 |
plt.tight_layout()
|
75 |
|
76 |
spectrogram_path = 'uploaded_spectrogram.png'
|
|
|
79 |
return spectrogram_path
|
80 |
|
81 |
# Converting a downloaded image to an audio spectrogram
|
82 |
+
def image_to_spectrogram_audio(image_path, sr=DEFAULT_SAMPLE_RATE):
|
83 |
image = Image.open(image_path).convert('L')
|
84 |
image = np.array(image)
|
85 |
y = spectrogram_image_to_audio(image, sr)
|
|
|
88 |
return img2audio_path
|
89 |
|
90 |
# Gradio interface
|
91 |
+
with gr.Blocks(title='Audio Steganography', theme=gr.themes.Soft(primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg")) as txt2spec:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
with gr.Tab("Text to Spectrogram"):
|
|
|
93 |
with gr.Group():
|
94 |
text = gr.Textbox(lines=2, placeholder="Enter your text:", label="Text")
|
95 |
with gr.Row(variant='panel'):
|
|
|
99 |
margin = gr.Slider(minimum=0, maximum=50, step=1, value=10, label="Indent")
|
100 |
letter_spacing = gr.Slider(minimum=0, maximum=50, step=1, value=5, label="Letter spacing")
|
101 |
generate_button = gr.Button("Generate")
|
102 |
+
|
103 |
with gr.Column(variant='panel'):
|
104 |
with gr.Group():
|
105 |
output_audio = gr.Audio(type="filepath", label="Generated audio")
|
|
|
108 |
def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
|
109 |
print("\n", text)
|
110 |
return create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
|
111 |
+
|
112 |
+
generate_button.click(gradio_interface_fn, inputs=[text, base_width, height, max_font_size, margin, letter_spacing], outputs=[output_audio, output_image])
|
|
|
|
|
|
|
|
|
113 |
|
114 |
with gr.Tab("Image to Spectrogram"):
|
|
|
115 |
with gr.Group():
|
116 |
with gr.Row(variant='panel'):
|
117 |
upload_image = gr.Image(type="filepath", label="Upload image")
|
118 |
convert_button = gr.Button("Convert to audio")
|
119 |
+
|
120 |
with gr.Column(variant='panel'):
|
121 |
output_audio_from_image = gr.Audio(type="filepath", label="Generated audio")
|
122 |
|
123 |
def gradio_image_to_audio_fn(upload_image):
|
124 |
return image_to_spectrogram_audio(upload_image)
|
125 |
+
|
126 |
+
convert_button.click(gradio_image_to_audio_fn, inputs=[upload_image], outputs=[output_audio_from_image])
|
|
|
|
|
|
|
|
|
127 |
|
128 |
with gr.Tab("Audio Spectrogram"):
|
129 |
with gr.Group():
|
130 |
with gr.Row(variant='panel'):
|
131 |
upload_audio = gr.Audio(type="filepath", label="Upload audio", scale=3)
|
132 |
decode_button = gr.Button("Show spectrogram", scale=2)
|
133 |
+
|
134 |
with gr.Column(variant='panel'):
|
135 |
decoded_image = gr.Image(type="filepath", label="Audio Spectrogram")
|
136 |
|
137 |
def gradio_decode_fn(upload_audio):
|
138 |
return display_audio_spectrogram(upload_audio)
|
139 |
+
|
140 |
+
decode_button.click(gradio_decode_fn, inputs=[upload_audio], outputs=[decoded_image])
|
141 |
+
|
142 |
+
txt2spec.launch(share=True)
|
|
|
|
|
|
|
|