python - 如何将频谱图图像与其人工标记的数据结合起来,在 Python 中使用 CNN 进行处理?
问题描述
我正在校园里做一个最后的项目:使用 CNN 从歌曲中估计音高。
CNN 的输入是一首歌曲的频谱图,由 生成plt.specgram()
,大小为 334 x 217。歌曲数据集取自MIR-QBSH,具有以下规范:8 秒持续时间,单声道,8KHz 采样,8 位量化,帧大小 = 256 ,重叠 = 0,第一帧从音频文件的第一个样本开始。
这是频谱图的一个示例:
据我现在了解,我需要数据标签(在我的情况下:音高标签)与 CNN 的频谱图相结合,以便能够处理计算。我的数据标签包含一首歌曲的 250 个音高标签。这些音高标签以半音(MIDI 编号)为单位。
这是上面频谱图的音高标签示例。我已经math.floor()
对原始文件中的这些音高标签做了方法来简化计算。
Pitch values: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 50, 50, 52, 52, 53, 54, 54, 53, 53, 53, 53, 54, 54, 54, 54, 54, 53, 0, 0, 54, 54, 54, 54, 54, 54, 53, 0, 0, 0, 0, 46, 46, 46, 47, 48, 48, 48, 48, 48, 49, 49, 49, 50, 50, 50, 50, 0, 0, 0, 50, 50, 50, 50, 50, 50, 50, 49, 0, 0, 51, 47, 47, 47, 47, 47, 47, 47, 47, 48, 49, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 58, 57, 58, 58, 58, 58, 58, 57, 57, 57, 57, 57, 57, 57, 58, 58, 57, 57, 57, 57, 56, 55, 55, 56, 56, 56, 56, 56, 55, 56, 56, 56, 56, 55, 55, 55, 56, 56, 54, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 52, 52, 52, 53, 53, 54, 54, 0, 0, 54, 54, 54, 54, 54, 54, 54, 54, 0, 0, 0, 54, 54, 54, 54, 54, 53, 52, 51, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 49, 49, 49, 49, 50, 50, 50, 50, 49, 0, 0, 0, 50, 49, 49, 49, 49, 49, 50, 50, 49, 0, 0, 47, 47, 47, 48, 48, 48, 48, 0, 0, 0, 0, 0, 0, 0]
我的问题是,在 Python 中的 CNN 处理频谱图之前,我应该如何组合频谱图及其音高标签?
解决方案
我已经解决了我的问题。它是这样工作的:
image_data = []
tm = time.time()
for img_item in os.listdir(image_path): #for every image in path
try:
img_array = cv2.imread(os.path.join(image_path, img_item))
spectrogram_preprocessing = resize_recolor_spectrogram(img_array) # convert image to grayscale and resize it to 250 x 160
# imread to array
spectrogram_preprocessing = np.array(spectrogram_preprocessing)
label = extract_pitch_label(os.path.join(label_path, img_item))
# combining label and image
image_data.append([spectrogram_preprocessing, label])
except Exception as e:
raise e
推荐阅读
- php - GuzzleHttp 尝试在 php 7 安装中将 JSON 字符串解码为数组时出错
- node-red - 修改包含对象数组的有效负载
- python - 我们如何从 python 3 中的字符串中删除所有表情符号值?
- php - 使用 bash,如何根据 PHP 脚本的反馈更改文件名?
- javascript - 保存复选框选择
- java - 如何将 java.util.stream.Stream 写入 StreamingResponseBody 输出流
- java - 从字节数组到图像java
- javascript - 使用 dynmac setInterval 反应 UseEffect
- c++ - 为什么 map.find 使用 < 运算符而不是 == 运算符?
- javascript - 不占空间的置顶