NeuHub图像垃圾分类api和百度图像识别api

京东

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

 

 

NeuHub图像垃圾分类api和百度图像识别api

上一篇:AcWing 893. 集合-Nim游戏


下一篇:AcWing 891. Nim游戏