Skip to content

Commit 7d43c34

Browse files
committed
Add Java code snippet
1 parent 904e52b commit 7d43c34

File tree

1 file changed

+64
-0
lines changed
  • articles/ai-services/computer-vision/Tutorials

1 file changed

+64
-0
lines changed

articles/ai-services/computer-vision/Tutorials/liveness.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@ The high-level steps involved in liveness orchestration are illustrated below:
9797

9898
#### [Java](#tab/java)
9999
```java
100+
String endpoint = System.getenv("VISION_ENDPOINT");
101+
String accountKey = System.getenv("VISION_KEY");
102+
103+
FaceSessionClient sessionClient = new FaceSessionClientBuilder()
104+
.endpoint(endpoint)
105+
.credential(new AzureKeyCredential(accountKey))
106+
.buildClient();
107+
108+
CreateLivenessSessionContent parameters = new CreateLivenessSessionContent(LivenessOperationMode.PASSIVE)
109+
.setDeviceCorrelationId("723d6d03-ef33-40a8-9682-23a1feb7bccd")
110+
.setSendResultsToClient(false);
111+
112+
CreateLivenessSessionResult creationResult = sessionClient.createLivenessSession(parameters);
113+
System.out.println("Session created.");
114+
System.out.println("Session id: " + creationResult.getSessionId());
115+
System.out.println("Auth token: " + creationResult.getAuthToken());
100116
```
101117

102118
#### [Python](#tab/python)
@@ -195,6 +211,16 @@ The high-level steps involved in liveness orchestration are illustrated below:
195211

196212
#### [Java](#tab/java)
197213
```java
214+
LivenessSession sessionResult = sessionClient.getLivenessSessionResult(creationResult.getSessionId());
215+
System.out.println("Session id: " + sessionResult.getId());
216+
System.out.println("Session status: " + sessionResult.getStatus());
217+
System.out.println("Liveness detection request id: " + sessionResult.getResult().getRequestId());
218+
System.out.println("Liveness detection received datetime: " + sessionResult.getResult().getReceivedDateTime());
219+
System.out.println("Liveness detection decision: " + sessionResult.getResult().getResponse().getBody().getLivenessDecision());
220+
System.out.println("Session created datetime: " + sessionResult.getCreatedDateTime());
221+
System.out.println("Auth token TTL (seconds): " + sessionResult.getAuthTokenTimeToLiveInSeconds());
222+
System.out.println("Session expired: " + sessionResult.isSessionExpired());
223+
System.out.println("Device correlation id: " + sessionResult.getDeviceCorrelationId());
198224
```
199225

200226
#### [Python](#tab/python)
@@ -284,6 +310,8 @@ The high-level steps involved in liveness orchestration are illustrated below:
284310

285311
#### [Java](#tab/java)
286312
```java
313+
sessionClient.deleteLivenessSession(creationResult.getSessionId());
314+
System.out.println("The session " + creationResult.getSessionId() + " is deleted.");
287315
```
288316

289317
#### [Python](#tab/python)
@@ -354,6 +382,28 @@ The high-level steps involved in liveness with verification orchestration are il
354382

355383
#### [Java](#tab/java)
356384
```java
385+
String endpoint = System.getenv("VISION_ENDPOINT");
386+
String accountKey = System.getenv("VISION_KEY");
387+
388+
FaceSessionClient sessionClient = new FaceSessionClientBuilder()
389+
.endpoint(endpoint)
390+
.credential(new AzureKeyCredential(accountKey))
391+
.buildClient();
392+
393+
CreateLivenessSessionContent parameters = new CreateLivenessSessionContent(LivenessOperationMode.PASSIVE)
394+
.setDeviceCorrelationId("723d6d03-ef33-40a8-9682-23a1feb7bccd")
395+
.setSendResultsToClient(false);
396+
397+
Path path = Paths.get("test.png");
398+
BinaryData data = BinaryData.fromFile(path);
399+
CreateLivenessWithVerifySessionResult creationResult = sessionClient.createLivenessWithVerifySession(parameters, data);
400+
401+
System.out.println("Session created.");
402+
System.out.println("Session id: " + creationResult.getSessionId());
403+
System.out.println("Auth token: " + creationResult.getAuthToken());
404+
System.out.println("The reference image:");
405+
System.out.println(" Face rectangle: " + creationResult.getVerifyImage().getFaceRectangle().getTop() + " " + creationResult.getVerifyImage().getFaceRectangle().getLeft() + " " + creationResult.getVerifyImage().getFaceRectangle().getWidth() + " " + creationResult.getVerifyImage().getFaceRectangle().getHeight());
406+
System.out.println(" The quality for recognition: " + creationResult.getVerifyImage().getQualityForRecognition());
357407
```
358408

359409
#### [Python](#tab/python)
@@ -456,6 +506,18 @@ The high-level steps involved in liveness with verification orchestration are il
456506

457507
#### [Java](#tab/java)
458508
```java
509+
LivenessWithVerifySession sessionResult = sessionClient.getLivenessWithVerifySessionResult(creationResult.getSessionId());
510+
System.out.println("Session id: " + sessionResult.getId());
511+
System.out.println("Session status: " + sessionResult.getStatus());
512+
System.out.println("Liveness detection request id: " + sessionResult.getResult().getRequestId());
513+
System.out.println("Liveness detection received datetime: " + sessionResult.getResult().getReceivedDateTime());
514+
System.out.println("Liveness detection decision: " + sessionResult.getResult().getResponse().getBody().getLivenessDecision());
515+
System.out.println("Verification result: " + sessionResult.getResult().getResponse().getBody().getVerifyResult().isIdentical());
516+
System.out.println("Verification confidence: " + sessionResult.getResult().getResponse().getBody().getVerifyResult().getMatchConfidence());
517+
System.out.println("Session created datetime: " + sessionResult.getCreatedDateTime());
518+
System.out.println("Auth token TTL (seconds): " + sessionResult.getAuthTokenTimeToLiveInSeconds());
519+
System.out.println("Session expired: " + sessionResult.isSessionExpired());
520+
System.out.println("Device correlation id: " + sessionResult.getDeviceCorrelationId());
459521
```
460522

461523
#### [Python](#tab/python)
@@ -551,6 +613,8 @@ The high-level steps involved in liveness with verification orchestration are il
551613

552614
#### [Java](#tab/java)
553615
```java
616+
sessionClient.deleteLivenessWithVerifySession(creationResult.getSessionId());
617+
System.out.println("The session " + creationResult.getSessionId() + " is deleted.");
554618
```
555619

556620
#### [Python](#tab/python)

0 commit comments

Comments
 (0)