• Process ONNX model output to create mask canvas.

    Converts raw model output (Float32Array) into a grayscale mask canvas. The output is normalized to 0-255 range and resized to match the original image dimensions.

    Parameters

    • output: Float32Array<ArrayBufferLike>

      Raw model output as Float32Array

    • originalSize: {
          width: number;
          height: number;
      }

      Original image dimensions for resizing

      • width: number
      • height: number
    • outputShape: [number, number, number, number] = ...

      Model output shape [batch, channels, height, width] (default: [1, 1, 320, 320])

    Returns HTMLCanvasElement

    HTMLCanvasElement containing the processed mask

    Throws

    When canvas context creation fails

    Example

    // After running ONNX inference
    const results = await session.run(input);
    const outputTensor = results[Object.keys(results)[0]] as ort.Tensor;
    const outputData = outputTensor.data as Float32Array;

    const maskCanvas = processModelOutput(
    outputData,
    { width: 800, height: 600 },
    [1, 1, 320, 320]
    );

    // Use the mask for background removal
    const cutout = naiveCutout(originalCanvas, maskCanvas);