MCPcopy
hub / github.com/justadudewhohacks/face-api.js / toNetInput

Function toNetInput

src/dom/toNetInput.ts:15–57  ·  view source on GitHub ↗
(inputs: TNetInput)

Source from the content-addressed store, hash-verified

13 * @returns A NetInput instance, which can be passed into one of the neural networks.
14 */
15export 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}

Callers 15

toNetInput.test.tsFile · 0.90
extractFacesFunction · 0.90
forwardMethod · 0.90
forwardMethod · 0.90
computeFaceDescriptorMethod · 0.90
forwardMethod · 0.90
forwardWithStatsMethod · 0.90

Calls 5

isMediaElementFunction · 0.90
isTensor3DFunction · 0.90
isTensor4DFunction · 0.90
awaitMediaLoadedFunction · 0.90
getIdxHintFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…