百度API接口python3如何使用【人脸搜索示例】

import urllib, sys
import urllib.request as urllib2#urllib2在py3中我们用urllib.request来替换
import ssl
import json

# client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【自己的AK】&client_secret=【自己的SK】'
request = urllib2.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request)
content = response.read()
if (content):
    access_token=json.loads(content)["access_token"]
    # print(access_token)

# url="https://aip.baidubce.com/rest/2.0/face/v3/search?access_token="+access_token

# encoding:utf-8
import urllib


'''
人脸搜索
'''
import base64
with open("G:\\PY\\face.png",'rb') as f:
    imageB = base64.b64encode(f.read())
image = str(imageB,'utf-8')

request_url = "https://aip.baidubce.com/rest/2.0/face/v3/search"


params = {"image_type":"BASE64","group_id_list":"faceLib,group1","quality_control":"LOW"}
params["image"]=image
params1=str(params)


# params = "{\"image\":\"027d8308a2ec665acb1bdf63e513bcb9\",\"image_type\":\"FACE_TOKEN\",\"group_id_list\":\"group_repeat,group_233\",\"quality_control\":\"LOW\",\"liveness_control\":\"NORMAL\"}"
# data = urllib.parse.urlencode(params).encode("utf-8")

# access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token

# request = urllib2.Request(url=request_url, data=params1)
# print(request)
# request.add_header('Content-Type', 'application/json')
# print(request)

response = urllib2.urlopen(request_url,data=urllib.parse.urlencode(params).encode("utf-8"))#data=urllib.parse.urlencode(data).encode("utf-8")
content = response.read()
if content:
    print(content)

 

posted @ 2019-08-02 20:55  程序杰杰  阅读(987)  评论(0编辑  收藏  举报