当前位置: 首页 > news >正文

品牌网站建设专家seo合作代理

品牌网站建设专家,seo合作代理,学校手机网站模板,找国外客户用哪个平台最近在使用华为AI平台ModelArts训练自己的图像识别模型,并部署了在线服务接口。供给客户端(如:鸿蒙APP/元服务)调用。 import核心能力: import { http } from kit.NetworkKit; import { fileIo } from kit.CoreFileK…

最近在使用华为AI平台ModelArts训练自己的图像识别模型,并部署了在线服务接口。供给客户端(如:鸿蒙APP/元服务)调用。

import核心能力:

import { http } from '@kit.NetworkKit';
import { fileIo } from '@kit.CoreFileKit';

一、先用测试工具调用在线服务接口,是否成功

 

接口接收到传入的图片文件,识别图片内容后成功返回结果。

注意:接口要求输入的参数名是images,值类型是文件file。

二、从手机相册选取一张图片,并复制到沙箱缓存中

/*** 将文件拷贝到缓存中* @param from 原文件地址(拍照/相册)* @param to 目标文件地址(缓存)*/copyFile(from: string, to: string): void {let fFile = fileIo.openSync(from);let tFile = fileIo.openSync(to, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); // 替换或创建文件fileIo.copyFileSync(fFile.fd, tFile.fd);fileIo.closeSync(fFile);fileIo.closeSync(tFile);}

三、读取已复制到缓存中的文件内容

/*** 读取文件内容* @param cacheImgUri:沙箱缓存文件地址* @returns*/readFileContent(cacheImgUri: string): ArrayBuffer {let fFile = fileIo.openSync(cacheImgUri, fileIo.OpenMode.READ_ONLY);let fStat = fileIo.lstatSync(cacheImgUri);let arrayBufFile: ArrayBuffer = new ArrayBuffer(fStat.size); // 文件大小fileIo.readSync(fFile.fd, arrayBufFile);fileIo.fsyncSync(fFile.fd);fileIo.closeSync(fFile);return arrayBufFile;}

四、构建请求体body的内容

/*** 构建请求体body* @param boundary 分隔符* @param fileName 文件名* @param fileContent 文件内容* @returns */buildBodyContent(boundary: string, fileName: string, fileContent: Uint8Array): ArrayBuffer {let txtEncoder = new util.TextEncoder();// 构建请求体前面内容let bodyPre = `--${boundary}\r\n`bodyPre = bodyPre + `Content-Disposition: form-data; name="images"; filename="${fileName}"\r\n`;bodyPre = bodyPre + 'Content-Type: application/octet-stream\r\n';bodyPre = bodyPre + '\r\n';let arrayPre = txtEncoder.encodeInto(bodyPre);// 构建请求体后面内容let bodyAft = '\r\n'bodyAft = bodyAft + `--${boundary}`bodyAft = bodyAft + '--\r\n'let arrayAft = txtEncoder.encodeInto(bodyAft);let body = buffer.concat([arrayPre, fileContent, arrayAft]); // 拼接请求体return body.buffer;}

五、按钮click事件调用aiAnalyseImg方法,发送请求在线服务接口

/*** 调用病虫害模型AI能力分析图片* @param imgUri 原图片地址(拍照/相册)* @returns */async aiAnalyseImg(imgUri: string): Promise<void> {// 华为云ModelArts平台病虫害模型console.debug('正在分析的图片地址:' + imgUri); // 从相册选取的图片地址// 文件名let fileName = imgUri.split('/').pop() as string;let cacheFilePath = `${getContext().cacheDir}/${fileName}`;this.copyFile(imgUri, cacheFilePath);// 从沙箱缓存中读取文件内容let fileContent: Uint8Array = new Uint8Array(this.readFileContent(cacheFilePath));// 构建请求体bodylet boundary: string ='--------' + (await systemDateTime.getCurrentTime(true)).toString();let bodyContent = this.buildBodyContent(boundary, fileName, fileContent);hilog.debug(0x0000, 'aiAnalyseImg', 'hilog输出bodyContent:' + bodyContent);hilog.debug(0x0000, 'aiAnalyseImg', 'hilog输出bodyContent大小:' + bodyContent.byteLength);// 请求地址:modelArts平台在线服务API接口let url: string ='https://b07b6d6054****96d5e4420****e.apig.cn-north-4.huaweicloudapis.com/v1/infers/c91****8-c678-4e73-****-37c****3a';let request = http.createHttp();let reqOpts: http.HttpRequestOptions = { // 设置请求参数method: http.RequestMethod.POST,header: {'X-Apig-AppCode': '40d29da14dbd87abe3484f6fa0e1b07767d5226540459dbf8620a8f7', // 模型平台AppCode'Content-Type': `multipart/form-data;boundary=${boundary}`,'Content-Length': bodyContent.byteLength.toString(),},extraData: bodyContent,};// 发起请求request.request(url, reqOpts).then((resp) => { // 请求成功,解析返回结果// TODO: 解析返回结果hilog.debug(0x0000, 'aiAnalyseImg', 'hilog输出结果:' + JSON.stringify(resp.result));}).catch((err: BusinessError) => {hilog.error(0x0000, 'aiAnalyseImg', 'catch输出错误:' + err.message);})}

注意:

1.构建请求体body中的Content-Type: application/octet-stream  与  header中设置的Content-Type: multipart/form-data

2.header参数中记得设置Content-Length: bodyContent.byteLength.toString()

http://www.tj-hxxt.cn/news/49009.html

相关文章:

  • 有哪些h5做的网站深度搜索
  • 湛江怎么做网站关键词优化设计一个公司网站多少钱
  • 电子商务网站建设基本组成培训机构排名
  • wordpress网站翻译插件百度推广登录入口电脑
  • 电商网站建设好么自己做网站网页归档
  • 做网站前期了解客户什么需求nba排行榜最新排名
  • 成都私人网站制作百度教育会员
  • 南宁专业网站建设自动app优化下载
  • 老网站301跳转新网站什么是seo营销
  • 电商平台开发项目seo短视频网页入口引流下载
  • 网站页面策划模板下载怎样自己开发一款软件
  • 政府网站平台建设与管理办法安卓aso优化
  • 北京公司注册核名网站北京网络优化推广公司
  • 建设银行网站怎么注销网银竞价恶意点击器
  • 专业展示设计网站郑州网站建设用户
  • 做网站不给钱seo营销推广平台
  • 凡客诚品倒闭了吗知乎青岛网站优化公司
  • 扬州网站建设link5南京网站设计优化公司
  • 如何做提升自己的网站常州网站建设优化
  • 公章在线制作网站做不了利尔化学股票最新消息
  • 电影聚合网站开发app开发费用一般多少钱
  • 网站建设哈尔滨网站建设1百度网页版登录入口官网
  • 淘宝客怎么做其他网站的推广铜川网站seo
  • 乐清英文网站建设专业做加盟推广的公司
  • 京icp备号在线查询苏州整站优化
  • 岳阳网站建设公司百度网站优化
  • 玉泉路做网站深圳新闻今日最新
  • wordpress oss压缩湘潭seo优化
  • 一个网站里面只放一个图片怎么做百度明星人气榜排名
  • 网站建设多少钱裙游戏推广员是诈骗吗