-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate image.ts into separate files and add it to the generation sc…
…ript (#192)
- Loading branch information
Kevin Scott
authored
Feb 3, 2022
1 parent
6972db9
commit 66c041a
Showing
9 changed files
with
158 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ ignore: | |
- "scripts" | ||
- "test" | ||
- "packages/test-scaffolding" | ||
- "*.generated.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/upscalerjs/src/image.test.ts → ...ages/upscalerjs/src/image.browser.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { tf } from './dependencies.generated'; | ||
import { isHTMLImageElement, isString, isFourDimensionalTensor, isThreeDimensionalTensor, isTensor } from './utils'; | ||
|
||
export const getUnknownError = (input: any) => new Error( | ||
[ | ||
`Unknown input provided to loadImage that cannot be processed: ${JSON.stringify(input)}`, | ||
`Can only handle a string pointing to a valid image resource, an HTMLImageElement element,`, | ||
`or a 3 or 4 rank tensor.`, | ||
].join(' '), | ||
); | ||
|
||
export const getInvalidTensorError = (input: tf.Tensor) => new Error( | ||
[ | ||
`Unsupported dimensions for incoming pixels: ${input.shape.length}.`, | ||
'Only 3 or 4 rank tensors are supported.', | ||
].join(' '), | ||
); | ||
|
||
export const getImageAsPixels = async ( | ||
pixels: string | HTMLImageElement | tf.Tensor, | ||
): Promise<{ | ||
tensor: tf.Tensor4D; | ||
type: 'string' | 'HTMLImageElement' | 'tensor'; | ||
}> => { | ||
if (isString(pixels)) { | ||
const img = await new Promise<HTMLImageElement>((resolve, reject) => { | ||
const img = new Image(); | ||
img.src = pixels; | ||
img.crossOrigin = 'anonymous'; | ||
img.onload = () => resolve(img); | ||
img.onerror = reject; | ||
}); | ||
return { | ||
tensor: tf.browser.fromPixels(img).expandDims(0) as tf.Tensor4D, | ||
type: 'string', | ||
}; | ||
} | ||
|
||
if (isHTMLImageElement(pixels)) { | ||
return { | ||
tensor: tf.browser.fromPixels(pixels).expandDims(0) as tf.Tensor4D, | ||
type: 'HTMLImageElement', | ||
}; | ||
} | ||
|
||
if (isTensor(pixels)) { | ||
if (isFourDimensionalTensor(pixels)) { | ||
return { | ||
tensor: pixels, | ||
type: 'tensor', | ||
}; | ||
} | ||
|
||
if (isThreeDimensionalTensor(pixels)) { | ||
return { | ||
tensor: pixels.expandDims(0) as tf.Tensor4D, | ||
type: 'tensor', | ||
}; | ||
} | ||
|
||
throw getInvalidTensorError(pixels); | ||
} | ||
|
||
throw getUnknownError(pixels); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { tf } from './dependencies.generated'; | ||
import { isHTMLImageElement, isString, isFourDimensionalTensor, isThreeDimensionalTensor, isTensor } from './utils'; | ||
|
||
export const getUnknownError = (input: any) => new Error( | ||
[ | ||
`Unknown input provided to loadImage that cannot be processed: ${JSON.stringify(input)}`, | ||
`Can only handle a string pointing to a valid image resource, an HTMLImageElement element,`, | ||
`or a 3 or 4 rank tensor.`, | ||
].join(' '), | ||
); | ||
|
||
export const getInvalidTensorError = (input: tf.Tensor) => new Error( | ||
[ | ||
`Unsupported dimensions for incoming pixels: ${input.shape.length}.`, | ||
'Only 3 or 4 rank tensors are supported.', | ||
].join(' '), | ||
); | ||
|
||
export const getImageAsPixels = async ( | ||
pixels: string | HTMLImageElement | tf.Tensor, | ||
): Promise<{ | ||
tensor: tf.Tensor4D; | ||
type: 'string' | 'HTMLImageElement' | 'tensor'; | ||
}> => { | ||
if (isString(pixels)) { | ||
const img = await new Promise<HTMLImageElement>((resolve, reject) => { | ||
const img = new Image(); | ||
img.src = pixels; | ||
img.crossOrigin = 'anonymous'; | ||
img.onload = () => resolve(img); | ||
img.onerror = reject; | ||
}); | ||
return { | ||
tensor: tf.browser.fromPixels(img).expandDims(0) as tf.Tensor4D, | ||
type: 'string', | ||
}; | ||
} | ||
|
||
if (isHTMLImageElement(pixels)) { | ||
return { | ||
tensor: tf.browser.fromPixels(pixels).expandDims(0) as tf.Tensor4D, | ||
type: 'HTMLImageElement', | ||
}; | ||
} | ||
|
||
if (isTensor(pixels)) { | ||
if (isFourDimensionalTensor(pixels)) { | ||
return { | ||
tensor: pixels, | ||
type: 'tensor', | ||
}; | ||
} | ||
|
||
if (isThreeDimensionalTensor(pixels)) { | ||
return { | ||
tensor: pixels.expandDims(0) as tf.Tensor4D, | ||
type: 'tensor', | ||
}; | ||
} | ||
|
||
throw getInvalidTensorError(pixels); | ||
} | ||
|
||
throw getUnknownError(pixels); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters