2016-06-09 86 views
0

我試圖導入一個NoteSequence文件到洋紅色,並得到 'google.protobuf.message.DecodeError:意外的結束組標記。'NoteSequence Protobuf解碼錯誤

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?

+0

此問題不再適用於包含編碼器,旋律和模型的最新版本。我試圖直接導入協議緩衝區,這不是最佳實踐。 –

回答

0

FromString需要一個序列化的protobuf,而不是一個ASCII格式的。您可以使用以下功能從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