async runWithInputs()

in src/nodes/shared/image-segmentation-viewer.ts [256:324]


  async runWithInputs(inputs: Inputs, services: Services) {
    const { segData, image } = inputs;
    this.services = services;
    if (segData === undefined || segData?.length === 0) {
      this.dispatchEvent(
        new CustomEvent("outputs", {
          detail: {
            error: {
              title: "Error",
              message: "No segments found",
            },
          },
        })
      );
      return;
    }
    if (
      this.cachedSegData &&
      this.cachedOutputImage &&
      this.cachedInputImage === image.canvasId
    ) {
      let outputImage = image;
      if (this.cachedOutputImage) {
        outputImage = {
          canvasId: this.services.resourceService.put(this.cachedOutputImage),
        };
      }

      this.dispatchEvent(
        new CustomEvent("outputs", {
          detail: {
            image: outputImage,
          },
        })
      );
      return;
    }

    if (image?.canvasId !== undefined) {
      const imgCanvas = services.resourceService.get(
        image.canvasId
      ) as HTMLCanvasElement;
      this.inputImage = imgCanvas;
      this.cachedOutputImage = imgCanvas;
    }
    this.canvas = document.createElement("canvas");
    this.masks = segData.map((x, i) => this.renderMask(x, i));
    const ctx = this.canvas.getContext("2d") as CanvasRenderingContext2D;
    ctx.save();
    const mask0 = this.masks[0];
    this.canvas.width = mask0.width;
    this.canvas.height = mask0.height;
    ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
    if (this.inputImage) {
      ctx.drawImage(this.inputImage, 0, 0);
    }
    ctx.globalAlpha = 0.6;
    this.masks.forEach((mask) => ctx.drawImage(mask, 0, 0));
    ctx.restore();

    this.cachedSegData = segData;
    this.cachedInputImage = image?.canvasId;

    this.dispatchEvent(
      new CustomEvent("outputs", {
        detail: { image: image },
      })
    );
  }