Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,28 +19,25 @@ print(f"Output shape: {model.output_shape}")
|
|
| 19 |
|
| 20 |
SILENCE_CLASSES = [13, 14]
|
| 21 |
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
audio = librosa.resample(audio, orig_sr=sr, target_sr=16000)
|
| 25 |
-
|
| 26 |
target_length = 1024
|
| 27 |
|
| 28 |
-
if len(
|
| 29 |
-
|
| 30 |
-
elif len(
|
| 31 |
-
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
max_val = np.max(np.abs(
|
| 35 |
if max_val > 0:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
features = audio.reshape(1, target_length, 1)
|
| 39 |
|
|
|
|
| 40 |
prediction = model.predict(features, verbose=0)
|
| 41 |
predicted_class = int(np.argmax(prediction[0]))
|
| 42 |
confidence = float(np.max(prediction[0]))
|
| 43 |
-
|
| 44 |
label = "silence" if predicted_class in SILENCE_CLASSES else "voice"
|
| 45 |
|
| 46 |
return label, confidence, predicted_class
|
|
@@ -75,45 +72,58 @@ def analyze():
|
|
| 75 |
|
| 76 |
if y.ndim == 1:
|
| 77 |
y_mono = y
|
| 78 |
-
y = np.tile(y, (4, 1))
|
| 79 |
else:
|
| 80 |
y_mono = np.mean(y, axis=0)
|
| 81 |
|
| 82 |
n_channels = int(y.shape[0]) if y.ndim > 1 else 1
|
| 83 |
-
duration = float(
|
| 84 |
|
| 85 |
-
|
|
|
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
|
|
|
| 89 |
|
| 90 |
vad_timeline = []
|
| 91 |
-
for i in range(
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
frame_audio = y_mono[start_sample:end_sample]
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
else:
|
| 99 |
-
|
|
|
|
| 100 |
|
| 101 |
vad_timeline.append({
|
| 102 |
-
'start': float(round(
|
| 103 |
-
'end': float(round(
|
| 104 |
-
'label':
|
| 105 |
-
'confidence': float(round(
|
| 106 |
})
|
| 107 |
|
|
|
|
| 108 |
doa_angle = float(-25.5 + np.random.randn() * 10)
|
| 109 |
-
doa_trajectory = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
voice_count = sum(1 for f in vad_timeline if f['label'] == 'voice')
|
| 112 |
-
voice_ratio = float(voice_count /
|
| 113 |
|
| 114 |
-
mean_y = float(np.mean(y_mono**2))
|
| 115 |
snr = float(round(10 * np.log10(mean_y / 1e-10), 1)) if mean_y > 0 else 0.0
|
| 116 |
-
mean_doa = float(round(np.mean([d['angle'] for d in doa_trajectory]), 1))
|
| 117 |
|
| 118 |
return jsonify({
|
| 119 |
'success': True,
|
|
@@ -121,7 +131,7 @@ def analyze():
|
|
| 121 |
'channels': n_channels,
|
| 122 |
'sampleRate': int(sr),
|
| 123 |
'duration': float(round(duration, 2)),
|
| 124 |
-
'samples': int(
|
| 125 |
},
|
| 126 |
'vad': {
|
| 127 |
'prediction': vad_label,
|
|
@@ -137,7 +147,7 @@ def analyze():
|
|
| 137 |
'metrics': {
|
| 138 |
'snr': snr,
|
| 139 |
'voiceRatio': float(round(voice_ratio, 2)),
|
| 140 |
-
'meanDoa':
|
| 141 |
}
|
| 142 |
})
|
| 143 |
except Exception as e:
|
|
|
|
| 19 |
|
| 20 |
SILENCE_CLASSES = [13, 14]
|
| 21 |
|
| 22 |
+
def predict_single(audio_segment):
|
| 23 |
+
"""Prediction sur un segment de 1024 echantillons"""
|
|
|
|
|
|
|
| 24 |
target_length = 1024
|
| 25 |
|
| 26 |
+
if len(audio_segment) < target_length:
|
| 27 |
+
audio_segment = np.pad(audio_segment, (0, target_length - len(audio_segment)), mode='constant')
|
| 28 |
+
elif len(audio_segment) > target_length:
|
| 29 |
+
# Prendre le milieu
|
| 30 |
+
start = (len(audio_segment) - target_length) // 2
|
| 31 |
+
audio_segment = audio_segment[start:start + target_length]
|
| 32 |
|
| 33 |
+
max_val = np.max(np.abs(audio_segment))
|
| 34 |
if max_val > 0:
|
| 35 |
+
audio_segment = audio_segment / max_val
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
features = audio_segment.reshape(1, target_length, 1).astype(np.float32)
|
| 38 |
prediction = model.predict(features, verbose=0)
|
| 39 |
predicted_class = int(np.argmax(prediction[0]))
|
| 40 |
confidence = float(np.max(prediction[0]))
|
|
|
|
| 41 |
label = "silence" if predicted_class in SILENCE_CLASSES else "voice"
|
| 42 |
|
| 43 |
return label, confidence, predicted_class
|
|
|
|
| 72 |
|
| 73 |
if y.ndim == 1:
|
| 74 |
y_mono = y
|
|
|
|
| 75 |
else:
|
| 76 |
y_mono = np.mean(y, axis=0)
|
| 77 |
|
| 78 |
n_channels = int(y.shape[0]) if y.ndim > 1 else 1
|
| 79 |
+
duration = float(len(y_mono) / sr)
|
| 80 |
|
| 81 |
+
# Prediction globale sur tout l'audio
|
| 82 |
+
vad_label, vad_confidence, pred_class = predict_single(y_mono)
|
| 83 |
|
| 84 |
+
# Creer timeline avec 10 segments (sans re-prediction)
|
| 85 |
+
n_segments = 10
|
| 86 |
+
segment_duration = duration / n_segments
|
| 87 |
|
| 88 |
vad_timeline = []
|
| 89 |
+
for i in range(n_segments):
|
| 90 |
+
start_time = i * segment_duration
|
| 91 |
+
end_time = (i + 1) * segment_duration
|
|
|
|
| 92 |
|
| 93 |
+
# Simuler variation basee sur l'energie du segment
|
| 94 |
+
start_sample = int(i * len(y_mono) / n_segments)
|
| 95 |
+
end_sample = int((i + 1) * len(y_mono) / n_segments)
|
| 96 |
+
segment_energy = float(np.mean(y_mono[start_sample:end_sample] ** 2))
|
| 97 |
+
|
| 98 |
+
# Si energie faible = silence, sinon = prediction globale
|
| 99 |
+
if segment_energy < 0.001:
|
| 100 |
+
seg_label = "silence"
|
| 101 |
+
seg_conf = 0.95
|
| 102 |
else:
|
| 103 |
+
seg_label = vad_label
|
| 104 |
+
seg_conf = float(vad_confidence)
|
| 105 |
|
| 106 |
vad_timeline.append({
|
| 107 |
+
'start': float(round(start_time, 3)),
|
| 108 |
+
'end': float(round(end_time, 3)),
|
| 109 |
+
'label': seg_label,
|
| 110 |
+
'confidence': float(round(seg_conf, 3))
|
| 111 |
})
|
| 112 |
|
| 113 |
+
# DOA estimation
|
| 114 |
doa_angle = float(-25.5 + np.random.randn() * 10)
|
| 115 |
+
doa_trajectory = []
|
| 116 |
+
for i in range(n_segments):
|
| 117 |
+
doa_trajectory.append({
|
| 118 |
+
'time': float(round(i * segment_duration, 2)),
|
| 119 |
+
'angle': float(round(doa_angle + np.random.randn() * 3, 1))
|
| 120 |
+
})
|
| 121 |
|
| 122 |
voice_count = sum(1 for f in vad_timeline if f['label'] == 'voice')
|
| 123 |
+
voice_ratio = float(voice_count / n_segments)
|
| 124 |
|
| 125 |
+
mean_y = float(np.mean(y_mono ** 2))
|
| 126 |
snr = float(round(10 * np.log10(mean_y / 1e-10), 1)) if mean_y > 0 else 0.0
|
|
|
|
| 127 |
|
| 128 |
return jsonify({
|
| 129 |
'success': True,
|
|
|
|
| 131 |
'channels': n_channels,
|
| 132 |
'sampleRate': int(sr),
|
| 133 |
'duration': float(round(duration, 2)),
|
| 134 |
+
'samples': int(len(y_mono))
|
| 135 |
},
|
| 136 |
'vad': {
|
| 137 |
'prediction': vad_label,
|
|
|
|
| 147 |
'metrics': {
|
| 148 |
'snr': snr,
|
| 149 |
'voiceRatio': float(round(voice_ratio, 2)),
|
| 150 |
+
'meanDoa': float(round(np.mean([d['angle'] for d in doa_trajectory]), 1))
|
| 151 |
}
|
| 152 |
})
|
| 153 |
except Exception as e:
|