京東
NeuHub圖像垃圾分類申請:http://neuhub.jd.com/gwtest/init/242
文檔:https://aidoc.jd.com/image/garbageClassification.html
import base64 import wx_sdk #我是將wx_sdk.py移同當前文件夾了 import json url = 'https://aiapi.jd.com/jdai/garbageImageSearch' f = open('nfsq.jpg', 'rb') #轉成base64 image_base64 = str(base64.b64encode(f.read()), encoding='utf-8') #自己xjb並湊的 bodys = "{\"cityId\"" + ':' + "\"310000\"" + ", " + "\"imgBase64\"" + ':' + "\"" + image_base64 + "\"" "}" #bodyStr = '{ "cityId":"310000", "imgBase64":"image_base64"}' params = { 'appkey' : '你的appkey', 'secretkey' : '你的secretkey'} response = wx_sdk.wx_post_req(url, params, bodyStr=bodys) #print(response.text) #將json格式轉成字典 result = json.loads(response.text) #輸出自己想要的一些信息 for key in result["result"]["garbage_info"]: if(key["confidence"] > 0.5): #只輸出置信度超過0.5的,官方建議為0.7 print(key["cate_name"], key["confidence"], key["garbage_name"])
百度
百度圖像識別api :https://ai.baidu.com/docs#/ImageClassify-API/ebc492b1
1. 安裝百度api
pip3 install baidu-aip
2. 代碼
from aip import AipImageClassify """ 你的 APPID AK SK """ APP_ID = '你的APP_ID' API_KEY = '你的APP_KEY' SECRET_KEY = '你的SECRET_KEY' client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY) def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() image = get_file_content('nfsq.jpg') #返回百科信息的結果數,默認為0,不返回;2為返回前2個結果的百科信息,以此類推。 options = {} options["baike_num"] = 5 """ 帶參數調用通用物體識別 """ result = client.advancedGeneral(image, options) # print(result) result_num = result['result_num'] for i in range(0, result_num): print(result['result'][i]['keyword'])
另一種使用api的方式是使用access_token
//檢測圖像中的主體位置,通用物體和場景識別的高級版是收費的?
1. 獲取access_token
from urllib import request import ssl import json gcontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1) # client_id 為官網獲取的AK, client_secret 為官網獲取的SK host = 'https://aip.baidubce.com/oauth/2.0/token?grant_' \ 'type=client_credentials&client_id=你的AK&client_secret=你的SKreq = request.Request(host) response = request.urlopen(req, context=gcontext).read().decode('UTF-8') result = json.loads(response) if (result): print(result['access_token'])
2. 將圖片用base64編碼
import base64 f = open('tiger.jpg', 'rb') img = base64.b64encode(f.read()) print(img)
3. 調用api
import requests import base64 host = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/object_detect' headers={ 'Content-Type':'application/x-www-form-urlencoded' } access_token= 'xxx' #步驟1中獲得的token host=host+'?access_token='+access_token f = open('destop.jpg', 'rb') img = base64.b64encode(f.read()) # print(img) data={} data['access_token']=access_token data['image'] =img res = requests.post(url=host,headers=headers,data=data) req=res.json() print(req['result'])
參考鏈接:
1. https://blog.csdn.net/cool_bot/article/details/90465167
2. https://blog.csdn.net/qq_40484582/article/details/82054009