Downloads · 30 days
0
heea/image-to-music
image-to-music is a machine learning model from heea. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
import matplotlib.pyplot as plt import numpy as np from scipy.io.wavfile import write
Downloads · 30 days
0
Access
Public
Updated Jul 10, 2023
Repo size
186 MB
Likes
2
Public
Click a slice to open those files.
.md1.6 KB · 52%
From the Hugging Face model README
import matplotlib.pyplot as plt import numpy as np from scipy.io.wavfile import write
image_path = 'hugging_face.jpg' image = plt.imread(image_path)
grayscale_image = np.mean(image, axis=2)
sample_rate = 44100 # Audio sample rate (Hz) duration = 0.1 # Duration of each note (seconds)
min_note = 40 # MIDI note number for the lowest pixel value max_note = 80 # MIDI note number for the highest pixel value
scaled_image = (grayscale_image - np.min(grayscale_image)) scaled_image *= (max_note - min_note) / np.max(scaled_image) scaled_image += min_note
total_duration = int(duration * sample_rate * grayscale_image.shape[1]) t = np.linspace(0, total_duration / sample_rate, total_duration, endpoint=False) audio_signal = np.zeros(total_duration) for i, column in enumerate(scaled_image.T): start = int(i * duration * sample_rate) end = int((i + 1) * duration * sample_rate) audio_signal[start:end] = np.sin(2 * np.pi * column * t[start:end])
audio_signal /= np.max(np.abs(audio_signal)) audio_signal *= 32767 # Scale the signal to the range of a 16-bit integer
audio_signal = audio_signal.astype(np.int16)
output_file = 'hugging_face.wav' write(output_file, sample_rate, audio_signal)
print(f"Audio file '{output_file}' generated successfully!")