Converting a Vision VNTextObservation to a String
Introduction
The Vision framework in iOS provides powerful tools for analyzing images, including text recognition. The `VNTextObservation` class represents a detected text region within an image. This article focuses on converting a `VNTextObservation` to a string, making the recognized text easily accessible for further processing.
Understanding VNTextObservation
A `VNTextObservation` encapsulates information about detected text in an image. It includes:
* **Bounding Box:** The rectangular area where the text was detected.
* **Confidence:** A score indicating the likelihood of the text being accurately recognized.
* **Text:** The recognized text itself, represented as a `String`.
* **Recognized Language:** The language the text is written in.
Methods for Text Extraction
There are two primary ways to obtain the recognized text from a `VNTextObservation`:
1. Direct Access to `string` Property
The most straightforward approach is to directly access the `string` property of the `VNTextObservation` object. This property holds the recognized text as a `String`.
**Code:**
“`swift
let observation = VNTextObservation() // Assuming you have a VNTextObservation object
let recognizedText = observation.string
“`
2. Recursive Traversal of `lines` Property
For more complex text recognition scenarios involving multiple lines, you can traverse the `lines` property. This property contains an array of `VNTextObservation` objects representing individual lines of text.
**Code:**
“`swift
func extractText(from observation: VNTextObservation) -> String {
var extractedText = “”
for line in observation.lines {
extractedText += line.string + “\n”
}
return extractedText.trimmingCharacters(in: .whitespacesAndNewlines)
}
“`
Example
**Code:**
“`swift
import Vision
// Assuming you have an image and a Vision request that recognizes text
let image = CIImage(image: UIImage(named: “image”)!)
let request = VNRecognizeTextRequest()
// Perform text recognition
let handler = VNImageRequestHandler(ciImage: image!)
try handler.perform([request])
// Access the first detected text observation
guard let observation = request.results?.first as? VNTextObservation else {
return
}
// Extract the recognized text
let text = observation.string
print(text) // Prints the recognized text
“`
**Output:**
“`
This is a sample text.
“`
Conclusion
Extracting text from a `VNTextObservation` is a fundamental step in many Vision-based applications. By using the `string` property or traversing the `lines` property, you can easily obtain the recognized text for further analysis, processing, or display.