我正在尝试将 NoteSequence 文件导入洋红色并得到'google.protobuf.message.DecodeError: 意外的结束组标记。
in.seq
id: "/id/midi/tmp/3d8d5785f488ffd8875f10a12858c6f6c2152068"
filename: "example.mid"
collection_name: "tmp"
ticks_per_beat: 220
time_signatures {
numerator: 4
denominator: 4
}
key_signatures {
}
tempos {
bpm: 240.0
}
notes {
pitch: 60
velocity: 100
end_time: 0.2375
}
notes {
pitch: 62
velocity: 100
start_time: 0.25
end_time: 0.4875
}
notes {
pitch: 64
velocity: 100
start_time: 0.5
end_time: 0.7375
}
notes {
pitch: 65
velocity: 100
start_time: 0.75
end_time: 0.9875
}
notes {
pitch: 67
velocity: 100
start_time: 1.0
end_time: 1.2375
}
notes {
pitch: 69
velocity: 100
start_time: 1.25
end_time: 1.4875
}
notes {
pitch: 71
velocity: 100
start_time: 1.5
end_time: 1.7375
}
notes {
pitch: 72
velocity: 100
start_time: 1.75
end_time: 1.9875
}
total_time: 1.9875
get_seq.py
from os import path
import sys
import os
sys.path.append( path.relpath("../../bazel-out/local-fastbuild/bin/magenta/convert_midi_dir_to_note_sequences.runfiles/") )
import midi_io
import note_sequence_io
import pretty_midi
import protobuf
import tensorflow as tf
file_path = path.relpath("../../out/conv/in.seq")
sys.path.append( path.relpath("../../bazel-genfiles/magenta/protobuf") )
import music_pb2
def read_file_generator(filename):
f = open(filename, 'r')
for line in f:
yield line.rstrip('n')
f.close()
generator = read_file_generator(file_path)
for i in generator:
print music_pb2.NoteSequence.FromString(i)
这应该打印如下行:
D451 3640 21D7 BAD4 089D E736 4038 0140
如何检索预期的原型?
FromString 需要一个序列化的 protobuf,而不是 ASCII 格式的 protobuf。您可以使用如下函数从 ASCII 解析:
from google.protobuf import text_format
def parse_test_proto(proto_type, proto_string):
instance = proto_type()
text_format.Merge(proto_string, instance)
return instance