forked from WaliMuhammadAhmad/AIVoice-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredict.py
More file actions
54 lines (43 loc) · 1.87 KB
/
predict.py
File metadata and controls
54 lines (43 loc) · 1.87 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
import os
import librosa
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
THRESHOLD = 0.5
def extract_mfcc_features(audio_path, n_mfcc=13, n_fft=2048, hop_length=512):
try:
audio_data, sr = librosa.load(audio_path, sr=None)
except Exception as e:
print(f"Error loading audio file {audio_path}: {e}")
return None
mfccs = librosa.feature.mfcc(y=audio_data, sr=sr, n_mfcc=n_mfcc, n_fft=n_fft, hop_length=hop_length)
return np.mean(mfccs.T, axis=0)
def analyze_audio(input_audio_path):
model = tf.keras.models.load_model("model.keras")
print("Model loaded...")
if not os.path.exists(input_audio_path):
print("Error: The specified file does not exist.")
elif not input_audio_path.lower().endswith(".wav"):
print("Error: The specified file is not a .wav file.")
print("Analyzing audio file...")
mfcc_features = extract_mfcc_features(input_audio_path)
print(mfcc_features)
if mfcc_features is not None:
scaler = StandardScaler()
mfcc_features_scaled = scaler.fit_transform(mfcc_features.reshape(1, -1))
print(mfcc_features_scaled)
prediction = model.predict(mfcc_features_scaled)
os.remove(input_audio_path)
print(prediction)
if prediction[0][0] < THRESHOLD:
print("The input audio is real.") # print also bcz return is not shown on terminal
return "The input audio is real."
else:
print("The input audio is fake.")
return "The input audio is fake."
else:
print("Error: Unable to process the input audio.")
return "Error: Unable to process the input audio."
if __name__ == "__main__":
input_file = input("Enter the path of the .wav file to analyze: ")
analyze_audio(input_file)