gradio/demo/spectogram.py

23 lines
529 B
Python
Raw Normal View History

2020-08-22 05:20:05 +08:00
# Demo: (Audio) -> (Image)
2020-08-20 19:57:07 +08:00
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
from scipy import signal
2020-08-22 05:20:05 +08:00
def spectrogram(audio):
2020-08-20 19:57:07 +08:00
sr, data = audio
2020-09-23 05:18:40 +08:00
if len(data.shape) == 2:
data = np.mean(data, axis=0)
frequencies, times, spectrogram_data = signal.spectrogram(data, sr, window="hamming")
2020-08-22 05:20:05 +08:00
plt.pcolormesh(times, frequencies, np.log10(spectrogram_data))
2020-08-20 19:57:07 +08:00
return plt
2020-11-11 22:15:53 +08:00
iface = gr.Interface(spectrogram, "audio", "plot")
2020-08-28 23:56:03 +08:00
2020-11-11 22:15:53 +08:00
iface.test_launch()
if __name__ == "__main__":
iface.launch()