Dimsumcat commited on
Commit
e546b40
·
verified ·
1 Parent(s): 9d19aca

Update breath-server/app.py

Browse files
Files changed (1) hide show
  1. breath-server/app.py +9 -3
breath-server/app.py CHANGED
@@ -19,10 +19,12 @@ input_details = interpreter.get_input_details()
19
  output_details = interpreter.get_output_details()
20
 
21
  def extract_features(y, sr):
 
22
  mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
23
  return np.mean(mfccs.T, axis=0)
24
 
25
  def segment_audio(y, sr, segment_length=2, hop_length=1):
 
26
  frames = []
27
  for start in range(0, len(y) - int(segment_length * sr), int(hop_length * sr)):
28
  end = start + int(segment_length * sr)
@@ -31,6 +33,7 @@ def segment_audio(y, sr, segment_length=2, hop_length=1):
31
  return np.array(frames, dtype=np.float32)
32
 
33
  def predict_periods(interpreter, y, sr, segment_length=2, hop_length=1):
 
34
  frames = segment_audio(y, sr, segment_length, hop_length)
35
 
36
  predictions = []
@@ -44,6 +47,7 @@ def predict_periods(interpreter, y, sr, segment_length=2, hop_length=1):
44
  return predicted_labels
45
 
46
  def get_periods_and_durations(predicted_labels, segment_length=2, hop_length=1):
 
47
  periods = []
48
  durations = []
49
  current_label = None
@@ -66,10 +70,12 @@ def get_periods_and_durations(predicted_labels, segment_length=2, hop_length=1):
66
  return periods, durations
67
 
68
  def process_audio(audio_path):
 
69
  y, sr = librosa.load(audio_path, sr=None)
70
  return y, sr
71
 
72
  def plot_waveform_with_predictions(y, sr, periods, durations, segment_length=2, hop_length=1):
 
73
  # Plot the audio waveform
74
  plt.figure(figsize=(10, 6))
75
  librosa.display.waveshow(y, sr=sr, alpha=0.6, label='Audio Waveform')
@@ -120,6 +126,7 @@ def predict():
120
  print(f'File saved at {file_path}')
121
 
122
  try:
 
123
  y, sr = process_audio(file_path)
124
  predicted_labels = predict_periods(interpreter, y, sr)
125
  periods, durations = get_periods_and_durations(predicted_labels)
@@ -133,11 +140,11 @@ def predict():
133
  # Generate the plot and return it as an image
134
  img_io = plot_waveform_with_predictions(y, sr, periods, durations)
135
 
136
- # Clean up file
137
  os.remove(file_path)
138
  print(f'File removed from {file_path}')
139
 
140
- # Ensure the image is returned with the correct headers
141
  return send_file(
142
  img_io,
143
  mimetype='image/png',
@@ -150,6 +157,5 @@ def predict():
150
  os.remove(file_path) # Clean up file in case of error
151
  return jsonify({'error': str(e)}), 500
152
 
153
-
154
  if __name__ == '__main__':
155
  app.run(host='0.0.0.0', port=5000, debug=True)
 
19
  output_details = interpreter.get_output_details()
20
 
21
  def extract_features(y, sr):
22
+ """Extract MFCC features from the audio."""
23
  mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=13)
24
  return np.mean(mfccs.T, axis=0)
25
 
26
  def segment_audio(y, sr, segment_length=2, hop_length=1):
27
+ """Segment audio into frames and extract features."""
28
  frames = []
29
  for start in range(0, len(y) - int(segment_length * sr), int(hop_length * sr)):
30
  end = start + int(segment_length * sr)
 
33
  return np.array(frames, dtype=np.float32)
34
 
35
  def predict_periods(interpreter, y, sr, segment_length=2, hop_length=1):
36
+ """Predict inhale/exhale periods based on audio features."""
37
  frames = segment_audio(y, sr, segment_length, hop_length)
38
 
39
  predictions = []
 
47
  return predicted_labels
48
 
49
  def get_periods_and_durations(predicted_labels, segment_length=2, hop_length=1):
50
+ """Determine the inhale/exhale periods and their durations."""
51
  periods = []
52
  durations = []
53
  current_label = None
 
70
  return periods, durations
71
 
72
  def process_audio(audio_path):
73
+ """Process the uploaded audio file using librosa."""
74
  y, sr = librosa.load(audio_path, sr=None)
75
  return y, sr
76
 
77
  def plot_waveform_with_predictions(y, sr, periods, durations, segment_length=2, hop_length=1):
78
+ """Generate a waveform plot with predicted inhale/exhale periods."""
79
  # Plot the audio waveform
80
  plt.figure(figsize=(10, 6))
81
  librosa.display.waveshow(y, sr=sr, alpha=0.6, label='Audio Waveform')
 
126
  print(f'File saved at {file_path}')
127
 
128
  try:
129
+ # Process the audio and make predictions
130
  y, sr = process_audio(file_path)
131
  predicted_labels = predict_periods(interpreter, y, sr)
132
  periods, durations = get_periods_and_durations(predicted_labels)
 
140
  # Generate the plot and return it as an image
141
  img_io = plot_waveform_with_predictions(y, sr, periods, durations)
142
 
143
+ # Clean up file after processing
144
  os.remove(file_path)
145
  print(f'File removed from {file_path}')
146
 
147
+ # Return the plot as an image
148
  return send_file(
149
  img_io,
150
  mimetype='image/png',
 
157
  os.remove(file_path) # Clean up file in case of error
158
  return jsonify({'error': str(e)}), 500
159
 
 
160
  if __name__ == '__main__':
161
  app.run(host='0.0.0.0', port=5000, debug=True)