| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- # encoding:utf-8
- import base64
- import urllib
- import urllib2, json
- from token import GetToken
-
- '''
- 人脸对比
- '''
- url = "https://aip.baidubce.com/rest/2.0/face/v3/match"
- def FaceRecg(face1, face2, token):
- global url
- f = open(face1, 'rb')
- # 参数images:图像base64编码
- img1 = base64.b64encode(f.read())
- # 二进制方式打开图文件
- f = open(face2, 'rb')
- # 参数images:图像base64编码
- img2 = base64.b64encode(f.read())
- params = json.dumps(
- [{"image": img1, "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW"},
- {"image": img2, "image_type": "BASE64", "face_type": "LIVE", "quality_control": "LOW"}
- ])
- '''
- params = {"image":img1 + ',' + img2,
- "image_type":"BASE64",
- "face_type":"LIVE",
- "quality_control":"LOW",
- "liveness_control":"HIGH"}
- params = urllib.urlencode(params)
- '''
- request_url = url + "?access_token=" + token
- request = urllib2.Request(url=request_url, data=params)
- request.add_header('Content-Type', 'application/json')
- #response = urllib2.urlopen(request)
- response = None
- try:
- response = urllib2.urlopen(request)
- except urllib2.URLError,e:
- print e.reason
- print e.reason[0]
- print e.reason[1]
- if (response == None):
- print "response=null"
- return None
- content = response.read()
- if content:
- print content
- js = json.loads(content)
- if (js.has_key('error_msg') and js['error_msg'] == "SUCCESS"):
- if (js.has_key('result')):
- #for item in js['result']:
- print(u"%2.2f%% " % (float(js['result']['score'])))
- if(float(js['result']['score']) >= 80):
- print 'OK'
- else:
- print 'FAIL'
-
-
- if __name__ == "__main__":
- #前面两个参数是图片路径
- Token = GetToken()
- FaceRecg('pic/1.jpg', 'pic/2.jpg', Token)
|