def test_get_2dline_from_pts(): print('check normal line') pts1 = [0, 1, 1] pts2 = [1, 0, 1] line = get_2dline_from_pts(pts1, pts2) assert CHECK_EQ_NUMPY(line, np.array([1, 1, -1]).reshape((3, 1))) print('check vertical line') pts1 = [1, 1, 1] pts2 = [1, 12, 1] line = get_2dline_from_pts(pts1, pts2) assert CHECK_EQ_NUMPY(line, np.array([-11, 0, 11]).reshape((3, 1))) print('check horizontal line') pts1 = [20, 0, 1] pts2 = [1, 0, 1] line = get_2dline_from_pts(pts1, pts2) assert CHECK_EQ_NUMPY(line, np.array([0, -19, 0]).reshape((3, 1))) print('check a point at infinity') pts1 = [0, 3, 0] pts2 = [1, 0, 1] line = get_2dline_from_pts(pts1, pts2) assert CHECK_EQ_NUMPY(line, np.array([3, 0, -3]).reshape((3, 1))) print('check line at infinity') pts1 = [0, 3, 0] pts2 = [1, 0, 0] line = get_2dline_from_pts(pts1, pts2) assert CHECK_EQ_NUMPY(line, np.array([0, 0, -3]).reshape((3, 1))) print('\n\nDONE! SUCCESSFUL!!\n')
def test_data_normalize(): print('testing 1-d data with data range') random_data = [1, 2, 3, 4, 6] normalized_data = data_normalize(random_data) assert CHECK_EQ_NUMPY(normalized_data, np.array([0, 0.2, 0.4, 0.6, 1])) print('testing 1-d data with given data range') random_data = [1, 2, 3, 4, 6] normalized_data = data_normalize(random_data, data_range=(0, 100)) assert CHECK_EQ_NUMPY(normalized_data, np.array([0.01, 0.02, 0.03, 0.04, 0.06])) print('testing 3-d data with data range') random_data = np.random.rand(2, 3, 4) * 100 normalized_data = data_normalize(random_data) assert np.max(normalized_data) == 1 and np.min(normalized_data) == 0 print('testing 1-d data with sum to 1') random_data = [1, 2, 3, 4, 6] normalized_data = data_normalize(random_data, method='sum') assert np.sum(normalized_data) == 1 print('testing 3-d data with sum to a value') random_data = np.random.rand(2, 3, 4) * 100 normalized_data = data_normalize(random_data, method='sum', sum=100) assert_almost_equal(np.sum(normalized_data), 100) assert normalized_data.shape == random_data.shape print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_find_peaks(): print('test two points') input_pts = [[300, 400, 1], [300, 500, 1]] image_size = [800, 600] std = 50 heatmap, _, _ = generate_gaussian_heatmap(input_pts, image_size=image_size, std=std) img = 1 - heatmap[:, :, -1] visualize_image(img, vis=True) peak_array, peak_global = image_find_peaks(img) CHECK_EQ_NUMPY(peak_array, np.array(input_pts).transpose()) visualize_image_with_pts(img, peak_array, vis=True) CHECK_EQ_NUMPY(peak_global, np.array([300, 400, 1]).reshape((3, 1))) print('test five points') input_pts = [[300, 350, 1], [300, 400, 1], [200, 350, 1], [280, 320, 1], [330, 270, 1]] image_size = [800, 600] std = 50 heatmap, _, _ = generate_gaussian_heatmap(input_pts, image_size=image_size, std=std) img = 1 - heatmap[:, :, -1] visualize_image(img, vis=True) peak_array, peak_global = image_find_peaks(img) CHECK_EQ_NUMPY(peak_array, np.array(input_pts).transpose()) visualize_image_with_pts(img, peak_array, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_safe_npdata(): print('test list with multiple values') data = [1, 2, 3] data_bak = copy.copy(data) npdata = safe_npdata(data) assert CHECK_EQ_NUMPY(npdata, np.array(data)) npdata += 100 assert CHECK_EQ_LIST_ORDERED(data, data_bak) print('test list with single value') data = [1] npdata = safe_npdata(data) assert CHECK_EQ_NUMPY(npdata, np.array(data)) print('test scalar') data = 10 npdata = safe_npdata(data) assert CHECK_EQ_NUMPY(npdata, np.array(data)) ######################################## test failure cases print('test edge case: tuple') data = (1, 2) try: npdata = safe_npdata(data) sys.exit('\nwrong! never should be here\n\n') except TypeError: print('the input should never be a tuple') print('\n\nDONE! SUCCESSFUL!!\n')
def test_safe_bbox(): print('test single list') bbox = [1, 2, 3, 4] good_bbox = safe_bbox(bbox) print(good_bbox) print(good_bbox.shape) assert CHECK_EQ_NUMPY(good_bbox, np.array(bbox).reshape((1, 4))) print('test list of list of 4 elements') bbox = [[1, 2, 3, 4], [5, 6, 7, 8]] good_bbox = safe_bbox(bbox) print(good_bbox) print(good_bbox.shape) assert CHECK_EQ_NUMPY(good_bbox, np.array(bbox).reshape((2, 4))) print('test (4, ) numpy array') bbox = np.array([1, 2, 3, 4]) good_bbox = safe_bbox(bbox) print(bbox.shape) print(good_bbox.shape) assert CHECK_EQ_NUMPY(good_bbox, np.array(bbox).reshape((1, 4))) print('test (N, 4) numpy array') bbox = np.random.rand(10, 4) good_bbox = safe_bbox(bbox) print(bbox.shape) print(good_bbox.shape) assert CHECK_EQ_NUMPY(good_bbox, bbox) ######################################## test failure cases bbox = [1, 2, 4] try: good_bbox = safe_bbox(bbox) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the length of list should be 4') bbox = [[1, 2, 4], [5, 7, 8]] try: good_bbox = safe_bbox(bbox) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the length of list should be 4') bbox = np.array([1, 2, 4]).reshape(3, ) try: good_bbox = safe_bbox(bbox) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the numpy array should be columns of 4') bbox = np.array([[1, 2, 4], [5, 7, 8]]) try: good_bbox = safe_bbox(bbox) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the numpy array should be columns of 4') print('\n\nDONE! SUCCESSFUL!!\n')
def test_get_2dpts_from_lines(): print('check normal point') line1 = [1, 1, -2] line2 = [1, -1, 0] pts = get_2dpts_from_lines(line1, line2) assert CHECK_EQ_NUMPY(pts, np.array([-2, -2, -2]).reshape((3, 1))) print('check vertical and horizontal line') line1 = [1, 0, -1] line2 = [0, 1, -1] pts = get_2dpts_from_lines(line1, line2) assert CHECK_EQ_NUMPY(pts, np.array([1, 1, 1]).reshape((3, 1))) print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_draw_mask(): # mask = '../rainbow.jpg' mask = '/home/xinshuo/Dropbox/test7.png' mask = Image.open(mask).convert('RGB') # mask = image_resize(mask, target_size=(500, 500)) mask = image_resize(mask, target_size=(1148, 749)) visualize_image(mask, vis=True) # image_path = '../lena.png' image_path = '/home/xinshuo/test8.png' print('test with pil image') img = Image.open(image_path).convert('RGB') img = image_resize(img, target_size=(1148, 749)) print(img.shape) print(mask.shape) masked_img = image_draw_mask(img, mask, transparency=0.5) # visualize_image(img, vis=True) # visualize_image(masked_img, vis=True) save_image(masked_img, save_path='7716_new.png') print('test with numpy image with different transparency') img = np.array( Image.open(image_path).convert('RGB')).astype('float32') / 255. img_bak = img.copy() masked_img = image_draw_mask(image_resize(img, target_size=(500, 500)), mask, transparency=0.9) visualize_image(img, vis=True) visualize_image(masked_img, vis=True) masked_img += 1 assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_draw_mask(): mask = '../rainbow.jpg' mask = Image.open(mask).convert('RGB') mask = image_resize(mask, target_size=(500, 500)) visualize_image(mask, vis=True) image_path = '../lena.png' print('test with pil image') img = Image.open(image_path).convert('RGB') masked_img = image_draw_mask(image_resize(img, target_size=(500, 500)), mask) visualize_image(img, vis=True) visualize_image(masked_img, vis=True) print('test with numpy image with different transparency') img = np.array( Image.open(image_path).convert('RGB')).astype('float32') / 255. img_bak = img.copy() masked_img = image_draw_mask(image_resize(img, target_size=(500, 500)), mask, transparency=0.9) visualize_image(img, vis=True) visualize_image(masked_img, vis=True) masked_img += 1 assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('\n\nDONE! SUCCESSFUL!!\n')
def test_pts_euclidean(): print('check single list') pts1 = [0, 1] pts2 = [3, 5] dist, dist_list = pts_euclidean(pts1, pts2) assert CHECK_EQ_LIST_ORDERED(pts1, [0, 1]) assert CHECK_EQ_LIST_ORDERED(pts2, [3, 5]) assert dist == 5 assert dist_list == [5] print('check list of list of 2 elements') pts1 = [[0, 1], [1, 1], [1, 4], [0, 0]] pts2 = [[3, 5], [4, 5], [6, 16], [1, 0]] dist, dist_list = pts_euclidean(pts1, pts2) assert CHECK_EQ_LIST_ORDERED(pts1, [[0, 1], [1, 1], [1, 4], [0, 0]]) assert CHECK_EQ_LIST_ORDERED(pts2, [[3, 5], [4, 5], [6, 16], [1, 0]]) assert dist == 6 assert dist_list == [5, 5, 13, 1] print('check numpy array with 2 elements') pts1 = np.array([0, 1]) pts2 = np.array([3, 5]) dist, dist_list = pts_euclidean(pts1, pts2) assert CHECK_EQ_NUMPY(pts1, np.array([0, 1])) assert CHECK_EQ_NUMPY(pts2, np.array([3, 5])) assert dist == 5 assert dist_list == [5] print('check numpy array with rows of 2 elements') pts1 = np.array([[0, 1], [1, 1], [1, 4], [0, 0]]).transpose() pts2 = np.array([[3, 5], [4, 5], [6, 16], [1, 0]]).transpose() print(pts1.shape) dist, dist_list = pts_euclidean(pts1, pts2) assert CHECK_EQ_NUMPY(pts1, np.array([[0, 1], [1, 1], [1, 4], [0, 0]]).transpose()) assert CHECK_EQ_NUMPY(pts2, np.array([[3, 5], [4, 5], [6, 16], [1, 0]]).transpose()) assert dist == 6 assert dist_list == [5, 5, 13, 1] ######################################## test edge cases print('check numpy array with rows of 2 elements') pts1 = np.random.rand(2, 0) pts2 = np.random.rand(2, 0) dist, dist_list = pts_euclidean(pts1, pts2) assert dist == 0 assert dist_list == [] print('\n\nDONE! SUCCESSFUL!!\n')
def test_get_center_crop_bbox(): print('check basic') bbox = [1, 1, 10, 10] crop_bbox = get_center_crop_bbox(bbox) print(bbox) print(crop_bbox) assert CHECK_EQ_NUMPY(crop_bbox, np.array([-4, -4, 10, 10]).reshape((1, 4))) print('\n\nDONE! SUCCESSFUL!!\n')
def test_safe_image(): image_path = '../lena.jpg' # test when the input image is pil image img_pil = Image.open(image_path) img_bak = np.array(img_pil).copy() copy_image, isnan = safe_image(img_pil) assert CHECK_EQ_NUMPY( copy_image, np.asarray(img_pil)), 'the original image should be equal to the copy' copy_image += 1 assert not CHECK_EQ_NUMPY( copy_image, np.asarray(img_pil)), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY(img_bak, np.asarray( img_pil)), 'the original image should be equal to the backup version' assert not isnan # test when the input image is a numpy image img_numpy = np.asarray(img_pil) # read only img_bak = img_numpy.copy() copy_image, isnan = safe_image(img_numpy) assert CHECK_EQ_NUMPY( copy_image, img_numpy), 'the original image should be equal to the copy' copy_image += 1 assert not CHECK_EQ_NUMPY( copy_image, img_numpy), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img_numpy), 'the original image should be equal to the backup version' assert not isnan print('\n\nDONE! SUCCESSFUL!!\n')
def test_image_mean(): print('test HWC with gray image') img = (np.random.rand(4, 4, 1) * 255.).astype('uint8') img_bak = img.copy() mean_img = image_mean(img) assert mean_img.shape == (4, 4, 1) assert CHECK_EQ_NUMPY( mean_img, img), 'the original image should be equal to the copy' mean_img += 1 assert not CHECK_EQ_NUMPY( mean_img, img), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('test NHWC with color images') img = (np.random.rand(12, 4, 4, 3) * 255.).astype('uint8') img_bak = img.copy() mean_img = image_mean(img) assert mean_img.shape == (4, 4, 3) assert CHECK_EQ_NUMPY( mean_img, np.mean(img, axis=0).astype( 'uint8')), 'the original image should be equal to the copy' mean_img += 1 assert not CHECK_EQ_NUMPY( mean_img, np.mean(img, axis=0).astype( 'uint8')), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('\n\nDONE! SUCCESSFUL!!\n')
def test_bbox_TLBR2TLWH(): print('check basic') bbox = [1, 1, 10, 10] clipped = bbox_TLBR2TLWH(bbox) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([1, 1, 9, 9]).reshape((1, 4))) print('check out of boundary and 0 height') bbox = [-1, 3, 20, 3] clipped = bbox_TLBR2TLWH(bbox) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([-1, 3, 21, 0]).reshape((1, 4))) print('check 0 height and width') bbox = [10, 30, 10, 30] clipped = bbox_TLBR2TLWH(bbox) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([10, 30, 0, 0]).reshape((1, 4))) print('check multi bboxes') bbox = np.array([[10, 30, 10, 30], [-1, 3, 20, 3]]) clipped = bbox_TLBR2TLWH(bbox) print(clipped) assert CHECK_EQ_NUMPY( clipped, np.array([[10, 30, 0, 0], [-1, 3, 21, 0]]).reshape((2, 4))) print('check x2 < x1') bbox = [10, 30, 9, 29] try: clipped = bbox_TLBR2TLWH(bbox) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print( 'the bottom right point coordinate should be no less than the top left one' ) print('\n\nDONE! SUCCESSFUL!!\n')
def test_bbox_TLWH2TLBR(): print('check basic') bbox = [1, 1, 10, 10] clipped = bbox_TLWH2TLBR(bbox) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([1, 1, 11, 11]).reshape((1, 4))) print('check out of boundary and 0 height') bbox = [-1, 3, 20, 0] clipped = bbox_TLWH2TLBR(bbox) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([-1, 3, 19, 3]).reshape((1, 4))) print('check 0 height and width') bbox = [10, 30, 0, 0] clipped = bbox_TLWH2TLBR(bbox) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([10, 30, 10, 30]).reshape((1, 4))) print('check multi bboxes') bbox = np.array([[10, 30, 0, 0], [-1, 3, 20, 0]]) clipped = bbox_TLWH2TLBR(bbox) print(clipped) assert CHECK_EQ_NUMPY( clipped, np.array([[10, 30, 10, 30], [-1, 3, 19, 3]]).reshape((2, 4))) print('check width < 0') bbox = [10, 30, -1, 29] try: clipped = bbox_TLWH2TLBR(bbox) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the width should be no less than 0') print('\n\nDONE! SUCCESSFUL!!\n')
def test_safe_batch_deep_image(): print('test 3HW') img = np.random.rand(3, 100, 100) img_bak = img.copy() batch_image, isnan = safe_batch_deep_image(img) assert CHECK_EQ_NUMPY(batch_image, img.reshape( (1, 3, 100, 100))), 'the original image should be equal to the copy' batch_image += 1 assert not CHECK_EQ_NUMPY(batch_image, img.reshape( (1, 3, 100, 100))), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' assert not isnan print('test N3HW') img = np.random.rand(12, 3, 100, 100) img_bak = img.copy() batch_image, isnan = safe_batch_deep_image(img) assert CHECK_EQ_NUMPY( batch_image, img), 'the original image should be equal to the copy' batch_image += 1 assert not CHECK_EQ_NUMPY( batch_image, img), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' assert not isnan ######################################## test failure cases print('test N1HW') img = np.random.rand(12, 1, 100, 100) try: batch_image, isnan = safe_batch_deep_image(img) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the batch image has wrong shape') print('test 1HW') img = np.random.rand(1, 100, 100) try: batch_image, isnan = safe_batch_deep_image(img) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the batch image has wrong shape') print('\n\nDONE! SUCCESSFUL!!\n')
def test_unpreprocess_batch_deep_image(): print('test HW3, no rgb2bgr') img = (np.random.rand(100, 200, 3) * 255.).astype('uint8') img_bak = img.copy() batch_image = preprocess_batch_deep_image(img, rgb2bgr=False) img_inv = unpreprocess_batch_deep_image(batch_image, bgr2rgb=False) assert img_inv.shape == (1, 100, 200, 3) assert CHECK_EQ_NUMPY( img, img_inv[0]), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('test NHW3, no rgb2bgr') img = (np.random.rand(12, 100, 200, 3) * 255.).astype('uint8') img_bak = img.copy() batch_image = preprocess_batch_deep_image(img, rgb2bgr=False) img_inv = unpreprocess_batch_deep_image(batch_image, bgr2rgb=False) assert img_inv.shape == (12, 100, 200, 3) assert CHECK_EQ_NUMPY( img, img_inv), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('test HW3, with rgb2bgr') img = (np.random.rand(100, 200, 3) * 255.).astype('uint8') img_bak = img.copy() batch_image = preprocess_batch_deep_image(img) img_inv = unpreprocess_batch_deep_image(batch_image) assert img_inv.shape == (1, 100, 200, 3) assert CHECK_EQ_NUMPY( img, img_inv[0]), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('test HW3, with rgb2bgr, pixel mean and std') img = (np.random.rand(12, 100, 200, 3) * 255.).astype('uint8') pixel_mean = [0.3, 0.4, 0.5] pixel_std = [0.3, 0.5, 0.7] batch_image = preprocess_batch_deep_image(img, pixel_mean=pixel_mean, pixel_std=pixel_std) img_inv = unpreprocess_batch_deep_image(batch_image, pixel_mean=pixel_mean, pixel_std=pixel_std) assert img_inv.shape == (12, 100, 200, 3) img_diff = img_inv - img assert not (np.logical_and(img_diff != 0, np.absolute(img_diff) != 255)).any() print('test HW3, with rgb2bgr, single pixel mean and std') img = (np.random.rand(12, 100, 200, 3) * 255.).astype('uint8') pixel_mean = 0.4 pixel_std = 0.5 batch_image = preprocess_batch_deep_image(img, pixel_mean=pixel_mean, pixel_std=pixel_std) img_inv = unpreprocess_batch_deep_image(batch_image, pixel_mean=pixel_mean, pixel_std=pixel_std) assert img_inv.shape == (12, 100, 200, 3) img_diff = img_inv - img assert not (np.logical_and(img_diff != 0, np.absolute(img_diff) != 255)).any() print('\n\nDONE! SUCCESSFUL!!\n')
def test_clip_bboxes_TLBR(): print('check basic') bbox = [1, 1, 10, 10] clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([1, 1, 5, 5]).reshape((1, 4))) print('check top left intersected') bbox = [-1, -1, 3, 3] clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([0, 0, 3, 3]).reshape((1, 4))) print('check bottom right intersected') bbox = [2, 3, 10, 30] clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([2, 3, 5, 5]).reshape((1, 4))) print('check left intersected') bbox = [-1, -2, 2, 30] clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([0, 0, 2, 5]).reshape((1, 4))) print('check right intersected') bbox = [2, -3, 10, 30] clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([2, 0, 5, 5]).reshape((1, 4))) print('check all intersected') bbox = [-2, -3, 10, 30] clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([0, 0, 5, 5]).reshape((1, 4))) print('check bottom right outside') bbox = [10, 10, 10, 30] clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([5, 5, 5, 5]).reshape((1, 4))) print('check top left outside') bbox = [-1, -1, -1, -1] clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([0, 0, 0, 0]).reshape((1, 4))) print('check same height') bbox = [-2, 3, 10, 3] clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([0, 3, 5, 3]).reshape((1, 4))) print('check same width') bbox = [2, -3, 2, 20] clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([2, 0, 2, 5]).reshape((1, 4))) print('check all same') bbox = [2, 3, 2, 3] clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY(clipped, np.array([2, 3, 2, 3]).reshape((1, 4))) print('check for multiple bboxes') bbox = np.array([[2, 3, 2, 3], [2, -3, 2, 20], [-2, 3, 10, 3]]) clipped = clip_bboxes_TLBR(bbox, 5, 5) print(clipped) assert CHECK_EQ_NUMPY( clipped, np.array([[2, 3, 2, 3], [2, 0, 2, 5], [0, 3, 5, 3]]).reshape((3, 4))) print('check for failure case') bbox = [-10, 30, 5, 5] try: clipped = clip_bboxes_TLBR(bbox, 5, 5) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print( 'the bottom right point coordinate should be larger than bottom left one' ) print('\n\nDONE! SUCCESSFUL!!\n')
def test_crop_center(): image_path = '../lena.jpg' img = Image.open(image_path).convert('RGB') #################################### test with 4 elements in center_rect ######################################### print('test 2d matrix intersected on the top left') np_data = (np.random.rand(5, 5) * 255.).astype('uint8') center_rect = [1, 2, 4, 6] img_padded, crop_bbox, crop_bbox_clipped = image_crop_center( np_data, center_rect=center_rect, pad_value=10) print(np_data) print(img_padded) print(img_padded.shape) assert img_padded.shape == ( 6, 4), 'the padded image does not have a good shape' assert CHECK_EQ_NUMPY(crop_bbox, np.array([[-1, -1, 4, 6]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[0, 0, 3, 5]])) print('test 2d matrix intersected on the bottom right') np_data = (np.random.rand(5, 5) * 255.).astype('uint8') center_rect = [3, 3, 5, 6] img_padded, crop_bbox, crop_bbox_clipped = image_crop_center( np_data, center_rect=center_rect, pad_value=10) print(np_data) print(img_padded) assert img_padded.shape == ( 6, 5), 'the padded image does not have a good shape' assert CHECK_EQ_NUMPY(crop_bbox, np.array([[1, 0, 5, 6]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[1, 0, 4, 5]])) print('test with color image, clipped on the left') center_rect = [0, 50, 100, 100] img_cropped, crop_bbox, crop_bbox_clipped = image_crop_center( img, center_rect=center_rect, pad_value=100) assert CHECK_EQ_NUMPY(crop_bbox, np.array([[-50, 0, 100, 100]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[0, 0, 50, 100]])) visualize_image(img, vis=True) visualize_image(img_cropped, vis=True) #################################### test with 2 elements in center_rect ######################################### print('test 2d matrix - basic') np_data = (np.random.rand(5, 5) * 255.).astype('uint8') center_rect = [3, 3] img_padded, crop_bbox, crop_bbox_clipped = image_crop_center( np_data, center_rect=center_rect, pad_value=10) print(np_data) print(img_padded) print(img_padded.shape) assert img_padded.shape == ( 3, 3), 'the padded image does not have a good shape' assert CHECK_EQ_NUMPY(crop_bbox, np.array([[1, 1, 3, 3]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[1, 1, 3, 3]])) print('test 2d matrix - boundary') np_data = (np.random.rand(5, 5) * 255.).astype('uint8') center_rect = [5, 5] img_padded, crop_bbox, crop_bbox_clipped = image_crop_center( np_data, center_rect=center_rect, pad_value=10) print(np_data) print(img_padded) assert img_padded.shape == ( 5, 5), 'the padded image does not have a good shape' assert CHECK_EQ_NUMPY(crop_bbox, np.array([[0, 0, 5, 5]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[0, 0, 5, 5]])) print('test 2d matrix - intersected') np_data = (np.random.rand(5, 5) * 255.).astype('uint8') center_rect = [7, 7] img_padded, crop_bbox, crop_bbox_clipped = image_crop_center( np_data, center_rect=center_rect, pad_value=10) print(np_data) print(img_padded) assert img_padded.shape == ( 7, 7), 'the padded image does not have a good shape' assert CHECK_EQ_NUMPY(crop_bbox, np.array([[-1, -1, 7, 7]])) assert CHECK_EQ_NUMPY(crop_bbox_clipped, np.array([[0, 0, 5, 5]])) print('test with color image, clipped on the center') center_rect = [100, 100] img_cropped, crop_bbox, crop_bbox_clipped = image_crop_center( img, center_rect=center_rect, pad_value=100) visualize_image(img, vis=True) visualize_image(img_cropped, vis=True) print('test with color image, interesected') center_rect = [600, 600] img_cropped, crop_bbox, crop_bbox_clipped = image_crop_center( img, center_rect=center_rect, pad_value=100) visualize_image(img, vis=True) visualize_image(img_cropped, vis=True) print('test with grayscale image') center_rect = [100, 100] img_cropped, crop_bbox, crop_bbox_clipped = image_crop_center( img.convert('L'), center_rect=center_rect, pad_value=100) visualize_image(img.convert('L'), vis=True) visualize_image(img_cropped, vis=True) print('\n\nDONE! SUCCESSFUL!!\n')
def test_safe_pts(): print('test single list') pts = [1, 4] good_pts = safe_pts(pts) print(good_pts) print(good_pts.shape) assert CHECK_EQ_NUMPY(good_pts, np.array(pts).reshape((2, 1))) assert CHECK_EQ_LIST_ORDERED(pts, [1, 4]) print('test list of list of 2 elements') pts = [[1, 2], [5, 8]] good_pts = safe_pts(pts) print(good_pts) print(good_pts.shape) assert CHECK_EQ_NUMPY(good_pts, np.array(pts).reshape((2, 2)).transpose()) assert CHECK_EQ_LIST_ORDERED(pts, [[1, 2], [5, 8]]) print('test (2, ) numpy array') pts = np.array([1, 4]) good_pts = safe_pts(pts) print(pts.shape) print(good_pts.shape) assert CHECK_EQ_NUMPY(good_pts, np.array(pts).reshape((2, 1))) assert CHECK_EQ_NUMPY(pts, np.array([1, 4])) print('test (2, N) numpy array') pts = np.random.rand(10, 2).transpose() pts_ori = copy.copy(pts) good_pts = safe_pts(pts) print(pts.shape) print(good_pts.shape) assert CHECK_EQ_NUMPY(good_pts, pts) assert CHECK_EQ_NUMPY(pts, pts_ori) ######################################## test failure cases print('test list of 3 elements') pts = [1, 2, 4] try: good_pts = safe_pts(pts) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the length of list should be 2') print('test list of list of 3 elements') pts = [[1, 2, 4], [5, 7, 8]] try: good_pts = safe_pts(pts) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the length of list should be 2') print('test numpy array with 3 elements') pts = np.array([1, 2, 4]).reshape(3, ) try: good_pts = safe_pts(pts) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the numpy array should be rows of 2') print('test numpy array with rows of 3') pts = np.array([[1, 2], [5, 7], [4, 6]]) try: good_pts = safe_pts(pts) sys.exit('\nwrong! never should be here\n\n') except AssertionError: print('the numpy array should be rows of 2') print('\n\nDONE! SUCCESSFUL!!\n')
def test_safe_image_like(): image_path = '../lena.jpg' print('test pil image in normal case') img_pil = Image.open(image_path) img_bak = np.array(img_pil).copy() copy_image, isnan = safe_image_like(img_pil) assert CHECK_EQ_NUMPY( copy_image, np.asarray(img_pil)), 'the original image should be equal to the copy' copy_image += 1 assert not CHECK_EQ_NUMPY( copy_image, np.asarray(img_pil)), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY(img_bak, np.asarray( img_pil)), 'the original image should be equal to the backup version' assert not isnan print('test numpy image in normal case') img_numpy = np.asarray(img_pil) # read only img_bak = img_numpy.copy() copy_image, isnan = safe_image_like(img_numpy) assert CHECK_EQ_NUMPY( copy_image, img_numpy), 'the original image should be equal to the copy' copy_image += 1 assert not CHECK_EQ_NUMPY( copy_image, img_numpy), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img_numpy), 'the original image should be equal to the backup version' assert not isnan print('test numpy image with arbitrary value') img_numpy = np.random.rand(100, 100) img_numpy += np.random.rand(100, 100) img_numpy += np.random.rand(100, 100) img_numpy += np.random.rand(100, 100) img_bak = img_numpy.copy() copy_image, isnan = safe_image_like(img_numpy) assert CHECK_EQ_NUMPY( copy_image, img_numpy), 'the original image should be equal to the copy' copy_image += 1 assert not CHECK_EQ_NUMPY( copy_image, img_numpy), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img_numpy), 'the original image should be equal to the backup version' assert not isnan print('test numpy image with nan') img_numpy = np.random.rand(100, 100) img_numpy[0, 0] = float('nan') img_bak = img_numpy.copy() copy_image, isnan = safe_image_like(img_numpy) assert isnan print('\n\nDONE! SUCCESSFUL!!\n')
def test_preprocess_batch_deep_image(): print('test HW3, no rgb2bgr') img = np.random.rand(100, 200, 3).astype('float32') img_bak = img.copy() batch_image = preprocess_batch_deep_image(img, rgb2bgr=False) assert batch_image.shape == (1, 3, 100, 200) assert CHECK_EQ_NUMPY( batch_image, np.transpose(img, (2, 0, 1)).reshape( (1, 3, 100, 200))), 'the original image should be equal to the copy' batch_image += 1 assert not CHECK_EQ_NUMPY( batch_image, np.transpose(img, (2, 0, 1)).reshape( (1, 3, 100, 200))), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('test NHW3, no rgb2bgr') img = np.random.rand(12, 100, 100, 3).astype('float32') img_bak = img.copy() batch_image = preprocess_batch_deep_image(img, rgb2bgr=False) assert CHECK_EQ_NUMPY(batch_image, np.transpose( img, (0, 3, 1, 2))), 'the original image should be equal to the copy' batch_image += 1 assert not CHECK_EQ_NUMPY(batch_image, np.transpose( img, (0, 3, 1, 2))), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('test HW3, with rgb2bgr') img = np.random.rand(100, 200, 3).astype('float32') img_bak = img.copy() batch_image = preprocess_batch_deep_image(img) assert batch_image.shape == (1, 3, 100, 200) assert CHECK_EQ_NUMPY( bgr2rgb(chw2hwc(batch_image[0])), img), 'the original image should be equal to the copy' assert CHECK_EQ_NUMPY( img_bak, img), 'the original image should be equal to the backup version' print('test HW3, with rgb2bgr, pixel mean and std') img = np.random.rand(2, 4, 3).astype('float32') img[img < 0.5] = 0.5 pixel_mean = [0.3, 0.4, 0.5] pixel_std = [0.9, 0.99, 0.999] batch_image = preprocess_batch_deep_image(img, pixel_mean=pixel_mean, pixel_std=pixel_std) assert batch_image.shape == (1, 3, 2, 4) assert_almost_equal( (chw2hwc(batch_image[0])), (bgr2rgb(img) - np.array(pixel_mean)) / np.array(pixel_std), err_msg='the original image should be equal to the copy') print('test HW3, with rgb2bgr, single pixel mean') img = np.random.rand(2, 4, 3).astype('float32') img[img < 0.5] = 0.5 pixel_mean = 0.5 pixel_std = [0.9, 0.99, 0.999] batch_image = preprocess_batch_deep_image(img, pixel_mean=pixel_mean, pixel_std=pixel_std) assert batch_image.shape == (1, 3, 2, 4) assert_almost_equal( (chw2hwc(batch_image[0])), (bgr2rgb(img) - np.array(pixel_mean)) / np.array(pixel_std), err_msg='the original image should be equal to the copy') print('test HW3, with rgb2bgr, single pixel std') img = np.random.rand(2, 4, 3).astype('float32') img[img < 0.5] = 0.5 pixel_mean = 0.5 pixel_std = 0.9 batch_image = preprocess_batch_deep_image(img, pixel_mean=pixel_mean, pixel_std=pixel_std) assert batch_image.shape == (1, 3, 2, 4) assert_almost_equal( (chw2hwc(batch_image[0])), (bgr2rgb(img) - np.array(pixel_mean)) / np.array(pixel_std), err_msg='the original image should be equal to the copy') print('\n\nDONE! SUCCESSFUL!!\n')
def test_generate_gaussian_heatmap(): print('test single point') input_pts = [300, 400, 1] image_size = [800, 600] std = 10 heatmap, mask, _ = generate_gaussian_heatmap(input_pts, image_size=image_size, std=std) assert heatmap.shape == (800, 600, 2) assert mask.shape == (1, 1, 2) assert CHECK_EQ_NUMPY(mask, np.array([[[1, 1]]])) visualize_image(heatmap[:, :, 0], vis=True) print('test two points') input_pts = [[300, 400, 1], [400, 400, 1]] image_size = [800, 600] std = 10 heatmap, mask, _ = generate_gaussian_heatmap(input_pts, image_size=image_size, std=std) assert heatmap.shape == (800, 600, 3) assert mask.shape == (1, 1, 3) assert CHECK_EQ_NUMPY(mask, np.array([[[1, 1, 1]]])) visualize_image(heatmap[:, :, -1], vis=True) visualize_image(heatmap[:, :, 0], vis=True) visualize_image(heatmap[:, :, 1], vis=True) print('test two points with invalid one') input_pts = [[300, 400, 1], [400, 400, -1]] image_size = [800, 600] std = 10 heatmap, mask, _ = generate_gaussian_heatmap(input_pts, image_size=image_size, std=std) assert heatmap.shape == (800, 600, 3) assert mask.shape == (1, 1, 3) assert CHECK_EQ_NUMPY(mask, np.array([[[1, 0, 1]]])) visualize_image(heatmap[:, :, -1], vis=True) visualize_image(heatmap[:, :, 0], vis=True) visualize_image(heatmap[:, :, 1], vis=True) print('test two points with invisible one') input_pts = [[300, 400, 1], [-400, -400, 0]] image_size = [800, 600] std = 10 heatmap, mask, mask_visible = generate_gaussian_heatmap( input_pts, image_size=image_size, std=std) assert heatmap.shape == (800, 600, 3) assert mask.shape == (1, 1, 3) assert CHECK_EQ_NUMPY(mask, np.array([[[1, 1, 1]]])) assert CHECK_EQ_NUMPY(mask_visible, np.array([[[1, 0, 1]]])) visualize_image(heatmap[:, :, -1], vis=True) visualize_image(heatmap[:, :, 0], vis=True) visualize_image(heatmap[:, :, 1], vis=True) print('test two points with invisible and invalid') input_pts = [[300, 400, -1], [-400, -400, 0]] image_size = [800, 600] std = 10 heatmap, mask, mask_visible = generate_gaussian_heatmap( input_pts, image_size=image_size, std=std) assert heatmap.shape == (800, 600, 3) assert mask.shape == (1, 1, 3) assert CHECK_EQ_NUMPY(mask, np.array([[[0, 1, 1]]])) assert CHECK_EQ_NUMPY(mask_visible, np.array([[[0, 0, 1]]])) visualize_image(heatmap[:, :, -1], vis=True) visualize_image(heatmap[:, :, 0], vis=True) visualize_image(heatmap[:, :, 1], vis=True) print('\n\nDONE! SUCCESSFUL!!\n')