调用目标检测百度接口api

# encoding:utf-8
import requests
import urllib.request
import base64
import json

'''
easydl物体检测
'''

request_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/detection/taozhou"
filename = '/home/user/图片/1.jpg'
headers = {
    'Content-Type' : 'application/json'
}

#打开图片文件
# with open(filename , 'rb') as file:
#     pic = base64.b64encode(file.read()).decode()
# data = {
#     'image':pic
# }
# request = requests.post(request_url,headers= headers, data=json.dumps(data))
# print(json.loads(request.content))
with open("/media/1.jpg", "rb") as f:
    base64_data = base64.b64encode(f.read())
    s = base64_data.decode('UTF8')

params = {"image": s}
params = json.dumps(params)
access_token = '24.634b8532083accfcc45f7d4914003d69.2592000.1610877982.282335-22896702'
request_url = request_url + "?access_token=" + access_token
#data = urllib.parse.urlencode({'image' : s}).encode()
request = urllib.request.Request(url=request_url, data=params.encode("utf-8"))
#request = urllib.request.Request(url=request_url, data=urllib.parse.urlencode(params).encode(encoding='UTF8'))
request.add_header('Content-Type', 'application/json')
#print(request)
response = urllib.request.urlopen(request)
content = response.read()
if content:
    print(content)

返回结果:
b'{"log_id":220830954428492646,"results":[' \
b'{"location":{"height":26,"left":11,"top":4,"width":16},"name":"normal","score":0.9995918869972229},' \
b'{"location":{"height":27,"left":100,"top":33,"width":16},"name":"normal","score":0.9994158744812012},' \
b'{"location":{"height":26,"left":29,"top":5,"width":15},"name":"normal","score":0.9993391633033752},' \
b'{"location":{"height":27,"left":82,"top":34,"width":16},"name":"normal","score":0.9993083477020264},' \
b'{"location":{"height":26,"left":47,"top":5,"width":15},"name":"normal","score":0.999206006526947},' \
b'{"location":{"height":27,"left":117,"top":6,"width":17},"name":"normal","score":0.9991251826286316},' \
b'{"location":{"height":28,"left":99,"top":5,"width":18},"name":"normal","score":0.9989053010940552},' \
b'{"location":{"height":27,"left":64,"top":33,"width":16},"name":"normal","score":0.9988359808921814},' \
b'{"location":{"height":27,"left":47,"top":33,"width":16},"name":"normal","score":0.998664379119873},' \
b'{"location":{"height":27,"left":118,"top":34,"width":16},"name":"normal","score":0.9985684156417847},' \
b'{"location":{"height":27,"left":64,"top":5,"width":16},"name":"normal","score":0.9985032081604004},' \
b'{"location":{"height":27,"left":152,"top":6,"width":18},"name":"normal","score":0.9984663724899292},' \
b'{"location":{"height":27,"left":135,"top":6,"width":16},"name":"normal","score":0.9982447624206543},' \
b'{"location":{"height":27,"left":82,"top":5,"width":16},"name":"normal","score":0.9980190992355347},' \
b'{"location":{"height":27,"left":153,"top":34,"width":16},"name":"normal","score":0.9967520236968994},' \
b'{"location":{"height":27,"left":29,"top":32,"width":16},"name":"normal","score":0.9967063069343567},' \
b'{"location":{"height":28,"left":135,"top":33,"width":16},"name":"normal","score":0.9958341121673584},' \
b'{"location":{"height":26,"left":171,"top":7,"width":11},"name":"normal","score":0.9646703004837036},' \
b'{"location":{"height":27,"left":10,"top":32,"width":17},"name":"normal","score":0.9606804847717285},' \
b'{"location":{"height":27,"left":171,"top":34,"width":11},"name":"normal","score":0.9183089137077332},' \
b'{"location":{"height":27,"left":0,"top":4,"width":9},"name":"normal","score":0.7153151631355286}]}\n'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值