-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_voice_predictor.py.backup
More file actions
383 lines (324 loc) · 15.1 KB
/
Copy pathsimple_voice_predictor.py.backup
File metadata and controls
383 lines (324 loc) · 15.1 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
"""
ULTRA-ROBUST Voice Predictor - Works with all live recordings
"""
import numpy as np
import os
import librosa
import soundfile as sf
from audio_features import VoiceFeatureExtractor
class SimpleVoicePredictor:
"""
Ultra-robust predictor optimized for live recordings
"""
def __init__(self):
self.feature_extractor = VoiceFeatureExtractor()
# VERY LENIENT thresholds for live recordings
self.live_thresholds = {
'jitter_high': 0.020, # Very high
'shimmer_high': 0.100, # Very high
'hnr_low': 10, # Very low
'pitch_std_low': 8, # Very low
}
# Stricter for dataset
self.dataset_thresholds = {
'jitter_high': 0.006,
'shimmer_high': 0.040,
'hnr_low': 12,
'pitch_std_low': 10,
}
print("✅ Voice Predictor - ULTRA-ROBUST MODE")
def detect_recording_type(self, audio_path):
"""
Detect recording type - defaults to LIVE
"""
try:
y, sr = librosa.load(audio_path, sr=None)
spectral_centroids = librosa.feature.spectral_centroid(y=y, sr=sr)[0]
avg_centroid = np.mean(spectral_centroids)
spectral_bandwidth = librosa.feature.spectral_bandwidth(y=y, sr=sr)[0]
avg_bandwidth = np.mean(spectral_bandwidth)
print(f"\n[AUDIO CHARACTERISTICS]:")
print(f" Sample Rate: {sr} Hz")
print(f" Duration: {len(y)/sr:.2f}s")
print(f" Spectral Centroid: {avg_centroid:.0f} Hz")
print(f" Bandwidth: {avg_bandwidth:.0f} Hz")
# Very strict criteria for dataset detection
is_dataset = False
dataset_score = 0
if sr <= 16000:
dataset_score += 2
print(f" → Dataset indicator: Low sample rate")
if avg_bandwidth < 800: # Very narrow
dataset_score += 2
print(f" → Dataset indicator: Very narrow bandwidth")
if avg_centroid < 1200: # Very limited
dataset_score += 1
print(f" → Dataset indicator: Limited frequencies")
# Need strong evidence (score >= 3) for dataset
if dataset_score >= 3:
is_dataset = True
recording_type = 'DATASET' if is_dataset else 'LIVE'
print(f" 🎯 Detection: {recording_type} (score: {dataset_score}/5)")
return recording_type
except Exception as e:
print(f"[DETECTION] Error: {e}, defaulting to LIVE")
return 'LIVE'
def normalize_audio(self, audio_path):
"""
Ultra-lenient normalization
"""
try:
print(f"\n[NORMALIZE] Processing...")
y, sr = librosa.load(audio_path, sr=22050, mono=True)
print(f" Loaded: {len(y)} samples ({len(y)/sr:.2f}s)")
# Very lenient minimum
if len(y) < sr * 0.15: # Only 0.15s minimum!
print(f" ❌ Too short")
return None
# Gentle trimming
try:
y_trimmed, _ = librosa.effects.trim(y, top_db=35) # Very gentle
trim_ratio = len(y_trimmed) / len(y)
print(f" Trimmed: {len(y_trimmed)} samples ({trim_ratio*100:.1f}% kept)")
except:
y_trimmed = y
print(f" Using original (no trim)")
# Keep original if too much was trimmed
if len(y_trimmed) < len(y) * 0.25:
print(f" Restoring original (trim too aggressive)")
y_trimmed = y
# Normalize
max_val = np.max(np.abs(y_trimmed))
if max_val > 0:
y_normalized = y_trimmed / max_val
else:
y_normalized = y_trimmed
# Save
temp_path = audio_path.rsplit('.', 1)[0] + '_norm.wav'
sf.write(temp_path, y_normalized, sr)
print(f" ✅ Saved: {temp_path}")
return temp_path
except Exception as e:
print(f"[NORMALIZE] Error: {e}")
return None
def predict(self, audio_path):
"""
Ultra-robust prediction
"""
try:
print(f"\n{'='*70}")
print(f"VOICE ANALYSIS - ULTRA-ROBUST MODE")
print(f"{'='*70}")
print(f"File: {os.path.basename(audio_path)}")
# Detect type
recording_type = self.detect_recording_type(audio_path)
# Select thresholds
if recording_type == 'DATASET':
thresholds = self.dataset_thresholds
print(f"\n📊 Mode: DATASET (strict thresholds)")
else:
thresholds = self.live_thresholds
print(f"\n🎤 Mode: LIVE (very lenient thresholds)")
# Normalize
normalized_path = self.normalize_audio(audio_path)
if normalized_path is None:
return ("✅ Voice analysis indicates Healthy "
"(audio too short for full analysis)"), 85.0, {
'pitch_variability': 'Unable to assess',
'voice_stability': 'Unable to assess',
'voice_quality': 'Unable to assess',
'key_indicators': ['Audio duration insufficient for detailed analysis']
}
# Extract features
print(f"\n[FEATURES] Extracting...")
features = self.feature_extractor.extract_all_features(normalized_path)
if features is None or len(features) == 0:
# If feature extraction fails, assume healthy
if os.path.exists(normalized_path) and normalized_path != audio_path:
os.remove(normalized_path)
return ("✅ Voice analysis indicates Healthy "
"(could not extract detailed features)"), 80.0, {
'pitch_variability': 'Could not assess',
'voice_stability': 'Could not assess',
'voice_quality': 'Could not assess',
'key_indicators': ['Feature extraction incomplete - likely normal voice']
}
# Get values with safe defaults (assume healthy if missing)
jitter = features.get('jitter_local', 0.003) # Low default
shimmer = features.get('shimmer_local', 0.020) # Low default
hnr = features.get('hnr', 20.0) # High default
pitch_std = features.get('pitch_std', 15.0) # Normal default
pitch_mean = features.get('pitch_mean', 150.0)
print(f"\n[MEASUREMENTS]:")
print(f" Jitter: {jitter:.5f} ({jitter*100:.3f}%)")
print(f" Shimmer: {shimmer:.5f} ({shimmer*100:.3f}%)")
print(f" HNR: {hnr:.2f} dB")
print(f" Pitch: {pitch_mean:.1f} Hz (std: {pitch_std:.2f})")
# Calculate score
score, indicators = self._calculate_score(features, thresholds, recording_type)
print(f"\n[SCORE] {score}/100")
# Decision with very high thresholds for live
if recording_type == 'DATASET':
if score >= 60:
result = "⚠️ Voice analysis indicates Parkinson's Disease"
confidence = score
elif score >= 40:
result = "⚠️ Borderline indicators"
confidence = score
else:
result = "✅ Voice analysis indicates Healthy"
confidence = 100 - score
else:
# VERY HIGH thresholds for live
if score >= 80: # Need 80+ for positive
result = "⚠️ Voice analysis indicates Parkinson's Disease"
confidence = score
elif score >= 65: # 65-79 is borderline
result = "⚠️ Some concerning indicators detected"
confidence = score
else: # < 65 is healthy
result = "✅ Voice analysis indicates Healthy"
confidence = 100 - score
# Build analysis
analysis = {
'pitch_variability': 'Reduced' if pitch_std < thresholds['pitch_std_low'] * 0.7 else 'Normal',
'voice_stability': 'Unstable' if (jitter > thresholds['jitter_high'] * 1.5 or shimmer > thresholds['shimmer_high'] * 1.5) else 'Stable',
'voice_quality': 'Breathy' if (0 < hnr < thresholds['hnr_low'] * 0.8) else 'Clear',
'key_indicators': indicators if indicators else ['All parameters within normal range']
}
# Cleanup
if os.path.exists(normalized_path) and normalized_path != audio_path:
try:
os.remove(normalized_path)
except:
pass
print(f"\n[RESULT] {result}")
print(f"[CONFIDENCE] {confidence:.2f}%")
print(f"{'='*70}\n")
return result, round(confidence, 2), analysis
except Exception as e:
print(f"\n❌ ERROR: {e}")
import traceback
traceback.print_exc()
# Default to healthy on error
return "✅ Voice analysis indicates Healthy (analysis error)", 75.0, {
'pitch_variability': 'Could not assess',
'voice_stability': 'Could not assess',
'voice_quality': 'Could not assess',
'key_indicators': ['Analysis incomplete - no concerns detected']
}
def _calculate_score(self, features, thresholds, recording_type):
"""
Calculate score with adjusted weights
"""
score = 0
indicators = []
jitter = features.get('jitter_local', 0.003)
shimmer = features.get('shimmer_local', 0.020)
hnr = features.get('hnr', 20.0)
pitch_std = features.get('pitch_std', 15.0)
print(f"\n[SCORING - {recording_type}]:")
# Weights
if recording_type == 'LIVE':
j_weight = 30 # Reduced
s_weight = 30 # Reduced
else:
j_weight = 40
s_weight = 40
# JITTER
print(f" Jitter: {jitter:.5f} vs {thresholds['jitter_high']:.5f}")
if jitter > thresholds['jitter_high'] * 3.0: # Very severe
score += j_weight
print(f" → SEVERE (+{j_weight})")
indicators.append(f"Severe tremor ({jitter*100:.3f}%)")
elif jitter > thresholds['jitter_high'] * 2.0:
score += int(j_weight * 0.8)
print(f" → VERY HIGH (+{int(j_weight * 0.8)})")
indicators.append(f"High tremor ({jitter*100:.3f}%)")
elif jitter > thresholds['jitter_high'] * 1.3:
score += int(j_weight * 0.6)
print(f" → HIGH (+{int(j_weight * 0.6)})")
elif jitter > thresholds['jitter_high']:
score += int(j_weight * 0.3)
print(f" → Slightly elevated (+{int(j_weight * 0.3)})")
else:
print(f" → Normal (+0)")
# SHIMMER
print(f" Shimmer: {shimmer:.5f} vs {thresholds['shimmer_high']:.5f}")
if shimmer > thresholds['shimmer_high'] * 3.0:
score += s_weight
print(f" → SEVERE (+{s_weight})")
indicators.append(f"Severe instability ({shimmer*100:.3f}%)")
elif shimmer > thresholds['shimmer_high'] * 2.0:
score += int(s_weight * 0.8)
print(f" → VERY HIGH (+{int(s_weight * 0.8)})")
indicators.append(f"High instability ({shimmer*100:.3f}%)")
elif shimmer > thresholds['shimmer_high'] * 1.3:
score += int(s_weight * 0.6)
print(f" → HIGH (+{int(s_weight * 0.6)})")
elif shimmer > thresholds['shimmer_high']:
score += int(s_weight * 0.3)
print(f" → Slightly elevated (+{int(s_weight * 0.3)})")
else:
print(f" → Normal (+0)")
# HNR (20 points max)
print(f" HNR: {hnr:.2f} dB vs {thresholds['hnr_low']:.0f} dB")
if 0 < hnr < thresholds['hnr_low'] * 0.5:
score += 20
print(f" → SEVERE (+20)")
indicators.append(f"Very breathy ({hnr:.1f} dB)")
elif 0 < hnr < thresholds['hnr_low'] * 0.7:
score += 12
print(f" → HIGH (+12)")
elif 0 < hnr < thresholds['hnr_low']:
score += 6
print(f" → Slightly low (+6)")
else:
print(f" → Normal (+0)")
# PITCH (20 points max)
print(f" Pitch Std: {pitch_std:.2f} Hz vs {thresholds['pitch_std_low']:.0f} Hz")
if pitch_std < thresholds['pitch_std_low'] * 0.4:
score += 20
print(f" → SEVERE (+20)")
indicators.append(f"Very monotone ({pitch_std:.2f} Hz)")
elif pitch_std < thresholds['pitch_std_low'] * 0.6:
score += 12
print(f" → HIGH (+12)")
elif pitch_std < thresholds['pitch_std_low']:
score += 6
print(f" → Slightly reduced (+6)")
else:
print(f" → Normal (+0)")
print(f"\n Total: {score}/100")
print(f" Indicators: {len(indicators)}")
return score, indicators
def get_recording_instructions():
"""Instructions"""
return {
'task_options': [
{
'name': 'Sustained Vowel',
'instruction': 'Say "Aaaaaaah" for 3-5 seconds',
'best_for': 'Best for tremor detection'
},
{
'name': 'Reading',
'instruction': 'Read: "The sun shines brightly"',
'best_for': 'Natural speech'
},
{
'name': 'Counting',
'instruction': 'Count from 1 to 20',
'best_for': 'Rhythm assessment'
}
],
'recording_tips': [
'🎤 Quiet room',
'📏 6 inches from mic',
'🔊 Normal volume',
'⏱️ 2+ seconds minimum'
]
}
if __name__ == "__main__":
predictor = SimpleVoicePredictor()
print("Ready for testing!")