参考博客:https://blog.csdn.net/qq_38783948/article/details/88239109
https://blog.csdn.net/Code_Mart/article/details/96902951
face++官方给出的API调用 urllib
# -*- coding: utf-8 -*-
import urllib.request
import urllib.error
import time
http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
key = "填上你的KEY"
secret = "填上你的SECRET"
filepath = r"本地图片的路径"
boundary = '----------%s' % hex(int(time.time() * 1000))
data = []
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_key')
data.append(key)
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_secret')
data.append(secret)
data.append('--%s' % boundary)
fr = open(filepath, 'rb')
data.append('Content-Disposition: form-data; name="%s"; filename=" "' % 'image_file')
data.append('Content-Type: %s\r\n' % 'application/octet-stream')
data.append(fr.read())
fr.close()
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_landmark')
data.append('1')
data.append('--%s' % boundary)
data.append('Content-Disposition: form-data; name="%s"\r\n' % 'return_attributes')
data.append(
"gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus")
data.append('--%s--\r\n' % boundary)
for i, d in enumerate(data):
if isinstance(d, str):
data[i] = d.encode('utf-8')
http_body = b'\r\n'.join(data)
# build http request
req = urllib.request.Request(url=http_url, data=http_body)
# header
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
try:
# post data to server
resp = urllib.request.urlopen(req, timeout=5)
# get response
qrcont = resp.read()
# if you want to load as json, you should decode first,
# for example: json.loads(qrount.decode('utf-8'))
print(qrcont.decode('utf-8'))
except urllib.error.HTTPError as e:
print(e.read().decode('utf-8'))
requests是对urllib的进一步封装,因此在使用上显得更加的便捷,建议在实际应用当中尽量使用request
基于http的post,get请求
r = requests.get(url, params=d)
参数d表示值为字符串的字典,d不是必须的
r = requests.post(url,data=data)
post请求需 设置请求参数data 请求对象response相应内容
r.status_code:响应状态码 r.raw:原始响应体,使用r.raw.read()读取
r.content:字节方式的响应体,需要进行解码
r.text:字符串形式的响应体,会自动根据响应头部的字符编码进行解码
r.headers:以字典对象存储服务器响应头,字典见键不区分大小写,若键不存在返回none。
r.json():Requests中内置的json解码器 r.raise_for_status():请求失败,抛出异常
r.url:获取请求链接 r.cookies:获取请求后的cookie r.encoding:获取编码格式
requests的调用
import requests
from json import JSONDecoder
http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
key = "mrsGICyUYSMx10jrfj2uCmOlyaBpzub_"
secret = "KelaCutz2ipoHGnwM8vfPz3ER-xSq6iJ"
filepath1 = "../data/emotion.png"
data = {"api_key":key, "api_secret": secret, "return_attributes": "gender,age,smiling,beauty"}
#必需的参数,注意key、secret、"gender,age,smiling,beauty"均为字符串,与官网要求一致
files = {"image_file": open(filepath1, "rb")}
'''以二进制读入图像,这个字典中open(filepath1, "rb")返回的是二进制的图像文件,所以"image_file"是二进制文件,符合官网要求'''
response = requests.post(http_url, data=data, files=files)
#POTS上传
#req_con = response.content.decode('utf-8')
#response的内容是JSON格式
#req_dict = JSONDecoder().decode(req_con)
#对其解码成字典格式
#print(req_dict)
#输出
print(response.json())