question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to save Spectrogram as image

See original GitHub issue

Is your feature request related to a problem? Please describe. I am currently working with spectrograms of streaming 2-second clips to perform sound event detection. I am working on an embedded device, and have to be able to process those 2 second files in a short time.

Currently I am bottlenecked by plt.savefig() as a way of saving the spectrogram after using librosa.display.specshow(). This operation alone takes ~0.8 seconds, which is around 75% of the end-to-end process (audio loading to detection).

This is the gist of what I’m doing after recording the windows.

Describe the solution you’d like It would be great if librosa.display.specshow() or another function return the raw spectrogram as an image, which can then be saved via cv2/skimage/etc.

Describe alternatives you’ve considered I tried saving the raw data , but the visualization is not as rich and wouldn’t be useful.

I can also increase the window size to be 5 seconds, but that would be detrimental to the accuracy of the detections.

Additional context Related issue which has been closed

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
bmcfeecommented, May 12, 2021

I’m going to close this issue out, as there’s nothing to be done on the librosa side. Our spectrogram code already produces raw matrix data, and if you want to convert that to an image without going through matplotlib, it’s possible but not something we would add as core functionality.

1reaction
turiancommented, May 6, 2021

I wrote some code for this recently for a paper I was publishing. Here’s the relevant code snippet, you can futz around with it. For example I wanted a gray colormap (for color blind readers) but you can change any of the options. I include a larger code sample so you see how you can adjust things:

        fig, ax = plt.subplots(figsize=set_size(0.225, 0.2))

        #plt.axis('off')
        img = librosa.display.specshow(D, x_axis='time',
#                            y_axis='log', hop_length=256, sr=SR,
                            y_axis='hz', hop_length=256, sr=SR,
                            fmax=MAX_HZ, ax=ax, cmap='gray')

        plt.xticks([0,1, 2])
        plt.ylim(30, 4200)
        if ylabels:
            plt.yticks([30, 1000, 2000, 3000, 4000])
        else:
            ax.get_yaxis().set_visible(False)
        ax.set_ylabel(None)
        ax.set_xlabel(None)

        plt.savefig("../output/final/figures/stft-%d-%s-ylabel-%s.pdf" % (f0hz, n_fft, ylabels))

        plt.plot()
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to save a Librosa spectrogram plot as a specific sized ...
Display the spectrogram as img (we can save it here). Save the img using savefig(). Display the image using plt.show() method.
Read more >
How can I save a Librosa spectrogram plot as a specific sized ...
To feed a model with an 'image' of the spectrogram, one should output only the data. This data be stored in any format,...
Read more >
Matlab – Save Spectrogram as an Image in MATLAB - iTecNote
I'm analyzing some sound clips using the spectrogram() function in MATLAB. I would like to save the spectrogram as an image (jpg, png,...
Read more >
How to train a FCNN with Spectrogram images?
Spectrograms will work with any network that can operate on images. A spectrogram, however, is not an image, and many image techniques will ......
Read more >
How to save spectrogram? - MATLAB Answers - MathWorks
Kindly tell me, how I can save the spectrogram without any labels, ticks or surrounding frame area as a JPEG image in my...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found