Java实现调用百度AI开放云平台(人脸识别API)

1.注册开发人员并查看官网API文档

   网址:http://ai.baidu.com/docs



2.在管理中心创建应用及查看相关参数 AppID APIKey SecretKey都需要保存起来


3.开发测试

 3.1、调用百度云人脸识别API需要两步

       1、首先根据创建的应用,获取token,该token有效期为1个月。token的授权服务地址:https://aip.baidubce.com/oauth/2.0/token;

                    发送请求(推荐使用POST),并在URL中带上以下参数:

                        grant_type: 必须参数,固定为“client_credentials”;

                        client_id: 必须参数,应用的API Key;

                        client_secret: 必须参数,应用的Secret Key;

       2、根据token调用api,服务地址:https://aip.baidubce.com/rest/2.0/face/v1/detect


具体实现代码如下:

package com.zmx.baiduaitest;

import org.apache.http.client.methods.CloseableHttpResponse;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangwenchao on 2017/9/29.
 */
public class FaceAPITest {

    public static void main(String[] args) {
        //  getToKenTest() ;
        faceDetecttest();
    }

    //获取token
    public static void getToKenTest(){

        //使用其测试百度云API---获取token
        //url: http://console.bce.baidu.com/ai

        String APPID ="10201***"; //管理中心获得

        //百度人脸识别应用apikey
        String API_KEY = "WVsGWvDxyn55hW8P*******"; //管理中心获得

        //百度人脸识别应用sercetkey
        String SERCET_KEY = "z0uSr16LGyTNIMRRnP6f8jCL*****"; //管理中心获得

        //百度人脸识别token 有效期一个月
        String TOKEN = null;


        String access_token_url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials"
                +"&client_id="+API_KEY +"&client_secret="+SERCET_KEY;

        CloseableHttpResponse response =  HttpClientUtils.doHttpsGet(access_token_url,null);

        System.out.println(HttpClientUtils.toString(response));


        //得到token = 24.1d786b9cdbdd8ac7cf55d56c7f38372b.2592000.1509244497.282335-10201425



    }

    //使用token调用API
    public static void faceDetecttest(){

        String token = "24.1d786b9cdbdd8ac7cf55d56c7f38372b.2592000.1509244497.282335-10201425";

        String Filepath = "E:/test.jpg";
        String image = Base64ImageUtils.GetImageStrFromPath(Filepath);
        String url = "https://aip.baidubce.com/rest/2.0/face/v1/detect?access_token="+token;

        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/x-www-form-urlencoded");

        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("image", image);
        bodys.put("face_fields", "age,beauty,expression,gender,glasses,race,qualities");

        try {
            CloseableHttpResponse response =  HttpClientUtils.doHttpsPost(url,headers,bodys);
            System.out.println(HttpClientUtils.toString(response));
        } catch (Exception e) {
            e.printStackTrace();
        }



    }

}

上述用到了几个工具类如下:

HttpClientUtils.java:httpclient工具类用于发起get、post请求

package com.zmx.baiduaitest;

import org.apache.http.*;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.*;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * HttpClient4.5.X实现的工具类
 * 可以实现http和ssl的get/post请求
 */
public class HttpClientUtils{
    //创建HttpClientContext上下文
    private static HttpClientContext context = HttpClientContext.create();

    //请求配置
    private static RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(120000)
                    .setSocketTimeout(60000)
                    .setConnectionRequestTimeout(60000)
                    .setCookieSpec(CookieSpecs.STANDARD_STRICT)
                    .setExpectContinueEnabled(true)
                    .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
                    .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build();

    //SSL的连接工厂
    private static SSLConnectionSocketFactory socketFactory = null;

    //信任管理器--用于ssl连接
    private static TrustManager manager = new X509TrustManager() {


        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

        }

        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    //ssl请求
    private static void enableSSL() {
        try {
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[]{manager}, null);
            socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
    }

    /**
     * https get请求
     * @param url
     * @param data
     * @return
     * @throws IOException
     */
    public static CloseableHttpResponse doHttpsGet(String url, String data){
        enableSSL();
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                                                   .register("http", PlainConnectionSocketFactory.INSTANCE)
                                                   .register("https", socketFactory).build();

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

        CloseableHttpClient httpClient = HttpClients.custom()
                                        .setConnectionManager(connectionManager)
                                        .setDefaultRequestConfig(requestConfig).build();

        HttpGet httpGet = new HttpGet(url);

        CloseableHttpResponse response = null;

        try {
            response = httpClient.execute(httpGet, context);
        }catch (Exception e){
            e.printStackTrace();
        }

        return response;
    }

    /**
     * https post请求 参数为名值对
     * @param url
     * @param headers
     * @param bodys
     * @return
     * @throws IOException
     */
    public static CloseableHttpResponse doHttpsPost(String url, Map<String, String> headers, Map<String, String> bodys) {
        enableSSL();
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                                                       .register("http", PlainConnectionSocketFactory.INSTANCE)
                                                       .register("https", socketFactory).build();

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

        CloseableHttpClient httpClient = HttpClients.custom()
                                         .setConnectionManager(connectionManager)
                                         .setDefaultRequestConfig(requestConfig).build();

        HttpPost httpPost = new HttpPost(url);

        for (Map.Entry<String, String> e : headers.entrySet()) {
            httpPost.addHeader(e.getKey(), e.getValue());
        }

        if (bodys != null) {
            List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
            for (String key : bodys.keySet()) {
                nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
            }
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, Consts.UTF_8);
            formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
            httpPost.setEntity(formEntity);
        }

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost, context);
        }catch (Exception e){}
        return response;
    }
    /**
     * https post请求 参数为名值对
     * @param url
     * @param values
     * @return
     * @throws IOException
     */
    public static CloseableHttpResponse doHttpsPost(String url, List<NameValuePair> values) {
        enableSSL();
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                                                       .register("http", PlainConnectionSocketFactory.INSTANCE)
                                                       .register("https", socketFactory).build();

        PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);

        CloseableHttpClient httpClient = HttpClients.custom()
                                         .setConnectionManager(connectionManager)
                                         .setDefaultRequestConfig(requestConfig).build();

        HttpPost httpPost = new HttpPost(url);

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(values, Consts.UTF_8);

        httpPost.setEntity(entity);

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost, context);
        }catch (Exception e){}
        return response;
    }

    /**
     * http get
     * @param url
     * @param data
     * @return
     */
    public static CloseableHttpResponse doGet(String url, String data) {

        CookieStore cookieStore = new BasicCookieStore();

        CloseableHttpClient httpClient = HttpClientBuilder.create()
                            .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
                            .setRedirectStrategy(new DefaultRedirectStrategy())
                            .setDefaultCookieStore(cookieStore)
                            .setDefaultRequestConfig(requestConfig).build();

        HttpGet httpGet = new HttpGet(url);

        CloseableHttpResponse response = null;

        try {
            response = httpClient.execute(httpGet, context);
        }catch (Exception e){}
        return response;
    }

    /**
     * http post
     *
     * @param url
     * @param values
     * @return
     */
    public static CloseableHttpResponse doPost(String url, List<NameValuePair> values) {
        CookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient httpClient = HttpClientBuilder.create()
                            .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())
                            .setRedirectStrategy(new DefaultRedirectStrategy())
                            .setDefaultCookieStore(cookieStore)
                            .setDefaultRequestConfig(requestConfig).build();

        HttpPost httpPost = new HttpPost(url);

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(values, Consts.UTF_8);

        httpPost.setEntity(entity);

        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost, context);
        }catch (Exception e){}
        return response;
    }


    /**
     * 直接把Response内的Entity内容转换成String
     *
     * @param httpResponse
     * @return
     */
    public static String toString(CloseableHttpResponse httpResponse) {
        // 获取响应消息实体
        String result = null;
        try {
            HttpEntity entity = httpResponse.getEntity();
            if (entity != null) {
                result = EntityUtils.toString(entity,"UTF-8");
            }
        }catch (Exception e){}finally {
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }



    public static void main(String[] args){
       //使用其测试百度云API---获取token
       //url: http://console.bce.baidu.com/ai

        String APPID ="10201425"; //管理中心获得

        //百度人脸识别应用apikey
        String API_KEY = "WVsGWvDxyn55hW8P0UR8shbo"; //管理中心获得

        //百度人脸识别应用sercetkey
        String SERCET_KEY = "z0uSr16LGyTNIMRRnP6f8jCLMLXAYkIj"; //管理中心获得

        //百度人脸识别token 有效期一个月
        String TOKEN = null;


        String access_token_url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials"
                                   +"&client_id="+API_KEY +"&client_secret="+SERCET_KEY;

        CloseableHttpResponse response =  HttpClientUtils.doHttpsGet(access_token_url,null);

        System.out.println(HttpClientUtils.toString(response));


        //得到token = 24.1d786b9cdbdd8ac7cf55d56c7f38372b.2592000.1509244497.282335-10201425


    }







}


Base64ImageUtils.java:图片转换工具类,用于图片文件与base64数据格式图片进行互转
package com.zmx.baiduaitest;



import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * Created by zhangwenchao on 2017/9/29.
 * 本地或者网络图片资源转为Base64字符串
 */
public class Base64ImageUtils {
    /**
     * @Title: GetImageStrFromUrl
     * @Description: 将一张网络图片转化成Base64字符串
     * @param imgURL 网络资源位置
     * @return Base64字符串
     */
    public static String GetImageStrFromUrl(String imgURL) {
        byte[] data = null;
        try {
            // 创建URL
            URL url = new URL(imgURL);
            // 创建链接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5 * 1000);
            InputStream inStream = conn.getInputStream();
            data = new byte[inStream.available()];
            inStream.read(data);
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过的字节数组字符串
        return encoder.encode(data);
    }

    /**
     * @Title: GetImageStrFromPath
     * @Description: (将一张本地图片转化成Base64字符串)
     * @param imgPath
     * @return
     */
    public static String GetImageStrFromPath(String imgPath) {
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过的字节数组字符串
        return encoder.encode(data);
    }

    /**
     * @Title: GenerateImage
     * @Description: base64字符串转化成图片
     * @param imgStr
     * @param imgFilePath  图片文件名,如“E:/tmp.jpg”
     * @return
     */
    public static boolean saveImage(String imgStr,String imgFilePath) {
        if (imgStr == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

3.2、人脸识别API使用方法

功能:检测人脸美丑及年龄

官网:http://ai.baidu.com/docs#FACE官网文档

请求参数:


是否必选类型说明
imagestringbase64编码后的图片数据,图片大小不超过2M。
max_face_numuint32最多处理人脸数目,默认值1
face_fieldsstring包括age,beauty,expression,faceshape,gender,glasses,landmark,race,qualities信息,逗号分隔,默认只返回人脸框、概率和旋转角度。
  • URL参数:

参数
access_token通过API Key和Secret Key获取的access_token,参考“Access Token获取


  • Header如下:

参数
Content-Typeapplication/x-www-form-urlencoded


  • Body中数据如下:
参数
image图像base64编码
max_face_num非必填,默认1
face_fields非必填

 

3.3、返回JSON字符串内容解释

age 年龄  (目前官网还是没有说明年龄的误差范围)

beauty 美丑分数 0-100 越大值越美,如花识别年龄38  美丑评分21  表示不太合理

{
  "result": [{
    "expression": 0,
    "face_probability": 1,
    "glasses": 0,
    "location": {
      "height": 155,
      "width": 136,
      "left": 103,
      "top": 49
    },
    "beauty": 21.201513290405,
    "race": "yellow",
    "expression_probablity": 0.77510279417038,
    "rotation_angle": 9,
    "yaw": 2.6519072055817,
    "roll": 10.0813331604,
    "qualities": {
      "completeness": 0,
      "occlusion": {
        "left_eye": 0,
        "chin": 0,
        "mouth": 0,
        "right_cheek": 0,
        "left_cheek": 0,
        "nose": 0,
        "right_eye": 0
      },
      "blur": 0,
      "type": {
        "cartoon": 0.0060238856822252,
        "human": 0.99397611618042
      },
      "illumination": 0
    },
    "gender_probability": 0.93728905916214,
    "age": 37.866649627686,
    "gender": "male",
    "glasses_probability": 0.99999260902405,
    "race_probability": 0.99436098337173,
    "pitch": -17.080112457275
  }],
  "result_num": 1,
  "log_id": 2045651588
}


        对于其他API也是同样的方式进行调用,相信大家都可以举一反三,今天项目用到一些复杂的API可以直接使用第三方提供的API,大体调用方式都类似。






  • 5
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: 人脸识别是通过计算机视觉和人工智能技术识别和分析人脸图像,进而实现人脸检测、人脸比对、人脸验证等功能。而百度提供的人脸识别API可以帮助我们快速、准确地实现人脸相关的功能。 要使用百度API进行人脸识别,首先需要在百度AI平台上注册账号并创建应用。在创建应用的过程中,会获得一个API Key和Secret Key,这两个密钥在我们的代码中起到了身份认证的作用。 使用Python调用百度API的步骤如下: 1. 安装必要的Python模块:在终端中使用pip命令安装需要的模块,如requests、base64等。 2. 导入所需的模块:在代码的开头导入需要使用的Python模块,如requests、base64等。 3. 设置API Key和Secret Key:将获得的API Key和Secret Key分别赋值给两个变量。 4. 读取待识别的人脸图像:使用Python的文件操作函数读取待识别的人脸图像文件,可以使用PIL等图像处理库对图像进行预处理。 5. 将图像转换为base64编码:使用base64等编码工具将人脸图像转换为base64编码的字符串。 6. 构建请求参数:将API Key、Secret Key和base64编码的人脸图像作为参数传递给API,并设置一些可选参数,如人脸识别的功能和阈值等。 7. 发送请求并获取结果:使用Python的请求库发送HTTP POST请求,并接收返回的结果。 8. 解析结果:对返回的结果进行解析,提取出需要的信息,如人脸的位置、特征等。 9. 处理结果:根据解析的结果进行相应的处理,如展示人脸图像、打印人脸信息等。 以上就是使用百度API进行人脸识别的基本步骤。当然,在实际应用中,还可以根据具体需求对结果进行进一步的处理和分析,例如人脸情绪分析、人脸属性检测等。 ### 回答2: 人脸识别是一种通过计算机技术来识别人脸的方法,而百度API提供了一种简单方便的方式来实现人脸识别。下面是使用百度API和Python代码进行人脸识别的示例: 首先,我们需要通过百度云控制台的人脸识别服务创建一个应用,并获取到API Key和Secret Key。 接下来,我们可以使用Python中的`requests`库来进行HTTP请求。首先,我们需要导入相应的库和模块: ```python import requests import base64 ``` 然后,我们需要定义获取API token的函数: ```python def get_access_token(api_key, secret_key): url = 'https://aip.baidubce.com/oauth/2.0/token' data = { 'grant_type': 'client_credentials', 'client_id': api_key, 'client_secret': secret_key } response = requests.post(url, data=data) access_token = response.json()['access_token'] return access_token ``` 接下来,我们可以定义一个函数来进行人脸识别: ```python def face_detection(api_key, secret_key, image_path): access_token = get_access_token(api_key, secret_key) url = 'https://aip.baidubce.com/rest/2.0/face/v3/detect' headers = { 'Content-Type': 'application/json' } with open(image_path, 'rb') as f: image = base64.b64encode(f.read()).decode('utf-8') data = { 'image': image, 'image_type': 'BASE64', 'face_field': 'age,gender,beauty', 'max_face_num': 10 } params = { 'access_token': access_token } response = requests.post(url, headers=headers, params=params, json=data) result = response.json() return result ``` 最后,我们可以调用上述函数来进行人脸识别,传入对应的API Key、Secret Key和人脸图片路径: ```python api_key = 'your_api_key' secret_key = 'your_secret_key' image_path = 'your_image_path' result = face_detection(api_key, secret_key, image_path) print(result) ``` 通过以上代码,我们可以使用百度API和Python来进行人脸识别。当然,以上代码只是简单的示例,具体的人脸识别功能还可以通过API的其他参数进行更多的设置和修改。 ### 回答3: 人脸识别是一种通过计算机技术识别和验证人脸特征的方法。百度提供了一系列人脸识别API,可以通过Python代码使用这些API。 首先,需要在百度AI开放平台上注册账号,并创建一个应用,获取API Key和Secret Key。 接下来,安装百度AI SDK包。在Python中,可以使用pip来安装: ``` pip install baidu-aip ``` 导入baidu-aip包,并初始化AipFace对象: ```python from aip import AipFace # 设置APPID/AK/SK APP_ID = 'your_app_id' API_KEY = 'your_api_key' SECRET_KEY = 'your_secret_key' client = AipFace(APP_ID, API_KEY, SECRET_KEY) ``` 接下来,可以使用AipFace对象调用百度API人脸识别功能。以下是一个简单的人脸检测代码示例: ```python import base64 # 读取图片并进行Base64编码 with open("face.jpg", "rb") as f: image = base64.b64encode(f.read()).decode("utf-8") # 调用人脸检测API result = client.detect(image, 'BASE64') # 解析结果 if 'result' in result: face_num = result['result']['face_num'] face_list = result['result']['face_list'] for face in face_list: face_location = face['location'] left = face_location['left'] top = face_location['top'] width = face_location['width'] height = face_location['height'] print(f"人脸位置:left={left}, top={top}, width={width}, height={height}") else: print("人脸检测失败") ``` 以上是一个简单的人脸检测的例子,可以通过修改参数和调用其他API实现不同的人脸识别功能,如人脸对比、人脸搜索等。 通过百度API和Python代码,我们可以快速实现人脸识别的功能,方便地应用于各种项目和应用场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值