1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# encoding:utf-8
from urllib import request,parse
import ssl
import base64
import json

'''
人脸检测与属性分析
'''

#文件路径
file = 'test.jpg'

#接口地址
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"

#将图片读取并转base64
f = open(r'%s' % file,'rb')
img = base64.b64encode(f.read())
f.close()

#设置图像来源 类型 领域
#BASE64
params={'image':'' + str(img, 'utf-8') + '','image_type':'BASE64','face_field':'age,beauty,faceshape,gender,glasses'}
'''
#FACE_TOKEN
params={'image':'027d8308a2ec665acb1bdf63e513bcb9','image_type':'FACE_TOKEN','face_field':'age,beauty,faceshape,gender,glasses'}
#URL
params={'image':'https://www.xsshome.cn/face.jpg','image_type':'URL','face_field':'age,beauty,faceshape,gender,glasses'}
'''

access_token = '11.11111222223333344444555556666677.1111122.1111122222.111112-11111222'

#请求转utf-8
data = parse.urlencode(params).encode('utf-8')

#拼接请求
print("request url stitching")
request_url = request_url + "?access_token=" + access_token
req = request.Request(url = request_url, data = data)
req.add_header('Content-Type', 'application/json')

#打开请求
print("urlopen")
response = request.urlopen(req)

#读取请求返回内容
print("read response")
content = response.read()

if content:
#输出返回内容
print(content)