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
# encoding:utf-8
import urllib3
import base64
import json

http=urllib3.PoolManager()


#人脸检测与属性分析

#文件路径
file1 = '1.jpg'
file2 = '2.jpg'

#接口地址
api = 'https://aip.baidubce.com/rest/2.0/face/v3/match
access_token = '11.11111222223333344444555556666677.1111122.1111122222.111112-11111222'
url=api + '?access_token=' + access_token

#将图片读取并转base64
print("read image 1")
f1 = open(r'%s' % file1,'rb')
img1 = base64.b64encode(f1.read())
f1.close()

print("read image 2")
f2 = open(r'%s' % file2,'rb')
img2 = base64.b64encode(f2.read())
f2.close()

#设置图像来源 类型 领域
params = [{'image':str(img1,'utf-8'),'image_type':'BASE64'},{'image':str(img2,'utf-8'),'image_type':'BASE64'}]
#params = {"images":str(img1,'utf-8') + ',' + str(img2,'utf-8')}


#请求转utf-8
print("urlencode")
data = json.dumps(params).encode('utf-8')
req = http.request('POST',
url,
body=data,
headers={'Content-Type':'application/json'})

content = str(req.data,'utf-8')
if content:
print(content)