NoteSequence Protobuf解码错误

时间:2016-06-09 19:20:03

标签: tensorflow magenta

我正在尝试将NoteSequence文件导入Magenta并且正在获取 '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

如何检索预期的protobufs?

1 个答案:

答案 0 :(得分:0)

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