Пример #1
0
def write_examples_to_dataset_file(example_list, categories, width, height, use_bgr_ordering, output_dataset):
    """
    Saves an array of examples to a dataset file.
    """
    print("Processing {} examples, using image size {}x{}, bgr={}".format(len(example_list), width, height, use_bgr_ordering))
    with open(output_dataset, 'w') as dataset_file:
        # Write header
        if categories:
            dataset_file.write("# Category labels\n")
            for i, category in enumerate(categories):
                dataset_file.write("# {} : {}\n".format(i, category))
        for example in example_list:
            # Try to read this as an image
            image = cv2.imread(example[1])
            if image is not None:
                # Write label
                dataset_file.write("{}".format(example[0]))
                print("Processing {0[0]} | {0[1]}".format(example))
                resized = modelHelpers.prepare_image_for_model(image, width, height, not use_bgr_ordering)
                # Write image data
                valuesWritten = 0
                for value in resized:
                    dataset_file.write("\t{}".format(value))
                    valuesWritten = valuesWritten + 1
                # Write label, source file as comment
                dataset_file.write("\t# class={0[2]}, source={0[1]}".format(example))
                dataset_file.write("\n")
                print("    Wrote {} data values".format(valuesWritten))
            else:
                print("Skipping {}, could not open as an image".format(example[1]))

    print()
    print("Wrote {} examples to {}".format(len(example_list), output_dataset))
Пример #2
0
def write_examples_to_dataset_file(example_list,
                                   categories,
                                   width,
                                   height,
                                   use_bgr_ordering,
                                   output_dataset,
                                   verbose=False):
    """
    Saves an array of examples to a dataset file.
    """
    print("Processing {} examples, using image size {}x{}, bgr={}".format(
        len(example_list), width, height, use_bgr_ordering))
    with open(output_dataset, 'w') as dataset_file:
        # Write header
        if categories:
            dataset_file.write("# Category labels\n")
            for i, category in enumerate(categories):
                dataset_file.write("# {} : {}\n".format(i, category))

        count = 0
        for example in example_list:
            # Try to read this as an image
            image = cv2.imread(example[1])
            if image is not None:
                if verbose:
                    print("Processing {0[0]} | {0[1]}".format(example))

                # Write label
                dataset_file.write("{}".format(example[0]))

                # Write image data
                resized = modelHelpers.prepare_image_for_model(
                    image,
                    width,
                    height,
                    not use_bgr_ordering,
                    convert_to_float=False,
                    preprocess_tag=None)
                dataset_file.write("\t")
                resized.tofile(dataset_file, sep="\t", format="%s")
                # Write label, source file as comment
                dataset_file.write(
                    "\t# class={0[2]}, source={0[1]}".format(example))
                dataset_file.write("\n")

                if verbose:
                    print("    Wrote {} data values".format(len(resized)))
                else:
                    if (count + 1) % 1000 == 0:
                        print(".", sep="", end="")
                count += 1
            else:
                print("Skipping {}, could not open as an image".format(
                    example[1]))

    print()
    print("Wrote {} examples to {}".format(len(example_list), output_dataset))
Пример #3
0
def write_examples_to_dataset_file(example_list, width, height,
                                   use_bgr_ordering, output_dataset):
    """
    Saves an array of examples to a dataset file.
    """
    print("Processing {} examples, using image size {}x{}, bgr={}".format(
        len(example_list), width, height, use_bgr_ordering))
    with open(output_dataset, 'w') as dataset_file:
        for example in example_list:
            # Write label
            dataset_file.write("{}".format(example[0]))
            print("Processing {} | {}".format(example[0], example[1]))
            image = cv2.imread(example[1])
            resized = modelHelpers.prepare_image_for_model(
                image, width, height, not use_bgr_ordering)
            # Write image data
            valuesWritten = 0
            for value in resized:
                dataset_file.write("\t{}".format(value))
                valuesWritten = valuesWritten + 1
            dataset_file.write("\n")
            print("    Wrote {} data values".format(valuesWritten))