Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,10 +8,64 @@ import librosa
|
|
8 |
import numpy as np
|
9 |
import soundfile as sf
|
10 |
import os
|
|
|
11 |
|
12 |
# Load your Pix2Pix model (make sure the path is correct)
|
13 |
model = load_model('./model_022600.h5', compile=False)
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# Function to process the input image and convert to audio
|
16 |
def process_image(input_image):
|
17 |
# Load and preprocess the input image
|
@@ -35,6 +89,9 @@ def process_image(input_image):
|
|
35 |
|
36 |
# Convert the image to a numpy array (spectrogram)
|
37 |
img = np.array(gen_image_resized)
|
|
|
|
|
|
|
38 |
|
39 |
# Convert the spectrogram back to audio using librosa
|
40 |
wav = librosa.feature.inverse.mel_to_audio(img, sr=44100, n_fft=2048, hop_length=512)
|
|
|
8 |
import numpy as np
|
9 |
import soundfile as sf
|
10 |
import os
|
11 |
+
import random
|
12 |
|
13 |
# Load your Pix2Pix model (make sure the path is correct)
|
14 |
model = load_model('./model_022600.h5', compile=False)
|
15 |
|
16 |
+
|
17 |
+
# Function to shift frequencies
|
18 |
+
def shift_frequencies(spectrogram, shift):
|
19 |
+
return np.roll(spectrogram, shift, axis=0)
|
20 |
+
|
21 |
+
# Function to apply a frequency filter
|
22 |
+
def apply_filter(spectrogram, low_cut, high_cut):
|
23 |
+
filtered = np.copy(spectrogram)
|
24 |
+
filtered[:low_cut, :] = 0 # Attenuate low frequencies
|
25 |
+
filtered[high_cut:, :] = 0 # Attenuate high frequencies
|
26 |
+
return filtered
|
27 |
+
|
28 |
+
# Function to add harmonics
|
29 |
+
def add_harmonics(spectrogram, harmonic_shift):
|
30 |
+
harmonics = np.roll(spectrogram, harmonic_shift, axis=0) * 0.5 # Weaken the harmonics
|
31 |
+
return np.clip(spectrogram + harmonics, 0, 1)
|
32 |
+
|
33 |
+
# Function to modulate the amplitude
|
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 decide which transformations to apply and with what parameters
|
38 |
+
def modify_spectrogram(spectrogram):
|
39 |
+
# Random decision for transformations
|
40 |
+
apply_shift = random.choice([True, False])
|
41 |
+
apply_filtering = random.choice([True, False])
|
42 |
+
apply_harmonics = random.choice([True, False])
|
43 |
+
apply_amplitude_modulation = random.choice([True, False])
|
44 |
+
|
45 |
+
# Randomly select the values for each transformation
|
46 |
+
if apply_shift:
|
47 |
+
shift_value = random.randint(-15, 15) # Random shift between -15 and 15
|
48 |
+
print(f"Applying frequency shift: {shift_value}")
|
49 |
+
spectrogram = shift_frequencies(spectrogram, shift=shift_value)
|
50 |
+
|
51 |
+
if apply_filtering:
|
52 |
+
low_cut = random.randint(10, 50) # Random low_cut between 10 and 50
|
53 |
+
high_cut = random.randint(300, 600) # Random high_cut between 300 and 600
|
54 |
+
print(f"Applying filter: low_cut={low_cut}, high_cut={high_cut}")
|
55 |
+
spectrogram = apply_filter(spectrogram, low_cut=low_cut, high_cut=high_cut)
|
56 |
+
|
57 |
+
if apply_harmonics:
|
58 |
+
harmonic_shift = random.randint(2, 10) # Random harmonic shift between 2 and 10
|
59 |
+
print(f"Applying harmonic shift: {harmonic_shift}")
|
60 |
+
spectrogram = add_harmonics(spectrogram, harmonic_shift=harmonic_shift)
|
61 |
+
|
62 |
+
if apply_amplitude_modulation:
|
63 |
+
factor = random.uniform(0.8, 2.0) # Random amplitude factor between 0.8 and 2.0
|
64 |
+
print(f"Applying amplitude modulation: factor={factor}")
|
65 |
+
spectrogram = modulate_amplitude(spectrogram, factor=factor)
|
66 |
+
|
67 |
+
return spectrogram
|
68 |
+
|
69 |
# Function to process the input image and convert to audio
|
70 |
def process_image(input_image):
|
71 |
# Load and preprocess the input image
|
|
|
89 |
|
90 |
# Convert the image to a numpy array (spectrogram)
|
91 |
img = np.array(gen_image_resized)
|
92 |
+
|
93 |
+
# Modify the spectrogram randomly
|
94 |
+
img = modify_spectrogram(img)
|
95 |
|
96 |
# Convert the spectrogram back to audio using librosa
|
97 |
wav = librosa.feature.inverse.mel_to_audio(img, sr=44100, n_fft=2048, hop_length=512)
|