{ "log_id": 149319909347709, "result": [{ "res_code": 0, "res_msg": [ 2001, 2002, 2003 ], "data": { "ocr": { "log_id": 2471272194, "words_result_num": 2, "words_result": [{ "words": " TSINGTAO" }, { "words": "青島睥酒" } ] }, "face": { "result_num": 1, "result": [{ "location": { "left": 90, "top": 92, "width": 111, "height": 99 }, "face_probability": 1, "rotation_angle": 6, "yaw": 11.61234664917, "pitch": -0.30852827429771, "roll": 8.8044967651367, "landmark": [{ "x": 105, "y": 110 }], "landmark72": [{ "x": 88, "y": 109 }], "gender": "male", "gender_probability": 0.99358034133911, "glasses": 0, "glasses_probability": 0.99991309642792, "race": "yellow", "race_probability": 0.99960690736771, "qualities": { "occlusion": { "left_eye": 0.000085282314103097, "right_eye": 0.00001094374601962, "nose": 3.2677664307812e-7, "mouth": 2.6582130940866e-10, "left_cheek": 8.752236624332e-8, "right_cheek": 1.0212766454742e-7, "chin": 4.2632994357028e-10 }, "blur": 4.5613666312237e-41, "illumination": 0, "completeness": 0, "type": { "human": 0.98398965597153, "cartoon": 0.016010366380215 } } }], "log_id": 2418894422 }, "antiporn": [{ "class_name": "色情", "probability": 0.014619 }, { "class_name": "正常", "probability": 0.171783 }, { "class_name": "性感", "probability": 0.813598 } ] } }] }
#!/bin/bash curl -i -k 'https://aip.baidubce.com/rest/2.0/solution/v1/face_audit?access_token=【调用鉴权接口获取的token】' --data 'configId=1&images=%2f9j%2f4AAQSkZJRgABAQEASABIAAD%2f【图片Base64编码需UrlEncode】' -H 'Content-Type:application/x-www-form-urlencoded'
<?php /** * 发起http post请求(REST API), 并获取REST请求的结果 * @param string $url * @param string $param * @return - http response body if succeeds, else false. */ function request_post($url = '', $param = '') { if (empty($url) || empty($param)) { return false; } $postUrl = $url; $curlPost = $param; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $postUrl); // 要求结果为字符串 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // post方式 curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost); $data = curl_exec($curl); var_dump(curl_error($curl)); curl_close($curl); return $data; } $token = '#####调用鉴权接口获取的token#####'; $url = 'https://aip.baidubce.com/rest/2.0/solution/v1/face_audit?access_token=' . $token; $img = file_get_contents('#####本地文件路径######'); $img = base64_encode($img); $bodys = array( "configId" => "1", "images" => $img ); $res = request_post($url, $bodys); var_dump($res);
/* * Copyright (C) 2017 Baidu, Inc. All Rights Reserved. */ package com.baidu.ai.aip.solution; import com.baidu.ai.aip.utils.Base64Util; import com.baidu.ai.aip.utils.FileUtil; import com.baidu.ai.aip.utils.HttpUtil; import java.net.URLEncoder; /** * 用户头像审核API. */ public class FaceAudit { /** * 重要提示代码中所需工具类 * FileUtil,Base64Util,HttpUtil,GsonUtils请从 * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72 * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2 * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3 * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3 * 下载 */ public static void main(String[] args) { // 用户头像审核 url String faceAuditUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/face_audit"; // 请求参数 String configId = "1"; String filePath = "#####本地文件路径#####"; try { byte[] imgData = FileUtil.readFileByBytes(filePath); String imgStr = Base64Util.encode(imgData); String params = "configId=" + configId + "&" + URLEncoder.encode("images", "UTF-8") + "=" + URLEncoder .encode(imgStr, "UTF-8"); String accessToken = "#####调用鉴权接口获取的token#####"; String result = HttpUtil.post(faceAuditUrl, accessToken, params); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
#!/usr/bin/env python # encoding:utf-8 # import base64 import urllib import urllib2 # ''' # 用户头像检测接口 # ''' faceAuditUrl = "https://aip.baidubce.com/rest/2.0/solution/v1/face_audit" # 参数images:图像base64编码,多张图片半角逗号分隔,configId:审核配置组ID params = {"configId": "1", "images": "【图片base64编码】"} params = urllib.urlencode(params) access_token = access_token.AuthService() faceAuditUrl = faceAuditUrl + "?access_token=" + access_token request = urllib2.Request(url=faceAuditUrl, data=params) request.add_header('Content-Type', 'application/x-www-form-urlencoded') response = urllib2.urlopen(request) content = response.read() if content: print content
#include <iostream> #include <curl/curl.h> #include "face_audit.h" // libcurl库下载链接:https://curl.haxx.se/download.html // 图像审核接口url const static std::string face_audit_url = "https://aip.baidubce.com/rest/2.0/solution/v1/face_audit"; static std::string face_audit_result; /** *curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在全局的静态变量当中 * @param 参数定义见libcurl文档 * @return 返回值定义见libcurl文档 **/ static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) { // 获取到的body存放在ptr中,先将其转换为string格式 face_audit_result = std::string((char *) ptr, size * nmemb); return size * nmemb; } /** * 调用图像审核接口,返回json格式的结果,具体格式解析见百度大脑文档 * @param json_result 以string格式返回的json格式的结果 * @param image_base64 以string格式传递的base64编码的图像数据(注:base64数据不包含格式信息(即不包含data:image/jpeg;base64)) * @param access_token 以string格式传入的access token数据,access token获取方式见access_token获取相关文档及代码 * @return 调用成功返回0,发生错误返回其他错误码 */ int face_audit(std::string &json_result, const std::string &config_id, const std::string &image_base64, const std::string &access_token){ std::string url = face_audit_url + "?access_token=" + access_token; CURL *curl = NULL; CURLcode result_code; int is_success; curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url.data()); curl_easy_setopt(curl, CURLOPT_POST, 1); curl_httppost *post = NULL; curl_httppost *last = NULL; curl_formadd(&post, &last, CURLFORM_COPYNAME, "configId", CURLFORM_COPYCONTENTS, config_id.data(), CURLFORM_END); curl_formadd(&post, &last, CURLFORM_COPYNAME, "images", CURLFORM_COPYCONTENTS, image_base64.data(), CURLFORM_END); curl_easy_setopt(curl, CURLOPT_HTTPPOST, post); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback); result_code = curl_easy_perform(curl); if (result_code != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s ", curl_easy_strerror(result_code)); is_success = 1; return is_success; } json_result = face_audit_result; curl_easy_cleanup(curl); is_success = 0; } else { fprintf(stderr, "curl_easy_init() failed."); is_success = 1; } return is_success; }
using System; using System.IO; using System.Net; using System.Text; using System.Web; namespace com.baidu.ai { public static class FaceAudit { // 图像审核 public static string audit() { string token = "#####调用鉴权接口获取的token#####"; string strbaser64 = FileUtils.getFileBase64("/Users/baidu/Documents/TestData/用户头像审核/一菲测试/123.JPG"); // 图片的base64编码 string host = "https://aip.baidubce.com/rest/2.0/solution/v1/face_audit?access_token=" + token; Encoding encoding = Encoding.Default; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host); request.Method = "post"; request.ContentType = "application/x-www-form-urlencoded"; request.KeepAlive = true; String str = "imgUrls=" + HttpUtility.UrlEncode("http://n.sinaimg.cn/news/transform/20170522/3mH2-fyfkzhs8600955.jpg"); // 调用方有自定义configId的情况 // String str ="configId=【用户自定义configId】" + "imgUrls=" + HttpUtility.UrlEncode("http://n.sinaimg.cn/news/transform/20170522/3mH2-fyfkzhs8600955.jpg"); byte[] buffer = encoding.GetBytes(str); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default); string result = reader.ReadToEnd(); Console.WriteLine("图像审核:"); Console.WriteLine(result); return result; } } }