package com.fgkj.services;
|
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileNotFoundException;
|
import java.io.IOException;
|
import java.util.*;
|
|
import com.fgkj.actions.ActionUtil;
|
|
/**
|
* 人脸检测与属性分析
|
*/
|
public class FaceDetect {
|
|
/**
|
* 重要提示代码中所需工具类 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 String faceDetect(String json) {
|
// 请求url
|
String url = "https://aip.baidubce.com/rest/2.0/face/v3/detect";
|
try {
|
// Map<String, Object> map = new HashMap<>();
|
// map.put("image", "027d8308a2ec665acb1bdf63e513bcb9");
|
// map.put("face_field", "faceshape,facetype");
|
// map.put("image_type", "FACE_TOKEN");
|
// map.put("accessToken", "************");
|
Map<String, Object> map = ActionUtil.getGson().fromJson(json,
|
HashMap.class);
|
|
String param = GsonUtils.toJson(map);
|
|
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间,
|
// 客户端可自行缓存,过期后重新获取。
|
// String accessToken = "[调用鉴权接口获取的token]";
|
String msg = HttpUtil.post(url, map.get("accessToken").toString(),
|
"application/json", param);
|
|
return msg;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 人脸搜索 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 String faceSearch(String json) {
|
// 请求url
|
String url = "https://aip.baidubce.com/rest/2.0/face/v3/search";
|
try {
|
/*
|
* Map<String, Object> map = new HashMap<>(); map.put("image",
|
* "027d8308a2ec665acb1bdf63e513bcb9"); map.put("liveness_control",
|
* "NORMAL"); map.put("group_id_list", "group_repeat,group_233");
|
* map.put("image_type", "FACE_TOKEN"); map.put("quality_control",
|
* "LOW");
|
*/
|
|
Map<String, Object> map = ActionUtil.getGson().fromJson(json,
|
HashMap.class);
|
|
String param = GsonUtils.toJson(map);
|
|
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间,
|
// 客户端可自行缓存,过期后重新获取。
|
// String accessToken = "[调用鉴权接口获取的token]";
|
|
String result = HttpUtil.post(url, map.get("accessToken")
|
.toString(), "application/json", param);
|
//System.out.println(result);
|
return result;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 添加人脸
|
*
|
* @param json
|
* @return
|
*/
|
public static String faceAdd(String json) {
|
// 请求url
|
String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add";
|
try {
|
// Map<String, Object> map = new HashMap<>();
|
// map.put("image", "027d8308a2ec665acb1bdf63e513bcb9");
|
// map.put("group_id", "group_repeat");
|
// map.put("user_id", "user1");
|
// map.put("user_info", "abc");
|
// map.put("liveness_control", "NORMAL");
|
// map.put("image_type", "FACE_TOKEN");
|
// map.put("quality_control", "LOW");
|
// map.put("accessToken", "*********************");
|
|
String param = GsonUtils.toJson(json);
|
Map map = ActionUtil.getGson().fromJson(json, HashMap.class);
|
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间,
|
// 客户端可自行缓存,过期后重新获取。
|
// String accessToken = "[调用鉴权接口获取的token]";
|
|
String result = HttpUtil.post(url, map.get("accessToken")
|
.toString(), "application/json", param);
|
//System.out.println(result);
|
return result;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 删除人脸
|
*
|
* @param json
|
* @return
|
*/
|
public static String faceDelete(String json) {
|
// 请求url
|
String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/delete";
|
try {
|
// Map<String, Object> map = new HashMap<>();
|
// map.put("user_id", "user1");
|
// map.put("group_id", "group1");
|
// map.put("face_token", "*************");
|
// map.put("accessToken", "*************");
|
|
String param = json;
|
Map map = ActionUtil.getGson().fromJson(json, HashMap.class);
|
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间,
|
// 客户端可自行缓存,过期后重新获取。
|
// String accessToken = "[调用鉴权接口获取的token]";
|
|
String result = HttpUtil.post(url, map.get("accessToken")
|
.toString(), "application/json", param);
|
//System.out.println(result);
|
return result;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 更新人脸
|
*
|
* @param json
|
* @return
|
*/
|
public static String faceUpdate(String json) {
|
// 请求url
|
String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/update";
|
try {
|
// Map<String, Object> map = new HashMap<>();
|
// map.put("image", "027d8308a2ec665acb1bdf63e513bcb9");
|
// map.put("group_id", "group_repeat");
|
// map.put("user_id", "user1");
|
// map.put("user_info", "cba");
|
// map.put("liveness_control", "NORMAL");
|
// map.put("image_type", "FACE_TOKEN");
|
// map.put("quality_control", "LOW");
|
// map.put("accessToken", "*************");
|
|
String param = json;
|
Map map = ActionUtil.getGson().fromJson(json, HashMap.class);
|
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间,
|
// 客户端可自行缓存,过期后重新获取。
|
// String accessToken = "[调用鉴权接口获取的token]";
|
|
String result = HttpUtil.post(url, map.get("accessToken")
|
.toString(), "application/json", param);
|
//System.out.println(result);
|
return result;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 获取用户人脸列表
|
*
|
* @param json
|
* @return
|
*/
|
public static String faceGetList(String json) {
|
// 请求url
|
String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/face/getlist";
|
try {
|
// Map<String, Object> map = new HashMap<>();
|
// map.put("user_id", "user1");
|
// map.put("group_id", "group1");
|
// map.put("accessToken", "*************");
|
|
String param = json;
|
Map map = ActionUtil.getGson().fromJson(json, HashMap.class);
|
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间,
|
// 客户端可自行缓存,过期后重新获取。
|
// String accessToken = "[调用鉴权接口获取的token]";
|
|
String result = HttpUtil.post(url, map.get("accessToken")
|
.toString(), "application/json", param);
|
//System.out.println(result);
|
return result;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 获取用户列表
|
*
|
* @param json
|
* @return
|
*/
|
public static String groupGetusers(String json) {
|
// 请求url
|
String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/getusers";
|
try {
|
// Map<String, Object> map = new HashMap<>();
|
// map.put("group_id", "group1");
|
// map.put("accessToken", "**********");
|
|
String param = json;
|
Map map = ActionUtil.getGson().fromJson(json, HashMap.class);
|
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间,
|
// 客户端可自行缓存,过期后重新获取。
|
// String accessToken = "[调用鉴权接口获取的token]";
|
|
String result = HttpUtil.post(url, map.get("accessToken")
|
.toString(), "application/json", param);
|
//System.out.println(result);
|
return result;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 人脸复制
|
* @param json
|
* @return
|
*/
|
public static String faceCopy(String json) {
|
// 请求url
|
String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/copy";
|
try {
|
// Map<String, Object> map = new HashMap<>();
|
// map.put("user_id", "group1");
|
// map.put("src_group_id", "**********"); 源组号
|
// map.put("dst_group_id", "**********"); 目标组号
|
// map.put("accessToken", "**********"); 目标组号
|
|
String param = json;
|
Map map = ActionUtil.getGson().fromJson(json, HashMap.class);
|
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间,
|
// 客户端可自行缓存,过期后重新获取。
|
// String accessToken = "[调用鉴权接口获取的token]";
|
|
String result = HttpUtil.post(url, map.get("accessToken").toString(), "application/json", param);
|
//System.out.println(result);
|
return result;
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
|
public static void main(String[] args) {
|
|
// map.put("image", "027d8308a2ec665acb1bdf63e513bcb9");
|
// map.put("face_field", "faceshape,facetype");
|
// map.put("image_type", "FACE_TOKEN");
|
// map.put("accessToken", "************");
|
// String json =
|
// "{'accessToken':'24.0363955052d87d6558e635ce1990a0ae.2592000.1590564753.282335-19585027',"
|
// + "'image_type':'BASE64',"
|
// +
|
// "'face_field':'age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,race,quality,eye_status,emotion,face_type,mask,spoofing',"
|
// + "}";
|
String imgstr = "";
|
File file = new File("src/img.txt");
|
String courseFile = file.getAbsolutePath();
|
//System.out.println(courseFile);
|
try {
|
FileInputStream fis = new FileInputStream(file);
|
byte[] b = new byte[1024];
|
int len = 0;
|
try {
|
while ((len = fis.read(b)) != -1) {
|
imgstr += new String(b);
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
} catch (FileNotFoundException e) {
|
e.printStackTrace();
|
}
|
Map map = new HashMap<String, String>();
|
map.put("accessToken",
|
"24.0363955052d87d6558e635ce1990a0ae.2592000.1590564753.282335-19585027");
|
map.put("image_type", "BASE64");
|
map.put("face_field",
|
"age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,race,quality,eye_status,emotion,face_type,mask,spoofing");
|
map.put("image", imgstr);
|
|
// System.out.println(json);
|
System.out.println(FaceDetect.faceDetect(ActionUtil.tojson(map)));
|
}
|
}
|