Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from keras.models import load_model
|
3 |
from tensorflow.keras.utils import img_to_array
|
4 |
-
from tensorflow.keras.utils import load_img
|
5 |
from numpy import expand_dims
|
6 |
from PIL import Image
|
7 |
import librosa
|
@@ -35,39 +34,33 @@ def add_harmonics(spectrogram, harmonic_shift):
|
|
35 |
def modulate_amplitude(spectrogram, factor):
|
36 |
return np.clip(spectrogram * factor, 0, 1) # Amplify or attenuate the white areas
|
37 |
|
38 |
-
# Function to randomly
|
39 |
def modify_spectrogram(spectrogram):
|
40 |
-
# Random decision for transformations
|
41 |
apply_shift = random.choice([True, False])
|
42 |
apply_filtering = random.choice([True, False])
|
43 |
apply_harmonics = random.choice([True, False])
|
44 |
apply_amplitude_modulation = random.choice([True, False])
|
45 |
|
46 |
-
# Randomly select the values for each transformation
|
47 |
if apply_shift:
|
48 |
-
shift_value = random.randint(-15, 15)
|
49 |
-
print(f"Applying frequency shift: {shift_value}")
|
50 |
spectrogram = shift_frequencies(spectrogram, shift=shift_value)
|
51 |
|
52 |
if apply_filtering:
|
53 |
-
low_cut = random.randint(10, 50)
|
54 |
-
high_cut = random.randint(300, 600)
|
55 |
-
print(f"Applying filter: low_cut={low_cut}, high_cut={high_cut}")
|
56 |
spectrogram = apply_filter(spectrogram, low_cut=low_cut, high_cut=high_cut)
|
57 |
|
58 |
if apply_harmonics:
|
59 |
-
harmonic_shift = random.randint(2, 10)
|
60 |
-
print(f"Applying harmonic shift: {harmonic_shift}")
|
61 |
spectrogram = add_harmonics(spectrogram, harmonic_shift=harmonic_shift)
|
62 |
|
63 |
if apply_amplitude_modulation:
|
64 |
-
factor = random.uniform(0.8, 2.0)
|
65 |
-
print(f"Applying amplitude modulation: factor={factor}")
|
66 |
spectrogram = modulate_amplitude(spectrogram, factor=factor)
|
67 |
|
68 |
return spectrogram
|
69 |
|
70 |
-
#
|
71 |
def save_spectrogram_image(spectrogram):
|
72 |
plt.figure(figsize=(10, 4))
|
73 |
plt.imshow(spectrogram, aspect='auto', origin='lower', cmap='gray')
|
@@ -81,9 +74,8 @@ def save_spectrogram_image(spectrogram):
|
|
81 |
plt.close()
|
82 |
return temp_image_path
|
83 |
|
84 |
-
#
|
85 |
def process_image(input_image):
|
86 |
-
# Load and preprocess the input image
|
87 |
def load_image(image, size=(256, 256)):
|
88 |
image = image.resize(size)
|
89 |
pixels = img_to_array(image)
|
@@ -121,12 +113,24 @@ def process_image(input_image):
|
|
121 |
|
122 |
return spectrogram_image_path, audio_file_path # Return the paths for both spectrogram image and audio
|
123 |
|
124 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
interface = gr.Interface(
|
126 |
-
fn=
|
127 |
inputs=gr.Image(type="pil"), # Input is an image
|
128 |
outputs=[gr.Image(type="filepath"), gr.Audio(type="filepath")], # Output both spectrogram image and audio file
|
129 |
-
title="Image to Audio Generator with Spectrogram Display",
|
130 |
description="Upload an image (preferably a spectrogram), and get an audio file generated using Pix2Pix. You can also see the modified spectrogram.",
|
131 |
)
|
132 |
|
|
|
1 |
import gradio as gr
|
2 |
from keras.models import load_model
|
3 |
from tensorflow.keras.utils import img_to_array
|
|
|
4 |
from numpy import expand_dims
|
5 |
from PIL import Image
|
6 |
import librosa
|
|
|
34 |
def modulate_amplitude(spectrogram, factor):
|
35 |
return np.clip(spectrogram * factor, 0, 1) # Amplify or attenuate the white areas
|
36 |
|
37 |
+
# Function to randomly apply transformations
|
38 |
def modify_spectrogram(spectrogram):
|
|
|
39 |
apply_shift = random.choice([True, False])
|
40 |
apply_filtering = random.choice([True, False])
|
41 |
apply_harmonics = random.choice([True, False])
|
42 |
apply_amplitude_modulation = random.choice([True, False])
|
43 |
|
|
|
44 |
if apply_shift:
|
45 |
+
shift_value = random.randint(-15, 15)
|
|
|
46 |
spectrogram = shift_frequencies(spectrogram, shift=shift_value)
|
47 |
|
48 |
if apply_filtering:
|
49 |
+
low_cut = random.randint(10, 50)
|
50 |
+
high_cut = random.randint(300, 600)
|
|
|
51 |
spectrogram = apply_filter(spectrogram, low_cut=low_cut, high_cut=high_cut)
|
52 |
|
53 |
if apply_harmonics:
|
54 |
+
harmonic_shift = random.randint(2, 10)
|
|
|
55 |
spectrogram = add_harmonics(spectrogram, harmonic_shift=harmonic_shift)
|
56 |
|
57 |
if apply_amplitude_modulation:
|
58 |
+
factor = random.uniform(0.8, 2.0)
|
|
|
59 |
spectrogram = modulate_amplitude(spectrogram, factor=factor)
|
60 |
|
61 |
return spectrogram
|
62 |
|
63 |
+
# Save the modified spectrogram image for display
|
64 |
def save_spectrogram_image(spectrogram):
|
65 |
plt.figure(figsize=(10, 4))
|
66 |
plt.imshow(spectrogram, aspect='auto', origin='lower', cmap='gray')
|
|
|
74 |
plt.close()
|
75 |
return temp_image_path
|
76 |
|
77 |
+
# Process the input image and convert to audio
|
78 |
def process_image(input_image):
|
|
|
79 |
def load_image(image, size=(256, 256)):
|
80 |
image = image.resize(size)
|
81 |
pixels = img_to_array(image)
|
|
|
113 |
|
114 |
return spectrogram_image_path, audio_file_path # Return the paths for both spectrogram image and audio
|
115 |
|
116 |
+
# Gradio Interface
|
117 |
+
def gradio_process_image(input_image):
|
118 |
+
spectrogram_image_path, audio_file_path = process_image(input_image)
|
119 |
+
|
120 |
+
# After Gradio finishes using these files, delete them to avoid keeping them around
|
121 |
+
def cleanup():
|
122 |
+
os.remove(spectrogram_image_path)
|
123 |
+
os.remove(audio_file_path)
|
124 |
+
print(f"Deleted temp files: {spectrogram_image_path}, {audio_file_path}")
|
125 |
+
|
126 |
+
return spectrogram_image_path, audio_file_path, cleanup
|
127 |
+
|
128 |
+
# Create the Gradio interface
|
129 |
interface = gr.Interface(
|
130 |
+
fn=gradio_process_image,
|
131 |
inputs=gr.Image(type="pil"), # Input is an image
|
132 |
outputs=[gr.Image(type="filepath"), gr.Audio(type="filepath")], # Output both spectrogram image and audio file
|
133 |
+
title="Image to Audio Generator with Spectrogram Display",
|
134 |
description="Upload an image (preferably a spectrogram), and get an audio file generated using Pix2Pix. You can also see the modified spectrogram.",
|
135 |
)
|
136 |
|