def main(_):
    if not os.path.exists(FLAGS.images_dir):
        raise ValueError('`images_dir` is not exist.')
    if not os.path.exists(FLAGS.annotations_json_dir):
        raise ValueError('`annotations_json_dir` is not exist.')
    if not os.path.exists(FLAGS.label_map_path):
        raise ValueError('`label_map_path` is not exist.')

    label_map = read_pbtxt_file.get_label_map_dict(FLAGS.label_map_path)

    writer = tf.python_io.TFRecordWriter(FLAGS.output_path)

    num_annotations_skiped = 0
    annotations_json_path = os.path.join(FLAGS.annotations_json_dir, '*.json')
    for i, annotation_file in enumerate(glob.glob(annotations_json_path)):
        if i % 100 == 0:
            print('On image %d', i)

        annotation_dict = _get_annotation_dict(FLAGS.images_dir,
                                               annotation_file)
        if annotation_dict is None:
            num_annotations_skiped += 1
            continue
        tf_example = create_tf_example(annotation_dict, label_map)
        writer.write(tf_example.SerializeToString())

    print('Successfully created TFRecord to {}.'.format(FLAGS.output_path))
def main(_):
    train, validate, test = sys.argv[1:4]
    #print("train, validate ,test=",(train, validate ,test))
    KindPath = ['Train', "Validate", "Test"]
    DirNum = [{}, {}, {}]
    if not os.path.exists(FLAGS.images_dir):
        raise ValueError('`images_dir` is not exist.')
    if not os.path.exists(FLAGS.annotations_json_dir):
        raise ValueError('`annotations_json_dir` is not exist.')
    if not os.path.exists(FLAGS.label_map_path):
        raise ValueError('`label_map_path` is not exist.')

    label_map = read_pbtxt_file.get_label_map_dict(FLAGS.label_map_path)

    TargetDir = FLAGS.output_path + os.sep + "Test"
    if os.path.isdir(TargetDir): rmtree(FLAGS.output_path + os.sep + "Test")

    writer = []
    for i in range(2):
        writer.append(
            tf.io.TFRecordWriter(FLAGS.output_path + os.sep + KindPath[i] +
                                 ".record"))

    num_annotations_skiped = 0
    annotations_json_path = os.path.join(FLAGS.annotations_json_dir, '*.json')

    annotations_json_path = []
    for root, dirs, files in os.walk(FLAGS.annotations_json_dir):
        for f in files:
            fullpath = os.path.join(root, f)
            if ".json" in fullpath:
                annotations_json_path.append(fullpath)

    for i, annotation_file in enumerate(annotations_json_path):
        ix = np.random.choice([0, 1, 2], size=1, p=[train, validate, test])[0]

        if ix == 2:
            DirNum[ix] = ImgCopy(annotation_file, FLAGS.output_path,
                                 DirNum[ix])
            continue

        #print(i," :",annotation_file)
        #if i % 100 == 0:
        #    print('On image %d', i)
        print("\n")
        print("ix=", ix)
        print("DirNum=", DirNum)
        print("annotation_file=", annotation_file)

        annotation_dict, DirNum[ix] = _get_annotation_dict(
            annotation_file, DirNum[ix])
        if annotation_dict is None:
            num_annotations_skiped += 1
            continue
        tf_example = create_tf_example(annotation_dict, label_map)
        writer[ix].write(tf_example.SerializeToString())
        #writer.write(tf_example.SerializeToString())
    #print('Successfully created TFRecord to {}.'.format(FLAGS.output_path))
    print("DirNum=", DirNum)
Esempio n. 3
0
def main(_):
    if not os.path.exists(FLAGS.images_dir):
        raise ValueError('`images_dir` is not exist.')
    if not os.path.exists(FLAGS.annotations_json_dir):
        raise ValueError('`annotations_json_dir` is not exist.')
    if not os.path.exists(FLAGS.label_map_path):
        raise ValueError('`label_map_path` is not exist.')

    label_map = read_pbtxt_file.get_label_map_dict(FLAGS.label_map_path)

    if not os.path.exists(FLAGS.output_path):
        os.makedirs(FLAGS.output_path)
        print("tfrecord文件夹不存在,已重新创建...")

    writer = tf.python_io.TFRecordWriter(FLAGS.output_path + "/train.record")

    num_annotations_skiped = 0
    annotations_json_path = os.path.join(FLAGS.annotations_json_dir, '*.json')
    print("annotations_json_path: ", annotations_json_path)

    annotations_json_pathAll = glob.glob(annotations_json_path)
    print("len(annotations_json_pathAll): ", len(annotations_json_pathAll))

    numImages = len(annotations_json_pathAll)
    randIndices = np.random.permutation(len(annotations_json_pathAll))
    print("randIndices: ", randIndices)

    for i in range(numImages):
        annotation_file = annotations_json_pathAll[randIndices[i]]
        print(i, ": ", annotation_file)
        if i % 10 == 0:
            print('On image %d...' % i)

        annotation_dict = _get_annotation_dict(FLAGS.images_dir,
                                               annotation_file)

        if annotation_dict is None:
            num_annotations_skiped += 1
            continue

        tf_example = create_tf_example(annotation_dict, label_map)
        writer.write(tf_example.SerializeToString())

    print('num_annotations_skiped: ', num_annotations_skiped)
    print('Successfully created TFRecord to {}.'.format(FLAGS.output_path))