Exemplo n.º 1
0
def main():
    LIST_OF_OBJECTS = []
    count = 1
    pbtxtFilepath = Path(PBTXT_FILE)

    #if pbtxt file does not exist then create the file, or if it exists then remove it
    if not pbtxtFilepath.exists():
        open(PBTXT_FILE, 'w').close()
    else:
        os.system('rm ' + PBTXT_FILE)

    for xmlFile in os.listdir(ANNOTATION_FILE_PATH):

        #rotating through all the annotation files
        with tf.gfile.GFile(ANNOTATION_FILE_PATH + '/' + xmlFile, 'r') as fid:
            xml_str = fid.read()
        xml = etree.fromstring(xml_str)
        data = dataset_util.recursive_parse_xml_to_dict(xml)[
            'annotation']  #reading from annotation tag
        objects = data['object']  #reading the attributes of the object
        filename = data['path']  #reading the path of the image file
        for object in objects:
            objectname = object['name'].strip()  #getting the object name

            if LIST_OF_OBJECTS.__contains__(objectname):
                #if a particular object has already occured then move to the next object
                continue
            else:
                #add the object to the object list
                LIST_OF_OBJECTS.append(objectname)

                writingToPbtxt(objectname)  #write the object to the pbtxt file

        count = count + 1
    print('The Number of Classes in the Dataset is : ', len(LIST_OF_OBJECTS))
def create_tf_record(output_filename,
                     label_map_dict,
                     annotations_dir,
                     image_dir,
                     examples,
                     faces_only=True,
                     mask_type='png'):
    """Creates a TFRecord file from examples.

  Args:
    output_filename: Path to where output file is saved.
    label_map_dict: The label map dictionary.
    annotations_dir: Directory where annotation files are stored.
    image_dir: Directory where image files are stored.
    examples: Examples to parse and save to tf record.
    faces_only: If True, generates bounding boxes for pet faces.  Otherwise
      generates bounding boxes (as well as segmentations for full pet bodies).
    mask_type: 'numerical' or 'png'. 'png' is recommended because it leads to
      smaller file sizes.
  """
    writer = tf.python_io.TFRecordWriter(output_filename)
    for idx, example in enumerate(examples):
        if idx % 100 == 0:
            logging.info('On image %d of %d', idx, len(examples))
        xml_path = os.path.join(annotations_dir, 'xmls', example + '.xml')
        mask_path = os.path.join(annotations_dir, 'trimaps', example + '.png')

        if not os.path.exists(xml_path):
            logging.warning('Could not find %s, ignoring example.', xml_path)
            continue
        with tf.gfile.GFile(xml_path, 'r') as fid:
            xml_str = fid.read()
        xml = etree.fromstring(xml_str)
        data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']

        try:
            tf_example = dict_to_tf_example(data,
                                            mask_path,
                                            label_map_dict,
                                            image_dir,
                                            faces_only=faces_only,
                                            mask_type=mask_type)
            writer.write(tf_example.SerializeToString())
        except ValueError:
            logging.warning('Invalid example: %s, ignoring.', xml_path)

    writer.close()
Exemplo n.º 3
0
def create_tf_record(writer, label_map_dict, annotations_dir, image_dir,
                     examples, value):
    #This function will create the TFR Record for both training and evaluation and will write it to the file
    for idx, example in enumerate(examples):
        if idx % 100 == 0:
            logging.info('On image %d of %d', idx, len(examples))
        path = os.path.join(annotations_dir, example.split('.')[0] + '.xml')
        if not os.path.exists(path):
            logging.warning('Could not find %s, ignoring example.', path)
            continue
        with tf.gfile.GFile(path, 'r') as fid:
            xml_str = fid.read()
        xml = etree.fromstring(xml_str)
        data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']

        tf_example = dict_to_tf_example(data, label_map_dict, image_dir, value)

        writer.write(tf_example.SerializeToString())
Exemplo n.º 4
0
def main():
    #contains the class names from pbtxt file
    label_map_dict = label_map_util.get_label_map_dict(FLAGS.label_map_path)

    for l in label_map_dict:
        print("label : " + l)

        stringToWrite = ''
        file_list = []  #contains all the files from the annotation file

        c = 0
        #taking all the files from the annotation directory
        for xmlFile in os.listdir(ANNOTATION_FILE_PATH):
            with tf.gfile.GFile(ANNOTATION_FILE_PATH + '/' + xmlFile,
                                'r') as fid:
                xml_str = fid.read()
            xml = etree.fromstring(xml_str)

            data = dataset_util.recursive_parse_xml_to_dict(xml)[
                'annotation']  #Getting data from annotation tag
            imageFile = data['filename']  #Getting data for filename
            objects = data[
                'object']  #Getting datails of an object from xml file
            objectname = ''
            allObjects = []
            for object in objects:
                objectname = object['name'].strip()
                allObjects.append(objectname)

            if not allObjects.__contains__(l):
                continue
            else:
                #Adding the files from xml files into a list
                if not file_list.__contains__(imageFile):
                    file_list.append(imageFile)

        LIST_OF_IMAGE_FILES = getListOfImgFiles()
        stringToWrite = creatingTrainingFile(LIST_OF_IMAGE_FILES, file_list,
                                             stringToWrite)
        TRAINING_FILE = TRAINING_FILE_PATH + '/' + l + '_train.txt'
        trainingfileopen = open(TRAINING_FILE, 'w')
        trainingfileopen.write(stringToWrite)
        trainingfileopen.close()
        print('created')
Exemplo n.º 5
0
def load_pascal_voc(origin_dir):
    data = {}
    cwd = os.getcwd()
    try:
        os.chdir(origin_dir)
    except OSError:
        print('Could not change directory.')

    for file in glob.glob("*.xml"):
        with tf.io.gfile.GFile(file, 'r') as fid:
            xml_str = fid.read()
        xml = etree.fromstring(xml_str)
        data[file] = dataset_util.recursive_parse_xml_to_dict(
            xml)['annotation']
    try:
        os.chdir(cwd)
    except OSError:
        print('Could not return to previous directory.')
    return data
Exemplo n.º 6
0
def create_tf_record(output_filename,
                     num_shards,
                     label_map_dict,
                     annotations_dir,
                     examples,
                     image_dir=FLAGS.data_dir,
                     faces_only=False):
  """Creates a TFRecord file from examples.
  Args:
    output_filename: Path to where output file is saved.
    num_shards: Number of shards for output file.
    label_map_dict: The label map dictionary.
    annotations_dir: Directory where annotation files are stored.
    image_dir: Directory where image files are stored.
    examples: Examples to parse and save to tf record.
    faces_only: If True, generates bounding boxes for pet faces.  Otherwise
      generates bounding boxes (as well as segmentations for full pet bodies).
    mask_type: 'numerical' or 'png'. 'png' is recommended because it leads to
      smaller file sizes.
  """
  with contextlib2.ExitStack() as tf_record_close_stack:
    output_tfrecords = tf_record_creation_util.open_sharded_output_tfrecords(
        tf_record_close_stack, output_filename, num_shards)
    for idx, example in enumerate(examples):
        if idx % 100 == 0:
            logging.info('On image %d of %d', idx, len(examples))
        path = os.path.join(annotations_dir, example + '.xml')
        with tf.gfile.GFile(path, 'r') as fid:
            xml_str = fid.read()
        xml = etree.fromstring(xml_str)
        data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']
        try:
            tf_example = dict_to_tf_example(data, FLAGS.data_dir, label_map_dict,
                                          FLAGS.ignore_difficult_instances)
            if tf_example:
                shard_idx = idx % num_shards
                output_tfrecords[shard_idx].write(tf_example.SerializeToString())
        except ValueError:
            logging.warning('Invalid example: %s, ignoring.', path)
Exemplo n.º 7
0
import sys
progPath = sys.argv[1]

Config = configparser.ConfigParser()
Config._interpolation = configparser.ExtendedInterpolation()
Config.read(progPath + "/config.ini")
IMG_TO_OBJECT_FILE_PATH = Config.get('tensorflow', 'IMG_TO_OBJECT_FILE_PATH')
fileopen = open(IMG_TO_OBJECT_FILE_PATH + '/imgnameToclassname.txt', 'w')
ANNOTATION_FILE_PATH = Config.get('tensorflow', 'ANNOTATION_FILE_PATH')

for xmlFile in os.listdir(ANNOTATION_FILE_PATH):
    print('xml file : ' + xmlFile)

    with tf.gfile.GFile(ANNOTATION_FILE_PATH + '/' + xmlFile, 'r') as fid:
        xml_str = fid.read()
    xml = etree.fromstring(xml_str)
    data = dataset_util.recursive_parse_xml_to_dict(xml)['annotation']
    objects = data['object']
    objectname = ""
    obj = []
    for object in objects:
        if obj.__contains__(object['name']):

            continue

        obj.append(object['name'])
        objectname = objectname + " " + object['name']
    imageFile = data['filename']
    fileopen.write(imageFile + '=' + objectname + '\n')
fileopen.close()