Politrees commited on
Commit
4b5ced5
·
verified ·
1 Parent(s): 68f607f

Update steganography.py

Browse files
Files changed (1) hide show
  1. steganography.py +27 -27
steganography.py CHANGED
@@ -7,7 +7,7 @@ import gradio as gr
7
  import soundfile as sf
8
  import os
9
 
10
- # Функция для создания изображения спектрограммы с текстом
11
  def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80, margin=10, letter_spacing=5):
12
  font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
13
  if os.path.exists(font_path):
@@ -42,14 +42,14 @@ def text_to_spectrogram_image(text, base_width=512, height=256, max_font_size=80
42
  image = np.where(image > 0, 255, image)
43
  return image
44
 
45
- # Преобразовываем изображение в аудиосигнал
46
  def spectrogram_image_to_audio(image, sr=22050):
47
  flipped_image = np.flipud(image)
48
  S = flipped_image.astype(np.float32) / 255.0 * 100.0
49
  y = librosa.griffinlim(S)
50
  return y
51
 
52
- # Функция для создания аудиофайла и спектрограммы из текста
53
  def create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing):
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)
@@ -59,7 +59,7 @@ def create_audio_with_spectrogram(text, base_width, height, max_font_size, margi
59
  plt.imsave(image_path, spec_image, cmap='gray')
60
  return audio_path, image_path
61
 
62
- # Функция для отображения спектрограммы аудиофайла
63
  def display_audio_spectrogram(audio_path):
64
  y, sr = librosa.load(audio_path)
65
  S = librosa.feature.melspectrogram(y=y, sr=sr)
@@ -74,7 +74,7 @@ def display_audio_spectrogram(audio_path):
74
  plt.close()
75
  return spectrogram_path
76
 
77
- # Преобразование загруженного изображения в спектрограмму звука
78
  def image_to_spectrogram_audio(image_path, sr=22050):
79
  image = Image.open(image_path).convert('L')
80
  image = np.array(image)
@@ -83,34 +83,34 @@ def image_to_spectrogram_audio(image_path, sr=22050):
83
  sf.write(audio_path, y, sr)
84
  return audio_path
85
 
86
- # Интерфейс Gradio
87
- with gr.Blocks(title='Аудио-Стеганография', theme=gr.themes.Soft(primary_hue="green", secondary_hue="green", spacing_size="sm", radius_size="lg")) as iface:
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 Канал</a></h2></center>")
93
  with gr.Column():
94
- gr.HTML("<center><h2><a href='https://t.me/+GMTP7hZqY0E4OGRi'>Telegram Чат</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("Текст в Спектрограмму"):
101
  with gr.Group():
102
- text = gr.Textbox(lines=2, placeholder="Введите свой текст:", label="Текст")
103
  with gr.Row(variant='panel'):
104
- base_width = gr.Slider(value=512, label="Ширина изображения", visible=False)
105
- height = gr.Slider(value=256, label="Высота изображения", visible=False)
106
- max_font_size = gr.Slider(minimum=10, maximum=130, step=5, value=80, label="Размер шрифта")
107
- margin = gr.Slider(minimum=0, maximum=50, step=1, value=10, label="Отступ")
108
- letter_spacing = gr.Slider(minimum=0, maximum=50, step=1, value=5, label="Расстояние между буквами")
109
- generate_button = gr.Button("Сгенерировать")
110
 
111
  with gr.Column(variant='panel'):
112
- output_audio = gr.Audio(type="filepath", label="Сгенерированный звук")
113
- output_image = gr.Image(type="filepath", label="Спектрограмма")
114
 
115
  def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
116
  return create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
@@ -121,14 +121,14 @@ with gr.Blocks(title='Аудио-Стеганография', theme=gr.themes.So
121
  outputs=[output_audio, output_image]
122
  )
123
 
124
- with gr.Tab("Изображение в Спектрограмму"):
125
  with gr.Group():
126
  with gr.Row(variant='panel'):
127
- upload_image = gr.Image(type="filepath", label="Загрузите изображение")
128
- convert_button = gr.Button("Конвертировать в аудио")
129
 
130
  with gr.Column(variant='panel'):
131
- output_audio_from_image = gr.Audio(type="filepath", label="Сгенерированный звук")
132
 
133
  def gradio_image_to_audio_fn(upload_image):
134
  return image_to_spectrogram_audio(upload_image)
@@ -139,14 +139,14 @@ with gr.Blocks(title='Аудио-Стеганография', theme=gr.themes.So
139
  outputs=[output_audio_from_image]
140
  )
141
 
142
- with gr.Tab("Спектрограмма аудио"):
143
  with gr.Group():
144
  with gr.Row(variant='panel'):
145
- upload_audio = gr.Audio(type="filepath", label="Загрузите аудиофайл")
146
- decode_button = gr.Button("Показать спектрограмму")
147
 
148
  with gr.Column(variant='panel'):
149
- decoded_image = gr.Image(type="filepath", label="Спектрограмма аудио")
150
 
151
  def gradio_decode_fn(upload_audio):
152
  return display_audio_spectrogram(upload_audio)
 
7
  import soundfile as sf
8
  import os
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
  font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
13
  if os.path.exists(font_path):
 
42
  image = np.where(image > 0, 255, image)
43
  return image
44
 
45
+ # Converting an image to an audio signal
46
  def spectrogram_image_to_audio(image, sr=22050):
47
  flipped_image = np.flipud(image)
48
  S = flipped_image.astype(np.float32) / 255.0 * 100.0
49
  y = librosa.griffinlim(S)
50
  return y
51
 
52
+ # Function for creating an audio file and a spectrogram from text
53
  def create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing):
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)
 
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)
 
74
  plt.close()
75
  return spectrogram_path
76
 
77
+ # Convert the uploaded image to a sound spectrogram
78
  def image_to_spectrogram_audio(image_path, sr=22050):
79
  image = Image.open(image_path).convert('L')
80
  image = np.array(image)
 
83
  sf.write(audio_path, y, sr)
84
  return audio_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 iface:
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("Text to Spectrogram"):
101
  with gr.Group():
102
+ text = gr.Textbox(lines=2, placeholder="Enter your text:", label="Text")
103
  with gr.Row(variant='panel'):
104
+ base_width = gr.Slider(value=512, label="Image Width", visible=False)
105
+ height = gr.Slider(value=256, label="Image Height", visible=False)
106
+ max_font_size = gr.Slider(minimum=10, maximum=130, step=5, value=80, label="Font Size")
107
+ margin = gr.Slider(minimum=0, maximum=50, step=1, value=10, label="Indent")
108
+ letter_spacing = gr.Slider(minimum=0, maximum=50, step=1, value=5, label="Distance between letters")
109
+ generate_button = gr.Button("Generate")
110
 
111
  with gr.Column(variant='panel'):
112
+ output_audio = gr.Audio(type="filepath", label="Generated Sound")
113
+ output_image = gr.Image(type="filepath", label="Spectrogram")
114
 
115
  def gradio_interface_fn(text, base_width, height, max_font_size, margin, letter_spacing):
116
  return create_audio_with_spectrogram(text, base_width, height, max_font_size, margin, letter_spacing)
 
121
  outputs=[output_audio, output_image]
122
  )
123
 
124
+ with gr.Tab("Image to Spectrogram"):
125
  with gr.Group():
126
  with gr.Row(variant='panel'):
127
+ upload_image = gr.Image(type="filepath", label="Upload an image")
128
+ convert_button = gr.Button("Convert to Audio")
129
 
130
  with gr.Column(variant='panel'):
131
+ output_audio_from_image = gr.Audio(type="filepath", label="Generated Sound")
132
 
133
  def gradio_image_to_audio_fn(upload_image):
134
  return image_to_spectrogram_audio(upload_image)
 
139
  outputs=[output_audio_from_image]
140
  )
141
 
142
+ with gr.Tab("Audio Spectrogram"):
143
  with gr.Group():
144
  with gr.Row(variant='panel'):
145
+ upload_audio = gr.Audio(type="filepath", label="Upload an audio file")
146
+ decode_button = gr.Button("Show a spectrogram")
147
 
148
  with gr.Column(variant='panel'):
149
+ decoded_image = gr.Image(type="filepath", label="Audio Spectrogram")
150
 
151
  def gradio_decode_fn(upload_audio):
152
  return display_audio_spectrogram(upload_audio)