def testGif(self): # Read some real GIFs path = os.path.join(prefix_path, "gif", "testdata", "scan.gif") WIDTH = 20 HEIGHT = 40 STRIDE = 5 shape = (12, HEIGHT, WIDTH, 3) with self.test_session(use_gpu=True) as sess: gif0 = io_ops.read_file(path) image0 = image_ops.decode_image(gif0) image1 = image_ops.decode_gif(gif0) gif0, image0, image1 = sess.run([gif0, image0, image1]) self.assertEqual(image0.shape, shape) self.assertAllEqual(image0, image1) for frame_idx, frame in enumerate(image0): gt = np.zeros(shape[1:], dtype=np.uint8) start = frame_idx * STRIDE end = (frame_idx + 1) * STRIDE if end <= WIDTH: gt[:, start:end, :] = 255 else: start -= WIDTH end -= WIDTH gt[start:end, :, :] = 255 self.assertAllClose(frame, gt) bad_channels = image_ops.decode_image(gif0, channels=1) with self.assertRaises(errors_impl.InvalidArgumentError): bad_channels.eval()
def testGif(self): # Read some real GIFs path = os.path.join(prefix_path, "gif", "testdata", "scan.gif") width = 20 height = 40 stride = 5 shape = (12, height, width, 3) with self.session(use_gpu=True) as sess: gif0 = io_ops.read_file(path) image0 = image_ops.decode_image(gif0) image1 = image_ops.decode_gif(gif0) gif0, image0, image1 = sess.run([gif0, image0, image1]) self.assertEqual(image0.shape, shape) self.assertAllEqual(image0, image1) for frame_idx, frame in enumerate(image0): gt = np.zeros(shape[1:], dtype=np.uint8) start = frame_idx * stride end = (frame_idx + 1) * stride if end <= width: gt[:, start:end, :] = 255 else: start -= width end -= width gt[start:end, :, :] = 255 self.assertAllClose(frame, gt) bad_channels = image_ops.decode_image(gif0, channels=1) with self.assertRaises(errors_impl.InvalidArgumentError): self.evaluate(bad_channels)
def testGif(self): # Read some real GIFs path = os.path.join(prefix_path, "gif", "testdata", "scan.gif") width = 20 height = 40 stride = 5 shape = (12, height, width, 3) with self.session(use_gpu=True) as sess: gif0 = io_ops.read_file(path) image0 = image_ops.decode_image(gif0) image1 = image_ops.decode_gif(gif0) gif0, image0, image1 = self.evaluate([gif0, image0, image1]) self.assertEqual(image0.shape, shape) self.assertAllEqual(image0, image1) for frame_idx, frame in enumerate(image0): gt = np.zeros(shape[1:], dtype=np.uint8) start = frame_idx * stride end = (frame_idx + 1) * stride if end <= width: gt[:, start:end, :] = 255 else: start -= width end -= width gt[start:end, :, :] = 255 self.assertAllClose(frame, gt) bad_channels = image_ops.decode_image(gif0, channels=1) with self.assertRaises(errors_impl.InvalidArgumentError): self.evaluate(bad_channels)
def _decode(self, image_buffer, mask_buffer): """Decodes the image buffer. Args: image_buffer: The tensor representing the encoded image tensor. mask_buffer: The tensor representing the encoded mask tensor. Returns: A tensor that represents decoded image of self._shape, or (?, ?, self._channels) if self._shape is not specified. """ image = image_ops.decode_image(image_buffer, 3) # Expected to be in jpg mask = image_ops.decode_image(mask_buffer, 1) # Expected to be in png composite = tf.stack([image, mask], axis=-1) assert tf.shape(composite)[2] == 4 return composite
def testJpeg(self): # Read a real jpeg and verify shape path = os.path.join(prefix_path, "jpeg", "testdata", "jpeg_merge_test1.jpg") with self.session(): jpeg0 = io_ops.read_file(path) image0 = image_ops.decode_image(jpeg0) image1 = image_ops.decode_jpeg(jpeg0) jpeg0, image0, image1 = self.evaluate([jpeg0, image0, image1]) self.assertEqual(len(jpeg0), 3771) self.assertEqual(image0.shape, (256, 128, 3)) self.assertAllEqual(image0, image1) with self.assertRaises(errors_impl.InvalidArgumentError): bad_channels = image_ops.decode_image(jpeg0, channels=4) self.evaluate(bad_channels)
def testJpeg(self): # Read a real jpeg and verify shape path = os.path.join(prefix_path, "jpeg", "testdata", "jpeg_merge_test1.jpg") with self.session(use_gpu=True) as sess: jpeg0 = io_ops.read_file(path) image0 = image_ops.decode_image(jpeg0) image1 = image_ops.decode_jpeg(jpeg0) jpeg0, image0, image1 = self.evaluate([jpeg0, image0, image1]) self.assertEqual(len(jpeg0), 3771) self.assertEqual(image0.shape, (256, 128, 3)) self.assertAllEqual(image0, image1) bad_channels = image_ops.decode_image(jpeg0, channels=4) with self.assertRaises(errors_impl.InvalidArgumentError): self.evaluate(bad_channels)
def path_to_image(path, image_size, num_channels, interpolation): img = io_ops.read_file(path) img = image_ops.decode_image( img, channels=num_channels, expand_animations=False) img = image_ops.resize_images_v2(img, image_size, method=interpolation) img.set_shape((image_size[0], image_size[1], num_channels)) return img
def path_to_image(path, image_size, num_channels, interpolation): img = io_ops.read_file(path) img = image_ops.decode_image(img, channels=num_channels, expand_animations=False) #img = image_ops.resize_images_v2(img, image_size, method=interpolation) #---------------------- This is the custom resizing size = image_size lo_dim = min(size) # Take width/height initial_width = shape(img)[0] initial_height = shape(img)[1] # Take the greater value, and use it for the ratio min_ = minimum(initial_width, initial_height) ratio = cast(min_, "float") / constant(lo_dim, dtype=float32) new_width = cast(cast(initial_width, "float") / ratio, "int32") new_height = cast(cast(initial_height, "float") / ratio, "int32") img = image.resize(img, [new_width, new_height]) img = image.resize_with_crop_or_pad(img, size[0], size[1]) #---------------------- img.set_shape((image_size[0], image_size[1], num_channels)) return img
def predic(request): image = request.FILES['image-file'] fs = FileSystemStorage() imagepath = fs.save(image.name, image) imagepath = fs.url(imagepath) test_image = '.' + imagepath print(test_image) #img = tf.keras.preprocessing.image.load_img(test_image) #x = tf.keras.preprocessing.image.img_to_array(img) #x = tf.data.Dataset.from_tensors(x) img = io_ops.read_file(test_image) img = image_ops.decode_image(img, channels=3, expand_animations=False) img = image_ops.resize_images_v2(img, (256, 256), method='bilinear') img.set_shape((256, 256, 3)) x = tf.data.Dataset.from_tensors(img) x = x.batch(1) #x = x.batch(1) #data = next(iter(x)) #img_path = os.path.join(BASE_DIR, 'media/photo') #img_path = os.path.dirname(img_path) # test_dataset = tf.keras.preprocessing.image_dataset_from_directory( # img_path, color_mode='rgb', batch_size=1) result = model.predict(x) result_ph = np.asarray(result[0][0]) result_normal = np.asarray(result[0][1]) contex = { 'result_ph': result_normal, 'result_normal': result_ph, } return render(request, "app/predict.html", context=contex)
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 testJpeg(self): # Read a real jpeg and verify shape path = os.path.join(prefix_path, "jpeg", "testdata", "jpeg_merge_test1.jpg") with self.test_session(use_gpu=True) as sess: jpeg0 = io_ops.read_file(path) image0 = image_ops.decode_image(jpeg0) image1 = image_ops.decode_jpeg(jpeg0) jpeg0, image0, image1 = sess.run([jpeg0, image0, image1]) self.assertEqual(len(jpeg0), 3771) self.assertEqual(image0.shape, (256, 128, 3)) self.assertAllEqual(image0, image1)
def testPng(self): # Read some real PNGs, converting to different channel numbers inputs = [(1, "lena_gray.png")] for channels_in, filename in inputs: for channels in 0, 1, 3, 4: with self.cached_session() as sess: path = os.path.join(prefix_path, "png", "testdata", filename) png0 = io_ops.read_file(path) image0 = image_ops.decode_image(png0, channels=channels) image1 = image_ops.decode_png(png0, channels=channels) png0, image0, image1 = self.evaluate([png0, image0, image1]) self.assertEqual(image0.shape, (26, 51, channels or channels_in)) self.assertAllEqual(image0, image1)
def testPng(self): # Read some real PNGs, converting to different channel numbers inputs = [(1, "lena_gray.png")] for channels_in, filename in inputs: for channels in 0, 1, 3, 4: with self.cached_session(use_gpu=True) as sess: path = os.path.join(prefix_path, "png", "testdata", filename) png0 = io_ops.read_file(path) image0 = image_ops.decode_image(png0, channels=channels) image1 = image_ops.decode_png(png0, channels=channels) png0, image0, image1 = self.evaluate([png0, image0, image1]) self.assertEqual(image0.shape, (26, 51, channels or channels_in)) self.assertAllEqual(image0, image1)
def load_image(path): print("Query: ") img_disp = PIL.Image.open(path) img_disp.thumbnail((224, 224)) image = io_ops.read_file(path) image = image_ops.decode_image(image, channels=3, expand_animations=False) image = image_ops.resize_images_v2(image, (224, 224), method='bilinear') image.set_shape((224, 224, 3)) image = image.numpy() image = tf.keras.applications.mobilenet_v2.preprocess_input(image) image = np.array([image]) return image
def load_image(path, image_size, num_channels, interpolation, smart_resize=False): """Load an image from a path and resize it.""" img = io_ops.read_file(path) img = image_ops.decode_image( img, channels=num_channels, expand_animations=False) if smart_resize: img = keras_image_ops.smart_resize(img, image_size, interpolation=interpolation) else: img = image_ops.resize_images_v2(img, image_size, method=interpolation) img.set_shape((image_size[0], image_size[1], num_channels)) return img
def testPng(self): # Read some real PNGs, converting to different channel numbers inputs = [(1, 'lena_gray.png')] for channels_in, filename in inputs: for channels in 0, 1, 3: with self.test_session(use_gpu=True) as sess: path = os.path.join(prefix_path, 'png', 'testdata', filename) png0 = io_ops.read_file(path) image0 = image_ops.decode_image(png0, channels=channels) image1 = image_ops.decode_png(png0, channels=channels) png0, image0, image1 = sess.run([png0, image0, image1]) self.assertEqual(image0.shape, (26, 51, channels or channels_in)) self.assertAllEqual(image0, image1)
def get_image(width, height, want_grayscale, filepath): """Returns an image loaded into an np.ndarray with dims [height, width, (3 or 1)]. Args: width: Width to rescale the image to. height: Height to rescale the image to. want_grayscale: Whether the result should be converted to grayscale. filepath: Path of the image file.. Returns: np.ndarray of shape (height, width, channels) where channels is 1 if want_grayscale is true, otherwise 3. """ with ops.Graph().as_default(): with session.Session(): file_data = io_ops.read_file(filepath) channels = 1 if want_grayscale else 3 image_tensor = image_ops.decode_image(file_data, channels=channels).eval() resized_tensor = image_ops.resize_images_v2( image_tensor, (height, width)).eval() return resized_tensor
def testInvalidChannels(self): image_bytes = b"unused" with self.assertRaises(ValueError): decode = image_ops.decode_image(image_bytes, channels=4)
def testInvalidBytes(self): image_bytes = b"ThisIsNotAnImage!" decode = image_ops.decode_image(image_bytes) with self.cached_session(): with self.assertRaises(errors_impl.InvalidArgumentError): self.evaluate(decode)
def decode_image(): """Decodes a image based on the headers.""" return image_ops.decode_image(image_buffer, channels=self._channels)
def decode_image(): """Decodes a png or jpg based on the headers.""" return image_ops.decode_image(image_buffer, self._channels)
def f2(): # Some operations that XLA cannot compile. image_ops.decode_image(io_ops.read_file('/tmp/bmp')) return array_ops.constant(31)
def decode_image(): """Decodes a image based on the headers.""" return math_ops.cast( image_ops.decode_image(image_buffer, channels=self._channels), self._dtype)
def decode_image(): return tf.cast(image_ops.decode_image(image_buffer, channels=3), tf.uint8)
def testInvalidBytes(self): image_bytes = b'ThisIsNotAnImage!' decode = image_ops.decode_image(image_bytes) with self.test_session(): with self.assertRaises(errors_impl.InvalidArgumentError): decode.eval()
def decode_image(): return image_ops.decode_image(image_buffer, channels=num_channels)