Identify text
The following APIs will allow you to identify text (words, tables, pages from a book) from an image.
For identifying text 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 the 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❯ Identify Convert Interpret Infer Learn More
? What would you like to identify? (Use arrow keys)❯ Identify Text Identify Entities Identify Labels
? Provide a friendly name for your resource <Enter a friendly name here>
? Would you also like to identify documents? <Enter 'y'>
? Who should have access? Auth users only❯ Auth and Guest users
Run amplify push
to create the resources in the cloud.
Identify text from image
Amplify will make calls to both Amazon Textract and Rekognition depending on the type of text you are looking to identify (i.e. image or document).
If you are detecting text from an image you would send in .plain
as your text format as shown below. Using .plain
with PredictionsIdentifyRequest.Options()
combines results from on device results from Core ML and AWS services to yield more accurate results.
func detectText(_ image: URL) async throws -> [Predictions.IdentifiedWord]? { do { let result = try await Amplify.Predictions.identify(.text, in: image) print("Identified text: \(result)") return result.words } catch let error as PredictionsError { print("Error identifying text: \(error)") throw error } catch { print("Unexpected error: \(error)") throw error } }}
func detectText(_ image: URL) -> AnyCancellable { Amplify.Publisher.create { try await Amplify.Predictions.identify(.text, in: image) } .sink(receiveCompletion: { completion in if case let .failure(error) = completion { print("Error identifying text: \(error)") } }, receiveValue: { value in print("Identified text: \(value)") })}
To get results that utilize on-device capabilities (Core ML), without combining results from the backend, you can use the following to pass into the options
argument of the Amplify.Predictions.identify
function.
let options = Predictions.Identify.Options(defaultNetworkPolicy: .offline)
Identify text in a document
Sending in .form
or .table
or .all
will do document analysis as well as text detection to detect tables and forms in a document. See below for an example with .form
.
func detectDocumentText(_ image: URL) async throws -> Predictions.Identify.DocumentText.Result { do { let result = try await Amplify.Predictions.identify( .textInDocument(textFormatType: .form), in: image ) print("Identified document text: \(result)") return result } catch let error as PredictionsError { print("Error identifying text in document: \(error)") throw error } catch { print("Unexpected error: \(error)") throw error }}
func detectDocumentText(_ image: URL) -> AnyCancellable { Amplify.Publisher.create { try await Amplify.Predictions.identify( .textInDocument(textFormatType: .form), in: image ) } .sink(receiveCompletion: { completion in if case let .failure(error) = completion { print("Error identifying text in document: \(error)") } }, receiveValue: { value in print("Identified text in document: \(value)") })}