1
$ curl -X POST "https://api- 
us.faceplusplus.com/facepp/v3/detect" -F 
"api_key=<api key>" \
-F "api_secret<api secret>" \
-F "image_url=<顔の入った写真のURL>" \
-F "return_landmark=1"

Hello, I am trying to write an equivalent python requests code for the above but I keep getting errors.

import requests
import json

API_KEY = "--------------"
API_SECRET = "-----------"
image_path="/Users/dukeglacia/Downloads/test_images2/eo.jpg"
vision_base_url="https://api-us.faceplusplus.com/facepp/v3/detect"

response = requests.post(
    vision_base_url,
    {
        'api_key': API_KEY,
        'api_secret': API_SECRET,
        # 'image_url': img_url,
        'image_file': image_path,
        'return_landmark': 1,
        'return_attributes': 'headpose,eyestatus,emotion,ethnicity,beauty,facequality,mouthstatus,eyegaze,gender,age,smiling'

    }
)

analysis = response.json()
print (analysis)

My error says that the arguments image_file not found. But as shown in the code below I have included the arguments.

blhsing
  • 91,368
  • 6
  • 71
  • 106
robotlover
  • 331
  • 1
  • 2
  • 12
  • Where the actual error you get from ? from python code or from the API endpoint ? I tried your python code and seems to send almost same request as `curl` except that `Content-Type` is `multipart/form-data` in `curl` but `application/x-www-form-urlencoded` in python – ymonad Sep 21 '18 at 06:38
  • So if that difference is the problem, you may try https://stackoverflow.com/questions/12385179/how-to-send-a-multipart-form-data-with-requests-in-python – ymonad Sep 21 '18 at 06:40
  • This is my error – robotlover Sep 21 '18 at 06:44
  • {'time_used': 65, 'error_message': 'MISSING_ARGUMENTS: image_url, image_file, image_base64', 'request_id': '1537512382,040315b2-25b5-41c7-92a7-5aa5a731b344'} – robotlover Sep 21 '18 at 06:46
  • So I think there is a error on the API side – robotlover Sep 21 '18 at 06:46

1 Answers1

1

According to FacePlusPlus's documentation, image_file should be a file instead of a path, so you should post the file binary with:

response = requests.post(
    vision_base_url,
    {
        'api_key': API_KEY,
        'api_secret': API_SECRET,
        'return_landmark': 1,
        'return_attributes': 'headpose,eyestatus,emotion,ethnicity,beauty,facequality,mouthstatus,eyegaze,gender,age,smiling'

    },
    files={'image_file': open(image_path, 'rb')}
)

Please refer to requests's documentation for more details of how you can upload a file.

blhsing
  • 91,368
  • 6
  • 71
  • 106