错误:传递给 tf.browser.fromPixels() 的像素必须是 HTMLVideoElement

我在使用 TensorFlow JS 和 Coco-SSD 模型进行对象检测,但我的图像是以 base64 字符串格式存储的,这与 tf.browser.fromPixels() 不兼容。我收到了这个错误

Error: pixels passed to tf.browser.fromPixels() must be either an HTMLVideoElement, HTMLImageElement, HTMLCanvasElement, ImageData in browser, or OffscreenCanvas, ImageData in webworker or {data: Uint32Array, width: number, height: number}, but was Function
refreshImage(){    this.httpClient.get('http://localhost:8080/image/get/test.jpg')          .subscribe(            res => {              this.retrieveResonse = res;              this.base64Data = this.retrieveResonse.picByte;              console.log(this.base64Data = this.retrieveResonse.picByte)              this.retrievedImage = 'data:image/jpeg;base64,' + this.base64Data;            }          );          this.predictWithCocoModel();  }  public async predictWithCocoModel(){    const model = await cocoSSD.load();    this.detectFrame(this.retrievedImage,model);    console.log('model loaded');  }  detectFrame = (retrievedImage, model) => {    model.detect(this.refreshImage).then(predictions => {      this.renderPredictions(predictions);      /*requestAnimationFrame(() => {        this.detectFrame(video, model);      });*/    });  }  renderPredictions = predictions => {    const canvas = <HTMLCanvasElement> document.getElementById("canvas");    const ctx = canvas.getContext("2d");    canvas.width  = 360;    canvas.height = 360;    ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);    // Font options.    const font = "16px sans-serif";    ctx.font = font;    ctx.textBaseline = "top";    ctx.drawImage(this.retrievedImage,0, 0,300,300);    predictions.forEach(prediction => {      let i = 0      const x = prediction.bbox[0];      const y = prediction.bbox[1];      const width = prediction.bbox[2];      const height = prediction.bbox[3];      if (prediction.class === 'person') {        i++      }      console.log(i);      // Draw the bounding box.      ctx.strokeStyle = "#00FFFF";      ctx.lineWidth = 2;      ctx.strokeRect(x, y, width, height);      // Draw the label background.      ctx.fillStyle = "#00FFFF";      const textWidth = ctx.measureText(prediction.class).width;      const textHeight = parseInt(font, 10); // base 10      ctx.fillRect(x, y, textWidth + 4, textHeight + 4);    });    predictions.forEach(prediction => {      const x = prediction.bbox[0];      const y = prediction.bbox[1];      // Draw the text last to ensure it's on top.      ctx.fillStyle = "#000000";      ctx.fillText(prediction.class, x, y);    });  };

回答:

如错误所示,model.detect 期望接收一个图像元素。

你传递给 detect 方法的却是一个函数 refreshImage,这导致了错误

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注