Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed crash when accessing characterChoices on empty image #416

Open
wants to merge 1 commit into
base: macos-support
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions TesseractOCR/G8Tesseract.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions Tests/RecognitionTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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];
Expand Down