/face/face_attributes_detect

优质
小牛编辑
130浏览
2023-12-01

1.接口描述

该 API 用于识别人脸各项属性的值。

  • 图片要求

    1. 格式为 JPG(JPEG),BMP,PNG,GIF,TIFF
    2. 宽和高大于 8px,小于等于4000px
    3. 小于等于 5 MB
  • 支持自动识别人脸方向

请求方式

POST

请求 URL

https://cloudapi.linkface.cn/face/face_attributes_detect

2.请求参数

字段类型必需描述
api_idstringAPI 账户
api_secretstringAPI 密钥
filefile见下方注释需上传的图片文件。上传本地图片进行检测时选取此参数
urlstring见下方注释图片网络地址。采用抓取网络图片方式时需选取此参数
image_idstring见下方注释图片的id。在云端上传过图片可采用
auto_rotateboolean开启图片自动旋转功能。开通:true,不开通:false。默认不开通

请求参数 file,urlimage_id 三选一。

url 中含有不少特殊字符,若将 URL 放入 Query String 中则需要对这些字符进行转义,所有中文和特殊字符必需以UTF-8编码转义。 目前支持 http/https 等协议的网络地址。下载限时 5s,超时后仍未下载完成则属于失败。 参数 file 需把图片文件的内容以 multipart/form-data 的形式放到 POST 消息体中。

3.返回参数

字段类型说明
request_idstring本次请求的id
statusstring状态,正常为 OK
image_idstring图片上传云端后的id 。使用file、url方式提交图片会返回此参数
agefloat年龄
attractivefloat魅力值
smilefloat微笑 (此值大于0.5则为微笑)
malefloat性别(此值大于0.5则为男性)
nonefloat是否有胡须(此值大于0.5则为无胡须)
yellowfloat是否为黄种人(此值大于0.5则为黄种人)
closefloat是否张嘴(此值大于0.5则为闭嘴,小于等于0.5为张嘴)
angryfloat生气(此值大于0.5则为生气)
  • 返回结果示例
{
    "request_id": "TIDc00981695c7a4e149020bddf26b35010",
    "image_id": "4272d83ea25e4d24963a8e3e71d5abec",
    "age": 36.933353424072266,
    "attractive": 68.05946350097656,
    "smile": 0.9855127334594727,
    "male": 1,
    "none": 4.4915745961409955e-18,
    "yellow": 0.000005684546977136051,
    "close": 0.999927282333374,
    "angry": 0.014483155682682991,
    "status": "OK"
}

4.错误码

以下请求参数列表仅列出了接口请求参数,其它参数见公共请求参数页面。

状态码status 字段说明
400ENCODING_ERROR参数非UTF-8编码
400DOWNLOAD_TIMEOUT网络地址图片获取超时
400IMAGE_ID_NOT_EXIST图片不存在
400CORRUPT_IMAGE文件不是图片文件或已经损坏
400DOWNLOAD_ERROR网络地址图片获取失败
400DOWNLOAD_TIMEOUT图片下载超时
400IMAGE_FILE_SIZE_TOO_BIG图片体积过大
400INVALID_IMAGE_FORMAT_OR_SIZE图片大小或格式不符合要求
400INVALID_ARGUMENT请求参数错误,具体原因见 reason 字段内容
401UNAUTHORIZED账号或密钥错误
401KEY_EXPIRED账号过期,具体情况见 reason 字段内容
403RATE_LIMIT_EXCEEDED调用频率超出限额
403NO_PERMISSION无调用权限
403OUT_OF_QUOTA调用次数超出限额
404NOT_FOUND请求路径错误
500INTERNAL_ERROR服务器内部错误
{
  "status": "INVALID_ARGUMENT",
  "reason": "must specify 'image_id', 'file' or 'url' argument",
  "request_id": "TID8bf47ab6eda64476973cc5f5b6ebf57e"
}

5.输入示例

  • cURL 样例
curl -X POST "https://cloudapi.linkface.cn/face/face_attributes_detect?api_id=ID&api_secret=SECRET" \
  -F file=@/PATH/TO/IMAGE
  • HTTPie 样例
http -f POST "https://cloudapi.linkface.cn/face/face_attributes_detect?api_id=ID&api_secret=SECRET" \
  file@/PATH/TO/IMAGE
  • C++ 样例
#include <iostream>
#include <cstring>
#include <exception>
#include <curl/curl.h>
#include <json/json.h>

using namespace std;
size_t callback(char *ptr, size_t size, size_t nmemb, string &stream){

  size_t sizes = size*nmemb;
  string temp(ptr,sizes);
  stream += temp;
  return sizes;
}
int main( int argv, char * argc[] ){

  CURL *curl;
  CURLM *multi_handle;
  CURLcode res;
  long code;
  string stream;
  struct curl_httppost *formpost = NULL;
  struct curl_httppost *lastptr = NULL;
  struct curl_slist *headerlist = NULL;

  try{

    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();

    if( curl ){

      curl_formadd(&formpost,&lastptr,CURLFORM_COPYNAME,"api_id",
                  CURLFORM_COPYCONTENTS, "ID",CURLFORM_END);
      curl_formadd(&formpost,&lastptr,CURLFORM_COPYNAME,"api_secret",
                  CURLFORM_COPYCONTENTS, "SECRET",CURLFORM_END);
      curl_formadd(&formpost,&lastptr,CURLFORM_COPYNAME,"file",
                  CURLFORM_FILE, argc[1], CURLFORM_END);

      curl_easy_setopt(curl, CURLOPT_URL, "https://cloudapi.linkface.cn/face/face_attributes_detect");
      curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
      curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
      curl_easy_setopt(curl, CURLOPT_WRITEDATA, &stream);

      #ifdef SKIP_PEER_VERIFICATION
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
      #endif
      #ifdef SKIP_HOSTNAME_VERIFICATION
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
      #endif

      res = curl_easy_perform(curl);

      if( res != CURLE_OK ){
        cout<<"curl_easy_perform() failed:"<<curl_easy_strerror(res)<<endl;
        return -1;
      }
      curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
      Json::Value res_data;
      Json::Reader *reader = new Json::Reader(Json::Features::strictMode());
      if(!reader->parse(stream, res_data)){
        cout<<"parse error";
        return -1;
      }
      cout<<"HTTP Status Code:"<<code<<endl;
      cout<<res_data<<endl;
      curl_easy_cleanup(curl);
  }
    curl_global_cleanup();

  }catch(exception &ex){
    cout<<"curl exception:"<<ex.what()<<endl;
  }
}
  • Java 样例
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class httpClientPost {

      public static final String api_id = "ID"; 
      public static final String api_secret = "SECRET"; 
      public static final String filepath = "C:/Users/card.jpg";//图片路径
      public static final String POST_URL = "https://cloudapi.linkface.cn/face/face_attributes_detect";

      public static void HttpClientPost() throws ClientProtocolException, IOException {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost post = new HttpPost(POST_URL);
            FileBody fileBody = new FileBody(new File(filepath));
            StringBody id = new StringBody(api_id);
            StringBody secret = new StringBody(api_secret);
            MultipartEntity entity = new MultipartEntity();
            entity.addPart("file", fileBody);
            entity.addPart("api_id", id);
            entity.addPart("api_secret", secret);
            post.setEntity(entity);

            HttpResponse response = httpclient.execute(post);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity entitys = response.getEntity();
                BufferedReader reader = new BufferedReader(
                    new InputStreamReader(entitys.getContent()));
                String line = reader.readLine();
                System.out.println(line);
            }else{
                HttpEntity r_entity = response.getEntity();
                String responseString = EntityUtils.toString(r_entity);
                System.out.println("错误码是:"+response.getStatusLine().getStatusCode()+"  "+response.getStatusLine().getReasonPhrase());
                System.out.println("出错原因是:"+responseString);
                //你需要根据出错的原因判断错误信息,并修改
            }

            httpclient.getConnectionManager().shutdown();
      }


    public static void main(String[] args) throws ClientProtocolException, IOException {
        HttpClientPost();
    }
}
  • Ruby 样例
require 'net/http/post/multipart'
require 'json'

begin
  uri = URI.parse('https://cloudapi.linkface.cn/face/face_attributes_detect')
  File.open("px.jpg") do |jpg|
    req = Net::HTTP::Post::Multipart.new uri.path,
          "file" => UploadIO.new(jpg, "image/jpeg", "idcard.jpg"),
          "api_id" => "ID",
          "api_secret" => "SECRET"
    res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
      http.request(req)
    end
    result = JSON.parse(res.body)
    puts result
  end
rescue Exception => e
  puts e.message
end
  • Python 样例

我们提供的样例支持 Python 2.7 & 3.4–3.7 和 PyPy,其他版本暂不提供,需要您查阅相关资料。

import requests

try:
    host = 'https://cloudapi.linkface.cn'
    url = host + '/face/face_attributes_detect'
    files = {'file': open('/image/file/path', 'rb')}
    data = {'api_id': 'ID','api_secret': 'SECRET'}

    response = requests.post(url, files = files, data = data)
    result = response.json()
    print result
except Exception as e:
    print("type error: " + str(e))
  • PHP 样例

我们提供的样例是基于 PHP 5.5 与 PHP 5.6 两个版本,其他版本暂不提供,需要您查阅相关资料。

以下样例是基于 PHP 5.5 版本的。

<?php
   $testurl = 'https://cloudapi.linkface.cn/face/face_attributes_detect';  
   $filePath = 'C:/Users/idcard.jpg';  //图片路径
   $fileContent = '@' . realpath($filePath);
   $post_data = array ('api_id' => 'ID','api_secret' => 'SECRET',
             'file'=>$fileContent);
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $testurl);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//您可以根据需要,决定是否打开SSL验证
   curl_setopt($ch, CURLOPT_POST,1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
   $output = curl_exec($ch);
   var_dump($output);
   curl_close($ch);
?>

以下样例是基于 PHP 5.6 版本的。

<?php
   $testurl = 'https://cloudapi.linkface.cn/face/face_attributes_detect';  
   $filePath = 'C:/Users/idcard.jpg';  //图片路径
   $fileContent = new \CURLFile($filePath);
   $post_data = array ('api_id' => 'ID','api_secret' => 'SECRET',
             'file' => $fileContent);
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $testurl);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//您可以根据需要,决定是否打开SSL验证
   curl_setopt($ch, CURLOPT_POST,1);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
   $output = curl_exec($ch);
   var_dump($output);
   curl_close($ch);
?>