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❯ Identify3 Convert4 Interpret5 Infer6 Learn More7 8? What would you like to identify?9 Identify Text10 Identify Entities11❯ Identify Labels12
13? Provide a friendly name for your resource14 <Enter a friendly name here>15
16? Would you like use the default configuration?17❯ Default Configuration18 Advanced Configuration19
20? Who should have access?21 Auth users only22❯ 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 result6 } catch let error as PredictionsError {7 print("Error identifying labels: \(error)")8 throw error9 } catch {10 print("Unexpected error: \(error)")11 throw error12 }13}14
15// To identify labels with unsafe content16func 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 result21 } catch let error as PredictionsError {22 print("Error identifying labels: \(error)")23 throw error24 } catch {25 print("Unexpected error: \(error)")26 throw error27 }28}