async runWithInputs()

in src/nodes/client/image-classification.ts [38:88]


  async runWithInputs(inputs: Inputs, services: Services) {
    const { image, modelid, device, quantized } = inputs;

    const _modelid = modelid?.trim();
    if (!image?.canvasId) {
      // No input node
      this.dispatchEvent(
        new CustomEvent("outputs", { detail: { results: null } })
      );
      return;
    }

    if (this.cachedResult && compareObjects(this.cachedInput, inputs)) {
      this.dispatchEvent(
        new CustomEvent("outputs", { detail: { results: this.cachedResult } })
      );
      return;
    }

    const canvas = services.resourceService.get(
      image.canvasId
    ) as HTMLCanvasElement;
    const data = canvas.toDataURL();

    const classifier: ImageClassificationPipeline = await this.getInstance(
      _modelid,
      quantized,
      device
    );

    const result = await classifier(data, {
      topk: 5,
    });

    const resultArray = (
      Array.isArray(result) ? result : [result]
    ) as ImageClassificationOutput;

    const classProb = resultArray.map((e: any) => ({
      className: e.label,
      probability: e.score,
    }));
    this.cachedInput = inputs;
    this.cachedResult = { classes: classProb };

    this.dispatchEvent(
      new CustomEvent("outputs", {
        detail: { results: { classes: classProb } },
      })
    );
  }