def test_disallow_duplicate_keypoint_ids(self): label_map_str = """ item { id: 1 name: 'person' keypoints: { id: 1 label: 'right_elbow' } keypoints: { id: 1 label: 'left_elbow' } } item { id: 2 name: 'face' keypoints: { id: 3 label: 'ear' } } """ label_map_proto = string_int_label_map_pb2.StringIntLabelMap() text_format.Merge(label_map_str, label_map_proto) with self.assertRaises(ValueError): label_map_util.convert_label_map_to_categories(label_map_proto, max_num_classes=2)
def main(tfrecords_filename, label_map=None): """ Visualize the image and label stored in tf record Blatantly stole from https://stackoverflow.com/questions/50391967/how-to-visualize-a-tfrecord :param tfrecords_filename: full path to the tfrecord file :param label_map: None by default :return: """ matplotlib.use('TkAgg') if label_map is not None: label_map_proto = pb.StringIntLabelMap() with tf.gfile.GFile(label_map, 'r') as f: text_format.Merge(f.read(), label_map_proto) class_dict = {} for entry in label_map_proto.item: class_dict[entry.id] = {'name': entry.display_name} sess = tf.Session() decoder = TfDecoder(label_map_proto_file=label_map, use_display_name=False) sess.run(tf.tables_initializer()) for record in tf.python_io.tf_record_iterator(tfrecords_filename): example = decoder.decode(record) host_example = sess.run(example) scores = np.ones(host_example['groundtruth_boxes'].shape[0]) vu.visualize_boxes_and_labels_on_image_array( host_example['image'], host_example['groundtruth_boxes'], host_example['groundtruth_classes'], scores, class_dict, max_boxes_to_draw=None, use_normalized_coordinates=True) plt.imshow(host_example['image']) plt.show()
def load_labelmap(labelmap_path): """Loads labelmap from the labelmap path. Args: labelmap_path: Path to the labelmap. Returns: A dictionary mapping class name to class numerical id A list with dictionaries, one dictionary per category. """ # pylint: disable=no-member label_map = string_int_label_map_pb2.StringIntLabelMap() with open(labelmap_path, "r") as fid: label_map_string = fid.read() text_format.Merge(label_map_string, label_map) labelmap_dict = {} categories = [] for item in label_map.item: labelmap_dict[item.name] = item.id categories.append({ "id": item.id, "name": item.name, "display_name": item.display_name, }) return labelmap_dict, categories
def test_keep_categories_with_unique_id(self): label_map_proto = string_int_label_map_pb2.StringIntLabelMap() label_map_string = """ item { id:2 name:'cat' } item { id:1 name:'child' } item { id:1 name:'person' } item { id:1 name:'n00007846' } """ text_format.Merge(label_map_string, label_map_proto) categories = label_map_util.convert_label_map_to_categories( label_map_proto, max_num_classes=3) self.assertListEqual([{ 'id': 2, 'name': u'cat' }, { 'id': 1, 'name': u'child' }], categories)
def load_labelmap(path): """Loads label map proto. Args: path: path to StringIntLabelMap proto text file. Returns: a StringIntLabelMapProto """ df = pd.read_csv(path) label_map = string_int_label_map_pb2.StringIntLabelMap() records = df.to_dict('records') def label_map_func(attr): o = OrderedDict(attr[1]) v = list(o.values()) a = v[0].split(" ") a = list(filter(lambda x: len(x.strip()) > 0, a)) label_map_item = string_int_label_map_pb2.StringIntLabelMapItem() if len(attr) >= 1 and len(a) == 2 and int(a[1]) > 0: label_map_item.name = str(a[0]) label_map_item.id = int(a[1]) label_map.item.append(label_map_item) return 1 else: return False for ii, record in enumerate(records): label_map_func((ii, record)) print("Validating now..., Records: ", len(label_map.item)) _validate_label_map(label_map) return label_map
def _generate_label_map(self, num_classes): label_map_proto = string_int_label_map_pb2.StringIntLabelMap() for i in range(1, num_classes + 1): item = label_map_proto.item.add() item.id = i item.name = 'label_' + str(i) item.display_name = str(i) return label_map_proto
def generate_label_map(labels): num_classes = len(labels) label_map_proto = string_int_label_map_pb2.StringIntLabelMap() for i in range(num_classes): item = label_map_proto.item.add() item.id = i + 1 item.name = labels[i] item.display_name = ' '.join(labels[i].split('_')) return str(label_map_proto)
def class_dict_to_label_map_str(class_dict): label_map_proto = string_int_label_map_pb2.StringIntLabelMap() for key,val in class_dict.items(): item = label_map_proto.item.add() item.name = key # 0 is reserved for 'background' only, which we aren't using item.id = val + 1 return text_format.MessageToString(label_map_proto)
def load_labelmap(path): with tf.gfile.GFile(path, 'r') as fid: label_map_string = fid.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) _validate_label_map(label_map) return label_map
def save_label_map(self): label_map = string_int_label_map_pb2.StringIntLabelMap() for kkk, vvv in self.label_map_dict.items(): new_item = string_int_label_map_pb2.StringIntLabelMapItem() new_item.name = vvv['name'] new_item.id = vvv['id'] label_map.item.append(new_item) lb_string = text_format.MessageToString(label_map) label_map_path = os.path.join(self.input_dir_path, "label_map.pbtxt") with tf.gfile.Open(label_map_path, "wb") as f: f.write(lb_string) print("Label map saved: {0}".format(label_map_path))
def _generate_label_map_with_hierarchy(self, num_classes, ancestors_dict, descendants_dict): label_map_proto = string_int_label_map_pb2.StringIntLabelMap() for i in range(1, num_classes + 1): item = label_map_proto.item.add() item.id = i item.name = 'label_' + str(i) item.display_name = str(i) if i in ancestors_dict: for anc_i in ancestors_dict[i]: item.ancestor_ids.append(anc_i) if i in descendants_dict: for desc_i in descendants_dict[i]: item.descendant_ids.append(desc_i) return label_map_proto
def write_label_map(categories, label_map_path): label_map = string_int_label_map_pb2.StringIntLabelMap() label_map_items = [] for i, category in enumerate(categories): idx = i + 1 proto = string_int_label_map_pb2.StringIntLabelMapItem() proto.id = idx proto.name = category label_map_items.append(proto) label_map.item.extend(label_map_items) label_map_str = text_format.MessageToString(label_map) with open(label_map_path, 'w') as label_map_file: label_map_file.write(label_map_str)
def load_classes(self, label_map_path, use_display_name=False): with open(label_map_path) as f: label_map_string = f.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) label_map_dict = {} for item in label_map.item: if use_display_name: label_map_dict[item.display_name] = item.id else: label_map_dict[item.name] = item.id return label_map_dict
def load_labelmap(path): """Loads label map proto. Args: path: path to StringIntLabelMap proto text file. Returns: a StringIntLabelMapProto """ with tf.gfile.GFile(path, "r") as fid: label_map_string = fid.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 load_labelmap(path): """Loads label map proto. Args: path: path to StringIntLabelMap proto text file. Returns: a StringIntLabelMapProto """ label_map_string = read_content(path) 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) _validate_label_map(label_map) return label_map
def test_get_label_map_dict_from_proto(self): label_map_string = """ item { id:2 name:'cat' } item { id:1 name:'dog' } """ label_map_proto = text_format.Parse( label_map_string, string_int_label_map_pb2.StringIntLabelMap()) label_map_dict = label_map_util.get_label_map_dict(label_map_proto) self.assertEqual(label_map_dict['dog'], 1) self.assertEqual(label_map_dict['cat'], 2)
def get_label_map_from_cvat_tfrecord_zip(cvat_tf_record_zip): """Extract label map from cvat tfrecord zip file. CVAT's tfrecord file contains: - label_map.pbtxt - *.tfrecord """ with tempfile.TemporaryDirectory() as temp_dir: with ZipFile(cvat_tf_record_zip) as cur_zip: with cur_zip.open('label_map.pbtxt', 'r') as f: content = f.read().decode('utf-8') labels = [] cur_label_map = string_int_label_map_pb2.StringIntLabelMap() text_format.Merge(content, cur_label_map) for item in cur_label_map.item: if item.name not in labels: labels.append(item.name) return labels
def load_label_map(path): """ :param path: Path to StringIntLabelMap proto text file :return: A StringIntLabelMapProto """ with tf.gfile.GFile(path, 'r') as fid: label_map_string = fid.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) validate_label_map(label_map) return label_map
def load_labelmap(path): """Loads label map proto. Args: path: path to StringIntLabelMap proto text file. Returns: a StringIntLabelMapProto """ with tf.gfile.GFile(path, 'r') as fid: #label_map_string = fid.read().decode() ---> Used with TF 1.0 label_map_string = fid.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) _validate_label_map(label_map) return label_map
def _load_labelmap(labelmap_path): """Loads labelmap from the labelmap path. Args: labelmap_path: Path to the labelmap. Returns: A dictionary mapping class name to class numerical id. """ label_map = string_int_label_map_pb2.StringIntLabelMap() with open(labelmap_path, 'r') as fid: label_map_string = fid.read() text_format.Merge(label_map_string, label_map) labelmap_dict = {} for item in label_map.item: labelmap_dict[item.name] = item.id return labelmap_dict
def test_convert_label_map_to_categories_lvis_frequency_and_counts(self): label_map_proto = string_int_label_map_pb2.StringIntLabelMap() label_map_string = """ item { id:1 name:'person' frequency: FREQUENT instance_count: 1000 } item { id:2 name:'dog' frequency: COMMON instance_count: 100 } item { id:3 name:'cat' frequency: RARE instance_count: 10 } """ text_format.Parse(label_map_string, label_map_proto) categories = label_map_util.convert_label_map_to_categories( label_map_proto, max_num_classes=3) self.assertListEqual([{ 'id': 1, 'name': u'person', 'frequency': 'f', 'instance_count': 1000 }, { 'id': 2, 'name': u'dog', 'frequency': 'c', 'instance_count': 100 }, { 'id': 3, 'name': u'cat', 'frequency': 'r', 'instance_count': 10 }], categories)
def detection(): data = request.get_data() json_data = json.loads(data.decode("utf-8")) flawCategorys = json_data["flawCategorys"] taskFileSet = json_data["taskFileSet"] label_map_string = label_string(flawCategorys) label_map = string_int_label_map_pb2.StringIntLabelMap() label_map = text_format.Merge(label_map_string, label_map) categories = label_map_util.convert_label_map_to_categories( label_map, max_num_classes=len(flawCategorys), use_display_name=True) category_index = label_map_util.create_category_index(categories) threading.Thread(target=detection_power, args=( taskFileSet, category_index, detection_graph, configs, )).start() status = {'status': 0} return jsonify(status)
def _load_labelmap(labelmap_path): """Loads labelmap from the labelmap path. Args: labelmap_path: Path to the labelmap. Returns: A dictionary mapping class name to class numerical id A list with dictionaries, one dictionary per category. """ label_map = string_int_label_map_pb2.StringIntLabelMap() with open(labelmap_path, 'r') as fid: label_map_string = fid.read() text_format.Merge(label_map_string, label_map) labelmap_dict = {} categories = [] for item in label_map.item: labelmap_dict[item.name] = item.id categories.append({'id': item.id, 'name': item.name}) return labelmap_dict, categories
def draw_tfrecord(tfrecords_filename, label_map=None): """ Draw the image and bounding box indicated in the groundtruth tf record file. :param tfrecords_filename: :param label_map: :return: """ if label_map is not None: label_map_proto = pb.StringIntLabelMap() with tf.gfile.GFile(label_map, 'r') as f: text_format.Merge(f.read(), label_map_proto) class_dict = {} for entry in label_map_proto.item: class_dict[entry.id] = {'name': entry.display_name} sess = tf.Session() decoder = TfDecoder(label_map_proto_file=label_map, use_display_name=False) sess.run(tf.tables_initializer()) iter_num = 0 for record in tf.python_io.tf_record_iterator(tfrecords_filename): iter_num += 1 print("This is the {} example".format(iter_num)) example = decoder.decode(record) host_example = sess.run(example) # Set the score of the groundth truth bounding box as 1 scores = np.ones(host_example['groundtruth_boxes'].shape[0]) vu.visualize_boxes_and_labels_on_image_array( host_example['image'], host_example['groundtruth_boxes'], host_example['groundtruth_classes'], scores, class_dict, max_boxes_to_draw=None, use_normalized_coordinates=True) plt.imshow(host_example['image']) plt.show(block=False) plt.pause(.1) plt.close("all")
def make(filename): """Make labels and save into a file""" label_map = string_int_label_map_pb2.StringIntLabelMap() all_items = [] res = api.get_classes() if isinstance(res, dict) and 'data' in res.keys(): data = res['data'] total = res['total'] if total == 0: all_items.append('person') # No class then use person only for _c in data: all_items.append(_c['class']) for i in range(len(all_items)): item = label_map.item.add() item.id = i + 1 item.name = all_items[i] print("{}: {}".format(i + 1, all_items[i])) f = open(filename, "w") f.write(text_format.MessageToString(label_map)) f.close()
def test_convert_label_map_with_keypoints_to_categories(self): label_map_str = """ item { id: 1 name: 'person' keypoints: { id: 1 label: 'nose' } keypoints: { id: 2 label: 'ear' } } """ label_map_proto = string_int_label_map_pb2.StringIntLabelMap() text_format.Merge(label_map_str, label_map_proto) categories = label_map_util.convert_label_map_to_categories( label_map_proto, max_num_classes=1) self.assertEqual('person', categories[0]['name']) self.assertEqual(1, categories[0]['id']) self.assertEqual(1, categories[0]['keypoints']['nose']) self.assertEqual(2, categories[0]['keypoints']['ear'])
#%% from google.protobuf import text_format from object_detection.protos import string_int_label_map_pb2 #%% with open('./res/labelmap.pbtxt', "rt") as fd: # fd = open('./res/labelmap.pbtxt',"rt") _text = fd.read() print(_text) # %% # 파싱 label_map = string_int_label_map_pb2.StringIntLabelMap() try: text_format.Merge(_text, label_map) except text_format.ParseError: label_map.ParseFromString(_text) # print(label_map) _dic = {} for item in label_map.item: print(item) print(item.name) print(item.id) _dic[item.name] = item.id print(_dic) # %%
from google.protobuf import text_format from object_detection.protos import string_int_label_map_pb2 LABEL_MAP = "data/label_map.pb.txt" f = open(LABEL_MAP).read() m = text_format.Parse(f, string_int_label_map_pb2.StringIntLabelMap()) layers = [item.name for item in m.item] layer_dict = dict([(item.id, item.name) for item in m.item])