Page updated Nov 16, 2023

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:

? Please select from one of the categories below (Use arrow keys) ❯ Identify Convert Interpret Infer Learn More ? What would you like to identify? Identify Text Identify Entities ❯ Identify Labels ? Provide a friendly name for your resource <Enter a friendly name here> ? Would you like use the default configuration? ❯ Default Configuration Advanced Configuration ? Who should have access? Auth users only ❯ Auth and Guest users
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:

func detectLabels(_ image: URL) async throws -> Predictions.Identify.Labels.Result { do { let result = try await Amplify.Predictions.identify(.labels(type: .labels), in: image) print("Identified labels: \(result.labels)") return result } catch let error as PredictionsError { print("Error identifying labels: \(error)") throw error } catch { print("Unexpected error: \(error)") throw error } } // To identify labels with unsafe content func detectAllLabels(_ image: URL) async throws -> Predictions.Identify.Labels.Result { do { let result = try await Amplify.Predictions.identify(.labels(type: .all), in: image) print("Identified labels: \(result.labels)") return result } catch let error as PredictionsError { print("Error identifying labels: \(error)") throw error } catch { print("Unexpected error: \(error)") throw error } }
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}