csprojectworkspace commited on
Commit
b038422
·
verified ·
1 Parent(s): 08855d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -13
app.py CHANGED
@@ -31,7 +31,10 @@ def predict_vad(audio, sr):
31
  start = (len(audio) - target_length) // 2
32
  audio = audio[start:start + target_length]
33
 
34
- audio = audio / (np.max(np.abs(audio)) + 1e-8)
 
 
 
35
  features = audio.reshape(1, target_length, 1)
36
 
37
  prediction = model.predict(features, verbose=0)
@@ -76,8 +79,8 @@ def analyze():
76
  else:
77
  y_mono = np.mean(y, axis=0)
78
 
79
- n_channels = y.shape[0] if y.ndim > 1 else 1
80
- duration = y.shape[-1] / sr
81
 
82
  vad_label, vad_confidence, pred_class = predict_vad(y_mono, sr)
83
 
@@ -96,23 +99,46 @@ def analyze():
96
  frame_label, frame_conf = vad_label, vad_confidence
97
 
98
  vad_timeline.append({
99
- 'start': round(start_sample / sr, 3),
100
- 'end': round(end_sample / sr, 3),
101
  'label': frame_label,
102
- 'confidence': round(frame_conf, 3)
103
  })
104
 
105
- doa_angle = -25.5 + np.random.randn() * 10
106
- doa_trajectory = [{'time': round(i * (duration/n_frames), 2), 'angle': round(doa_angle + np.random.randn()*3, 1)} for i in range(n_frames)]
 
 
 
107
 
108
- voice_ratio = sum(1 for f in vad_timeline if f['label'] == 'voice') / len(vad_timeline)
 
 
109
 
110
  return jsonify({
111
  'success': True,
112
- 'metadata': {'channels': int(n_channels), 'sampleRate': int(sr), 'duration': round(duration, 2), 'samples': int(y.shape[-1])},
113
- 'vad': {'prediction': vad_label, 'confidence': round(vad_confidence, 3), 'predicted_class': pred_class, 'timeline': vad_timeline},
114
- 'doa': {'angle': round(doa_angle, 1), 'confidence': 0.89, 'trajectory': doa_trajectory},
115
- 'metrics': {'snr': round(10 * np.log10(np.mean(y_mono**2) / 1e-10), 1), 'voiceRatio': round(voice_ratio, 2), 'meanDoa': round(np.mean([d['angle'] for d in doa_trajectory]), 1)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  })
117
  except Exception as e:
118
  import traceback
 
31
  start = (len(audio) - target_length) // 2
32
  audio = audio[start:start + target_length]
33
 
34
+ max_val = np.max(np.abs(audio))
35
+ if max_val > 0:
36
+ audio = audio / max_val
37
+
38
  features = audio.reshape(1, target_length, 1)
39
 
40
  prediction = model.predict(features, verbose=0)
 
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(y.shape[-1] / sr)
84
 
85
  vad_label, vad_confidence, pred_class = predict_vad(y_mono, sr)
86
 
 
99
  frame_label, frame_conf = vad_label, vad_confidence
100
 
101
  vad_timeline.append({
102
+ 'start': float(round(start_sample / sr, 3)),
103
+ 'end': float(round(end_sample / sr, 3)),
104
  'label': frame_label,
105
+ 'confidence': float(round(frame_conf, 3))
106
  })
107
 
108
+ doa_angle = float(-25.5 + np.random.randn() * 10)
109
+ doa_trajectory = [{'time': float(round(i * (duration/n_frames), 2)), 'angle': float(round(doa_angle + np.random.randn()*3, 1))} for i in range(n_frames)]
110
+
111
+ voice_count = sum(1 for f in vad_timeline if f['label'] == 'voice')
112
+ voice_ratio = float(voice_count / len(vad_timeline))
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,
120
+ 'metadata': {
121
+ 'channels': n_channels,
122
+ 'sampleRate': int(sr),
123
+ 'duration': float(round(duration, 2)),
124
+ 'samples': int(y.shape[-1])
125
+ },
126
+ 'vad': {
127
+ 'prediction': vad_label,
128
+ 'confidence': float(round(vad_confidence, 3)),
129
+ 'predicted_class': int(pred_class),
130
+ 'timeline': vad_timeline
131
+ },
132
+ 'doa': {
133
+ 'angle': float(round(doa_angle, 1)),
134
+ 'confidence': 0.89,
135
+ 'trajectory': doa_trajectory
136
+ },
137
+ 'metrics': {
138
+ 'snr': snr,
139
+ 'voiceRatio': float(round(voice_ratio, 2)),
140
+ 'meanDoa': mean_doa
141
+ }
142
  })
143
  except Exception as e:
144
  import traceback