-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict.py
165 lines (118 loc) · 3.7 KB
/
predict.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
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
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import pyaudio
import os
import wave
import librosa
import numpy as np
from sys import byteorder
from array import array
from struct import pack
from time import sleep
from train import modelcreate
from preparedata import features
THRESHOLD = 500
CHUNK_SIZE = 1024
FORMAT = pyaudio.paInt16
RATE = 16000
SILENCE = 30
def is_silent(snd_data):
return max(snd_data) < THRESHOLD
def normalize(snd_data):
MAXIMUM = 16384
times = float(MAXIMUM)/max(abs(i) for i in snd_data)
r = array('h')
for i in snd_data:
r.append(int(i*times))
return r
def trim(snd_data):
def _trim(snd_data):
snd_started = False
r = array('h')
for i in snd_data:
if not snd_started and abs(i)>THRESHOLD:
snd_started = True
r.append(i)
elif snd_started:
r.append(i)
return r
snd_data = _trim(snd_data)
snd_data.reverse()
snd_data = _trim(snd_data)
snd_data.reverse()
return snd_data
def add_silence(snd_data, seconds):
r = array('h', [0 for i in range(int(seconds*RATE))])
r.extend(snd_data)
r.extend([0 for i in range(int(seconds*RATE))])
return r
def record():
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT, channels=1, rate=RATE, input=True, output=True, frames_per_buffer=CHUNK_SIZE)
num_silent = 0
snd_started = False
r = array('h')
while 1:
snd_data = array('h', stream.read(CHUNK_SIZE))
if byteorder == 'big':
snd_data.byteswap()
r.extend(snd_data)
silent = is_silent(snd_data)
if silent and snd_started:
num_silent += 1
elif not silent and not snd_started:
snd_started = True
if snd_started and num_silent > SILENCE:
break
sample_width = p.get_sample_size(FORMAT)
stream.stop_stream()
stream.close()
p.terminate()
r = normalize(r)
r = trim(r)
r = add_silence(r, 0.5)
return sample_width, r
def record_to_file(path):
sample_width, data = record()
data = pack('<' + ('h'*len(data)), *data)
wf = wave.open(path, 'wb')
wf.setnchannels(1)
wf.setsampwidth(sample_width)
wf.setframerate(RATE)
wf.writeframes(data)
wf.close()
def main(new=True, delfile=True, file='test.wav'):
model = modelcreate()
model.load_weights('Results/model.h5')
file = os.path.join('Recordings', file)
if new:
print('Listening..')
record_to_file(file)
predfeat = features(file).reshape(1, -1)
male_prob = model.predict(predfeat)[0][0]
female_prob = 1 - male_prob
gender = 'male' if male_prob > female_prob else 'female'
if delfile:
os.remove(file)
return gender, female_prob, male_prob
if __name__ == '__main__':
model = modelcreate()
model.load_weights('Results/model.h5')
file = 'test.wav'
new = input('Do you want to test new ? ')
if new == 'y':
os.remove(file)
print('Listening..')
record_to_file(file)
elif new == 'n':
if not os.path.isfile(file):
print('No existing file found, record now.')
sleep(1)
print('Listening..')
record_to_file(file)
predfeat = features(file).reshape(1, -1)
male_prob = model.predict(predfeat)[0][0]
female_prob = 1 - male_prob
gender = 'male' if male_prob > female_prob else 'female'
print('Result:', gender)
print(f'Male: {male_prob * 100:.2f}% \nFemale: {female_prob * 100:.2f}%')