From 12f765ca3ea5e0b942793ff13bc4b562410e4240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ju=CC=88rgen=20Mayer?= Date: Thu, 5 Dec 2019 17:28:51 +0100 Subject: [PATCH] fixed crash when accessing characterChoices on empty image --- TesseractOCR/G8Tesseract.mm | 38 +++++++++++++++++++------------------ Tests/RecognitionTests.m | 15 +++++++++++++++ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/TesseractOCR/G8Tesseract.mm b/TesseractOCR/G8Tesseract.mm index a6de8a4f..b80bbb5e 100644 --- a/TesseractOCR/G8Tesseract.mm +++ b/TesseractOCR/G8Tesseract.mm @@ -811,7 +811,7 @@ - (G8HierarchicalRecognizedBlock *)hierarchicalBlockFromIterator:(tesseract::Res block.isNumeric = iterator->WordIsNumeric(); block.isBold = isBold; block.isItalic = isItalic; - } else if (iteratorLevel == G8PageIteratorLevelSymbol) { + } else if (iteratorLevel == G8PageIteratorLevelSymbol && block.text != nil) { // get character choices NSMutableArray *choices = [NSMutableArray array]; @@ -847,24 +847,26 @@ - (NSArray *)characterChoices if (resultIterator != NULL) { do { G8RecognizedBlock *block = [self blockFromIterator:resultIterator iteratorLevel:G8PageIteratorLevelSymbol]; - NSMutableArray *choices = [NSMutableArray array]; - - tesseract::ChoiceIterator choiceIterator(*resultIterator); - do { - const char *choiceWord = choiceIterator.GetUTF8Text(); - if (choiceWord != NULL) { - NSString *text = [NSString stringWithUTF8String:choiceWord]; - CGFloat confidence = choiceIterator.Confidence(); - - G8RecognizedBlock *choiceBlock = [[G8RecognizedBlock alloc] initWithText:text - boundingBox:block.boundingBox - confidence:confidence - level:G8PageIteratorLevelSymbol]; - [choices addObject:choiceBlock]; - } - } while (choiceIterator.Next()); + if(block.text != nil) { + NSMutableArray *choices = [NSMutableArray array]; + + tesseract::ChoiceIterator choiceIterator(*resultIterator); + do { + const char *choiceWord = choiceIterator.GetUTF8Text(); + if (choiceWord != NULL) { + NSString *text = [NSString stringWithUTF8String:choiceWord]; + CGFloat confidence = choiceIterator.Confidence(); + + G8RecognizedBlock *choiceBlock = [[G8RecognizedBlock alloc] initWithText:text + boundingBox:block.boundingBox + confidence:confidence + level:G8PageIteratorLevelSymbol]; + [choices addObject:choiceBlock]; + } + } while (choiceIterator.Next()); - [array addObject:[choices copy]]; + [array addObject:[choices copy]]; + } } while (resultIterator->Next(tesseract::RIL_SYMBOL)); delete resultIterator; } diff --git a/Tests/RecognitionTests.m b/Tests/RecognitionTests.m index 128d4724..6b435bff 100644 --- a/Tests/RecognitionTests.m +++ b/Tests/RecognitionTests.m @@ -229,6 +229,13 @@ helper.customPreprocessingType = G8CustomPreprocessingSimpleThreshold; }); + it(@"Should not crash when accessing character choices", ^{ + [helper recognizeImage]; + [[theBlock(^{ + [helper.tesseract characterChoices]; + }) shouldNot] raise]; + }); + it(@"Should recognize nothing", ^{ [helper recognizeImage]; @@ -358,6 +365,14 @@ return [helper.tesseract recognizedHierarchicalBlocksByIteratorLevel:level]; }; + it(@"Should not crash with empty image", ^{ + [[theBlock(^{ + helper.image = [XPlatformImage imageWithName:@"image_blank.png"]; + [helper recognizeImage]; + [helper.tesseract recognizedHierarchicalBlocksByIteratorLevel:G8PageIteratorLevelBlock]; + }) shouldNot] raise]; + }); + it(@"Should recognize structure of sample image", ^{ NSArray *blocks = recognizedHierarchicalBlocksForImageWithName(@"image_sample.jpg", G8PageIteratorLevelBlock); [[[blocks should] haveAtLeast:1] items];