Skip to content

Using BlinkInput OCR recognizer

juraskrlec edited this page Sep 12, 2017 · 1 revision

BlinkInput recognizer is responsible for generic OCR on images. Generic OCR is achieved though use of OcrParser objects - objects which parse pieces of information from OCR results.

Here we explain how to use OCR recognizer, it's settings class PPBlinkInputRecognizerSettings, and result class PPBlinkInputRecognizerResult to obtain raw OCR results, together with parsed OCR results.

Initializing the scanner for BlinkInput

To initialize OCR scanning use PPBlinkInputRecognizerSettings recognizer settings. As always, you need to add PPBlinkInputRecognizerSettings object to settings.scanSettings.

// 1. To specify we want to perform OCR recognition, initialize the OCR recognizer settings
PPBlinkInputRecognizerSettings *blinkInputRecognizerSettings = [[PPBlinkInputRecognizerSettings alloc] init];

// 2. Add at least one OCR Parser
[blinkInputRecognizerSettings addOcrParser:[[PPRawOcrParserFactory alloc] init] name:@"Raw ocr"];

// 3. Add the recognizer setting to a list of used recognizer
[settings.scanSettings addRecognizerSettings:blinkInputRecognizerSettings];

Obtaining OCR scanning results

After initializing the library and presenting the scanning view controller, scanning results will be passed to the scanningViewController:didOutputResults: method. This method will receive an array of PPRecognizerResult object. Specifically, OCR scanning results will be of type PPBlinkInputRecognizerResult.

To obtain the OCR result use the following code.

- (void)scanningViewController:(UIViewController<PPScanningViewController> *)scanningViewController
              didOutputResults:(NSArray *)results {

    // Here you process scanning results. Scanning results are given in the array of PPRecognizerResult objects.
    // Perform your logic here

    for (PPRecognizerResult *result in results) {
        if ([result isKindOfClass:[PPOcrRecognizerResult class]]) {
            PPBlinkInputRecognizerResult* blinkInputRecognizerResult = (PPBlinkInputRecognizerResult*)result;
            // process ocr recognizer result
        }
    };
}

OCR Parsers

Parsers are the most important feature the OCR engine and they are make reading and extracting data out of OCR results extremely simple. Using Parsers allows you to use OCR engine without the need to implement complex postprocessing algorithms, and at the same time to use additional error-reducing methods in the Parser algorithms.

To add OCR parsers, use the method addOcrParser: on PPBlinkInputRecognizerSettings instance. For example, to add Raw OCR Parser, use:

[blinkInputRecognizerSettings addOcrParser:[[PPRawOcrParserFactory alloc] init] name:@"Raw ocr"];

Each Ocr Parser is identified with a PPOcrParserFactory object responsible for creating the parser, and an unique name which you can define yourself.

Parsers allow you to simply access data written in the OCR results. Important: if you need raw OCR results, use PPRawOcrParserFactory.

To collect Parser results from PPBlinkInputRecognizerResult object, use the following code:

PPOcrRecognizerResult* blinkInputRecognizerResult = (PPOcrRecognizerResult*)result;

// The name has to be the same as given in the PPOcrRecognizerSettings initialization
NSLog(@"Raw ocr: %@", [blinkInputRecognizerResult parsedResultForName:@"Raw ocr"]);

Available PPOcrParserFactory subclasses are listed in the following table.

Generic parsers

OcrParserFactory class Data Example
PPRawOcrParserFactory The whole text on the image ABC\n\n123.45\n123123
PPPriceOcrParserFactory Price 23,45
PPIbanOcrParserFactory IBAN DE89 3704 0044 0532 0130 00
PPDateOcrParserFactory Date 13.14.2015
PPEmailOcrParserFactory Email [email protected]
PPRegexOcrParserFactory User defined format User defined format
PPTopUpOcrParserFactory TopUp code *123*12345678912345#
PPVinOcrParserFactory Vin number 1ABCD36EF7G1234567

OCR Parser groups

Ocr Parsers are internally grouped in one or more parser groups. Each parser group uses one OCR engine pass on the image, using one set of OCR parameters (character whitelist, font list, etc.). This allows for reusing of OCR results between different parsers.

However, some parsers are intrinsically very different, for example, PPRawOcrParserFactory and PPPriceOcrParserFactory. Price parser requires OCR to recognize just digits, punctuation and currency sighs, while Raw ocr parser uses the whole character list. It makes sense to use these parsers in two separate groups.

To do that, use the following code:

// To specify we want to perform OCR recognition, initialize the OCR recognizer settings
PPBlinkInputRecognizerSettings *blinkInputRecognizerSettings = [[PPBlinkInputRecognizerSettings alloc] init];

// We want raw OCR parsing
[blinkInputRecognizerSettings addOcrParser:[[PPRawOcrParserFactory alloc] init] name:@"Raw OCR" group:@"Raw OCR group"];

// We want to parse prices from raw OCR result as well
[blinkInputRecognizerSettings addOcrParser:[[PPPriceOcrParserFactory alloc] init] name:@"Price" group:@"Price group"];

// Add the recognizer setting to a list of used recognizer
[settings.scanSettings addRecognizerSettings:blinkInputRecognizerSettings];

By default, when parser group is not specified, parsers are added in in default group with name "defaultParserGroup"

You can obtain Parser results from different groups using the following code:

PPBlinkInputRecognizerResult* ocrRecognizerResult = (PPBlinkInputRecognizerResult*)result;

// The name and groups have to be the same as given in the PPOcrRecognizerSettings initialization
NSLog(@"Raw ocr: %@", [ocrRecognizerResult parsedResultForName:@"Raw OCR" parserGroup:@"Raw OCR group"]);
NSLog(@"Price: %@", [ocrRecognizerResult parsedResultForName:@"Price" parserGroup:@"Price group"];
Clone this wiki locally