File size: 3,597 Bytes
94baaee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
#import zipfile
#zip_ref = zipfile.ZipFile("dataset/train.zip.zip")
#zip_ref.extractall()
#zip_ref.close()

import os
import argparse 
import numpy as np
from scipy.io import wavfile 
from hmmlearn import hmm
import librosa
import pickle
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile 
from librosa.feature import mfcc

class HMMTrainer(object):
    def __init__(self, model_name='GaussianHMM', n_components=4, cov_type='diag', n_iter=1000):
        self.model_name = model_name
        self.n_components = n_components
        self.cov_type = cov_type
        self.n_iter = n_iter
        self.models = []

        if self.model_name == 'GaussianHMM':
            self.model = hmm.GaussianHMM(n_components=self.n_components, 
                    covariance_type=self.cov_type, n_iter=self.n_iter)
        else:
            raise TypeError('Invalid model type')

    # X is a 2D numpy array where each row is 13D
    def train(self, X):
        np.seterr(all='ignore')
        self.models.append(self.model.fit(X))

    # Run the model on input data
    def get_score(self, input_data):
        return self.model.score(input_data)

input_folder='aplikasi\dataset'

os.listdir(input_folder)

for dirname in os.listdir(input_folder):
  # Get the name of the subfolder 
  subfolder = os.path.join(input_folder, dirname)
  #print(subfolder)
  label = subfolder[subfolder.rfind('/') + 1:]
  print(label)

sampling_freq, audio = librosa.load("dataset\kata_benda\College_fazrin.wav")

mfcc_features = mfcc(sampling_freq,audio)

print('\nNumber of windows =', mfcc_features.shape[0])
print('Length of each feature =', mfcc_features.shape[1])

mfcc_features = mfcc_features.T
plt.matshow(mfcc_features)
plt.title('MFCC')

hmm_models = []
for dirname in os.listdir(input_folder):
    subfolder = os.path.join(input_folder, dirname)
    if not os.path.isdir(subfolder): 
         continue
    label = subfolder[subfolder.rfind('/') + 1:]
    X = np.array([])
    y_words = []
for filename in [x for x in os.listdir(subfolder) if x.endswith('.wav')][:-1]:
            filepath = os.path.join(subfolder, filename)
            sampling_freq, audio = librosa.load(filepath)            
            mfcc_features = mfcc(sampling_freq, audio)
            if len(X) == 0:
                X = mfcc_features[:,:15]
            else:
                X = np.append(X, mfcc_features[:,:15], axis=0)            
            y_words.append(label)
print('X.shape =', X.shape)
hmm_trainer = HMMTrainer()
hmm_trainer.train(X)
hmm_models.append((hmm_trainer, label))
hmm_trainer = None

# Test files
input_files = [
            'dataset\kata_benda\College_fazrin.wav',
            'dataset\kata_kerja\Don_t worry about it_ummu.wav',
            'dataset\kata_keterangan\OAF_goose_fear.wav',
            'dataset\kata_sifat\Difficult_fazrin.wav'
            ]

for input_file in input_files:
      sampling_freq, audio = librosa.load(input_file)

# Extract MFCC features
      mfcc_features = mfcc(sampling_freq, audio)
      mfcc_features=mfcc_features[:,:15]

      scores=[]
      for item in hmm_models:
          hmm_model, label = item
            
          score = hmm_model.get_score(mfcc_features)
          scores.append(score)
      index=np.array(scores).argmax()
      
      print("\nTrue:", input_file[input_file.find('/')+1:input_file.rfind('/')])
      print("Predicted:", hmm_models[index][1])

print(f'result : { hmm_models[index][1]}')

# Simpan model dalam bentuk format pkl (pickle)
with open('prolove.pkl', 'wb') as r:
  pickle.dump(hmm_models,r)