Skip to content

Commit 0d0aca7

Browse files
committed
finish remaining sections
1 parent 8568b72 commit 0d0aca7

File tree

1 file changed

+165
-28
lines changed
  • articles/cognitive-services/Face/QuickStarts

1 file changed

+165
-28
lines changed

articles/cognitive-services/Face/QuickStarts/go-sdk.md

Lines changed: 165 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -165,40 +165,182 @@ These code samples show you how to complete basic tasks using the Face service c
165165
> [!NOTE]
166166
> This quickstart assumes you've [created environment variables](https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account#configure-an-environment-variable-for-authentication) for your Face key and endpoint, named `FACE_SUBSCRIPTION_KEY` and `FACE_ENDPOINT` respectively.
167167
168-
Create a `main` function and add the following code to it to instantiate a client with your endpoint and key. You create a [CognitiveServicesAuthorizer](https://godoc.org/github.com/Azure/go-autorest/autorest#CognitiveServicesAuthorizer) object with your key, and use it with your endpoint to create a [Client](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face#Client) object.
168+
Create a **main** function and add the following code to it to instantiate a client with your endpoint and key. You create a [CognitiveServicesAuthorizer](https://godoc.org/github.com/Azure/go-autorest/autorest#CognitiveServicesAuthorizer) object with your key, and use it with your endpoint to create a [Client](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face#Client) object. This code also instantiates a context object, which is needed for the creation of client objects. It also defines a remote location where some of the sample images in this quickstart are found.
169169

170170
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_main_client)]
171171

172172

173-
## Example task 1
173+
## Detect faces in an image
174174

175-
Example: Create a new method to read in the data and add it to a [Request](https://docs.microsoft.com/dotnet/) object as an array of [Points](https://docs.microsoft.com/dotnet/). Send the request with the [send()](https://docs.microsoft.com/dotnet/) method
175+
Add the following code in your **main** method. This defines a remote sample image and specifies which face features to extract from the image. It also specifies which AI model to use to extract data from the detected face(s). See [Specify a recognition model](../Face-API-How-to-Topics/specify-recognition-model.md) for information on these options. Finally, the **[DetectWithURL](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face#Client.DetectWithURL)** method performs the face detection operation on the image and saves the results in program memory.
176176

177-
```Go
177+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_detect)]
178178

179-
```
179+
### Display detected face data
180180

181-
<!--
182-
If this code sample is in a function, tell the reader to call it. For example:
181+
The next block of code takes the first element in the array of [DetectedFace](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face#DetectedFace) objects and prints its attributes to the console. If you used an image with multiple faces, you should iterate through teh array instead.
183182

184-
Call the `example()` function.
183+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_detect_display)]
185184

186-
-->
185+
## Find similar faces
187186

188-
## Example task 2
187+
The following code takes a single detected face (source) and searches a set of other faces (target) to find matches. When it finds a match, it prints the ID of the matched face to the console.
189188

190-
Example: Create a new method to read in the data and add it to a [Request](https://docs.microsoft.com/dotnet/) object as an array of [Points](https://docs.microsoft.com/dotnet/). Send the request with the [send()](https://docs.microsoft.com/dotnet/) method
189+
### Detect faces for comparison
191190

192-
```Go
191+
First, save a reference to the face you detected in the [Detect faces in an image](#detect-faces-in-an-image) section. This will be the source face.
193192

194-
```
193+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_similar_single_ref)]
194+
195+
Then enter the following code to detect a set of faces in a different image. These will be the target faces.
196+
197+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_similar_multiple_ref)]
198+
199+
### Find matches
200+
201+
The following code uses the **[FindSimilar](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face#Client.FindSimilar)** method to find all of the target faces that match the source face.
202+
203+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_similar)]
204+
205+
### Print matches
206+
207+
The following code prints the match details to the console.
208+
209+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_similar_print)]
210+
211+
212+
## Create and train a person group
213+
214+
To step through this scenario, you need to save the following images to the root directory of your project: https://github.com/Azure-Samples/cognitive-services-sample-data-files/tree/master/Face/images.
215+
216+
This group of images contains three sets of single-face images that correspond to three different people. The code will define three **PersonGroup Person** objects and associate them with image files that start with `woman`, `man`, and `child`.
217+
218+
### Create PersonGroup
219+
220+
Once you've downloaded your images, add the following code to the bottom of your **main** method. This code authenticates a **[PersonGroupClient](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face#PersonGroupClient)** object and then uses it to define a new **PersonGroup**.
221+
222+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_pg_setup)]
223+
224+
### Create PersonGroup Persons
225+
226+
The next block of code authenticates a **[PersonGroupPersonClient](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face#PersonGroupPersonClient)** and uses it to define three new **PersonGroup Person** objects. These each represent a single person in the set of images.
227+
228+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_pgp_setup)]
229+
230+
### Assign faces to Persons
231+
232+
The following code sorts the images by their prefix, detects faces, and assigns the faces to each respective **PersonGroup Person** object, based on the image file name.
233+
234+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_pgp_assign)]
235+
236+
### Train PersonGroup
237+
238+
Once you've assigned faces, you must train the **PersonGroup** so that it can identify the visual features associated with each of its **Person** objects. The following code calls the asynchronous **train** method and polls the result, printing the status to the console.
239+
240+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_pg_train)]
241+
242+
## Identify a face
243+
244+
The following code takes an image with multiple faces and looks to find the identity of each person in the image. It compares each detected face to a **PersonGroup**, a database of different **Person** objects whose facial features are known.
245+
246+
> [!IMPORTANT]
247+
> In order to run this example, you must first run the code in [Create and train a person group](#create-and-train-a-person-group).
248+
249+
### Get a test image
250+
251+
The following code looks in the root of your project for an image _test-image-person-group.jpg_ and loads it into program memory. You can find this image in the same repo as the images used in [Create and train a person group](#create-and-train-a-person-group): https://github.com/Azure-Samples/cognitive-services-sample-data-files/tree/master/Face/images.
252+
253+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_id_source_get)]
254+
255+
### Detect source faces in test image
256+
257+
The next code block does ordinary face detection on the test image to retrieve all of the faces and save them to an array.
258+
259+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_id_source_detect)]
260+
261+
### Identify faces
262+
263+
The **[Identify](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face#Client.Identify)** method takes the array of detected faces and compares them to the given **PersonGroup** (defined and trained in the earlier section). If it can match a detected face to a **Person** in the group, it saves the result.
264+
265+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_id)]
266+
267+
This code then prints detailed match results to the console.
195268

196-
<!--
197-
If this code sample is in a function, tell the reader to call it. For example:
269+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_id_print)]
198270

199-
Call the `example()` function.
200271

201-
-->
272+
## Verify faces
273+
274+
The Verify operation takes a face ID and either another face ID or a **Person** object and determines whether they belong to the same person.
275+
276+
The following code detects faces in two source images and then verifies each of them against a face detected from a target image.
277+
278+
### Get test images
279+
280+
The following code blocks declare variables that will point to the target and source images for the verification operation.
281+
282+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_ver_images)]
283+
284+
### Detect faces for verification
285+
286+
The following code detects faces in the source and target images and saves them to variables.
287+
288+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_ver_detect_source)]
289+
290+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_ver_detect_target)]
291+
292+
### Get verification results
293+
294+
The following code compares each of the source images to the target image and prints a message indicating whether they belong to the same person.
295+
296+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_ver)]
297+
298+
299+
## Take a snapshot for data migration
300+
301+
The Snapshots feature lets you move your saved face data, such as a trained **PersonGroup**, to a different Azure Cognitive Services Face subscription. You may want to use this feature if, for example, you've created a **PersonGroup** object using a free trial subscription and now want to migrate it to a paid subscription. See the [Migrate your face data](../Face-API-How-to-Topics/how-to-migrate-face-data.md) for a broad overview of the Snapshots feature.
302+
303+
In this example, you will migrate the **PersonGroup** you created in [Create and train a person group](#create-and-train-a-person-group). You can either complete that section first, or use your own Face data construct(s).
304+
305+
### Set up target subscription
306+
307+
First, you must have a second Azure subscription with a Face resource; you can do this by repeating the steps in the [Setting up](#setting-up) section.
308+
309+
Then, create the following variables near the top of your **main** method. You'll also need to create new environment variables for the subscription ID of your Azure account, as well as the key, endpoint, and subscription ID of your new (target) account.
310+
311+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_target_client)]
312+
313+
Then, put your subscription ID value into an array for the next steps.
314+
315+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_snap_target_id)]
316+
317+
### Authenticate target client
318+
319+
Later in your script, save your original client object as the source client, and then authenticate a new client object for your target subscription.
320+
321+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_snap_target_auth)]
322+
323+
### Take a snapshot
324+
325+
The next step is to take the snapshot with **[Take](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face#SnapshotClient.Take)**, which saves your original subscription's face data to a temporary cloud location. This method returns an ID that you use to query the status of the operation.
326+
327+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_snap_take)]
328+
329+
Next, query the ID until the operation has completed.
330+
331+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_snap_query)]
332+
333+
### Apply the snapshot
334+
335+
Use the **[Apply](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face#SnapshotClient.Apply)** operation to write your newly uploaded face data to your target subscription. This method also returns an ID.
336+
337+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_snap_apply)]
338+
339+
Again, query the ID until the operation has completed.
340+
341+
[!code-go[](~/cognitive-services-quickstart-code/go/Face/FaceQuickstart.go?name=snippet_snap_apply_query)]
342+
343+
Once you've completed these steps, you'll be able to access your face data constructs from your new (target) subscription.
202344

203345
## Run the application
204346

@@ -215,19 +357,14 @@ If you want to clean up and remove a Cognitive Services subscription, you can de
215357
* [Portal](../../cognitive-services-apis-create-account.md#clean-up-resources)
216358
* [Azure CLI](../../cognitive-services-apis-create-account-cli.md#clean-up-resources)
217359

218-
## Troubleshooting
219-
220-
<!--
221-
This section is optional. If you know of areas that people commonly run into trouble, help them resolve those issues in this section
222-
-->
360+
If you created a **PersonGroup** in this quickstart and you want to delete it, call the **[Delete](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face#PersonGroupClient.Delete)** method. If you migrated data using the Snapshot feature in this quickstart, you'll also need to delete the **PersonGroup** saved to the target subscription.
223361

224362
## Next steps
225363

226-
> [!div class="nextstepaction"]
227-
>[Next article]()
364+
In this quickstart, you learned how to use the Face library for Go to do basis tasks. Next, explore the reference documentation to learn more about the library.
228365

229-
## See also
366+
> [!div class="nextstepaction"]
367+
> [Face API reference (Go)](https://godoc.org/github.com/Azure/azure-sdk-for-go/services/cognitiveservices/v1.0/face)
230368
231-
* [What is the Face service?](#)
232-
* [API reference](#)
233-
* The source code for this sample can be found on [GitHub](#).
369+
* [What is the Face service?](../overview.md)
370+
* The source code for this sample can be found on [GitHub](https://github.com/Azure-Samples/cognitive-services-quickstart-code/blob/master/go/Face/FaceQuickstart.go).

0 commit comments

Comments
 (0)