百度AI人脸识别接口应用实例

百度AI人脸识别接口应用实例

pom 文件

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.dunk</groupId>
	<artifactId>baiai-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	
	<dependencies>
		<!-- https://mvnrepository.com/artifact/com.baidu.aip/java-sdk -->
		<dependency>
			<groupId>com.baidu.aip</groupId>
			<artifactId>java-sdk</artifactId>
			<version>4.11.3</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.12</version>
		</dependency>
	</dependencies>
</project>

实例代码

package com.dunk.common.utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import org.apache.commons.codec.binary.Base64;
import org.json.JSONArray;
import org.json.JSONObject;

import com.baidu.aip.face.AipFace;

public class BaiduAiUtil {

	public static final String APP_ID = "your app id";
	public static final String API_KEY = "your api key";
	public static final String SECRET_KEY = "your secret key";
	public static AipFace client;
	public static String groupId = "your group id";

	public static void main(String[] args) {
		

		client = new AipFace(APP_ID, API_KEY, SECRET_KEY);
		client.setConnectionTimeoutInMillis(2000);
		client.setSocketTimeoutInMillis(60000);

		// 传入可选参数调用接口,根据需求自行设置
		HashMap<String, String> options = new HashMap<String, String>();
		options.put("face_type", "LIVE"); 
		options.put("liveness_control", "LOW");
		String image = getBase64(options);
		String imageType = "BASE64";
		String userId = "your user id";
		//faceRegister(image, imageType, userId, options);
		//faceDetect(image, imageType, options);
		//faceCheck(image, imageType, options);
		faceSearch(image, imageType, options);
	}
	/**
	 * face update 
	 * @param image
	 * @param imageType
	 * @param groupId
	 * @param userId
	 * @param options
	 */
	public static void faceUpdate(String image, String imageType, String userId, HashMap<String, String> options) {
		JSONObject res = client.updateUser(image, imageType, groupId, userId, options);
		System.out.println(res.toString(2));
	}
	
	/**
	 * face register
	 * @param image
	 * @param imageType
	 * @param groupId
	 * @param userId
	 * @param options
	 */
	public static void faceRegister(String image, String imageType, String userId, HashMap<String, String> options) {
		JSONObject res = client.addUser(image, imageType, groupId, userId, options);
		System.out.println(res.toString(2));
	}

	/**
	 * face detect
	 * @param image
	 * @param imageType
	 * @param options
	 */
	public static void faceDetect(String image, String imageType, HashMap<String, String> options) {
		JSONObject res = client.detect(image, imageType, options);
		System.out.println(res.toString(2));
	}
	
	/**
	 * face check
	 * @param image
	 * @param imageType
	 * @param options
	 */
	public static void faceCheck(String image, String imageType, HashMap<String, String> options) {
		JSONObject res = client.detect(image, imageType, options);
		System.out.println(res.toString(2));
	}
	
	/**
	 * face search
	 * @param image
	 * @param imageType
	 * @param groupId
	 * @param options
	 */
	public static void faceSearch(String image, String imageType, HashMap<String, String> options) {
		JSONObject res = client.search(image, imageType, groupId, options);
		System.out.println(res.toString(2));
	}
	
	/**
	 * get face existed
	 * @param userId
	 * @param options
	 */
	public void faceGet(String userId, HashMap<String, String> options) {
		JSONObject res = client.getUser(userId, groupId, options);
		System.out.println(res.toString(2));
	}

	public static String getBase64(HashMap<String, String> options) {
		InputStream in = null;
		byte[] data = null;
		try {
			in = new FileInputStream("your image path");
			data = new byte[in.available()];
			in.read(data);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new String(Base64.encodeBase64(data));
	}
}

参考文档:百度人脸识别SDK文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值