|
| 1 | +--- |
| 2 | +title: "Face Java client library quickstart" |
| 3 | +description: Use the Face client library for Java to detect faces and identify faces (facial recognition search). |
| 4 | +#services: cognitive-services |
| 5 | +author: PatrickFarley |
| 6 | +manager: nitinme |
| 7 | +ms.service: azure-ai-vision |
| 8 | +ms.subservice: azure-ai-face |
| 9 | +ms.custom: |
| 10 | + - ignite-2023 |
| 11 | +ms.topic: include |
| 12 | +ms.date: 08/08/2024 |
| 13 | +ms.author: pafarley |
| 14 | +--- |
| 15 | + |
| 16 | +Get started with facial recognition using the Face client library for Java. Follow these steps to install the package and try out the example code for basic tasks. The Face service provides you with access to advanced algorithms for detecting and recognizing human faces in images. Follow these steps to install the package and try out the example code for basic face identification using remote images. |
| 17 | + |
| 18 | +[Reference documentation](https://aka.ms/azsdk-java-face-ref) | [Library source code](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/face/azure-ai-vision-face) | [Package (Maven)](https://central.sonatype.com/artifact/com.azure/azure-ai-vision-face) | [Samples](https://github.com/Azure/azure-sdk-for-java/tree/main/sdk/face/azure-ai-vision-face/src/samples) |
| 19 | + |
| 20 | +## Prerequisites |
| 21 | + |
| 22 | +* Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services/) |
| 23 | +* The current version of the [Java Development Kit (JDK)](https://www.microsoft.com/openjdk) |
| 24 | +* [Apache Maven](https://maven.apache.org/download.cgi) installed. On Linux, install from the distribution repositories if available. |
| 25 | +* Once you have your Azure subscription, <a href="https://portal.azure.com/#create/Microsoft.CognitiveServicesFace" title="Create a Face resource" target="_blank">create a Face resource</a> in the Azure portal to get your key and endpoint. After it deploys, select **Go to resource**. |
| 26 | + * You'll need the key and endpoint from the resource you create to connect your application to the Face API. |
| 27 | + * You can use the free pricing tier (`F0`) to try the service, and upgrade later to a paid tier for production. |
| 28 | + |
| 29 | + |
| 30 | +## Create environment variables |
| 31 | + |
| 32 | +[!INCLUDE [create environment variables](../face-environment-variables.md)] |
| 33 | + |
| 34 | +## Identify and verify faces |
| 35 | + |
| 36 | +1. Install the client library |
| 37 | + |
| 38 | + Open a console window and create a new folder for your quickstart application. Copy the following content to a new file. Save the file as `pom.xml` in your project directory: |
| 39 | + |
| 40 | + <!-- [!INCLUDE][](https://raw.githubusercontent.com/Azure-Samples/cognitive-services-quickstart-code/master/java/Face/pom.xml)] --> |
| 41 | + ```xml |
| 42 | + <project xmlns="http://maven.apache.org/POM/4.0.0" |
| 43 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 44 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 45 | + <modelVersion>4.0.0</modelVersion> |
| 46 | + <groupId>com.example</groupId> |
| 47 | + <artifactId>my-application-name</artifactId> |
| 48 | + <version>1.0.0</version> |
| 49 | + <dependencies> |
| 50 | + <!-- https://mvnrepository.com/artifact/com.azure/azure-ai-vision-face --> |
| 51 | + <dependency> |
| 52 | + <groupId>com.azure</groupId> |
| 53 | + <artifactId>azure-ai-vision-face</artifactId> |
| 54 | + <version>1.0.0-beta.1</version> |
| 55 | + </dependency> |
| 56 | + <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --> |
| 57 | + <dependency> |
| 58 | + <groupId>org.apache.httpcomponents</groupId> |
| 59 | + <artifactId>httpclient</artifactId> |
| 60 | + <version>4.5.13</version> |
| 61 | + </dependency> |
| 62 | + <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> |
| 63 | + <dependency> |
| 64 | + <groupId>com.google.code.gson</groupId> |
| 65 | + <artifactId>gson</artifactId> |
| 66 | + <version>2.11.0</version> |
| 67 | + </dependency> |
| 68 | + </dependencies> |
| 69 | + </project> |
| 70 | + ``` |
| 71 | + |
| 72 | + Install the SDK and dependencies by running the following in the project directory: |
| 73 | + |
| 74 | + ```console |
| 75 | + mvn clean dependency:copy-dependencies |
| 76 | + ``` |
| 77 | + |
| 78 | +1. Create a new Java application |
| 79 | + |
| 80 | + Create a file named `Quickstart.java`, open it in a text editor, and paste in the following code: |
| 81 | + |
| 82 | + > [!NOTE] |
| 83 | + > If you haven't received access to the Face service using the [intake form](https://aka.ms/facerecognition), some of these functions won't work. |
| 84 | + |
| 85 | + [!code-java[](~/cognitive-services-quickstart-code/java/Face/Quickstart.java?name=snippet_single)] |
| 86 | + |
| 87 | + |
| 88 | +1. Run your face recognition app from the application directory with the `javac` and `java` commands. |
| 89 | + |
| 90 | + #### [Windows](#tab/windows) |
| 91 | + |
| 92 | + ```console |
| 93 | + javac -cp target\dependency\* Quickstart.java |
| 94 | + java -cp .;target\dependency\* Quickstart |
| 95 | + ``` |
| 96 | + |
| 97 | + #### [Linux](#tab/linux) |
| 98 | + |
| 99 | + ```console |
| 100 | + javac -cp target/dependency/* Quickstart.java |
| 101 | + java -cp .:target/dependency/* Quickstart |
| 102 | + ``` |
| 103 | + |
| 104 | + --- |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | +## Output |
| 109 | + |
| 110 | +```console |
| 111 | +========IDENTIFY FACES======== |
| 112 | + |
| 113 | +Create a person group (3761e61a-16b2-4503-ad29-ed34c58ba676). |
| 114 | +Create a person group person 'Family1-Dad'. |
| 115 | +Check whether image is of sufficient quality for recognition |
| 116 | +Add face to the person group person(Family1-Dad) from image `Family1-Dad1.jpg` |
| 117 | +Check whether image is of sufficient quality for recognition |
| 118 | +Add face to the person group person(Family1-Dad) from image `Family1-Dad2.jpg` |
| 119 | +Create a person group person 'Family1-Mom'. |
| 120 | +Check whether image is of sufficient quality for recognition |
| 121 | +Add face to the person group person(Family1-Mom) from image `Family1-Mom1.jpg` |
| 122 | +Check whether image is of sufficient quality for recognition |
| 123 | +Add face to the person group person(Family1-Mom) from image `Family1-Mom2.jpg` |
| 124 | +Create a person group person 'Family1-Son'. |
| 125 | +Check whether image is of sufficient quality for recognition |
| 126 | +Add face to the person group person(Family1-Son) from image `Family1-Son1.jpg` |
| 127 | +Check whether image is of sufficient quality for recognition |
| 128 | +Add face to the person group person(Family1-Son) from image `Family1-Son2.jpg` |
| 129 | + |
| 130 | +Train person group 3761e61a-16b2-4503-ad29-ed34c58ba676. |
| 131 | +Training status: succeeded. |
| 132 | + |
| 133 | +Pausing for 60 seconds to avoid triggering rate limit on free account... |
| 134 | +4 face(s) with 4 having sufficient quality for recognition. |
| 135 | +Person 'Family1-Dad' is identified for the face in: identification1.jpg - d7995b34-1b72-47fe-82b6-e9877ed2578d, confidence: 0.96807. |
| 136 | +Verification result: is a match? true. confidence: 0.96807 |
| 137 | +Person 'Family1-Mom' is identified for the face in: identification1.jpg - 844da0ed-4890-4bbf-a531-e638797f96fc, confidence: 0.96902. |
| 138 | +Verification result: is a match? true. confidence: 0.96902 |
| 139 | +No person is identified for the face in: identification1.jpg - c543159a-57f3-4872-83ce-2d4a733d71c9. |
| 140 | +Person 'Family1-Son' is identified for the face in: identification1.jpg - 414fac6c-7381-4dba-9c8b-fd26d52e879b, confidence: 0.9281. |
| 141 | +Verification result: is a match? true. confidence: 0.9281 |
| 142 | + |
| 143 | +========DELETE PERSON GROUP======== |
| 144 | + |
| 145 | +Deleted the person group 3761e61a-16b2-4503-ad29-ed34c58ba676. |
| 146 | + |
| 147 | +End of quickstart. |
| 148 | +``` |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | +## Clean up resources |
| 153 | + |
| 154 | +If you want to clean up and remove an Azure AI services subscription, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it. |
| 155 | + |
| 156 | +* [Azure portal](../../../multi-service-resource.md?pivots=azportal#clean-up-resources) |
| 157 | +* [Azure CLI](../../../multi-service-resource.md?pivots=azcli#clean-up-resources) |
| 158 | + |
| 159 | +## Next steps |
| 160 | + |
| 161 | +In this quickstart, you learned how to use the Face client library for Java to do basic face identification. Next, learn about the different face detection models and how to specify the right model for your use case. |
| 162 | + |
| 163 | +> [!div class="nextstepaction"] |
| 164 | +> [Specify a face detection model version](../../how-to/specify-detection-model.md) |
| 165 | +
|
| 166 | +* [What is the Face service?](../../overview-identity.md) |
| 167 | +* More extensive sample code can be found on [GitHub](https://aka.ms/FaceSamples). |
0 commit comments