async runWithInputs()

in src/nodes/client/object-detection.ts [40:99]


  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 detector: ObjectDetectionPipeline = await this.getInstance(
      _modelid,
      quantized,
      device
    );

    // Predict segments
    const result = await detector(data, {
      // TODO: Add option for threshold
      // threshold: 0.5,
      percentage: true,
    });

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

    const outputVB = resultArray.map((x: any) => {
      return {
        label: x.label,
        score: x.score,
        box: {
          left: x.box.xmin,
          top: x.box.ymin,
          width: x.box.xmax - x.box.xmin,
          height: x.box.ymax - x.box.ymin,
        },
      };
    });
    this.cachedInput = inputs;
    this.cachedResult = { results: outputVB };

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