Commit 02431ab7 authored by lucasdaniele's avatar lucasdaniele
Browse files

entrainement du modèle + ajout métrique

parent a3af55ae
......@@ -42,7 +42,7 @@ 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'
......
......@@ -51,7 +51,7 @@ def nouvelleValeur(sig, rate, victoire):
pickle.dump(model, open(filename, 'wb'))
# %%
chemin = "input/files/"
nomFichier = "file"
nomFichier = "10_"
extensionFichier = ".wav"
rate, sig = wavfile.read(chemin + nomFichier + extensionFichier)
sig=sig[:,0]
......@@ -71,8 +71,6 @@ for k in range(int(np.round(len(sig)/rate))-1):
stop = True
os.remove("rec"+str(k)+extensionFichier)
if (choix == "invalide"):
os.remove("rec"+str(k)+extensionFichier)
os.remove("rec"+str(k)+extensionFichier)
# %%
# %%
#!/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 python_speech_features import mfcc
from python_speech_features import delta
from python_speech_features import logfbank
import pickle
import random
# %%
def estCris(model, chemin):
(rate,sig) = wavfile.read(chemin)
x = mfcc(sig,rate,len(sig)/rate)[0]
x = x.reshape(1,len(x))
#print(model.predict_proba(x))
return int(model.predict(x)[0][0])
def metrique(model):
files = os.listdir('input/metrique/negatif')
n_neg = len(files)
n_bonnePrediction_neg = n_neg
for i in range(len(files)):
n_bonnePrediction_neg = n_bonnePrediction_neg + estCris(model, 'input/metrique/negatif/'+files[i])
print("taux neg:" + str(n_bonnePrediction_neg/n_neg))
files = os.listdir('input/metrique/positif')
n_pos = len(files)
n_bonnePrediction_pos = 0
for i in range(len(files)):
n_bonnePrediction_pos = n_bonnePrediction_pos + estCris(model, 'input/metrique/positif/'+files[i])
print("taux neg:" + str(n_bonnePrediction_pos/n_pos))
return (n_bonnePrediction_neg+n_bonnePrediction_pos)/(n_neg+n_pos)*100
def changerSamples(n_neg,n_pos):
#On remet les fichiers que l'on utilise plus à leur place
files = os.listdir('input/metrique/negatif')
for i in range(len(files)):
os.rename('input/metrique/negatif/' + files[i], "input/negatif/" + files[i])
files = os.listdir('input/metrique/positif')
for i in range(len(files)):
os.rename('input/metrique/positif/' + files[i], "input/positif/" + files[i])
#On choisit des samples au hasard
files = os.listdir('input/negatif')
for i in range(n_neg):
k = random.randint(0, len(files)-1)
os.rename('input/negatif/' + files[k], "input/metrique/negatif/" + files[k])
files.pop(k)
files = os.listdir('input/positif')
for i in range(n_pos):
k = random.randint(0, len(files)-1)
os.rename('input/positif/' + files[k], "input/metrique/positif/" + files[k])
files.pop(k)
# %%
#On charge le modele
filename = 'saves/finalized_model.sav'
model = pickle.load(open(filename, 'rb'))
print(str(int(np.round(metrique(model)))) +"%")
# %%
#Permet d'avoir un oeil neuf de temps en temps
changerSamples(50,50)
# %%
......@@ -3,6 +3,7 @@
#%%
import numpy as np
from scipy.io import wavfile
from playsound import playsound
import os
import pandas as pd
from python_speech_features import mfcc
......@@ -11,11 +12,15 @@ from python_speech_features import logfbank
import pickle
# %%
def ecouter(nom,sampleRate, data):
wavfile.write(nom, sampleRate, data)
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)
print(model.predict_proba(x))
return model.predict(x)[0]
# %%
filename = 'saves/finalized_model.sav'
......@@ -24,3 +29,18 @@ print(estCris(model, "input/tests/cri0.wav"))
print(estCris(model, "input/tests/cri2.wav"))
print(estCris(model, "input/tests/cri18.wav"))
# %%
chemin = "input/files/"
nomFichier = "10_"
extensionFichier = ".wav"
rate, sig = wavfile.read(chemin + nomFichier + extensionFichier)
sig=sig[:,0]
for k in range(int(np.round(len(sig)/rate))-1):
ecouter("rec"+str(k)+extensionFichier, rate, sig[(k)*rate:(k+1)*rate])
if(estCris(model, "rec"+str(k)+extensionFichier)==1):
os.rename("rec"+str(k)+extensionFichier, "input/tests/positif/"+nomFichier+str(k)+extensionFichier)
else:
os.rename("rec"+str(k)+extensionFichier, "input/tests/negatif/"+nomFichier+str(k)+extensionFichier)
# %%
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