def test_image_is_the_right_dtype(self):
        """Make sure the images are of type uint8 i.e. [0,255] range
        """
        faux_data = 100 * np.ones(shape=(200, 200, 3), dtype=np.float32)

        with self.assertRaises(Exception):
            pipe_utils.data_is_ok(faux_data, use='patch', raise_exception=True)
    def test_image_has_alpha_channel(self):
        """Make sure that an image with an alpha channel doesn't pass the data
        check
        """
        faux_data = np.ones(shape=(200, 200, 4), dtype=np.uint8)

        with self.assertRaises(Exception):
            pipe_utils.data_is_ok(faux_data, use='patch', raise_exception=True)
    def test_image_is_in_wrong_range(self):
        """See that if a image mistakenly normalized to [0,1), and then converted
        to uint8 raises an alert
        """
        faux_data = np.ones(shape=(200, 200, 3), dtype=np.uint8)

        with self.assertRaises(Exception):
            pipe_utils.data_is_ok(faux_data, use='patch', raise_exception=True)
    def test_wrongly_shaped_image_raises_exception(self):
        """Make sure an exception is raised if the data in not of the correct
        shape
        """
        faux_data = 100 * np.ones(shape=(300, 300, 3), dtype=np.uint8)

        with self.assertRaises(Exception):
            pipe_utils.data_is_ok(faux_data, use='patch', raise_exception=True)
Example #5
0
def save_as_retinanet_data(ds,
                           image_save_dir,
                           annotations_save_dir,
                           percent_test_set=.2,
                           include_labels=LABELS_TO_INCLUDE):
    """Save the data in the csv format expected by RetinaNet

    Args
    ----
    ds (list : ObjImage) : Image data set
    image_save_dir (str) :
    annotations_save_dir (str) :
    percent_test_set (float) : value in [0, 1), percent of data to use as test
    """

    test_annot_path = os.path.join(annotations_save_dir, "test_annot.csv")
    train_annot_path = os.path.join(annotations_save_dir, "train_annot.csv")

    if not os.path.exists(image_save_dir):
        print("Creating directory to save processed images")
        os.makedirs(image_save_dir)

    if not os.path.exists(annotations_save_dir):
        print("Creating directory to save annotation file")
        os.makedirs(annotations_save_dir)

    with open(test_annot_path, 'a') as test_file, open(train_annot_path,
                                                       'a') as train_file:

        test_writer = csv.writer(test_file)
        train_writer = csv.writer(train_file)

        for img in ds:
            # skip this image if the data isn't in the right format
            if not pipe_utils.data_is_ok(img.data):
                continue

            image_path = os.path.join(image_save_dir, img.image_id + '.png')
            skio.imsave(image_path, img.data)

            # Randomly determine if the image is for training or testing
            if np.random.uniform() < .2:
                writer = test_writer
            else:
                writer = train_writer

            # Make a note of every feature and bounding location in the image
            all_features = img.get_features()
            for label, locations in all_features.items():
                label = MAP_TO_LOCAL_LABELS[label]
                if label in include_labels:
                    for location in locations:
                        row = [image_path, *location, label]
                        writer.writerow(row)

            # Write a blank row if no features in this image
            if not all_features:
                row = [image_path, '', '', '', '', '']
                writer.writerow(row)
    def test_data_check_doesnt_have_to_raise_exception(self):
        """Repeat test_wrongly_shaped_image_raises_exception just to make sure
        that an exception doesnt have to be raise, and that a 'False' is 
        returned instead
        """
        faux_data = 100 * np.ones(shape=(300, 300, 3), dtype=np.uint8)

        self.assertFalse(pipe_utils.data_is_ok(faux_data, use='patch'))
Example #7
0
def read_raw_image(path, report=True, check_data=False):
    """Import a raw image 

    This could be as 8 bit or 16 bit... or 10 bit like some of the files...

    Args
    ----
    path (str) : path to the image file
    report (bool) : output a short log on the imported data 

    Raises
    ------
    ImportError : If the image passed does not have a file extension that's expected
    AssertionError : if data_is_ok fails when check_data=True

    Returns 
    -------
    numpy array of raw image 
    """
    ext = os.path.splitext(path)[1]

    if ext in [".jpg", ".png"]:
        img = imread(path)
    elif ext == ".tif":
        img = tifffile.imread(path)
    else:
        raise ImportError("{} Not a supported extension".format(ext))

    if report:
        print("Image {} loaded".format(os.path.basename(path)))
        print("\tShape: ", img.shape)
        print("\tdtype: ", img.dtype)
        print("Values: ({:.2f},{:.2f})".format(img.min(), img.max())),

    if check_data:
        pipe_utils.data_is_ok(img, raise_exception=True)

    return img
    def test_patch_data_shape_is_ok(self):
        """Make sure data check returns true for an obj data shape
        """
        faux_data = 100 * np.ones(shape=(200, 200, 3), dtype=np.uint8)

        self.assertTrue(pipe_utils.data_is_ok(faux_data, use='patch'))