Face Plus Plus Python API Changing Parameters

Viewed 818
# -*- 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()

I am trying to change the parameters for the face recognition API but I have not been able to do so. I would like to change the code like

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) 

As in I would like to add more parameters so that the information I get is rich. How do I do this? Right now I'm only getting the coordinates of the face and face_token.

2 Answers

Used the requests library with locally stored images to implement Faceplusplus API. You can select any number of parameters based on your needs. The code below has all parameters listed.

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")

I had the same issue when I tried to use the code sample. I would recommend you to use this simpler version of the code that works on Python, you can add other features as well such as '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)
Related