* Search for images with content matching target wrods. * * @param {string[]} filePaths An array of paths to image files * @param {string[]} targetWords What target words to search for. An image * will be considered a match if its content (as determined by * `imageClassifer`) matc
(filePaths, targetWords, inferenceCallback)
| 133 | * be invoked when the model is running inference on image data. |
| 134 | */ |
| 135 | async searchFromFiles(filePaths, targetWords, inferenceCallback) { |
| 136 | // Read the content of the image files as tensors with dimensions |
| 137 | // that match the requirement of the image classifier. |
| 138 | const {height, width} = this.getImageSize(); |
| 139 | const imageTensors = []; |
| 140 | for (const file of filePaths) { |
| 141 | const imageTensor = await readImageAsTensor(file, height, width); |
| 142 | imageTensors.push(imageTensor); |
| 143 | } |
| 144 | |
| 145 | // Combine images to a batch for accelerated inference. |
| 146 | const axis = 0; |
| 147 | const batchImageTensor = tf.concat(imageTensors, axis); |
| 148 | if (inferenceCallback != null) { |
| 149 | inferenceCallback(); |
| 150 | } |
| 151 | |
| 152 | // Run inference. |
| 153 | const t0 = tf.util.now(); |
| 154 | const classNamesAndProbs = await this.classify(batchImageTensor); |
| 155 | const tElapsedMillis = tf.util.now() - t0; |
| 156 | |
| 157 | const foundItems = searchForKeywords( |
| 158 | classNamesAndProbs, filePaths, targetWords); |
| 159 | |
| 160 | // TensorFlow.js memory cleanup. |
| 161 | tf.dispose([imageTensors, batchImageTensor, imageTensors]); |
| 162 | |
| 163 | return { |
| 164 | targetWords, |
| 165 | numSearchedFiles: filePaths.length, |
| 166 | foundItems, |
| 167 | tElapsedMillis |
| 168 | }; |
| 169 | } |
| 170 | |
| 171 | /** Get the required image sizes (height and width). */ |
| 172 | getImageSize() { |
no test coverage detected