Swift
On-Device AI Scanning (OCR)
The Vision SDK allows for on-device AI scanning (OCR), enabling offline extraction of structured information from documents such as shipping labels. This is ideal when low latency and offline functionality are critical, such as in warehouse and logistics environments.
New APIs(OCR) (Recommended)
π οΈ Step 1: Downloading On-Device OCR
Before using the on-device OCR, you must downlaod the model using the following methods. This ensures the necessary AI models are downloaded.
β οΈ You must wait for the completion block to succeed before proceeding to model loading.
π οΈ Step 2: Loading On-Device OCR
Before using the on-device OCR, you must load the model using the following methods. This ensures the necessary AI models are loaded and are ready for use.
β οΈ You must wait for the completion block to succeed before proceeding to data extraction.
π§© Selecting the Model Type
When preparing for on-device AI scanning, you can choose which document model you want to use by specifying the forModelClass parameter. This lets the SDK know which type of document youβre scanning so it can load the appropriate AI model.
Here are the supported options:
π¦ For Shipping Labels
Use this to scan and extract structured data from shipping labels.
π For Bill of Lading (BOL)
Use this to extract data from Bill of Lading documents.
π·οΈ For Item Labels
Use this to process and extract structured details from product/item labels.
π·οΈ For Document Classification
Use this to process and extract document type from a document image.
π Make sure the model is prepared successfully before scanning. Each model may vary in size and complexity depending on the document type.
π οΈ Model Management Methods
These methods can be use to handle different models.
Cancel Downloading
Use this method to cancel any in progress download.
Model Updating
This method can be used to check for updates for a given model.
Model Unloading
Use this model to unload any pre-loaded models from memory.
Model Deleting
This method deletes the pre-downloaded model from the disk. It automatically unloads the model if already loaded.
Model Loading Status Check
This method checks if the given model is loaded in the memory or not. Returns true or false.
This method returns all models and their respective sizes loaded in the memory.
Model Downloaded Status Check
This method checks if the given model is has been downladed or not. Returns model information if existing model found or returns nil.
This method returns all downloaded models and their respective data.
π§ Step 3: Extracting Data from Image
Once the model is downloaded and loaded successfully, use the following method to perform OCR on a given image.
Parameters:
ciImage: TheCIImageyou want to process.barcodes: A list ofDetectedCodevalues representing barcodes detected in the image (if any).checkImageSharpness: A boolean whether VisionSDK should check if the given image is sharp enough.modelClass: Model Class of the model you want to use for extraction. Make sure the model is already loadedwithModelSize: Model Size of the model you want to use for extraction. Make sure the model is already loadedcompletion: A closure returning the extracted data or error.
π The returned data follows the same structure as the PackageX Cloud OCR API response.
β Full Sample Flow
Legacy Support(OCR) (NOT Recommended for new users)
π οΈ Step 1: Preparing On-Device OCR
Before using the on-device OCR, you must prepare the model using the following methods. This ensures the necessary AI models are downloaded and ready to use.
Option 1: Auto Model Size Configuration
If you want Vision SDK to automatically determine the best model size based on your PackageX subscription:
Option 2: Explicit Model Size Configuration
You can also specify the model size manually using .micro (faster, smaller) or .large (slower, more accurate):
β οΈ You must wait for the completion block to succeed before proceeding to OCR extraction.
π§© Selecting the Model Type
When preparing for on-device AI scanning, you can choose which document model you want to use by specifying the forModelClass parameter. This lets the SDK know which type of document youβre scanning so it can load the appropriate AI model.
Here are the supported options:
π¦ For Shipping Labels
Use this to scan and extract structured data from shipping labels.
π For Bill of Lading (BOL)
Use this to extract data from Bill of Lading documents.
π·οΈ For Item Labels
Use this to process and extract structured details from product/item labels.
π·οΈ For Document Classification
Use this to process and extract document type from a document image.
π Make sure the model is prepared successfully before scanning. Each model may vary in size and complexity depending on the document type.
π§ Step 2: Extracting Data from Image
Once the model is prepared successfully, use the following method to perform OCR on a given image.
Parameters:
ciImage: TheCIImageyou want to process.barcodes: A list ofStringvalues representing barcodes detected in the image (if any).completion: A closure returning the extracted data or error.
π The returned data follows the same structure as the PackageX Cloud OCR API response.