Commit a193a478 authored by LouisDeOliveira's avatar LouisDeOliveira
Browse files

ajout des fichiers

parent b4d44320
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"conversion_mel_IRMAS","provenance":[],"collapsed_sections":[],"toc_visible":true,"mount_file_id":"1obbS6-vX1z--9i1mMt84W34a1OqQO8Dw","authorship_tag":"ABX9TyPpHeWmgS16jAN/ZFzmeKZ4"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","metadata":{"id":"zQQC4ApGdNxA","executionInfo":{"status":"ok","timestamp":1618244287875,"user_tz":-120,"elapsed":828,"user":{"displayName":"guillaume ALLEGRE","photoUrl":"","userId":"15788823947103069938"}}},"source":["import scipy\n","import os\n","from scipy.io import wavfile\n","from scipy import signal\n","import matplotlib.pyplot as plt\n","import numpy as np\n","import os\n","import librosa\n","import librosa.display\n","\n","\n","import librosa\n","import numpy\n","\n","\n","\n","# settings\n","hop_length = 512 # number of samples per time-step in spectrogram\n","n_mels = 256 # number of bins in spectrogram. Height of image\n","time_steps = 256 # number of time-steps. Width of image\n"],"execution_count":31,"outputs":[]},{"cell_type":"code","metadata":{"id":"2Y62suo5gFCc","executionInfo":{"status":"ok","timestamp":1618244288081,"user_tz":-120,"elapsed":431,"user":{"displayName":"guillaume ALLEGRE","photoUrl":"","userId":"15788823947103069938"}}},"source":["data_path = './drive/MyDrive/Automatant-Musique/Data_IRMAS/IRMAS-TrainingData'\n","data_converted_path = './drive/MyDrive/Automatant-Musique/Data_converted_train_IRMAS'\n","lst_instru = ['org','cel','cla','sax','flu','gac','gel','pia','tru','vio','voi']\n","\n","train_img = []\n","train_label = []\n","\n","\n","LIMIT = False\n","num_limit = 10 # max is 100"],"execution_count":32,"outputs":[]},{"cell_type":"code","metadata":{"id":"jz5Z5PUCvhdA","executionInfo":{"status":"ok","timestamp":1618244288903,"user_tz":-120,"elapsed":541,"user":{"displayName":"guillaume ALLEGRE","photoUrl":"","userId":"15788823947103069938"}}},"source":["def index(intru):\n","\n"," for i in range(len(lst_instru)):\n"," if instru == lst_instru[i]:\n"," return i\n","\n","def scale_minmax(X, min=0.0, max=1.0):\n"," X_std = (X - X.min()) / (X.max() - X.min())\n"," X_scaled = X_std * (max - min) + min\n"," return X_scaled\n","\n","def spectrogram_image(y, sr, out, hop_length, n_mels):\n"," # use log-melspectrogram\n"," mels = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=n_mels,\n"," n_fft=hop_length*2, hop_length=hop_length)\n"," mels = numpy.log(mels + 1e-9) # add small number to avoid log(0)\n","\n"," # min-max scale to fit inside 8-bit range\n"," img = scale_minmax(mels, 0, 255).astype(numpy.uint8)\n"," img =np.expand_dims(img, axis=2)\n","\n"," train_img.append(img)\n"," train_label.append(index(instru))\n"," \n","\n","def save_img(data_path, instru, wavfile):\n","\n"," x_path = './drive/MyDrive/Automatant-Musique/Data_IRMAS/IRMAS-TrainingData' + '/' + instru + '/' + wavfile\n"," x_save_path = './drive/MyDrive/Automatant-Musique/Data_converted_train_IRMAS' + '/' + instru + '/' + wavfile[:-3] + \"png\"\n","\n"," y, sr = librosa.load(x_path, offset=1.0, duration=2.0)\n","\n"," # extract a fixed length window\n"," start_sample = 0 # starting at beginning\n"," length_samples = time_steps*hop_length\n"," window = y\n","\n"," \n"," # convert to PNG\n"," spectrogram_image(window, sr=sr, out=x_save_path, hop_length=hop_length, n_mels=n_mels)\n"],"execution_count":33,"outputs":[]},{"cell_type":"code","metadata":{"id":"soaiQoJbtbSQ","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1618245214678,"user_tz":-120,"elapsed":924867,"user":{"displayName":"guillaume ALLEGRE","photoUrl":"","userId":"15788823947103069938"}},"outputId":"5ca52630-1cc3-41fa-b70d-c654737612d5"},"source":["\n","for instru in os.listdir(data_path):\n"," print(instru)\n"," for wavfile in os.listdir(data_path + '/' + instru):\n"," if wavfile[-3:] != 'wav':\n"," continue\n"," \n"," save_img(data_converted_path, instru, wavfile)\n","\n"],"execution_count":34,"outputs":[{"output_type":"stream","text":["cla\n","org\n","vio\n","pia\n","sax\n","cel\n","gac\n","flu\n","gel\n","tru\n","voi\n",".ipynb_checkpoints\n"],"name":"stdout"}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"VAIlPGE9ZbDZ","executionInfo":{"status":"ok","timestamp":1618245726173,"user_tz":-120,"elapsed":2121,"user":{"displayName":"guillaume ALLEGRE","photoUrl":"","userId":"15788823947103069938"}},"outputId":"86517ad6-d98b-4402-8402-469e064a95ea"},"source":["train_img = np.array(train_img)\n","print(np.shape(train_img))\n","np.save('./drive/MyDrive/Automatant-Musique/Data_converted_train_IRMAS/train_images.npy', train_img)\n","np.save('./drive/MyDrive/Automatant-Musique/Data_converted_train_IRMAS/train_labels.npy', train_label)"],"execution_count":35,"outputs":[{"output_type":"stream","text":["(6705, 256, 87, 1)\n"],"name":"stdout"}]}]}
This diff is collapsed.
This diff is collapsed.
import os
from utils.img2midi import image2midi
from utils.midi2img import midi2image
def images_from_MIDI(dataset_path):
for subdir, dirs, files in os.walk(dataset_path):
for filename in files:
try:
path = os.path.join(subdir, filename)
path_bis = os.path.split(path)[-1]
print(path_bis)
midi2image(path, "./images/")
except:
pass
if __name__ == '__main__':
images_from_MIDI("./MIDI")
import os
import numpy as np
import cv2
def make_dataset(image_path, output_path):
dataset = []
n = 0
for image_path in os.scandir(image_path):
try:
n += 1
print(n)
img = cv2.imread(os.path.abspath(image_path))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if len(np.unique(img)) > 1:
img = np.reshape(np.array(img).astype(
'float32')/255.0, (106, 106, 1))
dataset.append(img)
except:
pass
np.save(output_path, dataset)
if __name__ == '__main__':
make_dataset("./images", "./test_bis.npy")
dataset = np.load("./test_bis.npy")
print(np.shape(dataset))
cv2.imshow('test', dataset[1000])
cv2.waitKey(0)
import cv2
import numpy as np
def threshold_processing(path, output_path, value=128, ):
img = np.array(cv2.imread((path), cv2.IMREAD_GRAYSCALE))
_, img = cv2.threshold(img, value, 255, cv2.THRESH_BINARY)
cv2.imshow('test', img)
cv2.waitKey(0)
cv2.imwrite(output_path, img)
def threshold_processing_img(img, value=128, ):
img = cv2.threshold(img, value, 255, cv2.THRESH_BINARY)
return img
if __name__ == '__main__':
threshold_processing('./GAN_training_images/1.png', './test.png')
from utils.img2midi import image2midi
from utils.midi2img import midi2image
import tensorflow as tf
from tensorflow import keras
import cv2
import numpy as np
gan = keras.models.load_model("./GAN")
generator, discriminator = gan.layers
img = generator(tf.random.normal(shape=[1, 256]))[0]
cv2.imwrite('./GAN_generated_images/test.png', np.abs(img)*255)
image2midi("./GAN_generated_images/test.png", './generated_midi/test.mid')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment