(inputs: TNetInput)
| 13 | * @returns A NetInput instance, which can be passed into one of the neural networks. |
| 14 | */ |
| 15 | export async function toNetInput(inputs: TNetInput): Promise<NetInput> { |
| 16 | if (inputs instanceof NetInput) { |
| 17 | return inputs |
| 18 | } |
| 19 | |
| 20 | let inputArgArray = Array.isArray(inputs) |
| 21 | ? inputs |
| 22 | : [inputs] |
| 23 | |
| 24 | if (!inputArgArray.length) { |
| 25 | throw new Error('toNetInput - empty array passed as input') |
| 26 | } |
| 27 | |
| 28 | const getIdxHint = (idx: number) => Array.isArray(inputs) ? ` at input index ${idx}:` : '' |
| 29 | |
| 30 | const inputArray = inputArgArray.map(resolveInput) |
| 31 | |
| 32 | inputArray.forEach((input, i) => { |
| 33 | if (!isMediaElement(input) && !isTensor3D(input) && !isTensor4D(input)) { |
| 34 | |
| 35 | if (typeof inputArgArray[i] === 'string') { |
| 36 | throw new Error(`toNetInput -${getIdxHint(i)} string passed, but could not resolve HTMLElement for element id ${inputArgArray[i]}`) |
| 37 | } |
| 38 | |
| 39 | throw new Error(`toNetInput -${getIdxHint(i)} expected media to be of type HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | tf.Tensor3D, or to be an element id`) |
| 40 | } |
| 41 | |
| 42 | if (isTensor4D(input)) { |
| 43 | // if tf.Tensor4D is passed in the input array, the batch size has to be 1 |
| 44 | const batchSize = input.shape[0] |
| 45 | if (batchSize !== 1) { |
| 46 | throw new Error(`toNetInput -${getIdxHint(i)} tf.Tensor4D with batchSize ${batchSize} passed, but not supported in input array`) |
| 47 | } |
| 48 | } |
| 49 | }) |
| 50 | |
| 51 | // wait for all media elements being loaded |
| 52 | await Promise.all( |
| 53 | inputArray.map(input => isMediaElement(input) && awaitMediaLoaded(input)) |
| 54 | ) |
| 55 | |
| 56 | return new NetInput(inputArray, Array.isArray(inputs)) |
| 57 | } |
no test coverage detected
searching dependent graphs…