Spaces:
Runtime error
Runtime error
Commit ·
5569dfb
1
Parent(s): 5108790
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,12 +5,23 @@ import soundfile as sf
|
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
-
def process_color_to_audio(array, sample_rate=44100, duration=5
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
return audio_data, sample_rate
|
| 15 |
|
| 16 |
def main():
|
|
@@ -22,8 +33,6 @@ def main():
|
|
| 22 |
image = Image.open(uploaded_file)
|
| 23 |
st.image(image, caption='Uploaded PNG image', use_column_width=True)
|
| 24 |
|
| 25 |
-
st.write("Audio will be generated with default parameters.")
|
| 26 |
-
|
| 27 |
if st.button("Generate Audio"):
|
| 28 |
array = np.array(image)
|
| 29 |
audio_data, sample_rate = process_color_to_audio(array)
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
+
def process_color_to_audio(array, sample_rate=44100, duration=5):
|
| 9 |
+
r_mean = np.mean(array[:, :, 0]) / 255.0
|
| 10 |
+
g_mean = np.mean(array[:, :, 1]) / 255.0
|
| 11 |
+
b_mean = np.mean(array[:, :, 2]) / 255.0
|
| 12 |
+
|
| 13 |
+
min_freq = 100
|
| 14 |
+
max_freq = 1000
|
| 15 |
+
|
| 16 |
+
r_freq = min_freq + (1 - r_mean) * (max_freq - min_freq)
|
| 17 |
+
g_freq = min_freq + (1 - g_mean) * (max_freq - min_freq)
|
| 18 |
+
b_freq = min_freq + (1 - b_mean) * (max_freq - min_freq)
|
| 19 |
+
|
| 20 |
+
t = np.linspace(0, duration, int(sample_rate * duration), endpoint=False)
|
| 21 |
+
|
| 22 |
+
audio_data = np.sin(2 * np.pi * r_freq * t) + np.sin(2 * np.pi * g_freq * t) + np.sin(2 * np.pi * b_freq * t)
|
| 23 |
+
audio_data /= np.max(np.abs(audio_data))
|
| 24 |
+
|
| 25 |
return audio_data, sample_rate
|
| 26 |
|
| 27 |
def main():
|
|
|
|
| 33 |
image = Image.open(uploaded_file)
|
| 34 |
st.image(image, caption='Uploaded PNG image', use_column_width=True)
|
| 35 |
|
|
|
|
|
|
|
| 36 |
if st.button("Generate Audio"):
|
| 37 |
array = np.array(image)
|
| 38 |
audio_data, sample_rate = process_color_to_audio(array)
|