in segment-anything-webgpu/index.js [35:77]
async function decode() {
// Only proceed if we are not already decoding
if (isDecoding) {
decodePending = true;
return;
}
isDecoding = true;
// Prepare inputs for decoding
const reshaped = imageProcessed.reshaped_input_sizes[0];
const points = lastPoints
.map((x) => [x.position[0] * reshaped[1], x.position[1] * reshaped[0]])
.flat(Infinity);
const labels = lastPoints.map((x) => BigInt(x.label)).flat(Infinity);
const num_points = lastPoints.length;
const input_points = new Tensor("float32", points, [1, 1, num_points, 2]);
const input_labels = new Tensor("int64", labels, [1, 1, num_points]);
// Generate the mask
const { pred_masks, iou_scores } = await model({
...imageEmbeddings,
input_points,
input_labels,
});
// Post-process the mask
const masks = await processor.post_process_masks(
pred_masks,
imageProcessed.original_sizes,
imageProcessed.reshaped_input_sizes,
);
isDecoding = false;
updateMaskOverlay(RawImage.fromTensor(masks[0][0]), iou_scores.data);
// Check if another decode is pending
if (decodePending) {
decodePending = false;
decode();
}
}