def test_gray_to_bgr(self): image_pil = Image.open(TEST_GRAY_IMAGE) ip = ImagePrep() image_cv = ip(image_pil) self.assertEqual(520, len(image_cv)) self.assertEqual(400, len(image_cv[0])) self.assertEqual(3, len(image_cv[0][0])) ip = ImagePrep(convert_to_color=False) image_cv = ip(image_pil) self.assertEqual(520, len(image_cv)) self.assertEqual(400, len(image_cv[0])) self.assertEqual(33, image_cv[0][0])
def _test_image_area(self, image): area = _compute_area(image) new_area = np.random.randint(area / 2, area * 2) ip = ImagePrep(image_area=new_area) new_image = ip(image) self.assertAreaIs(new_image, new_area) self.assertAspMatch(new_image, image)
def _test_scale_width(self, image): oh, ow = _get_dims(image) new_width = np.random.randint(ow / 2, ow * 2) ip = ImagePrep(scale_width=new_width) new_image = ip(image) nh, nw = _get_dims(new_image) self.assertSideIs(nw, new_width)
def _test_scale_height(self, image): oh, ow = _get_dims(image) new_height = np.random.randint(oh / 2, oh * 2) ip = ImagePrep(scale_height=new_height) new_image = ip(image) nh, nw = _get_dims(new_image) self.assertSideIs(nh, new_height)
def _test_convert_to_gray(self, image): oh, ow = _get_dims(image) ip = ImagePrep(convert_to_gray=True) new_image = ip(image) nh, nw = _get_dims(new_image) self.assertEqual(len(new_image.shape), 2) self.assertEqual(oh, nh) self.assertEqual(ow, nw)
def test_ensembleConfig(self): np.random.seed(42) for i in range(100): # run 100 random tests config = produce_rand_config(self.image_cv) ip = ImagePrep(**config) imageSeq = run_imageprep_seq(self.image_cv, config) imageEns = ip(self.image_cv) self.assertTrue(np.array_equiv(imageSeq, imageEns))
def _test_image_size(self, image): oh, ow = _get_dims(image) new_height = np.random.randint(oh / 2, oh * 2) new_width = np.random.randint(ow / 2, ow * 2) ip = ImagePrep(image_size=[new_height, new_width]) new_image = ip(image) nh, nw = _get_dims(new_image) self.assertSideIs(nh, new_height) self.assertSideIs(nw, new_width)
def _test_max_width(self, image, direction='down'): oh, ow = _get_dims(image) if direction == 'down': max_width = ow / 2 else: max_width = ow * 2 ip = ImagePrep(max_width=max_width) new_image = ip(image) nh, nw = _get_dims(new_image) self.assertSideNotMoreThan(nw, max_width) self.assertAspMatch(image, new_image)
def _test_max_height(self, image, direction='down'): oh, ow = _get_dims(image) if direction == 'down': max_height = oh / 2 else: max_height = oh * 2 ip = ImagePrep(max_height=max_height) new_image = ip(image) nh, nw = _get_dims(new_image) self.assertSideNotMoreThan(nh, max_height) self.assertAspMatch(image, new_image)
def test_returnTypes(self): pil_img = self.image_pil cv_img = self.image_cv no_conv_arg = ImagePrep() return_same = ImagePrep(return_same=True) return_pil = ImagePrep(return_pil=True) # no converstion arguments, should return CV new_pil = no_conv_arg(pil_img) new_cv = no_conv_arg(cv_img) self.assertTrue(_is_CV(new_pil)) self.assertTrue(_is_CV(new_cv)) # return same image type new_pil = return_same(pil_img) new_cv = return_same(cv_img) self.assertTrue(_is_PIL(new_pil)) self.assertTrue(_is_CV(new_cv)) # return PIL images new_pil = return_pil(pil_img) new_cv = return_pil(cv_img) self.assertTrue(_is_PIL(new_pil)) self.assertTrue(_is_PIL(new_cv))
def _test_max_side(self, image, direction='down'): oh, ow = _get_dims(image) mxs = max(oh, ow) if direction == 'down': max_side = mxs / 2 else: max_side = mxs * 2 ip = ImagePrep(max_side=max_side) new_image = ip(image) nh, nw = _get_dims(new_image) nmxs = max(nh, nw) self.assertSideNotMoreThan(nmxs, max_side) self.assertAspMatch(image, new_image)
def run_imageprep_seq(image, config): ''' Runs ImagePrep sequentially by evaluating the arguments in-order ''' op_order = [ 'convert_to_color', 'convert_to_gray', 'max_height', 'max_width', 'max_side', 'scale_height', 'scale_width', 'image_size', 'image_area', 'crop_frac' ] for i in op_order: # convert_to_color, if defaulted, will break this test # if convert_to_gray is also true (i.e., it will run last # and override convert to gray). args = {'convert_to_color': False, i: config.get(i, None)} ip = ImagePrep(**args) image = ip(image) return image
def test_cropping(self): '''Attempts to validate the cropping.''' image = self.image_cv oh, ow = _get_dims(image) def _get_frac(orig, new1, new2): return (float(abs((new2 - dimtol) - (new1 + dimtol))) / orig, float(abs((new2 + dimtol) - (new1 - dimtol))) / orig) # test crop type one crop_frac = np.random.rand() * .5 + .5 #(crop no more than 50%) ip = ImagePrep(crop_frac=crop_frac) new_image = ip(image) fnd = _subarray_in_array(image, new_image) self.assertTrue(fnd) x1, y1, x2, y2 = fnd min_frac, max_frac = _get_frac(oh, x1, x2) self.assertGreaterEqual(crop_frac, min_frac) self.assertGreaterEqual(max_frac, crop_frac) min_frac, max_frac = _get_frac(ow, y1, y2) self.assertGreaterEqual(crop_frac, min_frac) self.assertGreaterEqual(max_frac, crop_frac) # test crop type 2 crop_frac = [np.random.rand() * .5 + .5, np.random.rand() * .5 + .5] ip = ImagePrep(crop_frac=crop_frac) new_image = ip(image) fnd = _subarray_in_array(image, new_image) self.assertTrue(fnd) x1, y1, x2, y2 = fnd min_frac, max_frac = _get_frac(oh, x1, x2) self.assertGreaterEqual(crop_frac[0], min_frac) self.assertGreaterEqual(max_frac, crop_frac[0]) min_frac, max_frac = _get_frac(ow, y1, y2) self.assertGreaterEqual(crop_frac[1], min_frac) self.assertGreaterEqual(max_frac, crop_frac[1]) # test crop type 3 crop_frac = [ np.random.rand() * .5, np.random.rand() * .5, np.random.rand() * .5, np.random.rand() * .5 ] ip = ImagePrep(crop_frac=crop_frac) new_image = ip(image) fnd = _subarray_in_array(image, new_image) self.assertTrue(fnd) x1, y1, x2, y2 = fnd min_frac, max_frac = _get_frac(oh, x1, oh) self.assertTrue((1. - crop_frac[0] >= min_frac) and (1. - crop_frac[0] <= max_frac)) self.assertGreaterEqual(1 - crop_frac[0], min_frac) self.assertGreaterEqual(max_frac, 1 - crop_frac[0]) min_frac, max_frac = _get_frac(ow, 0, y2) self.assertGreaterEqual(1 - crop_frac[1], min_frac) self.assertGreaterEqual(max_frac, 1 - crop_frac[1]) min_frac, max_frac = _get_frac(oh, 0, x2) self.assertGreaterEqual(1 - crop_frac[2], min_frac) self.assertGreaterEqual(max_frac, 1 - crop_frac[2]) min_frac, max_frac = _get_frac(ow, y1, ow) self.assertGreaterEqual(1 - crop_frac[3], min_frac) self.assertGreaterEqual(max_frac, 1 - crop_frac[3])