Page updated Jan 16, 2024

Label objects in an image

The following APIs will enable you identify real world objects (chairs, desks, etc) in images. These objects are referred to as "labels" from images.

For labeling images on iOS we use both AWS backend services as well as Apple's on-device Core ML Vision Framework to provide you with the most accurate results. If your device is offline, we will return results only from Core ML. On the other hand, if you are able to connect to AWS Services, we will return a unioned result from both the service and Core ML. Switching between backend services and Core ML is done automatically without any additional configuration required.

Set up your backend

If you haven't already done so, run amplify init inside your project and then amplify add auth (we recommend selecting the default configuration).

Run amplify add predictions, then use the following answers:

1? Please select from one of the categories below (Use arrow keys)
2❯ Identify
3 Convert
4 Interpret
5 Infer
6 Learn More
7
8? What would you like to identify?
9 Identify Text
10 Identify Entities
11❯ Identify Labels
12
13? Provide a friendly name for your resource
14 <Enter a friendly name here>
15
16? Would you like use the default configuration?
17❯ Default Configuration
18 Advanced Configuration
19
20? Who should have access?
21 Auth users only
22❯ Auth and Guest users

The Advanced Configuration will allow you to select moderation for unsafe content or all of the identified labels. Default uses both.

Run amplify push to create the resources in the cloud

Working with the API

You can identify real world objects such as chairs, desks, etc. which are referred to as “labels” by using the following sample code:

1func detectLabels(_ image: URL) async throws -> Predictions.Identify.Labels.Result {
2 do {
3 let result = try await Amplify.Predictions.identify(.labels(type: .labels), in: image)
4 print("Identified labels: \(result.labels)")
5 return result
6 } catch let error as PredictionsError {
7 print("Error identifying labels: \(error)")
8 throw error
9 } catch {
10 print("Unexpected error: \(error)")
11 throw error
12 }
13}
14
15// To identify labels with unsafe content
16func detectAllLabels(_ image: URL) async throws -> Predictions.Identify.Labels.Result {
17 do {
18 let result = try await Amplify.Predictions.identify(.labels(type: .all), in: image)
19 print("Identified labels: \(result.labels)")
20 return result
21 } catch let error as PredictionsError {
22 print("Error identifying labels: \(error)")
23 throw error
24 } catch {
25 print("Unexpected error: \(error)")
26 throw error
27 }
28}
1func detectLabels(_ image: URL) -> AnyCancellable {
2 Amplify.Publisher.create {
3 try await Amplify.Predictions.identify(.labels(type: .labels), in: image)
4 }
5 .sink(receiveCompletion: { completion in
6 if case let .failure(error) = completion {
7 print("Error identifying labels: \(error)")
8 }
9 }, receiveValue: { value in
10 print("Identified labels: \(labels)")
11 })
12}
13
14// To identify labels with unsafe content
15func detectAllLabels(_ image: URL) -> AnyCancellable {
16 Amplify.Publisher.create {
17 try await Amplify.Predictions.identify(.labels(type: .all), in: image)
18 }
19 .sink(receiveCompletion: { completion in
20 if case let .failure(error) = completion {
21 print("Error identifying labels: \(error)")
22 }
23 }, receiveValue: { value in
24 print("Identified labels: \(labels)")
25 })
26}