SDK API Example
SDK API Example for V1
- Construct a service client.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
String ak = "ak"; String sk = "sk"; String projectId = "projectId"; //Proxy information. If you need to use a proxy to access the service, add the proxy information here. ProxyHostInfo proxyHostInfo = new ProxyHostInfo("proxy", 8080, "UserName", "Password" ); //Service info String url = "https://face.cn-north-4.myhuaweicloud.com"; String region = "cn-north-4"; AuthInfo authInfo = new AuthInfo(url, region, ak, sk); FrsClient frsClient = new FrsClient(authInfo, projectId); //FrsClient frsClient = new FrsClient(authInfo, projectId, proxyHostInfo);//If you need to use a proxy to access the service, add the proxy information to the third parameter.
- Create a face set.
1 2 3
CreateExternalFields createExternalFields = new CreateExternalFields(); createExternalFields.addField("testInteger", FieldType.INTEGER); CreateFaceSetResult createFaceSetResult = frsClient.getFaceSetService().createFaceSet(faceSetName, faceSetCapacity, createExternalFields);
- Add faces.
1 2 3 4 5 6 7
String imageId = "image1"; File image = new File("data/face.jpg"); byte[] fileData = FileUtils.readFileToByteArray(image); String imageBase64 = Base64.encodeBase64String(fileData); AddExternalFields addExternalFields = new AddExternalFields(); addExternalFields.addField("testInteger", 123); AddFaceResult addFaceResult = frsClient.getFaceService().addFaceByBase64(faceSetName, imageId, imageBase64, addExternalFields);
- Invoke the Face Detection service.
1 2 3
String obsUrl = "/bucket/face.jpg"; String attr = "0,1,2,3,4,5"; DetectFaceResult detectFaceResult = frsClient.getDetectService().detectFaceByObsUrl(obsUrl, attr);
SDK API Example for V2
- Construct a service client.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
String ak = "ak"; String sk = "sk"; String projectId = "projectId"; //Proxy information. If you need to use a proxy to access the service, add the proxy information here. ProxyHostInfo proxyHostInfo = new ProxyHostInfo("proxy", 8080, "UserName", "Password" ); //Service info String url = "https://face.cn-north-4.myhuaweicloud.com"; String region = "cn-north-4"; AuthInfo authInfo = new AuthInfo(url, region, ak, sk); FrsClient frsClient = new FrsClient(authInfo, projectId); //FrsClient frsClient = new FrsClient(authInfo, projectId, proxyHostInfo);//If you need to use a proxy to access the service, add the proxy information to the third parameter.
- Create a face set.
1 2 3
CreateExternalFields createExternalFields = new CreateExternalFields(); createExternalFields.addField("testInteger", FieldType.INTEGER); CreateFaceSetResult createFaceSetResult = frsClient.getV2().getFaceSetService().createFaceSet(faceSetName, faceSetCapacity, createExternalFields);
- Add faces.
1 2 3 4 5 6 7
String imageId = "image1"; File image = new File("data/face.jpg"); byte[] fileData = FileUtils.readFileToByteArray(image); String imageBase64 = Base64.encodeBase64String(fileData); AddExternalFields addExternalFields = new AddExternalFields(); addExternalFields.addField("testInteger", 123); AddFaceResult addFaceResult = frsClient.getV2().getFaceService().addFaceByBase64(faceSetName, imageId, imageBase64, addExternalFields);
- Invoke the Face Detection service.
1 2 3
String obsUrl = "/bucket/face.jpg"; String attr = "1,2"; DetectFaceResult detectFaceResult = frsClient.getV2().getDetectService().detectFaceByObsUrl(obsUrl, attr);
Last Article: Quick Start
Next Article: Mappings Between Java SDK & APIs
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.