404Brain-Not-Found-yeah
commited on
Upload predict.py
Browse files- predict.py +87 -0
predict.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import os
|
3 |
+
import logging
|
4 |
+
import soundfile as sf
|
5 |
+
import librosa
|
6 |
+
import traceback
|
7 |
+
|
8 |
+
logging.basicConfig(level=logging.DEBUG)
|
9 |
+
logger = logging.getLogger(__name__)
|
10 |
+
|
11 |
+
def extract_features(file_path):
|
12 |
+
"""Extract audio features from a file."""
|
13 |
+
try:
|
14 |
+
logger.info(f"Starting feature extraction for: {file_path}")
|
15 |
+
|
16 |
+
# Verify file exists
|
17 |
+
if not os.path.exists(file_path):
|
18 |
+
logger.error(f"File does not exist: {file_path}")
|
19 |
+
return None
|
20 |
+
|
21 |
+
# Verify file format
|
22 |
+
try:
|
23 |
+
with sf.SoundFile(file_path) as sf_file:
|
24 |
+
logger.info(f"Audio file info: {sf_file.samplerate}Hz, {sf_file.channels} channels")
|
25 |
+
except Exception as e:
|
26 |
+
logger.error(f"Error reading audio file with soundfile: {str(e)}\n{traceback.format_exc()}")
|
27 |
+
return None
|
28 |
+
|
29 |
+
# Load audio file with error handling
|
30 |
+
try:
|
31 |
+
logger.info("Loading audio file...")
|
32 |
+
y, sr = librosa.load(file_path, duration=30, sr=None)
|
33 |
+
if len(y) == 0:
|
34 |
+
logger.error("Audio file is empty")
|
35 |
+
return None
|
36 |
+
logger.info(f"Successfully loaded audio: {len(y)} samples, {sr}Hz sample rate")
|
37 |
+
except Exception as e:
|
38 |
+
logger.error(f"Error loading audio: {str(e)}\n{traceback.format_exc()}")
|
39 |
+
return None
|
40 |
+
|
41 |
+
# Ensure minimum duration
|
42 |
+
duration = len(y) / sr
|
43 |
+
logger.info(f"Audio duration: {duration:.2f} seconds")
|
44 |
+
if duration < 1.0:
|
45 |
+
logger.error("Audio file is too short (less than 1 second)")
|
46 |
+
return None
|
47 |
+
|
48 |
+
features_dict = {}
|
49 |
+
|
50 |
+
try:
|
51 |
+
# 1. MFCC (13 features x 2 = 26)
|
52 |
+
logger.info("Extracting MFCC features...")
|
53 |
+
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
|
54 |
+
features_dict['mfccs_mean'] = np.mean(mfccs, axis=1)
|
55 |
+
features_dict['mfccs_var'] = np.var(mfccs, axis=1)
|
56 |
+
logger.info(f"MFCC features shape: {mfccs.shape}")
|
57 |
+
except Exception as e:
|
58 |
+
logger.error(f"Error extracting MFCC: {str(e)}\n{traceback.format_exc()}")
|
59 |
+
return None
|
60 |
+
|
61 |
+
try:
|
62 |
+
# 2. Chroma Features
|
63 |
+
logger.info("Extracting chroma features...")
|
64 |
+
chroma = librosa.feature.chroma_stft(y=y, sr=sr)
|
65 |
+
features_dict['chroma'] = np.mean(chroma, axis=1)
|
66 |
+
logger.info(f"Chroma features shape: {chroma.shape}")
|
67 |
+
except Exception as e:
|
68 |
+
logger.error(f"Error extracting chroma features: {str(e)}\n{traceback.format_exc()}")
|
69 |
+
return None
|
70 |
+
|
71 |
+
# Combine all features
|
72 |
+
try:
|
73 |
+
logger.info("Combining features...")
|
74 |
+
features = np.concatenate([
|
75 |
+
features_dict['mfccs_mean'],
|
76 |
+
features_dict['mfccs_var'],
|
77 |
+
features_dict['chroma']
|
78 |
+
])
|
79 |
+
logger.info(f"Final feature vector shape: {features.shape}")
|
80 |
+
return features
|
81 |
+
except Exception as e:
|
82 |
+
logger.error(f"Error combining features: {str(e)}\n{traceback.format_exc()}")
|
83 |
+
return None
|
84 |
+
|
85 |
+
except Exception as e:
|
86 |
+
logger.error(f"Unexpected error in feature extraction: {str(e)}\n{traceback.format_exc()}")
|
87 |
+
return None
|