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

品牌网站建设专家seo外包公司专家

品牌网站建设专家,seo外包公司专家,aws wordpress 站群,网站建设的原因最近在使用华为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.khdw.cn/news/40360.html

相关文章:

  • 网站建设开题报告ppt模板长尾关键词挖掘网站
  • 什么网站做软文啥是网络推广
  • 常州微元宝网站建设网络推广方式有哪些
  • 律师网站建设最近最新新闻
  • 盘锦网站建设流程站长工具seo综合查询是什么
  • 自己电脑做网站教程自学seo大概需要多久
  • 镇江专业网站制作公司外国网站的浏览器
  • 成都网站建设公司招聘重庆seo关键词排名
  • 婚纱摄影网站设计论文最近的热点新闻
  • 武汉网站seo技术网站推广引流
  • 网上车辆租赁网站怎么做推广普通话ppt课件
  • 大浪做网站公司有名的seo外包公司
  • 自己做视频用什么软件江西省seo
  • 企业管理平台app宁波网站优化公司推荐
  • 网页类型分类7种太原seo排名优化软件
  • 游戏开科技软件免费百度搜索关键词排名优化推广
  • 站长之家素材网站河北软文搜索引擎推广公司
  • 免费申请注册网站优化大师手机版
  • 哪家网站做推广好今日国内新闻
  • 用css div做一个电影网站下载安装百度一下
  • 网站建设免费模版设计网站排行
  • 可靠的邢台做网站关键词排名seo优化
  • 哪些网站动效做的不错百度下载并安装最新版
  • 网站由哪几部分组成百度打广告怎么收费
  • 深圳微信网站建设微信推广软件
  • 招聘网站毕业设计search搜索引擎
  • 网站建设经验心得企业seo关键词优化
  • 什么网站做视频给钱怎么自己制作网页
  • jsp做网站图片怎么存储seo引擎优化外包
  • 深圳做网站的人seo技术培训山东