public class IaiClientTest {
private static final String iaiSecretId = "xxx";
private static final String iaiSecretKey = "ccc";
private static final String REGIONID = "ap-beijing";
private static final String groupPre = "cccy_";
private static final String groupSuf = "_0001";
private static final String noticeGroupPre = "cc-cc";
/**
* 初始化CosClient相关配置, appid、accessKey、secretKey、region.
* @return COSClient.
*/
public static IaiClient getIaiClient() {
// 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey
Credential cred = new Credential(iaiSecretId, iaiSecretKey);
ClientProfile profile = new ClientProfile("TC3-HMAC-SHA256");
// 实例化要请求产品的client对象
IaiClient client = new IaiClient(cred, REGIONID, profile);
log.info("connect iaiclient success.");
return client;
}
/**
*拼接groupid.
* @param noticeFlag notice_flag.
* @param bkey bkey.
* @return groupId.
*/
private static String getGroupId(boolean noticeFlag, int bkey) {
if (noticeFlag) {
return String.format("%s%s%s", noticeGroupPre, bkey, groupSuf);
} else {
return String.format("%s%s%s", groupPre, bkey, groupSuf);
}
}
/**
* 创建groupID.
* @return boolean.
*/
public static Boolean createGroup() {
var client = getIaiClient();
var groupId = getGroupId(true, 40840);
try {
CreateGroupRequest request = new CreateGroupRequest();
request.setGroupId(groupId);
request.setGroupName(groupId);
request.setFaceModelVersion("3.0");
client.CreateGroup(request);
log.debug("Create group. GroupId: %s", groupId);
return false;
} catch (TencentCloudSDKException e) {
return true;
} catch (Exception e) {
var msg =String.format("Create group id: %s failed. Ex: %s", groupId);
throw new AppResourceConflictException(msg);
}
}
/**
* 创建person.
* @param groupId groupId.
* @param personId personId.
* @param personName personName.
* @param b64file b64file.
*/
public static CreatePersonResponse createPerson(
String groupId, String personId, String personName, String b64file) {
var client = getIaiClient();
try {
var req = new CreatePersonRequest();
req.setGroupId(groupId);
req.setPersonId(personId);
req.setPersonName(personName);
if (b64file != null) {
req.setImage(b64file);
}
var res = client.CreatePerson(req);
log.debug("CreatePerson result. GroupId: %s. PersonId: %s. res: %s",
groupId, personId, res);
return res;
//{"FaceId": "3139479249070616485", "RequestId": "b8437bb4-d81a-412b-9f4c-8136fa3a578b"}
} catch (TencentCloudSDKException e) {
var msg = String.format("Create person failed. GroupId: %s. PersonId: %s. Ex: %s",
groupId, personId, e);
throw new AppResourceConflictException(msg);
}
}
/**
* iai请求删除人脸数据 根据groupID.
* @param personid b64file.
* @return DetectFaceResponse.
*/
public static DeletePersonFromGroupResponse deletePersonFromGroup(
String personid, boolean noticeFlag, int bkey) {
var client = getIaiClient();
try {
String groupId = getGroupId(noticeFlag, bkey);
var isCrea = createGroup();
if (!isCrea) {
return null;
}
DeletePersonFromGroupRequest req = new DeletePersonFromGroupRequest();
// set attribute
req.setPersonId(personid);
req.setGroupId(groupId);
return client.DeletePersonFromGroup(req);
} catch (TencentCloudSDKException e) {
throw new AppResourceConflictException(String.format("IAI connection failed! %s", e));
}
}
/**
* iai请求人脸识别.
* @param b64file b64file.
* @return DetectFaceResponse.
*/
public DetectFaceResponse detectFace(String b64file) {
var client = getIaiClient();
try {
DetectFaceRequest req = new DetectFaceRequest();
// set attribute
req.setImage(b64file);
req.setNeedFaceAttributes(1L);
req.setNeedQualityDetection(1L);
req.setFaceModelVersion("3.0");
return client.DetectFace(req);
} catch (TencentCloudSDKException e) {
throw new AppResourceConflictException(String.format("IAI connection failed! %s", e));
}
}
public static void main(String[] args) {
//createGroup();
//createPerson("wipro-may_50038_0001",
//"person_050038_047602_5df9cb5dec41364e842d2ppp",
//"person_040840_021544_5df9cb5dec41364e842d2ppp",
// "");
//deletePersonFromGroup("notice_040840_021544_5e05a2e3ec4136568471e0fa",
// true, 40840);
}
}
腾讯人脸识别
最新推荐文章于 2024-04-10 20:32:21 发布