Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_color_init(self):
# valid data
random_valid_data = (255.0 * np.random.rand(IM_HEIGHT, IM_WIDTH, 3)).astype(np.uint8)
im = ColorImage(random_valid_data)
self.assertEqual(im.height, IM_HEIGHT)
self.assertEqual(im.width, IM_WIDTH)
self.assertEqual(im.channels, 3)
self.assertTrue(np.allclose(im.data, random_valid_data))
# invalid channels
random_data = np.random.rand(IM_HEIGHT, IM_WIDTH).astype(np.uint8)
caught_bad_channels = False
try:
im = ColorImage(random_data)
except:
caught_bad_channels = True
self.assertTrue(caught_bad_channels)
# invalid type
random_data = np.random.rand(IM_HEIGHT, IM_WIDTH, 3).astype(np.float32)
caught_bad_dtype = False
try:
im = ColorImage(random_data)
except:
caught_bad_dtype = True
self.assertTrue(caught_bad_dtype)
def test_binary_init(self):
# valid data
random_valid_data = (255.0 * np.random.rand(IM_HEIGHT, IM_WIDTH)).astype(np.uint8)
binary_data = 255 * (random_valid_data > BINARY_THRESH)
im = BinaryImage(random_valid_data, threshold=BINARY_THRESH)
self.assertEqual(im.height, IM_HEIGHT)
self.assertEqual(im.width, IM_WIDTH)
self.assertEqual(im.channels, 1)
self.assertTrue(np.allclose(im.data, binary_data))
# invalid channels
random_data = np.random.rand(IM_HEIGHT, IM_WIDTH, 3).astype(np.uint8)
caught_bad_channels = False
try:
im = BinaryImage(random_data)
except:
caught_bad_channels = True
self.assertTrue(caught_bad_channels)
# invalid type
random_data = np.random.rand(IM_HEIGHT, IM_WIDTH).astype(np.float32)
def test_transform(self):
random_valid_data = (255.0 * np.random.rand(IM_HEIGHT, IM_WIDTH, 3)).astype(np.uint8)
im = ColorImage(random_valid_data)
translation = np.array([2,2])
im_tf = im.transform(translation, 0.0)
self.assertTrue(np.allclose(im[0,0], im_tf[2,2]))
def test_resize(self):
random_valid_data = (255.0 * np.random.rand(IM_HEIGHT, IM_WIDTH, 3)).astype(np.uint8)
im = ColorImage(random_valid_data)
big_scale = 2.0
big_im = im.resize(big_scale)
self.assertEqual(big_im.height, big_scale * IM_HEIGHT)
self.assertEqual(big_im.width, big_scale * IM_WIDTH)
small_scale = 0.5
small_im = im.resize(small_scale)
self.assertEqual(small_im.height, small_scale * IM_HEIGHT)
self.assertEqual(small_im.width, small_scale * IM_WIDTH)
'tmkl2':
hashers.TMKL2(frames_per_second=15),
'tmkl1':
hashers.TMKL1(frames_per_second=15)
}
for filepath in [
'perception/testing/videos/v1.m4v',
'perception/testing/videos/v2.m4v'
]:
# Ensure synchronized hashing
hashes1 = {
hasher_name: hasher.compute(filepath)
for hasher_name, hasher in video_hashers.items()
}
hashes2 = hashers.tools.compute_synchronized_video_hashes(
filepath=filepath, hashers=video_hashers)
assert hashes1 == hashes2
def test_opencv_hasher(hasher: hashers.ImageHasher, image1: str, image2: str):
# For OpenCV hashers we make sure the distance we compute
# is the same as inside OpenCV
f1 = image1
f2 = image2
opencv_distance = hasher.hasher.compare(
hasher.hasher.compute(hashers.tools.read(f1)),
hasher.hasher.compute(hashers.tools.read(f2)))
if hasher.distance_metric == 'hamming':
opencv_distance /= hasher.hash_length
np.testing.assert_approx_equal(
opencv_distance,
hasher.compute_distance(hasher.compute(f1), hasher.compute(f2)),
significant=4)
def main():
ids = discover_cams()
assert ids, "[!] No camera detected."
cfg = {}
cfg['cam_id'] = ids[0]
cfg['filter_depth'] = True
cfg['frame'] = 'realsense_overhead'
sensor = RgbdSensorFactory.sensor('realsense', cfg)
sensor.start()
camera_intr = sensor.color_intrinsics
color_im, depth_im, _ = sensor.frames()
sensor.stop()
print("intrinsics matrix: {}".format(camera_intr.K))
fig, axes = plt.subplots(1, 2)
for ax, im in zip(axes, [color_im.data, depth_im.data]):
ax.imshow(im)
ax.axis('off')
plt.show()
logger.info('Capturing images from sensor %s' %(sensor_name))
save_dir = os.path.join(output_dir, sensor_name)
if not os.path.exists(save_dir):
os.mkdir(save_dir)
# read params
sensor_type = sensor_config['type']
sensor_frame = sensor_config['frame']
# read camera calib
tf_filename = '%s_to_world.tf' %(sensor_frame)
T_camera_world = RigidTransform.load(os.path.join(config['calib_dir'], sensor_frame, tf_filename))
T_camera_world.save(os.path.join(save_dir, tf_filename))
# setup sensor
sensor = RgbdSensorFactory.sensor(sensor_type, sensor_config)
# start the sensor
sensor.start()
camera_intr = sensor.ir_intrinsics
camera_intr.save(os.path.join(save_dir, '%s.intr' %(sensor.frame)))
# get raw images
for i in range(sensor_config['num_images']):
logger.info('Capturing image %d' %(i))
message = 'Hit ENTER when ready.'
utils.keyboard_input(message=message)
# read images
color, depth, ir = sensor.frames()
# save processed images
self.assertTrue(np.allclose(im.data, binary_data))
# invalid channels
random_data = np.random.rand(IM_HEIGHT, IM_WIDTH, 3).astype(np.uint8)
caught_bad_channels = False
try:
im = BinaryImage(random_data)
except:
caught_bad_channels = True
self.assertTrue(caught_bad_channels)
# invalid type
random_data = np.random.rand(IM_HEIGHT, IM_WIDTH).astype(np.float32)
caught_bad_dtype = False
try:
im = BinaryImage(random_data)
except:
caught_bad_dtype = True
self.assertTrue(caught_bad_dtype)
def test_io(self, height=50, width=100):
color_data = (255 * np.random.rand(height, width, 3)).astype(np.uint8)
im = ColorImage(color_data, 'a')
file_root = COLOR_IM_FILEROOT
# save and load png
filename = file_root + '.png'
im.save(filename)
loaded_im = ColorImage.open(filename)
self.assertTrue(np.sum(np.abs(loaded_im.data - im.data)) < 1e-5, msg='ColorImage data changed after load png')
# save and load jpg
filename = file_root + '.jpg'
im.save(filename)
loaded_im = ColorImage.open(filename)
# save and load npy
filename = file_root + '.npy'
im.save(filename)
loaded_im = ColorImage.open(filename)
self.assertTrue(np.sum(np.abs(loaded_im.data - im.data)) < 1e-5, msg='ColorImage data changed after load npy')
# save and load npz
filename = file_root + '.npz'
im.save(filename)
loaded_im = ColorImage.open(filename)
self.assertTrue(np.sum(np.abs(loaded_im.data - im.data)) < 1e-5, msg='ColorImage data changed after load npz')