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
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 contentfunc 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 }}
func detectLabels(_ image: URL) -> AnyCancellable { Amplify.Publisher.create { try await Amplify.Predictions.identify(.labels(type: .labels), in: image) } .sink(receiveCompletion: { completion in if case let .failure(error) = completion { print("Error identifying labels: \(error)") } }, receiveValue: { value in print("Identified labels: \(labels)") })}
// To identify labels with unsafe contentfunc detectAllLabels(_ image: URL) -> AnyCancellable { Amplify.Publisher.create { try await Amplify.Predictions.identify(.labels(type: .all), in: image) } .sink(receiveCompletion: { completion in if case let .failure(error) = completion { print("Error identifying labels: \(error)") } }, receiveValue: { value in print("Identified labels: \(labels)") })}