-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
57 lines (45 loc) · 1.56 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
import os
import sys
import joblib
import numpy as np
import sounddevice as sd
import scipy.io.wavfile as wav
from shared.manhuw import extract_features, load, preprocess_audio
def record_audio(duration, fs=22050):
print(f'Recording for {duration} seconds...')
recording = sd.rec(int(duration * fs), samplerate=fs, channels=1)
sd.wait()
return recording
def save_recording(recording, filename, fs=22050):
wav.write(filename, fs, recording)
# Load the model through joblib
model_filename = input('Enter model filename: ')
model_path = os.path.join(os.getcwd(), model_filename)
if not os.path.isfile(model_path):
sys.exit('Invalid model file')
model = joblib.load(model_path)
print('Loaded model')
# Record and save the recording
recording_duration = 20
sample_rate = 22050
recording_filename='recording.wav'
recording = record_audio(duration=recording_duration, fs=sample_rate)
save_recording(recording, filename=recording_filename, fs=sample_rate)
print('Stop recording...')
# Load recording
print('Loading recording...')
audio_data, sr = load(recording_filename)
# Preprocess data
print('Preprocessing recording...')
audio_data, sr = preprocess_audio(audio_data, sr)
save_recording(audio_data, filename=recording_filename, fs=sr)
# Extract features from recording
print('Extracting features...')
features = extract_features(audio_data, sr)
# Reshape the features array
features = np.array(features).reshape(1, -1)
# Predict using the model
print('Predicting...')
prediction = model.predict(features)
print(f"Predicted Reciter: {prediction[0]}")
print(prediction)