| 127 | let isPredicting = false; |
| 128 | |
| 129 | async function predict() { |
| 130 | ui.isPredicting(); |
| 131 | while (isPredicting) { |
| 132 | // Capture the frame from the webcam. |
| 133 | const img = await getImage(); |
| 134 | |
| 135 | // Make a prediction through mobilenet, getting the internal activation of |
| 136 | // the mobilenet model, i.e., "embeddings" of the input images. |
| 137 | const embeddings = truncatedMobileNet.predict(img); |
| 138 | |
| 139 | // Make a prediction through our newly-trained model using the embeddings |
| 140 | // from mobilenet as input. |
| 141 | const predictions = model.predict(embeddings); |
| 142 | |
| 143 | // Returns the index with the maximum probability. This number corresponds |
| 144 | // to the class the model thinks is the most probable given the input. |
| 145 | const predictedClass = predictions.as1D().argMax(); |
| 146 | const classId = (await predictedClass.data())[0]; |
| 147 | img.dispose(); |
| 148 | |
| 149 | ui.predictClass(classId); |
| 150 | await tf.nextFrame(); |
| 151 | } |
| 152 | ui.donePredicting(); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Captures a frame from the webcam and normalizes it between -1 and 1. |