fffiloni commited on
Commit
99578a5
1 Parent(s): ae4e904

implement spectrogram method

Browse files
Files changed (1) hide show
  1. app.py +25 -2
app.py CHANGED
@@ -58,6 +58,26 @@ def check_outputs_folder(folder_path):
58
  else:
59
  print(f'The folder {folder_path} does not exist.')
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  def infer(video_in):
62
 
63
  # check if 'outputs' dir exists and empty it if necessary
@@ -92,7 +112,9 @@ def infer(video_in):
92
  print_directory_contents('./outputs/tmp')
93
  wave_files = get_wav_files('./outputs/tmp')
94
  print(wave_files)
95
- return wave_files[0]
 
 
96
 
97
  with gr.Blocks() as demo:
98
  with gr.Column(elem_id="col-container"):
@@ -100,11 +122,12 @@ with gr.Blocks() as demo:
100
  video_in = gr.Video(label='Video IN')
101
  submit_btn = gr.Button("Submit")
102
  output_sound = gr.Audio(label="Audio OUT")
 
103
  #output_sound = gr.Textbox(label="Audio OUT")
104
  submit_btn.click(
105
  fn = infer,
106
  inputs = [video_in],
107
- outputs = [output_sound],
108
  show_api = False
109
  )
110
  demo.launch(show_api=False, show_error=True)
 
58
  else:
59
  print(f'The folder {folder_path} does not exist.')
60
 
61
+ def plot_spectrogram(wav_file, output_image):
62
+ # Read the WAV file
63
+ sample_rate, audio_data = wavfile.read(wav_file)
64
+
65
+ # Check if audio_data is stereo (2 channels) and convert it to mono (1 channel) if needed
66
+ if len(audio_data.shape) == 2:
67
+ audio_data = audio_data.mean(axis=1)
68
+
69
+ # Create a plot for the spectrogram
70
+ plt.figure(figsize=(10, 4))
71
+ plt.specgram(audio_data, Fs=sample_rate, NFFT=1024, noverlap=512, cmap='inferno', aspect='auto')
72
+ plt.title('Spectrogram')
73
+ plt.xlabel('Time [s]')
74
+ plt.ylabel('Frequency [Hz]')
75
+
76
+ # Save the plot as an image file
77
+ plt.colorbar(label='Intensity [dB]')
78
+ plt.savefig(output_image)
79
+ plt.close()
80
+
81
  def infer(video_in):
82
 
83
  # check if 'outputs' dir exists and empty it if necessary
 
112
  print_directory_contents('./outputs/tmp')
113
  wave_files = get_wav_files('./outputs/tmp')
114
  print(wave_files)
115
+ plot_spectrogram(wave_files[0], 'spectrogram.png')
116
+
117
+ return wave_files[0], 'spectrogram.png'
118
 
119
  with gr.Blocks() as demo:
120
  with gr.Column(elem_id="col-container"):
 
122
  video_in = gr.Video(label='Video IN')
123
  submit_btn = gr.Button("Submit")
124
  output_sound = gr.Audio(label="Audio OUT")
125
+ output_spectrogram = gr.Image(label='Spectrogram')
126
  #output_sound = gr.Textbox(label="Audio OUT")
127
  submit_btn.click(
128
  fn = infer,
129
  inputs = [video_in],
130
+ outputs = [output_sound, output_spectrogram],
131
  show_api = False
132
  )
133
  demo.launch(show_api=False, show_error=True)