Commit a3af55ae authored by lucasdaniele's avatar lucasdaniele
Browse files

Machine learning debut

parent 67909f00
{
"python.pythonPath": "/usr/bin/python3"
}
\ No newline at end of file
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#%%
import numpy as np
from scipy.io import wavfile
import os
import pandas as pd
from python_speech_features import mfcc
from sklearn.neighbors import KNeighborsClassifier
import pickle
#%%
def traiter(d,v):
res = np.zeros(len(d)+1)
res[0] = v
res[1:len(d)+1] = d
return np.asarray(res)
def dataToCsv():
files = os.listdir('input/negatif')
data = []
for i in range(len(files)):
(rate,sig) = wavfile.read("input/negatif/"+files[i])
data.append(traiter(mfcc(sig,rate,len(sig)/rate)[0],0))
files = os.listdir('input/positif')
for i in range(len(files)):
(rate,sig) = wavfile.read("input/positif/"+files[i])
data.append(traiter(mfcc(sig,rate,len(sig)/rate)[0],1))
data = np.asarray(data)
data = pd.DataFrame(data,columns=["y","0","1","2","3","4","5","6","7","8","9","10","11","12"])
data.to_csv("input/res.csv")
# %%
dataToCsv()
# %%
model = KNeighborsClassifier()
d = pd.read_csv("input/res.csv", names=["k","y","0","1","2","3","4","5","6","7","8","9","10","11","12"])
d = d.drop([0])
d = d.drop("k", axis=1)
y = d["y"]
X = d.drop("y", axis=1)
print(y)
model.fit(X,y)
model.score(X,y)
filename = 'saves/finalized_model.sav'
pickle.dump(model, open(filename, 'wb'))
# %%
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# %%
import numpy as np
from scipy.io import wavfile
from playsound import playsound
import os
import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
import pickle
from python_speech_features import mfcc
# %%
def choisir():
choix = input("cris? Y/N/S (Yes, No, Stop) : ")
if ((choix == "y") | (choix == "Y")):
return "positif"
if ((choix == "n") | (choix == "N")):
return "negatif"
if ((choix == "s") | (choix == "S")):
return "stop"
return "invalide"
def ecouter(nom,sampleRate, data):
wavfile.write(nom, sampleRate, data)
playsound(nom)
def traiter(d,v):
res = np.zeros(len(d)+1)
res[0] = v
res[1:len(d)+1] = d
return np.asarray(res)
def nouvelleValeur(sig, rate, victoire):
d = pd.read_csv("input/res.csv", names=["k","y","0","1","2","3","4","5","6","7","8","9","10","11","12"])
d = d.drop([0])
d = d.drop("k", axis=1)
nouvelleLigne = pd.DataFrame(traiter(mfcc(sig,rate,len(sig)/rate)[0],victoire).reshape(1, 14), columns=["y","0","1","2","3","4","5","6","7","8","9","10","11","12"])
d = d.append(nouvelleLigne)
d.to_csv("input/res.csv")
d = pd.read_csv("input/res.csv", names=["k","y","0","1","2","3","4","5","6","7","8","9","10","11","12"])
d = d.drop([0])
d = d.drop("k", axis=1)
y = d["y"]
X = d.drop("y", axis=1)
model = KNeighborsClassifier()
model.fit(X,y)
print(model.score(X,y))
filename = 'saves/finalized_model.sav'
pickle.dump(model, open(filename, 'wb'))
# %%
chemin = "input/files/"
nomFichier = "file"
extensionFichier = ".wav"
rate, sig = wavfile.read(chemin + nomFichier + extensionFichier)
sig=sig[:,0]
stop=False
for k in range(int(np.round(len(sig)/rate))-1):
if (not(stop)):
ecouter("rec"+str(k)+extensionFichier, rate, sig[(k)*rate:(k+1)*rate])
choix = choisir()
if (choix == "positif"):
os.rename("rec"+str(k)+extensionFichier, "input/positif/"+nomFichier+str(k)+extensionFichier)
nouvelleValeur(sig[(k)*rate:(k+1)*rate], rate, 1)
if (choix == "negatif"):
os.rename("rec"+str(k)+extensionFichier, "input/negatif/"+nomFichier+str(k)+extensionFichier)
nouvelleValeur(sig[(k)*rate:(k+1)*rate], rate, 0)
if (choix=="stop"):
stop = True
os.remove("rec"+str(k)+extensionFichier)
if (choix == "invalide"):
os.remove("rec"+str(k)+extensionFichier)
# %%
# %%
This diff is collapsed.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#%%
import numpy as np
from scipy.io import wavfile
import os
import pandas as pd
from python_speech_features import mfcc
from python_speech_features import delta
from python_speech_features import logfbank
import pickle
# %%
def estCris(model, chemin):
(rate,sig) = wavfile.read(chemin)
x = mfcc(sig,rate,len(sig)/rate)[0]
x = x.reshape(1,len(x))
return model.predict(x), model.predict_proba(x)
# %%
filename = 'saves/finalized_model.sav'
model = pickle.load(open(filename, 'rb'))
print(estCris(model, "input/tests/cri0.wav"))
print(estCris(model, "input/tests/cri2.wav"))
print(estCris(model, "input/tests/cri18.wav"))
# %%
......@@ -34,9 +34,9 @@ def decouperExtraits(data, seuil, dureeSilence, sampleRate, sampleRateChoosen, s
k=0
d=0
f=0
for i in range(0,int(np.round(dureeExtrait)*sampleRateChoosen-step)-1000):
for i in range(0,int(np.round(dureeExtrait)*sampleRateChoosen-step)-100000):
if (i%40000==0):
afficherAvancement(start_time, i,int(np.round(dureeExtrait)*sampleRateChoosen-step)-1000)
afficherAvancement(start_time, i,int(np.round(dureeExtrait)*sampleRateChoosen-step)-10000)
if ((abs(data[int(np.round(i*echelle))])<seuil) & onAir):
if(k<step):
k+=1
......
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