生活随笔
收集整理的這篇文章主要介紹了
机器视觉-EasyDL商品检测-标准版-Demo
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
機器視覺-EasyDL商品檢測-標準版
功能:
????EasyDL是百度大腦中的一個定制化訓練和服務平臺,EasyDL零售版是EasyDL針對零售場景推出的行業版,定制商品檢測服務是EasyDL零售版的一項服務,專門用于訓練貨架合規性檢查、自助結算臺、無人零售貨柜等場景下的定制化商品檢測AI模型,訓練出的模型將以API的形式為客戶提供服務,API接口可以返回商品的名稱和商品在圖中的位置,適用于識別貨架中的商品信息,商品計數,輔助貨架商品陳列合規檢查,如鋪貨率、陳列情況等。具體細節(包括操作步驟)請看相關API技術文檔:
https://ai.baidu.com/docs#/EasyDL_Retail_Intro/top
下面是我寫的Demo,目前平天上沒提供相關Demo。一開始用PHP寫的,后來為了兼容公司這邊實際情況,改成了NODEJS了,開發過程中遇到了很多坑點,參數交互的時候,一定要細心。百度大腦后臺對應的服務器有的支持Ajax跨域,有的不支持,這個是自己實際測出來的,這個要注意,還有就是我的這個Demo是本地通過請求獲取token,然后在拿著token直接在本地訪問百度的接口,把圖片的base64編碼扔了過去,實際情況可能不是這樣的,比如圖片的話你可能自己服務器也要存,這種情況請根據實際業務需求進行對應更改。切記不要暴露自己的?API?KEY??和?Secret?Key?。同時百度的EasyDL分兩個模式,一個是定制版一個是標準版,我這次整理的是標準版的接口,標準版不涉及到自己訓練數據。所以相對簡單。同時標準版和定制版的Demo可以通用的。只是定制版本再加上自己去平臺訓練數據參數調整等一些額外的操作等。還有很多流程和細節,請直接看上面API文檔鏈接。
server.js
var?http?=?require("http");var?https?=?require('https');var?fs?=?require('fs');var?url?=?require('url');function?gettoken(response)?{?//?瀏覽器端發來get請求var?apikey?=?"XXX換成自己的";??//API?Keyvar?secretkey?=?"XXXX換成自己的";?//Secret?Keyvar?url?=?"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="?+?apikey?+?"&client_secret="?+?secretkey;https.get(url,function(res){??//通過get方法獲取對應地址中的頁面信息var?chunks?=?[];var?size?=?0;res.on('data',function(chunk){???//監聽事件?傳輸chunks.push(chunk);size?+=?chunk.length;});res.on('end',function(){??//數據傳輸完var?data?=?Buffer.concat(chunks,size);var?html?=?data.toString();var?obj?=?JSON.parse(html);var?token?=?obj.access_token;console.log(token);response.write(token);response.end();});});}http.createServer(function(request,?response)?{response.writeHead(200,?{"Content-Type":?"text/html"});//text/plainvar?pathname?=?url.parse(request.url).pathname;console.log(pathname);if(pathname?==?"/token"){console.log("request?get?token!");gettoken(response);}else{fs.readFile(pathname.substr(1),?function?(err,?data)?{if?(err)?{console.log(err);//?HTTP?狀態碼:?404?:?NOT?FOUND//?Content?Type:?text/htmlresponse.writeHead(404,?{'Content-Type':?'text/html'});}else{//?HTTP?狀態碼:?200?:?OK//?Content?Type:?text/htmlresponse.writeHead(200,?{'Content-Type':?'text/html'});//?響應文件內容response.write(data.toString());}//??發送響應數據response.end();});}//response.write("Hello?World");//response.end();}).listen(8888);console.log("nodejs?start?listen?8888?port!");console.log("http://127.0.0.1:8888/client.html");
client.html
<div?style="width:?100%;height:?100%"><div?style="position:absolute;left:0;top:0px;"><input?accept="image/*"??id="upload_file"?type="file"></div><div?style="background-color:?#5bc0de;?position:absolute;width:48%;height:200px;left:1%;top:30px;"><img?src=""?id="productimg"?width="100%"?height="100%"?/></div><div?style="position:absolute;width:48%;height:200px;left:51%;top:30px;"><textarea?id="base64_output"??style="height:?100%;width:100%"></textarea></div><div?style="position:absolute;width:98%;height:400px;left:1%;top:250px;"><textarea?id="imgmessage"??style="height:?100%;width:100%"></textarea></div></div><script>document.getElementById("upload_file").onchange?=?function?(e)?{var?file?=?e.target.files[0];$_("productimg").src=URL.createObjectURL(file);gen_base64();};function?$_(id)?{return?document.getElementById(id);}function?gen_base64()?{var?file?=?$_('upload_file').files[0];r?=?new?FileReader();??//本地預覽r.onload?=?function(){$_('base64_output').value?=?r.result;var?imgbase64?=?r.result;work(imgbase64);}r.readAsDataURL(file);????//Base64}document.getElementById("upimage").onchange?=?function?()?{gen_base64();};</script><script>function?work(imgbase64)?{var?url?=?"./token";var?httpRequest?=?new?XMLHttpRequest();httpRequest.open('GET',?url,?true);httpRequest.setRequestHeader("Content-type","application/json");httpRequest.send();httpRequest.onreadystatechange?=?function?()?{if?(httpRequest.readyState?==?4?&&?httpRequest.status?==?200)?{var?token?=?httpRequest.responseText;//獲取到json字符串,還需解析//console.log(json);//document.write(json);getimgmessage(imgbase64,token);}};}function?getimgmessage(imgbase64,token){var?index?=?imgbase64.indexOf(',');imgbase64?=?imgbase64.substring(index+1,imgbase64.length);//document.write(imgbase64);var?obj?=?{?"image":?imgbase64};var?httpRequest?=?new?XMLHttpRequest();var?url?=?"https://aip.baidubce.com/rpc/2.0/easydl/v1/retail/drink?access_token="?+?token;httpRequest.open('POST',?url,?true);httpRequest.setRequestHeader("Content-type","application/json");httpRequest.send(JSON.stringify(obj));/***?獲取數據后的處理程序*/httpRequest.onreadystatechange?=?function?()?{if?(httpRequest.readyState?==?4?&&?httpRequest.status?==?200)?{var?json?=?httpRequest.responseText;var?analysisresults?=unescape(json.replace(/\\u/g,?'%u'));//console.log(json);//document.write(unescape(json.replace(/\\u/g,?'%u')));var?analysisresultss?=?JSON.parse(analysisresults);document.getElementById('imgmessage').value?=?dealstring(analysisresultss);}};}function??dealstring(analysisresultss){var?string?=?"log_id:"?+?analysisresultss.log_id?+?"\n";string?=?string?+?"results?"?+?analysisresultss.results.length?+?"\n";for(var?index?=?0?;index?<analysisresultss.results.length?;index?++){var?strtmp?=?"";if(index?<?10)?strtmp?=?"000";else?if(index?<?100)strtmp?=?"00";else?strtmp?=?"0";string?=?string?+?strtmp?+?index?+?"."?+?JSON.stringify(analysisresultss.results[index])?+?"\n";}return?string;}</script>
運行結果:(NODE+Win7)
?
總結
以上是生活随笔為你收集整理的机器视觉-EasyDL商品检测-标准版-Demo的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。