如何将labelmap.prototxt转换为一个集合?咖啡



我有标签map.prototxt,我想将其转换为一个集合,因为我想将它与Intel Neural Compute Stick SDK一起使用。

输入

home/labelmap.prototxt

item {
name: "none_of_the_above"
label: 0
display_name: "background"
}
item {
name: "1"
label: 1
display_name: "person"
}
item {
name: "2"
label: 2
display_name: "bicycle"
}
item {
name: "3"
label: 3
display_name: "car"
}

输出:

set(["background","person","bicycle","car"])

我试过如下:

>>> with open('labelmap_coco.prototxt') as f:
...     d = literal_eval(f)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python2.7/ast.py", line 80, in literal_eval
return _convert(node_or_string)
File "/usr/lib/python2.7/ast.py", line 79, in _convert
raise ValueError('malformed string')
ValueError: malformed string

阅读关于proto缓冲区的信息,在caffe-ssd proto文件中持久消息LabelMap,这样您就可以从prototxt中读取对象数据。这是一件非常好的事情。查看代码:

from caffe.proto import caffe_pb2
from google.protobuf import text_format as tf
f = open('labelmap.prototxt', 'r')
lm = caffe_pb2.LabelMap()
lm = tf.Parse(f.read(), lm)
display_names = [x.display_name for x in lm.item]

最新更新