Ionic framework 用电容器在离子框架中实现TensorFlowjs模型

Ionic framework 用电容器在离子框架中实现TensorFlowjs模型,ionic-framework,tensorflow.js,capacitor,Ionic Framework,Tensorflow.js,Capacitor,我正在做一个项目,我正在使用Ionic和TensorFlow进行机器学习。我已将TensorFlow模型转换为tensorflowjs模型。我已经将tensorflowjs模型的model.json文件和碎片文件放在了爱奥尼亚的assets文件夹中。基本上,我已经把我的tensorflowjs模型放在了爱奥尼亚的资产文件夹中。我想使用电容器访问相机,并允许用户拍照。然后,这些照片将被传递到资产中的tensorflowjs模型,以获取并显示该用户的预测 这是我的打字脚本代码: import { C

我正在做一个项目,我正在使用Ionic和TensorFlow进行机器学习。我已将TensorFlow模型转换为tensorflowjs模型。我已经将tensorflowjs模型的model.json文件和碎片文件放在了爱奥尼亚的assets文件夹中。基本上,我已经把我的tensorflowjs模型放在了爱奥尼亚的资产文件夹中。我想使用电容器访问相机,并允许用户拍照。然后,这些照片将被传递到资产中的tensorflowjs模型,以获取并显示该用户的预测

这是我的打字脚本代码:

import { Component, OnInit, ViewChild, ElementRef, Renderer2 } from '@angular/core';

import { Plugins, CameraResultType, CameraSource} from '@capacitor/core';

import { DomSanitizer, SafeResourceUrl} from '@angular/platform-browser';

import { Platform } from '@ionic/angular';

import * as tf from '@tensorflow/tfjs';

import { rendererTypeName } from '@angular/compiler';

import { Base64 } from '@ionic-native/base64/ngx';

import { defineCustomElements } from '@ionic/pwa-elements/loader';

const { Camera } = Plugins;

@Component({

  selector: 'app-predict',

  templateUrl: './predict.page.html',

  styleUrls: ['./predict.page.scss'],

})

export class PredictPage{

  linearModel : tf.Sequential;

  prediction : any;

  InputTaken : any;

  ctx: CanvasRenderingContext2D;

  pos = { x: 0, y: 0 };

  canvasElement : any;

  photo: SafeResourceUrl;

  model: tf.LayersModel;

  constructor(public el : ElementRef , public renderer : Renderer2 , public platform : Platform, private base64: Base64,

    private sanitizer: DomSanitizer) 

  {}

  

  async takePicture() {

    const image = await Camera.getPhoto({

        quality: 90,

        allowEditing: true,

        resultType: CameraResultType.DataUrl,

        source: CameraSource.Camera});

      const model = await tf.loadLayersModel('src/app/assets/model.json');

      this.photo = this.sanitizer.bypassSecurityTrustResourceUrl(image.base64String);

      defineCustomElements(window);

    const pred = await tf.tidy(() => {

          // Make and format the predications

        const output = this.model.predict((this.photo)) as any;

                                

          // Save predictions on the component

        this.prediction = Array.from(output.dataSync()); 

        });

  }

}
在这段代码中,我导入了必要的工具。然后,我有一个构造函数和一个
takepicture()
函数。在takepicture功能中,我为用户提供了拍照功能。然而,我在将拍摄的照片传递到tensorflowjs模型以获得预测时遇到了麻烦。我将在这行代码中将拍摄的照片传递给tensorflowjs模型:

const output = this.model.predict((this.photo)) as any;
但是,我得到一个错误,指出:

“SafeSourceUrl”类型的参数不能分配给“Tensor | Tensor[]”类型的参数。\n类型“SafeSourceUrl”缺少“Tensor[]”类型的以下属性:长度、弹出、推送、concat等


如果我能收到一些关于这个主题的指导,我将不胜感激。

模型希望您传递一个
张量输入,但您传递的是tfjs生态系统中没有的其他图像格式。您应该首先将
this.photo
转换为张量,或者更简单地将
image.base64String
转换为张量

由于您似乎正在使用node,请尝试以下代码

//将base64映像移动到缓冲区中
常量b=Buffer.from(image.base64String,“base64”)
//得到张量
const image=tf.node.decodeImage(b)
//计算输出
const output=this.model.predict(image)如有;
此处的其他转换解决方案: