错误:ConnectionError at /trait
HTTPSConnectionPool(host='aip.baidubcse.com', port=443): Max retries exceeded with url: /rest/2.0/face/v3/detect?acces_token=24.5f998d7e9a328101007dbdab6af23051.2592000.1653304808.282335-26040449 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002A6B02743C8>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
Request Method: GET
Request URL: http://127.0.0.1:8000/trait
Django Version: 3.2.13
Exception Type: ConnectionError
Exception Value:
HTTPSConnectionPool(host='aip.baidubcse.com', port=443): Max retries exceeded with url: /rest/2.0/face/v3/detect?acces_token=24.5f998d7e9a328101007dbdab6af23051.2592000.1653304808.282335-26040449 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x000002A6B02743C8>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed'))
Exception Location: D:\Python\daima\face_recognize\face_recognize_django\venv\lib\site-packages\requests\adapters.py, line 519, in send
Python Executable: D:\Python\daima\face_recognize\face_recognize_django\venv\Scripts\python.exe
Python Version: 3.7.0
部分代码
if not request.session.get('is_login', None):
# 没登陆,回到首页,未登陆
return redirect("/home")
# now_personId = request.session['name']
''' 获取access token '''
""" host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + self.AK + '&client_secret=' + self.SK"""
'''填充自己申请的AK和SK'''
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Xli5UAihCH93gj3hT0MUXywG&client_secret=Gnc8gqLswWvqmIGEGdC6KLcLXGb5CzEE'
request1 = urllib2.Request(host) # 此处不能取名为request
request1.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib2.urlopen(request1)
content = response.read()
if (content):
token = json.loads(content)['access_token']
response.close() # 可无
''' 图片信息获取处理成base64编码格式 '''
imgPath = 'static/image/login/this_guy.jpg'
with open(imgPath, "rb") as f: # 转为二进制格式
base64_data = base64.b64encode(f.read()) # 使用base64进行加密
f.close()
base64_data = str(base64_data, 'utf-8')
''' 人脸检测与属性分析 '''
request_url = "https://aip.baidubcse.com/rest/2.0/face/v3/detect"
request_url = request_url + "?acces_token=" + token
header = {'Content-Type': 'application/json'}
params = {"image": base64_data, "image_type": "BASE64",
"face_field": "age,beauty,expression,face_shape,gender,glasses,face_shape,race"}
response1 = requests.post(url=request_url, data=params, headers=header)
# response = urllib2.urlopen(request, urllib.parse.urlencode(params).encode(encoding='utf-8'))
content = response1.json()
face_list = content['result']['face_list'][0]
# location = face_list['location']
age = face_list['age']
beauty = face_list['beauty']
expression = face_list['expression']['type']
gender = face_list['gender']['type']
glasses = face_list['glasses']['type']
face_shape = face_list['face_shape']['type']
# emotione=face_list['emotione']['type']
race = face_list['race']['type']
trait = {'age': age, 'beauty': beauty, 'expression': expression, gender: 'gender', glasses: 'glasses',
face_shape: 'face_shape', race: 'race'}
return render(request, "trait.html", trait)