This topic describes how to use the Java SDK to delete faces of a person.
Usage notes
When you delete faces, you must specify the IDs of the faces to be deleted and the corresponding person ID. For more information about the parameters, see API operation for deleting faces.
You need to use the endpoints of the Content Moderation API to call this SDK. For more information about the endpoints, see Endpoints.
Usage notes
When you delete faces, you must specify the IDs of the faces to be deleted and the corresponding person ID. For more information about the parameters, see API operation for deleting faces.
Prerequisites
Java dependencies are installed. For more information, see Installation.
NoteYou must use the Java version described in the Installation topic to install the dependencies. Otherwise, subsequent operation calls fail.
The Extension.Uploader utility class is downloaded and imported into your project if you submit a local image or a binary image stream for image moderation.
Sample code for deleting faces
import java.util.Arrays;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.green.model.v20180509.DeleteFacesRequest;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.HttpResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
public class FaceDeleteFaceRequestSample {
public static void main(String[] args) throws Exception {
/**
* The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. To avoid security risks, we recommend that you use a RAM user to call API operations or perform routine O&M.
* Common ways to obtain environment variables:
* Method 1:
* Obtain the AccessKey ID of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of your RAM user: System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
* Method 2:
* Obtain the AccessKey ID of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_ID");
* Obtain the AccessKey secret of your RAM user: System.getProperty("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
*/
DefaultProfile profile = DefaultProfile.getProfile(
"cn-shanghai",
"We recommend that you obtain the AccessKey ID of your RAM user from environment variables",
"We recommend that you obtain the AccessKey secret of your RAM user from environment variables");
DefaultProfile.addEndpoint("cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
// Note: We recommend that you reuse the instantiated client as much as possible. This improves moderation performance and avoids repeated client connections.
IAcsClient client = new DefaultAcsClient(profile);
DeleteFacesRequest deleteFacesRequest = new DeleteFacesRequest();
deleteFacesRequest.setAcceptFormat(FormatType.JSON); // Specify the API response format.
deleteFacesRequest.setMethod(com.aliyuncs.http.MethodType.POST); // Specify the request method.
deleteFacesRequest.setEncoding("utf-8");
JSONObject data = new JSONObject();
/**
* personId: the ID of the custom person. This parameter is required.
* faceIds: the IDs of the faces that are added.
*/
data.put("personId", "personId_test_3");
data.put("faceIds", Arrays.asList("31820666292926465"));
deleteFacesRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
/**
* Specify the connection timeout period and read timeout period.
*/
deleteFacesRequest.setConnectTimeout(3000);
deleteFacesRequest.setReadTimeout(6000);
try {
HttpResponse httpResponse = client.doAction(deleteFacesRequest);
if (httpResponse.isSuccess()) {
JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
System.out.println(JSON.toJSONString(scrResponse, true));
if (200 == scrResponse.getInteger("code")) {
JSONObject resultObject = scrResponse.getJSONObject("data");
if (200 == resultObject.getInteger("code")) {
System.out.println(resultObject.getString("personId"));
} else {
System.out.println("task process fail:" + resultObject.getInteger("code"));
}
} else {
System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
}
} else {
System.out.println("response not success. status:" + httpResponse.getStatus());
}
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}