GraySpectrogram / README.md
mickylan2367's picture
Update README
61dae2a
|
raw
history blame
1.74 kB
---
license: cc-by-sa-4.0
language:
- en
tags:
- music
- spectrogram
size_categories:
- n<1K
---
## Google/MusicCapsをスペクトログラムにしたデータ。
### データの基本情報
<table>
<thead>
<td>画像</td>
<td>caption</td>
<td>data_idx</td>
<td>number</td>
</thead>
<tbody>
<tr>
<td>1025px × 216px</td>
<td>音楽の説明</td>
<td>どのデータから生成されたデータか</td>
<td>5秒ずつ区切ったデータのうち、何番目か</td>
</tr>
</tbody>
</table>
### データ作った方法
* コード:https://colab.research.google.com/drive/13m792FEoXszj72viZuBtusYRUL1z6Cu2?usp=sharing
* 参考にしたKaggle Notebook : https://www.kaggle.com/code/osanseviero/musiccaps-explorer
```python
from PIL import Image
import IPython.display
import cv2
# 1. wavファイルを解析
y, sr = librosa.load("wavファイルなど")
# 2. フーリエ変換を適用して周波数成分を取得
D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max) # librosaを用いてデータを作る
image = Image.fromarray(np.uint8(D), mode='L') # 'L'は1チャンネルのグレースケールモードを指定します
image.save('spectrogram_{}.png')
```
### ♪復元方法
```python
im = Image.open("pngファイル")
db_ud = np.uint8(np.array(im))
amp = librosa.db_to_amplitude(db_ud)
print(amp.shape)
# (1025, 861)は20秒のwavファイルをスペクトログラムにした場合
# (1025, 431)は10秒のwavファイルをスペクトログラムにした場合
# (1025, 216)は5秒のwavファイルをスペクトログラムにした場合
y_inv = librosa.griffinlim(amp*200)
display(IPython.display.Audio(y_inv, rate=sr))
```