• Lv0
    粉丝0

积分7 / 贡献0

提问3答案被采纳0文章0

使用MindSpore Lite JS API开发AI应用 代码跑不通

wakaka 显示全部楼层 发表于 2024-3-21 11:26:43

log的错误:promise getRawFileContent failed, error code: undefined, message: Cannot read property resourceManager of undefined,data:undefined,name=TypeError


import { GlobalContext } from './GlobalContext';
import mindSporeLite from '@ohos.ai.mindSporeLite';
import common from '@ohos.app.ability.common';
import { BusinessError } from '@ohos.base';

export class Test {
  value: number = 0;
  foo(): void {
    GlobalContext.getContext().setObject("value", this.value);
  }
}

export async function testMSL() {
  let value: number = 0;
  GlobalContext.getContext().setObject("value", value);
  let globalContext = GlobalContext.getContext().getObject("value") as common.UIAbilityContext;
  let inputBuffer: ArrayBuffer | null = null;
  let inputName: string = 'add.bin';
  console.log('========win',globalContext)

    try {
      globalContext.resourceManager.getRawFileContent(inputName)
        .then((value: Uint8Array) => {
          inputBuffer = value.buffer;
        })
        .catch((error: BusinessError) => {
          console.error("========getRawFileContent promise error is " + error);
        });
    } catch (error) {
      let code = (error as BusinessError).code;
      let message = (error as BusinessError).message;
      let data=(error as BusinessError).data;
      let name=(error as BusinessError).name;
      let stack=(error as BusinessError).stack;
      console.error(`========promise getRawFileContent failed, error code: ${code}, message: ${message},data:${data},name=${name},stack=${stack}`);
    }

  console.info('========final')
  // 1.创建上下文
  let context: mindSporeLite.Context = {};
  context.target = ['cpu'];
  context.cpu = {}
  context.cpu.threadNum = 1;
  context.cpu.threadAffinityMode = 0;
  context.cpu.precisionMode = 'enforce_fp32';3

  // 2.加载模型
  let modelFile = 'add.ms';
  let msLiteModel: mindSporeLite.Model = await mindSporeLite.loadModelFromFile(modelFile, context);


  // 3.设置输入数据
  let modelInputs: mindSporeLite.MSTensor[] = msLiteModel.getInputs();
  if (inputBuffer != null) {
    modelInputs[0].setData(inputBuffer as ArrayBuffer);
  }

  // 4.执行推理并打印输出
  console.log('=========MSLITE predict start=====')
  msLiteModel.predict(modelInputs).then((modelOutputs: mindSporeLite.MSTensor[]) => {
    let output0 = new Float32Array(modelOutputs[0].getData());
    for (let i = 0; i < output0.length; i++) {
      console.log(output0[i].toString());
    }
  })
  console.log('=========MSLITE predict success=====')

}
您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

精彩评论7

m0_62263312

沙发 发表于 2024-3-25 00:27:31
我在导入mindsporelite时会报错The target device of this project does not include the required SystemCapability. <ArkTSCheck>,请问楼主有遇到吗,要怎么解决啊

wakaka

发表于 2024-3-25 16:22  IP属地: 广东省广州市

回复 m0_62263312: 我也遇到,没管,想着能不能先把这个错误先解决再搞你说的这个错误

golden-king

发表于 2024-3-28 20:16  IP属地: - 中国江苏省无锡市 中国电信IDC

回复 m0_62263312: 这个报错不用管 个人认为是ide对mindSporeLite代码提示不足造成的,实际代码是可以跑得

taohuabo

发表于 2024-7-5 17:05  IP属地: 浙江省温州市

回复 golden-king: 报错提示中有syscap.json字样,你把这个在文档里搜索一下,会告诉你如何添加额外的能力,报错就解决了,你现在关于mindSpore Lite的使用进展如何?

taohuabo

发表于 2024-7-5 17:12  IP属地: 浙江省温州市

回复 m0_62263312: 请教,你那个add.bin文件是哪里来的?

golden-king

发表于 2024-7-19 10:21  IP属地: 江苏省

回复 taohuabo: 你想了解什么

【5 条回复】

golden-king

板凳 发表于 2024-3-28 20:13:38
不要用样例的代码 那个代码跑不通的 问题出在那个GlobalContext 对象 样例中的这个对象不存在resourceManager。UIAbility类中的context是存在resourceManager对象的,你可以用这个context去获取bin文件

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部