(
content: Uint8Array, channels = 0, dtype = 'int32',
expandAnimations = true)
| 153 | * @doc {heading: 'Operations', subheading: 'Images', namespace: 'node'} |
| 154 | */ |
| 155 | export function decodeImage( |
| 156 | content: Uint8Array, channels = 0, dtype = 'int32', |
| 157 | expandAnimations = true): Tensor3D|Tensor4D { |
| 158 | util.assert( |
| 159 | dtype === 'int32', |
| 160 | () => 'decodeImage could only return Tensor of type `int32` for now.'); |
| 161 | |
| 162 | const imageType = getImageType(content); |
| 163 | |
| 164 | // The return tensor has dtype uint8, which is not supported in |
| 165 | // TensorFlow.js, casting it to int32 which is the default dtype for image |
| 166 | // tensor. If the image is BMP, JPEG or PNG type, expanding the tensors |
| 167 | // shape so it becomes Tensor4D, which is the default tensor shape for image |
| 168 | // ([batch,imageHeight,imageWidth, depth]). |
| 169 | switch (imageType) { |
| 170 | case ImageType.JPEG: |
| 171 | return decodeJpeg(content, channels); |
| 172 | case ImageType.PNG: |
| 173 | return decodePng(content, channels); |
| 174 | case ImageType.GIF: |
| 175 | // If not to expand animations, take first frame of the gif and return |
| 176 | // as a 3D tensor. |
| 177 | return tidy(() => { |
| 178 | const img = decodeGif(content); |
| 179 | return expandAnimations ? img : img.slice(0, 1).squeeze([0]); |
| 180 | }); |
| 181 | case ImageType.BMP: |
| 182 | return decodeBmp(content, channels); |
| 183 | default: |
| 184 | return null; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Encodes an image tensor to JPEG. |
nothing calls this directly
no test coverage detected
searching dependent graphs…