metrique.py 2.32 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/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)

# %%