-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlabels.py
29 lines (24 loc) · 875 Bytes
/
labels.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/python3
from google.protobuf import text_format
import string_int_label_map_pb2
from six import string_types
# Parse a labels protobuf file into a python map
# Adapted from retrain/models/research/object_detection/utils/label_map_util.py
def load_labelmap(path):
with open (path, "r") as labels_file:
label_map_string=labels_file.read()
label_map = string_int_label_map_pb2.StringIntLabelMap()
try:
text_format.Merge(label_map_string, label_map)
except text_format.ParseError:
label_map.ParseFromString(label_map_string)
return label_map
def get_labels(label_map_path):
label_map = load_labelmap(label_map_path)
label_map_dict = {}
for item in label_map.item:
label_map_dict[item.id] = item.display_name
return label_map_dict
from pprint import pprint
labels = get_labels('mscoco_label_map.pbtxt')
pprint(labels)