Wav file to png file(spectrum) for machine learning

We firtly start import library

Look at on github

https://github.com/flavves/Wav-file-to-png-file-spectrum-for-machine-learning

download dataset here :

 https://wetransfer.com/downloads/d731d25849ca25f7f6bd0349392d8c2920210103141036/fd98e1

get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
import pandas as pd
import random
from scipy.io import wavfile
from sklearn.preprocessing import scale
import librosa.display
import librosa
import matplotlib.pyplot as plt
import os

firtly we need understand how is it first. You can see end of the title full code.

We need set veriables


sampling_rate=44100
sayac=1

We use librosa in this code. İf we have a a lot of wav file we need use this code sample

while 1:
    if sayac < 2000:
            
        dosya_konum="we/dataset/not/2 ("+str(sayac)+").wav"
        data, sr = librosa.load(dosya_konum, sr=sampling_rate, mono=True)
        data = scale(data)
        
        melspec = librosa.feature.melspectrogram(y=data, sr=sr, n_mels=128)
           
        log_melspec = librosa.power_to_db(melspec, ref=np.max)  
        librosa.display.specshow(log_melspec, sr=sr)
            
            # create saving directory
        directory = 'we/dataset/notcough_foto/'
        if not os.path.exists(directory):
            os.makedirs(directory)
        foto_ad="a"+str(sayac)    
        plt.savefig(directory + '/' + (foto_ad) + '.png')
        sayac=sayac+1
        print("oldu")
        print(sayac)
    else:
        print("hata")

İf we want one wav file to convert png file we need use this code sample

dosya=input("dosya adı girin: ")
dosya_konum="we/dataset/not/"+dosya+".wav"
data, sr = librosa.load(dosya_konum, sr=sampling_rate, mono=True)
data = scale(data)
        
melspec = librosa.feature.melspectrogram(y=data, sr=sr, n_mels=128)
           
log_melspec = librosa.power_to_db(melspec, ref=np.max)  
librosa.display.specshow(log_melspec, sr=sr)
            
# kayıt yolu
directory = 'we/dataset/'
if not os.path.exists(directory):
    os.makedirs(directory)
foto_ad="deneme"+str(sayac)    
plt.savefig(directory + '/' + (foto_ad) + '.png')

Then we use this code machine learning. You can look at my other sample code. This code working on detect cought voice. https://github.com/flavves/oksurukTespit

But this code writen turkish if you dont understend this code mail me batuhanokmen@gmail.com ı can explain.

you can choice my other machine learning code.
# -*- coding: utf-8 -*-
"""
Created on Wed Jan  6 01:20:45 2021

@author: yazılım
"""


"""

birleşik program

"""

get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
import pandas as pd
import random
from scipy.io import wavfile
from sklearn.preprocessing import scale
import librosa.display
import librosa
import matplotlib.pyplot as plt
import os


from keras.callbacks import ModelCheckpoint, TensorBoard

import numpy as np 
import pandas as pd 


import os
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.layers import Dense, Conv2D, MaxPool2D, Flatten, Dropout
import time

import matplotlib.pyplot as plt

from tensorflow.keras.preprocessing.image import ImageDataGenerator, load_img, img_to_array

from tensorflow.keras.models import load_model



sampling_rate=44100
sayac=1







"""

Spektogram kaydetmesi Tekil

"""
dosya=input("dosya adı girin: ")

# dosya konumunu yazın


dosya_konum="we/dataset/not/"+dosya+".wav"
data, sr = librosa.load(dosya_konum, sr=sampling_rate, mono=True)
data = scale(data)
        
melspec = librosa.feature.melspectrogram(y=data, sr=sr, n_mels=128)
           
log_melspec = librosa.power_to_db(melspec, ref=np.max)  
librosa.display.specshow(log_melspec, sr=sr)
            
# kayıt yolu yazın
directory = 'we/dataset'
if not os.path.exists(directory):
    os.makedirs(directory)
foto_ad="a"+str(sayac)    
plt.savefig(directory + '/' + (foto_ad) + '.png')

resim_yolu=(directory + '/' + (foto_ad) + '.png')

"""

ÖKSÜRÜK TESPİTİ

"""


# training oksuruk deil






image = tf.keras.preprocessing.image.load_img(resim_yolu)
giris = tf.keras.preprocessing.image.img_to_array(image)
giris = np.array([giris])  # Convert single image to a batch.





"""

Tahmin

"""


save_path = '.'
model2 = load_model(os.path.join(save_path,"bionluk.h5"))
pred = model2.predict(giris)
pred
for a in pred:
    print(a[1])
    sonuc=a[0]
    sonuc1=a[1]
    
if sonuc >0.50:
    print("SONUÇ ÖKSÜRÜK")
else:
    print("NEGATİF")

Wav file to png file(spectrum) for machine learning” için bir yorum

Bir yanıt yazın