Apple Vision framework – Text extraction from image
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Apple's Vision framework gives iOS and macOS apps a built-in way to perform OCR, meaning text recognition from images, without sending every image to a remote service. For most modern Apple-platform apps, the standard entry point is VNRecognizeTextRequest.
The workflow is straightforward once the pieces are clear: provide an image, create a recognition request, run it through a request handler, and read the recognized strings from the resulting observations. The real work is in handling orientation, tuning accuracy, and processing results in a way that fits your app.
The Core OCR Flow in Vision
Vision text extraction usually follows these steps:
- obtain an image as
CGImage,CIImage, or pixel buffer - create a
VNRecognizeTextRequest - configure recognition settings
- execute the request with
VNImageRequestHandler - read
VNRecognizedTextObservationresults
Here is a minimal Swift example that extracts lines of text from a UIImage:
To bridge UIImage.Orientation into Vision's orientation type, add a small helper:
Understanding the Main Request Options
VNRecognizeTextRequest has a few settings that matter immediately.
recognitionLevel controls the speed and quality tradeoff:
- '
.fastis useful for quick scanning or real-time scenarios' - '
.accurateis better for receipts, documents, and small text'
recognitionLanguages helps Vision choose the right language model. If you know the text is English, French, or another specific language, set that explicitly instead of relying on broad detection.
usesLanguageCorrection tells Vision to prefer plausible words over raw character guesses. That often improves document OCR, but it can hurt if you are scanning product codes, serial numbers, or intentionally unusual identifiers.
Vision also returns bounding boxes with each observation. That matters when you want to draw highlights over detected text, let users tap extracted regions, or preserve layout information.
Processing Results
Each VNRecognizedTextObservation may contain multiple text candidates ranked by confidence. For a simple app, taking the first candidate is usually fine. If you are building a scanner for forms or labels, reading several candidates can help when you want to apply your own validation logic.
Example:
This is especially useful when the OCR output must match a known pattern, such as an invoice number or a license plate format. In that case, the top candidate is not always the best business result.
Working with Camera Frames
For live capture, you usually feed Vision frames from AVCaptureVideoDataOutput. The API shape is similar, but instead of a CGImage, you pass a pixel buffer into the handler.
That design lets you build camera-based features such as:
- scanning labels in real time
- reading a document before taking a photo
- showing highlighted text boxes while the user moves the camera
When you do this, control request frequency. Running full OCR on every frame can overwhelm the device, so most apps sample frames or cancel outdated requests when a new frame arrives.
Common Pitfalls
The most frequent issue is wrong image orientation. If you skip the orientation parameter or map it incorrectly, OCR quality drops sharply because the model is effectively reading rotated text.
Another common mistake is doing OCR on the main thread. Text recognition can be expensive, especially with .accurate, so it should run off the UI thread and update the interface only when results are ready.
Developers also assume OCR failure means Vision is broken when the actual problem is input quality. Low contrast, motion blur, tiny text, or aggressive image compression can all ruin recognition. Cropping to the text area and using a clearer source image often helps more than changing code.
Finally, do not overcorrect with language settings. If your app scans mixed-language content or identifiers, forcing one language and enabling language correction can replace valid strings with dictionary words that look "more likely" to the model.
Summary
- '
VNRecognizeTextRequestis the standard Vision API for extracting text from images.' - The basic flow is image input, request configuration, handler execution, and observation parsing.
- '
recognitionLevel,recognitionLanguages, andusesLanguageCorrectionstrongly affect results.' - Correct orientation handling is essential for reliable OCR.
- For camera-based OCR, run recognition asynchronously and avoid processing every frame at full accuracy.
Related reading
- Are modern CNN convolutional neural network as DetectNet rotate invariant?
- Are there any plans for ROI Pooling layer in tensorflow for object detection?
- Are there any special image compression algorithms for face cases?
- Assertion failed queryDescriptors.type trainDescCollection0.type in knnMatchImpl,
- Application Loader stuck at Authenticating with the iTunes store when uploading an iOS app
- Application tried to present modally an active controller?
- Automatically add watermark to an image
- Automatically recognize patterns in images
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.