# 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'
调用目标检测百度接口api
最新推荐文章于 2024-04-24 15:08:20 发布