-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassify.py
88 lines (70 loc) · 2.97 KB
/
classify.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
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
import os
import mmap
import numpy
import librosa
import cPickle
import argparse
import contextlib
from keras.models import load_model
def parseArguments():
parser = argparse.ArgumentParser(prog='PROG')
parser.add_argument('-i' , '--input_name' , required=True, help="input map name")
parser.add_argument('-o' , '--output_name' , required=True, help="output map name")
parser.add_argument('-m' , '--model' , required=True, help="model to use [MEC, ASC, PMLDC, CGDLC, ITC]")
parser.add_argument('-s' , '--sampling_rate' , required=True, help="audio_sampling rate", type=int)
parser.add_argument('-b' , '--bits' , default=32 , help="sample bit depth" , type=int)
parser.add_argument('-c' , '--channels' , default=1 , help="audio channels" , type=int)
args = parser.parse_args()
model = args.model
if model not in ["MEC", "ASC", "PMLDC", "CGDLC", "ITC"]:
parser.error("argument -m/--model must be one of the following [MEC, ASC, PMLDC, CGDLC, ITC]")
args.model = os.path.join("model", model)
return args
if __name__ == '__main__':
args = parseArguments()
bits = args.bits
model_path = args.model
input_name = args.input_name
output_name = args.output_name
audio_channels = args.channels
sampling_rate = args.sampling_rate
# Bytes to read from buffer
bytes_aquired = sampling_rate*bits/8
y = []
audio_buffer = []
# Open and read input map
# This version only works on Windows
input_map = mmap.mmap(-1, bytes_aquired, input_name)
audio_buffer = input_map.read(bytes_aquired)
input_map.close()
if len(audio_buffer) > 0:
# Load data to numpy array
data = numpy.fromstring(audio_buffer, numpy.float32)
#print data.shape
Fs = sampling_rate
for chn in xrange(audio_channels):
y.append(data[chn::audio_channels])
y = numpy.array(y).T
if y.ndim == 2:
if y.shape[1] == 1:
y = y.flatten()
elif y.shape[1] == 2:
y = (y[:,1] / 2) + (y[:,0] / 2)
else:
print("Too many channels")
exit(0)
# Load CNN model
model = load_model(model_path)
# Extract spectogram
specgram = librosa.feature.melspectrogram(y=y, sr=Fs)
specgram = librosa.power_to_db(specgram, ref=numpy.max)
specgram = specgram.reshape(1, 128, 313, 1)
prediction = model.predict(specgram)
result = prediction[0][0]
#Uncomment to print result
print(class_names[int(result)], round(result, 2))
# Write results to map
# Writing with name only works on Windows
output_map = mmap.mmap(-1, 15, output_name)
output_map.write(class_names[int(result)] + ", " + str(round(result, 2)))