PYTHON 3 calls face++ API code (easy to understand)

    The face recognition project recently called the API of face++, but the code given by the official website is python2. It is estimated that many beginners have spent a lot of effort in changing the code to Python3 like me. Now this article will simply tell you how to call any API of face++ with just a dozen lines of code.

    Attach the code first, the running environment: Python3.6

import requests
from json import JSONDecoder

http_url = "https://api-cn.faceplusplus.com/facepp/v3/detect"
 #URL you want to call the API

 key = "public key"
 secret = "private key"
 #A pair of keys provided by face++

 filepath1 = "D:\py\image\WIN_20180412_21_52_13_Pro.jpg" #Absolute
 path of image file

 data = { "api_key" :key , "api_secret" : secret , "return_attributes" : "gender,age,smiling,beauty" }
 #Required Parameters, note that key, secret, "gender,age,smiling,beauty" are all strings, which are consistent with the requirements of the official website

 files = { "image_file" : open (filepath1 , "rb" )}
'''The image is read in binary. In this dictionary, open(filepath1, "rb") returns a binary image file, so "image_file" is a binary file, which meets the requirements of the official website'''

 response = requests.post(http_url , data =data , files =files)
 #POTS upload

 req_con = response.content.decode( 'utf-8' )
 #response content is in JSON format

 req_dict = JSONDecoder().decode(req_con) #Decode
 it into dictionary format
 print (req_dict)
 #output

    I am calling the Detect API. The calling URL and calling method are clearly written on the official website. If you want to call different methods, use the corresponding URL and calling method.

What I want to emphasize is request parameters, if you know how to pass parameters, then you can call all APIs on face++.

1. Parameter name

    The parameter name is a string, except for the FILE parameter, all other parameters are written into the data dictionary: the parameter name is used as the key, and the variable name or string is used as the value (such as key, secret, "gender, age, smiling, beauty" in the above program "). Usually the values ​​are strings.

Let's take a look at the request parameters given by the official website:


(If you can't see clearly, please click on the full screen to see the picture)

我想特别声明的是参数“return_attributes",这个参数作为键,后面对应的值就是你请求的返回属性(注意CS中attribute一般是属性的意思,它是可数名词,不是四级词汇归因于。。。),我只请求了四个变量"gender,age,smiling,beauty",想加的话直接在字符串里写就行了,它们之间用逗号间隔。

 
 
"return_attributes": "gender,age,smiling,beauty"

   

  除FILE参数其余参数都存在data字典里了,那么现在该处理FILE文件了。

  官网的描述是:

image_file

File

一个图片,二进制文件,需要用post multipart/form-data的方式上传。

参数名“image_file”,注意这个是作为files字典的键,不是让你建立一个叫image_file的文件对象。我的代码是:

files = {"image_file": open(filepath1, "rb")}                     

    open(filepath1, "rb")返回的是二进制的图像文件,rb:以二进制读取。所以"image_file"键对应的值是二进制文件,符合官网要求


2.返回值

    返回值经JSONDecode之后是字典类型的,官网例子如下:



    具体返回值中的参数代表什么意思查询官网的返回值表格就行了。

我们回过头来看看关键点:

    1.不同API的URL

    2.此API要求的请求参数

    3.处理字典类型的返回值

怎么样,调用face++的API是不是很简单呢!


Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326819182&siteId=291194637