Face Plus Plus Python API更改参数

时间:2018-09-21 03:06:44

标签: python api image-processing machine-learning deep-learning

# -*- coding: utf-8 -*-
import urllib2
import urllib
import time
http_url = 'https://api-us.faceplusplus.com/facepp/v3/detect'
key = ""
secret = ""
filepath = r"iop.jpg"
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="co33.jpg"' % 'image_file')
data.append('Content-Type: %s\r\n' % 'application/octet-stream')
data.append(fr.read())
fr.close()
data.append('--%s--\r\n' % boundary)

http_body='\r\n'.join(data)
#buld http request
req=urllib2.Request(http_url)
#header
req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
req.add_data(http_body)
try:
    #post data to server
    resp = urllib2.urlopen(req, timeout=5)
    #get response
    qrcont=resp.read()
    print qrcont

except urllib2.HTTPError as e:
    print e.read()

我正在尝试更改面部识别API的参数,但我无法更改。我想更改

之类的代码
def detect_image(img_path):
    endpoint = 'https://api-us.faceplusplus.com'
    img_file = base64.encodestring(open(img_path, 'rb').read())
    try:
        response = requests.post(
            endpoint + '/facepp/v3/detect',
            {
                'api_key': API_KEY,
                'api_secret': API_SECRET,
                # 'image_url': img_url,
                'image_base64': img_file,
                'return_landmark': 1,
                'return_attributes': 'headpose,eyestatus,facequality,mouthstatus,eyegaze'
            }
        )
        # 5秒スリープ
        time.sleep(5) 

与之类似,我想添加更多参数,以便获得丰富的信息。我该怎么做呢?现在,我只获得face和face_token的坐标。

2 个答案:

答案 0 :(得分:1)

使用请求库和本地存储的图像来实现Faceplusplus API。您可以根据需要选择任意数量的参数。下面的代码列出了所有参数。

import requests
import base64

def faceplusAPI(filepath):
    http_url = 'https://api-us.faceplusplus.com/facepp/v3/detect'
    key = "YOUR API KEY"
    secret = "API SECRET"

    fr = open(filepath,'rb')
    img64 = base64.b64encode(fr.read())
    payload = {
                'api_key': key, 
                'api_secret': secret, 
                'image_base64':img64,
                'return_attributes': 'age,gender,headpose,smiling,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus'
                }
    fr.close()
    try:
        res = requests.post(http_url, data=payload)
        return res
    except Exception as e:
        print('Error:')
        print(e)

response = faceplusAPI("IMG_8934.JPG")

答案 1 :(得分:0)

当我尝试使用代码示例时,我遇到了同样的问题。我建议您使用适用于Python的此代码的更简单版本,也可以添加其他功能,例如“ return_landmark”。

import requests

http_url = 'https://api-us.faceplusplus.com/facepp/v3/detect'
key = "YOUR_API_KEY"
secret = "YOUR_SECRET_API_KEY"
attributes="gender,age,ethnicity"
url="YOUR_IMAGE_URL"

json_resp = requests.post(http_url,
      data = { 
          'api_key': key,
          'api_secret': secret,
          'image_url': url,
          'return_attributes': attributes
      }
)

print("Response : ", json_resp.text)