def _load_namemap(labelmap_path):
    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(item.display_name)
    return labelmap_dict, categories
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def load_labelmap(path):
    """Loads label map proto.
    Args:
      path: path to StringIntLabelMap proto text file.
    Returns:
      a StringIntLabelMapProto
    """
    with tf.compat.v1.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
Ejemplo n.º 4
0
def load_labelmap(path):
    # https://github.com/tensorflow/models/blob/67fd2bef6500c14b95e0b0de846960ed13802405/research/object_detection/utils/label_map_util.py#L159
    """Loads label map proto.
  Args:
    path: path to StringIntLabelMap proto text file.
  Returns:
    a StringIntLabelMapProto
  """
    with tf.io.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(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
Ejemplo n.º 6
0
def load_labelmap(path):
    """Loads label map proto.

  Args:
    path: path to StringIntLabelMap proto text file.
  Returns:
    a StringIntLabelMapProto
  """
    with tf.compat.v2.io.gfile.GFile(path, 'rb') as fid:
        #with tf.compat.v2.io.gfile.GFile(path, 'r') as fid: # use this line to run it with TensorFlow version 2.x
        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
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
def load_labels(labels_name):
    labels_url = 'https://raw.githubusercontent.com/tensorflow/models/master/research/object_detection/data/' + labels_name

    labels_path = tf.keras.utils.get_file(
        fname=labels_name,
        origin=labels_url,
        cache_dir=pathlib.Path('.tmp').absolute())

    labels_file = open(labels_path, 'r')
    labels_string = labels_file.read()

    labels_map = string_int_label_map_pb2.StringIntLabelMap()
    try:
        text_format.Merge(labels_string, labels_map)
    except text_format.ParseError:
        labels_map.ParseFromString(labels_string)

    labels_dict = {}
    for item in labels_map.item:
        labels_dict[item.id] = item.display_name

    return labels_dict
Ejemplo n.º 9
0
def load_pbtxt_file(path):
    """Read .pbtxt file.

    Args:
        path: Path to StringIntLabelMap proto text file (.pbtxt file).

    Returns:
        A StringIntLabelMapProto.

    Raises:
        ValueError: If path is not exist.
    """
    if not tf.gfile.Exists(path):
        raise ValueError('`path` is not exist.')

    with tf.gfile.GFile(path, 'r') as fid:
        pbtxt_string = fid.read()
        pbtxt = string_int_label_map_pb2.StringIntLabelMap()
        try:
            text_format.Merge(pbtxt_string, pbtxt)
        except text_format.ParseError:
            pbtxt.ParseFromString(pbtxt_string)
    return pbtxt
    print('Probablity = {0:.2f}% that this is a {1}'.format(
        scores[0] * 100, LABELS[classes[0]]))
    print('Probablity = {0:.2f}% that this is a {1}'.format(
        scores[1] * 100, LABELS[classes[1]]))
    print(boxes[0])
    print(boxes[1])

    return 'recognized successfully'


if PRE_TRAINED_MODEL is None:
    PRE_TRAINED_MODEL = load_pre_trained_model()

if LABELS is None:
    DESTINATION_FOLDER = UPLOAD_FOLDER = os.path.join(
        os.path.dirname(__file__), 'datasets')
    PATH_TO_LABELS = os.path.join(DESTINATION_FOLDER, 'data',
                                  'mscoco_label_map.pbtxt')
    NUM_CLASSES = 90

    label_map = string_int_label_map_pb2.StringIntLabelMap()
    with tf.gfile.GFile(PATH_TO_LABELS, 'r') as fid:
        label_map_string = fid.read()
        text_format.Merge(label_map_string, label_map)

    LABELS = {item.id: item.display_name for item in label_map.item}

if __name__ == "__main__":
    print('recognition __main__')
else:
    print('recognition ' + __name__)