-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinal1.py
77 lines (67 loc) · 2.1 KB
/
final1.py
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
74
75
76
77
import python_speech_features
from python_speech_features import mfcc
from python_speech_features import logfbank
from python_speech_features import base
from matplotlib import pyplot as plt
import scipy.io.wavfile as wav
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import train_test_split
import pickle
import pandas as pd
from sklearn.preprocessing import LabelEncoder
import struct
import wave
def featuresplot(sig,rate,typo):
m = mfcc(sig,rate)
fbank_feat = logfbank(sig,rate)
mlst = []
for i in range(0, len(m)):
l = m[0:4]
mlst.append(m[i][2])
m=[]
m.append(np.mean(mlst))
clst=[]
for i in range(0, len(fbank_feat)):
l = m[0:4]
clst.append(np.mean(fbank_feat[i]))
c=[]
c.append(np.mean(clst))
plt.plot(m,c, typo)
return m[0],c[0]
with open ('sample_data.pkl','rb') as pickle_file:
dataset = pickle.load(pickle_file)
dataset = np.array(dataset)
le = LabelEncoder()
data = pd.DataFrame(dataset)
X, Y = data.iloc[:,1:], dataset[:,0]
Y = le.fit_transform(Y)
#print le.classes_
#print Y[:5]
#print (X.shape, Y.shape)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state = 42)
#print (X_train.shape, y_train.shape)
clf = svm.SVC(kernel = 'linear', C = 3.0, gamma = 10)
clf.fit(X_train, y_train)
#acc = clf.score(X_test, y_test)
#print str(acc*100)+"% accuracy"
print (clf.score(X_train, y_train))
print (clf.score(X_test, y_test))
lst = ['flute','piano','trumpet','violin']
file= 'piano/sample_'+'10'+'.wav'
audio_file = wave.open(file)
length = audio_file.getnframes()
signal = np.zeros(length)
for i in range (0,length):
data = audio_file.readframes(1)
data = struct.unpack("<h", data)
signal[i] = int(data[0])
rate = audio_file.getframerate()
signal = np.divide(signal, float(2**15))
#typo = '.g'
#m,c = featuresplot(signal, rate, typo)
#print (m,c)
#instrument = clf.predict([[m , c]])
#print (lst[instrument[0]])