def testGrayscale(self): img_bytes = [[[255], [0]], [[255], [0]]] encoded_bytes = [ 0x42, 0x40, 0x3d, 0, 0, 0, 0, 0, 0, 0, 0x36, 0, 0, 0, 0x28, 0, 0, 0, 0x2, 0, 0, 0, 0x2, 0, 0, 0, 0x1, 0, 0x8, 0, 0, 0, 0, 0, 0x10, 0, 0, 0, 0x13, 0xb, 0, 0, 0x13, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0, 0, 0, 0xff, 0, 0, 0, ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = image_ops.decode_bmp(img_in) with self.test_session(): decoded = decode.eval() self.assertAllEqual(decoded, img_bytes)
def _loadFileAndTest(self, filename, width, height, frames, bmp_filename, index): """Loads an video file and validates the output tensor. Args: filename: The filename of the input file. width: The width of the video. height: The height of the video. frames: The frames of the video. """ with self.test_session(): path = os.path.join(resource_loader.get_data_files_path(), 'testdata', filename) with open(path, 'rb') as f: contents = f.read() bmp_path = os.path.join(resource_loader.get_data_files_path(), 'testdata', bmp_filename) with open(bmp_path, 'rb') as f: bmp_contents = f.read() image_op = image_ops.decode_bmp(bmp_contents) image = image_op.eval() self.assertEqual(image.shape, (height, width, 3)) video_op = ffmpeg.decode_video(contents) video = video_op.eval() self.assertEqual(video.shape, (frames, height, width, 3)) self.assertAllEqual(video[index,:,:,:], image)
def testex1(self): img_bytes = [[[0, 0, 255], [0, 255, 0]], [[255, 0, 0], [255, 255, 255]]] # Encoded BMP bytes from Wikipedia encoded_bytes = [ 0x42, 0x40, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x36, 0, 0, 0, 0x28, 0, 0, 0, 0x2, 0, 0, 0, 0x2, 0, 0, 0, 0x1, 0, 0x18, 0, 0, 0, 0, 0, 0x10, 0, 0, 0, 0x13, 0xb, 0, 0, 0x13, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0, 0xff, 0, 0, 0, 0xff, 0, 0, 0, ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = array_ops.squeeze(image_ops.decode_bmp(img_in)) with self.test_session(): decoded = decode.eval() self.assertAllEqual(decoded, img_bytes)
def _loadFileAndTest(self, filename, width, height, frames, bmp_filename, index): """Loads an video file and validates the output tensor. Args: filename: The filename of the input file. width: The width of the video. height: The height of the video. frames: The frames of the video. bmp_filename: The filename for the bmp file. index: Index location inside the video. """ with self.cached_session(): path = os.path.join(resource_loader.get_data_files_path(), 'testdata', filename) with open(path, 'rb') as f: contents = f.read() bmp_path = os.path.join(resource_loader.get_data_files_path(), 'testdata', bmp_filename) with open(bmp_path, 'rb') as f: bmp_contents = f.read() image_op = image_ops.decode_bmp(bmp_contents) image = image_op.eval() self.assertEqual(image.shape, (height, width, 3)) video_op = ffmpeg.decode_video(contents) video = video_op.eval() self.assertEqual(video.shape, (frames, height, width, 3)) # ffmpeg produces results where channels can be off 1. self.assertAllClose(video[index, :, :, :], image, atol=1)
def testIncompleteBody(self): # Encoded BMP bytes from Wikipedia encoded_bytes = [ 0x42, 0x40, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x36, 0, 0, 0, 0x28, 0, 0, 0, 0x2, 0, 0, 0, 0x2, 0, 0, 0, 0x1, 0, 0x18, 0, 0, 0, 0, 0, 0x10, 0, 0, 0, 0x13, 0xb, 0, 0, 0x13, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0, ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = array_ops.squeeze(image_ops.decode_bmp(img_in)) with self.test_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "requires at least 68 bytes, got 62 bytes"): decoded = decode.eval()
def _loadFileAndTest(self, filename, width, height, frames, bmp_filename, index): """Loads an video file and validates the output tensor. Args: filename: The filename of the input file. width: The width of the video. height: The height of the video. frames: The frames of the video. """ with self.test_session(): path = os.path.join(resource_loader.get_data_files_path(), 'testdata', filename) with open(path, 'rb') as f: contents = f.read() bmp_path = os.path.join(resource_loader.get_data_files_path(), 'testdata', bmp_filename) with open(bmp_path, 'rb') as f: bmp_contents = f.read() image_op = image_ops.decode_bmp(bmp_contents) image = image_op.eval() self.assertEqual(image.shape, (height, width, 3)) video_op = ffmpeg.decode_video(contents) video = video_op.eval() self.assertEqual(video.shape, (frames, height, width, 3)) self.assertAllEqual(video[index, :, :, :], image)
def testBmp(self): # Read a real bmp and verify shape path = os.path.join(prefix_path, "bmp", "testdata", "lena.bmp") with self.session(use_gpu=True) as sess: bmp0 = io_ops.read_file(path) image0 = image_ops.decode_image(bmp0) image1 = image_ops.decode_bmp(bmp0) bmp0, image0, image1 = self.evaluate([bmp0, image0, image1]) self.assertEqual(len(bmp0), 4194) self.assertAllEqual(image0, image1)
def testBmp(self): # Read a real bmp and verify shape path = os.path.join(prefix_path, "bmp", "testdata", "lena.bmp") with self.session(use_gpu=True) as sess: bmp0 = io_ops.read_file(path) image0 = image_ops.decode_image(bmp0) image1 = image_ops.decode_bmp(bmp0) bmp0, image0, image1 = sess.run([bmp0, image0, image1]) self.assertEqual(len(bmp0), 4194) self.assertAllEqual(image0, image1)
def testIncompleteHeader(self): # Encoded BMP bytes from Wikipedia encoded_bytes = [ 0x42, 0x40, 0x46, 0, 0, 0, ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = array_ops.squeeze(image_ops.decode_bmp(img_in)) with self.test_session(): with self.assertRaisesRegexp(errors_impl.InvalidArgumentError, "requires at least 32 bytes to find the header"): decoded = decode.eval()
def testIncompleteHeader(self): # Encoded BMP bytes from Wikipedia encoded_bytes = [ 0x42, 0x40, 0x46, 0, 0, 0, ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = array_ops.squeeze(image_ops.decode_bmp(img_in)) with self.test_session(): with self.assertRaisesRegexp( errors_impl.InvalidArgumentError, "requires at least 32 bytes to find the header"): decoded = decode.eval()
def testex1(self): img_bytes = [[[0, 0, 255], [0, 255, 0]], [[255, 0, 0], [255, 255, 255]]] # Encoded BMP bytes from Wikipedia # BMP header bytes: https://en.wikipedia.org/wiki/List_of_file_signatures encoded_bytes = [ 0x42, 0x4d, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x36, 0, 0, 0, 0x28, 0, 0, 0, 0x2, 0, 0, 0, 0x2, 0, 0, 0, 0x1, 0, 0x18, 0, 0, 0, 0, 0, 0x10, 0, 0, 0, 0x13, 0xb, 0, 0, 0x13, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0, 0xff, 0, 0, 0, 0xff, 0, 0, 0, ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = array_ops.squeeze(image_ops.decode_bmp(img_in)) with self.cached_session(): decoded = self.evaluate(decode) self.assertAllEqual(decoded, img_bytes)
def testGrayscale(self): img_bytes = [[[255], [0]], [[255], [0]]] # BMP header bytes: https://en.wikipedia.org/wiki/List_of_file_signatures encoded_bytes = [ 0x42, 0x4d, 0x3d, 0, 0, 0, 0, 0, 0, 0, 0x36, 0, 0, 0, 0x28, 0, 0, 0, 0x2, 0, 0, 0, 0x2, 0, 0, 0, 0x1, 0, 0x8, 0, 0, 0, 0, 0, 0x10, 0, 0, 0, 0x13, 0xb, 0, 0, 0x13, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0, 0, 0, 0xff, 0, 0, 0, ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) # TODO(b/159600494): Currently, `decode_bmp` op does not validate input # magic bytes. decode = image_ops.decode_bmp(img_in) with self.cached_session(): decoded = self.evaluate(decode) self.assertAllEqual(decoded, img_bytes)
def testex1(self): img_bytes = [[[0, 0, 255], [0, 255, 0]], [[255, 0, 0], [255, 255, 255]]] # Encoded BMP bytes from Wikipedia encoded_bytes = [ 0x42, 0x40, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x36, 0, 0, 0, 0x28, 0, 0, 0, 0x2, 0, 0, 0, 0x2, 0, 0, 0, 0x1, 0, 0x18, 0, 0, 0, 0, 0, 0x10, 0, 0, 0, 0x13, 0xb, 0, 0, 0x13, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0, 0xff, 0, 0, 0, 0xff, 0, 0, 0, ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = array_ops.squeeze(image_ops.decode_bmp(img_in)) with self.test_session(): decoded = decode.eval() self.assertAllEqual(decoded, img_bytes)
def testGrayscale(self): img_bytes = [[[255], [0]], [[255], [0]]] encoded_bytes = [ 0x42, 0x40, 0x3d, 0, 0, 0, 0, 0, 0, 0, 0x36, 0, 0, 0, 0x28, 0, 0, 0, 0x2, 0, 0, 0, 0x2, 0, 0, 0, 0x1, 0, 0x8, 0, 0, 0, 0, 0, 0x10, 0, 0, 0, 0x13, 0xb, 0, 0, 0x13, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0, 0, 0, 0xff, 0, 0, 0, ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = image_ops.decode_bmp(img_in) with self.test_session(): decoded = decode.eval() self.assertAllEqual(decoded, img_bytes)
def testIncompleteBody(self): # Encoded BMP bytes from Wikipedia encoded_bytes = [ 0x42, 0x40, 0x46, 0, 0, 0, 0, 0, 0, 0, 0x36, 0, 0, 0, 0x28, 0, 0, 0, 0x2, 0, 0, 0, 0x2, 0, 0, 0, 0x1, 0, 0x18, 0, 0, 0, 0, 0, 0x10, 0, 0, 0, 0x13, 0xb, 0, 0, 0x13, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff, 0, 0, ] byte_string = bytes(bytearray(encoded_bytes)) img_in = constant_op.constant(byte_string, dtype=dtypes.string) decode = array_ops.squeeze(image_ops.decode_bmp(img_in)) with self.test_session(): with self.assertRaisesRegexp( errors_impl.InvalidArgumentError, "requires at least 68 bytes, got 62 bytes"): decoded = decode.eval()