-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassification.py
More file actions
173 lines (149 loc) · 5.29 KB
/
Copy pathclassification.py
File metadata and controls
173 lines (149 loc) · 5.29 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import numpy as np
import scipy.signal
import timeit
from python_speech_features import mfcc
import pygame
import time
from scipy.io import wavfile
import subprocess
import panda as import pdb; pdb.set_trace()
from tflite_runtime.interpreter import interpreter
pygame.init()
display_height = 600
display_width = 600
black = (0,0,0)
alpha = (0,88,255)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
gameDisplay = pygame.display.set_mode((display_width, display_height)
pygame.display.set_caption("Musical instrument recognition system")
gameDisplay.fill(black)
guitarImg = pygame.image.load('./instruments/acoustic_guitar.jpg')
bassImg = pygame.image.load('./instruments/bass_drum.jpg')
celloImg = pygame.image.load('./instruments/cello.jpg')
clarinetImg = pygame.image.load('./instruments/clarinet.jpg')
doubleImg = pygame.image.load('./instruments/double_bass.jpg')
fluteImg = pygame.image.load('./instruments/flute.jpg')
saxImg = pygame.image.load('./instruments/saxophone.jpg')
snareImg = pygame.image.load('./instruments/snare_drum.jpg')
violinImg = pygame.image.load('./instruments/violin.jpg')
hihatImg = pygame.image.load('./instruments/hihat.jpg')
# List of instruments trained
'''
Acoustic Guitar
Bass Drum
Cello
Clarinet
Double Bass
Flute
Hi-Hat
Saxophone
Snare Drum
Violin
'''
# Parameters
sample_rate = 44100
step = 1600
nfeat = 13
nfilt = 26
nfft = 512
minScaler = -71.02
maxScaler = 69.70
model_path = 'sound_model.tflite'
# Load model (interpreter)
interpreter = interpreter(model_path)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf',30)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2),(display_height/2))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
def close():
pygame.quit()
quit()
def text_objects(text, font):
textSurface = font.render(text, True, alpha)
return textSurface, textSurface.get_rect()
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
smallText = pygame.font.SysFont('comicsansms', 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2)),(y+(h/2)))
gameDisplay.blit(textSurf, textRect)
def showInstruments(value):
instruments_images = [guitarImg, bassImg, celloImg, clarinetImg, doubleImg, fluteImg, hihatImg, saxImg, snareImg, violinImg]
gameDisplay.blit(instruments_images[value],(0,0))
def s2t():
intruments = ['0 - Acoustic_guitar','1 - Bass_drum','2 - Cello', '3 - Clarinet','4 - Double_bass','5 - Flute','6 - Hi=hat','7 - Saxophone','8 - Snare_drum','9 - Violin']
rate, wav = wavfile.read('./wavfiles/teste.wav');
y_pred = build_predictions(rate, wav)
value = max(set(y_pred), key = y_pred.count)
print('Prediction: ', instruments[value])
print('y_pred: ',y_pred)
showInstruments(value)
def s4t():
intruments = ['0 - Acoustic_guitar','1 - Bass_drum','2 - Cello', '3 - Clarinet','4 - Double_bass','5 - Flute','6 - Hi=hat','7 - Saxophone','8 - Snare_drum','9 - Violin']
record = 'arecord -d 2 --rate 16000 soundrecognition.wav'
p = subprocess.Popen(record, shell=True)
time.sleep(4)
rate, wav = wavfile.read('./soundrecognition.wav')
y_pred = build_predictions(rate, wav)
value = max(set(y_pred), key = y_pred.count)
print('Prediction: ', instruments[value])
print('y_pred: ',y_pred)
showInstruments(value)
def build_predictions(rate, wav):
mask = envelope(wav, rate, 0.0005)
wav = wav[mask]
y_pred = []
print('Extracting MFCC features...')
y_prob = []
print('Rate: ', rate)
for i in range(0, wav.shape[0] - int(rate/10), int(rate/10)):
sample = wav[i:i+int(rate/10)]
x = mfcc(sample, rate, numcep = nfeat, nfilt = nfilt, nfft = nfft)
x = (x - minScaler) / (maxScaler - minScaler)
x = x.reshape(1, x.shape[0], x.shape[1], 1)
input_shape = input_details[0]['index']
in_tensor = np.float32(x)
interpreter.set_tensor(input_shape, in_tensor)
interpreter.invoke()
y_hat = interpreter.get_tensor(output_details[0]['index'])
y_prob.append(y_hat)
y_pred.append(np.argmax(y_hat))
return y_pred
def envelope(y, rate, threshold):
print('Enveloping the audio signal...')
mask = []
y = pd.Series(y).apply(np.abs)
y_mean = y.rolling(window = int(rate/10), min_periods = 1, center = True).mean()
for mean in y_mean:
if mean > threshold:
mask.append(True)
else:
mask.append(False)
return mask
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT;
pygame.quit()
quit()
button("Abrir", 150,450,100,50,green,bright_green,s2t)
button("Gravar", 550,450,100,50,green,bright_red,s4t)
pygame.display.update()
if __name__ == '__main__':
main()