成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

基于HarmonyOS的AI圖像識別應(yīng)用開發(fā)(ETS)

系統(tǒng) OpenHarmony
本案例是通過網(wǎng)絡(luò)請求連接到百度云,調(diào)用百度云AI圖像識別的API,再將結(jié)果返回至應(yīng)用顯示。

??想了解更多關(guān)于開源的內(nèi)容,請訪問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??

前言

原本打算在九聯(lián)開發(fā)板上搭配攝像頭開發(fā),去實現(xiàn)拍照并對圖片進(jìn)行AI識別的應(yīng)用開發(fā),但是遇到了點問題。

不過基于HarmonyOS的AI圖像識別案例可以正常運(yùn)作,于是作此文章作為小分享O(∩_∩)O。

概述

本案例是通過網(wǎng)絡(luò)請求連接到百度云,調(diào)用百度云AI圖像識別的API,再將結(jié)果返回至應(yīng)用顯示。??百度云文檔??。

舉例效果圖:

正文

一、創(chuàng)建項目

項目選擇HarmonyOS的Empty Ability模板,API選擇8,語言選擇ets。

二、添加權(quán)限及導(dǎo)入模塊

1、在config.json文件中添加權(quán)限。

"reqPermissions": [
{
"name": "ohos.permission.INTERNET"
}
]

2、在index.ets文件中導(dǎo)入模塊,第一個是資源管理模塊,第二個是網(wǎng)絡(luò)模塊。

import resourceManager from '@ohos.resourceManager';
import http from '@ohos.net.http';

三、創(chuàng)建網(wǎng)絡(luò)請求并根據(jù)百度云API傳參

【木棉花】:基于HarmonyOS的AI圖像識別應(yīng)用開發(fā)(ETS)-開源基礎(chǔ)軟件社區(qū)

該API調(diào)用前需要獲取access_token,具體方法見其??文檔??(注意的是創(chuàng)建應(yīng)用后要去開啟圖像識別的服務(wù))。

【木棉花】:基于HarmonyOS的AI圖像識別應(yīng)用開發(fā)(ETS)-開源基礎(chǔ)軟件社區(qū)

定義變量

@State access_token: string = 'Hello World'
@State Base64Str: string = 'Hello World'
@State result_description: string = 'description'
@State result_keyword: string = 'keyword'
@State result_root: string = 'root'
@State result_image: string = 'image'

并上傳要識別的圖片到項目中,此案例中使用的是一張蓮藕的圖片。

編寫函數(shù)獲取access_token

GetAccessToken() {
let httpRequest = http.createHttp();
httpRequest.request(
//自行替換AK和SK
"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【百度云應(yīng)用的AK】&client_secret=【百度云應(yīng)用的SK】",
{
method: http.RequestMethod.POST,
connectTimeout: 60000,
readTimeout: 60000,
}, (err, data) => {
if (!err) {
let obj = JSON.parse(data.result.toString());
this.access_token=obj.access_token
console.info('Result1:' + data.result);
console.info('Result1_token:' + this.access_token);
} else {
console.info('Result1_error:' + JSON.stringify(err));
httpRequest.destroy();
}
})
}

編寫函數(shù)編碼圖片并去掉編碼頭

resourceManager.getResourceManager 此API只適用于FA模型,stage模型不適用。(所以在標(biāo)準(zhǔn)系統(tǒng)相機(jī)開發(fā)模型為stage時,不能用此方法對圖片編碼)。

//base64編碼
GetBase64(){
let that = this
resourceManager.getResourceManager((error, mgr) => {
if (error != null) {
console.log("ResourceManager error is " + error)
} else {
mgr.getMediaBase64($r('app.media.lianou').id, (error, value) => {
if (error != null) {
console.log("base64_error is " + error)
} else {
console.info('base64_result:' + value)
that.Base64Str = that.getCaption(value)
console.info('base64Str:' + this.Base64Str)
}
});
}
});
}
//去掉編碼頭
getCaption(obj) {
var index = obj.lastIndexOf("\,");
obj = obj.substring(index + 1, obj.length);
return obj;
}

編寫函數(shù)調(diào)用圖像識別API

注:這里 header:{‘Content-Type’: ‘a(chǎn)pplication/x-www-form-urlencoded’} 才能傳image參數(shù)到百度云。HTTP請求頭字段,默認(rèn){‘Content-Type’: ‘a(chǎn)pplication/json’} 。筆者將能在harmonyOS模擬器上跑起來的同樣代碼復(fù)制到一個新建的openHarmony項目中,但是會報錯:缺參。通過后臺調(diào)試發(fā)現(xiàn)能接收url的參數(shù)access_token和header參數(shù),當(dāng)header為默認(rèn)類型時能接收到extraData里的參數(shù),但當(dāng)header為’application/x-www-form-urlencoded’時,無法收到extraData里的參數(shù),所以初步認(rèn)為是筆者所用OH系統(tǒng)版本的網(wǎng)絡(luò)請求庫不支持application/x-www-form-urlencoded傳參。

AI_request() {
let httpRequest = http.createHttp();
httpRequest.request(
"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general?access_token="+ this.access_token,
{
method: http.RequestMethod.POST,
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
extraData: {
'image': this.Base64Str,
'baike_num': 1
},
connectTimeout: 60000,
readTimeout: 60000,
}, (err, data) => {
if (!err) {
let obj = JSON.parse(data.result.toString());
this.result_description = obj.result[0].baike_info.description;
this.result_keyword = obj.result[0].keyword;
this.result_image = obj.result[0].baike_info.image_url;
this.result_root = obj.result[0].root;

console.info('Result_description:' + this.result_description)
console.info('Result_keyword:' + this.result_keyword)
console.info('Result_root:' + this.result_root)
console.info('Result_image:' + this.result_image)
} else {
console.info('Result2_error:' + JSON.stringify(err));
httpRequest.destroy();
}
})
}

四、編寫UI界面調(diào)用函數(shù)

因為圖片編碼需要點時間,為了避免傳參時出現(xiàn)錯誤,于是添加了一個延遲函數(shù)。

build() {
Column({ space: 10 }) {
Button('AI識別')
.onClick(() => {
this.GetBase64()
this.GetAccessToken()
setTimeout(()=>{
this.AI_request()
},1400)
})
Image(this.result_image)
.width(150)
.height(150)

Row({ space: 20 }) {
Text(this.result_keyword)
.fontSize(20)
.width(150)
.height(35)
.textAlign(TextAlign.Start)
.margin(15)
Text(this.result_root)
.fontSize(20)
.textAlign(TextAlign.Start)
.width(150)
.height(35)
.margin(15)
}.width('100%')
.height(35)

Text(this.result_description)
.fontSize(20)
.textAlign(TextAlign.Start)
.width('90%')
.height(250)
}
.width('100%')
.height('100%')

}
}

結(jié)語

以上就是本次的小分享啦!

??想了解更多關(guān)于開源的內(nèi)容,請訪問:??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

??https://ost.51cto.com??。

責(zé)任編輯:jianghua 來源: ??51CTO開源基礎(chǔ)軟件社區(qū)
相關(guān)推薦

2023-11-30 09:55:27

鴻蒙鄰分類器

2022-10-20 09:33:35

2024-06-18 08:16:49

2016-12-01 14:23:32

iosandroid

2016-05-11 10:06:05

谷歌圖像識別web開發(fā)

2022-04-13 11:24:18

ETS開發(fā)HarmonyOS鴻蒙

2022-10-11 23:35:28

神經(jīng)網(wǎng)絡(luò)VGGNetAlexNet

2021-04-09 20:49:44

PythonOCR圖像

2019-07-21 22:22:37

圖像識別AI機(jī)器視覺

2022-05-07 15:34:16

ETS低代碼應(yīng)用

2017-03-28 08:47:33

圖像識別技術(shù)

2020-09-21 07:00:00

語音識別AI人工智能

2023-11-24 09:26:29

Java圖像

2022-10-19 07:42:41

圖像識別神經(jīng)網(wǎng)絡(luò)

2017-07-20 17:27:01

互聯(lián)網(wǎng)

2025-01-11 23:14:52

2021-02-03 17:15:35

圖像識別AI人工智能

2022-08-19 14:14:13

人工智能人臉識別安全

2018-04-24 10:45:00

Python人工智能圖像識別

2017-11-06 16:50:38

人工智能圖像識別數(shù)據(jù)邏輯
點贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 成人免费在线观看 | 91免费在线 | 亚洲一区二区三区免费 | 国产91丝袜在线18 | 亚洲一二三区在线观看 | h片免费看 | 一级毛片视频在线观看 | 亚洲视频一区在线观看 | 欧美无乱码久久久免费午夜一区 | 国产精品美女久久久久久久久久久 | 日韩成人影院 | 亚洲精品v日韩精品 | 日本一区二区高清视频 | 一区二区手机在线 | 免费a国产| 黄色男女网站 | 亚洲一区二区精品视频 | 久久国产精彩视频 | 久久精品99国产精品 | 国产精品美女久久久久久久久久久 | 午夜在线小视频 | 亚洲国产精品一区二区久久 | 亚洲美女网站 | 一级在线免费观看 | av网站免费在线观看 | 人操人人| 国产精品99久久久久久宅男 | 毛片一级片 | 中文字幕在线视频一区二区三区 | 91在线中文字幕 | 国产国产精品久久久久 | 亚州一区二区三区 | 精品国产乱码久久久久久闺蜜 | 中文字幕在线一区二区三区 | 日本天堂视频在线观看 | 国产视频一二三区 | 亚洲综合中文字幕在线观看 | 久久精品免费 | 激情一区二区三区 | 欧美三级三级三级爽爽爽 | 国产一区亚洲 |