Skip to content

Commit 37e5940

Browse files
committed
Merge pull request #126 from BamX/default-resolution
Added default source resolution and clamping to min/max values.
2 parents ace7dfb + 3165853 commit 37e5940

File tree

6 files changed

+54
-5
lines changed

6 files changed

+54
-5
lines changed

TesseractOCR/G8Tesseract.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@
1111
#import "G8Constants.h"
1212
#import "G8TesseractDelegate.h"
1313

14+
/**
15+
* Default value of `sourceResolution` property.
16+
*/
17+
extern NSInteger const kG8DefaultResolution;
18+
19+
/**
20+
* Minimum alowed value of `sourceResolution` property.
21+
*/
22+
extern NSInteger const kG8MinCredibleResolution;
23+
24+
/**
25+
* Maximum alowed value of `sourceResolution` property.
26+
*/
27+
extern NSInteger const kG8MaxCredibleResolution;
28+
1429
/**
1530
* `G8Tesseract` encapsulates the Tesseract library and provides properties
1631
* and methods for performing text recognition and analysis on a target image.
@@ -95,7 +110,10 @@
95110

96111
/**
97112
* The resolution of the source image in pixels per inch so font size
98-
* information can be calculated in results. Change it after setting image.
113+
* information can be calculated in results. It should be from 70 to
114+
* 2400 or it will be clamped.
115+
*
116+
* @default Default value is 72
99117
*/
100118
@property (nonatomic, assign) NSInteger sourceResolution;
101119

TesseractOCR/G8Tesseract.mm

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#import "genericvector.h"
2323
#import "strngs.h"
2424

25+
NSInteger const kG8DefaultResolution = 72;
26+
NSInteger const kG8MinCredibleResolution = 70;
27+
NSInteger const kG8MaxCredibleResolution = 2400;
28+
2529
namespace tesseract {
2630
class TessBaseAPI;
2731
};
@@ -110,7 +114,7 @@ - (id)initWithLanguage:(NSString *)language
110114
_engineMode = engineMode;
111115
_pageSegmentationMode = G8PageSegmentationModeSingleBlock;
112116
_variables = [NSMutableDictionary dictionary];
113-
_sourceResolution = 300;
117+
_sourceResolution = kG8DefaultResolution;
114118
_rect = CGRectZero;
115119

116120
_monitor = new ETEXT_DESC();
@@ -393,7 +397,6 @@ - (void)setImage:(UIImage *)image
393397
pixDestroy(&pix);
394398

395399
_image = image;
396-
_sourceResolution = 300;
397400
_rect = (CGRect){CGPointZero, self.imageSize};
398401

399402
[self resetFlags];
@@ -439,6 +442,15 @@ - (void)setRect:(CGRect)rect
439442
- (void)setSourceResolution:(NSInteger)sourceResolution
440443
{
441444
if (_sourceResolution != sourceResolution) {
445+
if (sourceResolution > kG8MaxCredibleResolution) {
446+
NSLog(@"Source resolution is too big: %ld > %ld", (long)sourceResolution, (long)kG8MaxCredibleResolution);
447+
sourceResolution = kG8MaxCredibleResolution;
448+
}
449+
else if (sourceResolution < kG8MinCredibleResolution) {
450+
NSLog(@"Source resolution is too small: %ld < %ld", (long)sourceResolution, (long)kG8MinCredibleResolution);
451+
sourceResolution = kG8MinCredibleResolution;
452+
}
453+
442454
_sourceResolution = sourceResolution;
443455

444456
_tesseract->SetSourceResolution((int)sourceResolution);
@@ -801,7 +813,7 @@ - (Pix *)pixForImage:(UIImage *)image
801813
default:
802814
NSLog(@"Cannot convert image to Pix with bpp = %d", bpp);
803815
}
804-
pixSetYRes(pix, 300);
816+
pixSetYRes(pix, (l_int32)self.sourceResolution);
805817

806818
CFRelease(imageData);
807819

TestsProject/TestsProject/well_scaned_page.hOCR

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</span>
66
</p>
77
</div>
8-
<div class='ocr_carea' id='block_1_2' title="bbox 7 79 805 1022">
8+
<div class='ocr_carea' id='block_1_2' title="bbox 7 79 806 1022">
99
<p class='ocr_par' dir='ltr' id='par_1_2' title="bbox 9 79 804 251">
1010
<span class='ocr_line' id='line_1_2' title="bbox 53 79 804 102; baseline 0.003 -6"><span class='ocrx_word' id='word_1_2' title='bbox 53 79 95 96; x_wconf 85' lang='eng' dir='ltr'>This</span> <span class='ocrx_word' id='word_1_3' title='bbox 101 80 169 96; x_wconf 79' lang='eng' dir='ltr'><strong>volume</strong></span> <span class='ocrx_word' id='word_1_4' title='bbox 175 80 189 96; x_wconf 90' lang='eng' dir='ltr'><strong>is</strong></span> <span class='ocrx_word' id='word_1_5' title='bbox 196 80 224 97; x_wconf 89' lang='eng' dir='ltr'><strong>the</strong></span> <span class='ocrx_word' id='word_1_6' title='bbox 230 81 283 97; x_wconf 96' lang='eng' dir='ltr'><strong>result</strong></span> <span class='ocrx_word' id='word_1_7' title='bbox 289 81 307 97; x_wconf 79' lang='eng' dir='ltr'><strong>of</strong></span> <span class='ocrx_word' id='word_1_8' title='bbox 313 81 342 97; x_wconf 88' lang='eng' dir='ltr'><strong>the</strong></span> <span class='ocrx_word' id='word_1_9' title='bbox 348 81 455 101; x_wconf 65' lang='eng' dir='ltr'><strong>cooperative</strong></span> <span class='ocrx_word' id='word_1_10' title='bbox 462 81 513 97; x_wconf 61' lang='eng' dir='ltr'><strong>efl’ort</strong></span> <span class='ocrx_word' id='word_1_11' title='bbox 518 81 536 97; x_wconf 90' lang='eng' dir='ltr'><strong>of</strong></span> <span class='ocrx_word' id='word_1_12' title='bbox 541 87 594 102; x_wconf 79' lang='eng' dir='ltr'><strong>many</strong></span> <span class='ocrx_word' id='word_1_13' title='bbox 600 87 669 102; x_wconf 75' lang='eng' dir='ltr'><strong>persons</strong></span> <span class='ocrx_word' id='word_1_14' title='bbox 676 82 709 98; x_wconf 88' lang='eng' dir='ltr'><strong>and</strong></span> <span class='ocrx_word' id='word_1_15' title='bbox 716 87 726 98; x_wconf 87' lang='eng' dir='ltr'><strong>a</strong></span> <span class='ocrx_word' id='word_1_16' title='bbox 732 84 804 98; x_wconf 82' lang='eng' dir='ltr'><strong>number</strong></span>
1111
</span>

TestsProject/TestsProjectTests/G8RecognitionTestsHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ typedef NS_ENUM(NSUInteger, G8CustomPreprocessing) {
3131
@property (nonatomic, assign) CGSize boundingSizeForResizing;
3232
@property (nonatomic, assign) G8CustomPreprocessing customPreprocessingType;
3333

34+
- (void)setupTesseract;
3435
- (void)recognizeImage;
3536
- (void)recognizeImageUsingOperation;
3637
- (UIImage *)thresholdedImageForImage:(UIImage *)sourceImage;

TestsProject/TestsProjectTests/G8RecognitionTestsHelper.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ - (void)waitTimeLmit:(NSTimeInterval)maximumWait whileTrue:(BOOL (^)())shouldKee
5050

5151
- (void)setupTesseract
5252
{
53+
if (self.tesseract == nil) {
54+
self.tesseract = [[G8Tesseract alloc] init];
55+
}
56+
5357
self.tesseract.delegate = self;
5458

5559
self.tesseract.language = kG8Languages;

TestsProject/TestsProjectTests/RecognitionTests.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,20 @@
125125
[[theValue(block.level) should] equal:theValue(G8PageIteratorLevelWord)];
126126
});
127127

128+
it(@"Should clamp source resolution", ^{
129+
[helper setupTesseract];
130+
131+
[[theValue(helper.tesseract.sourceResolution) should] equal:theValue(kG8DefaultResolution)];
132+
133+
helper.tesseract.sourceResolution = 50;
134+
[[theValue(helper.tesseract.sourceResolution) should] beInTheIntervalFrom:theValue(kG8MinCredibleResolution)
135+
to:theValue(kG8MaxCredibleResolution)];
136+
137+
helper.tesseract.sourceResolution = 3000;
138+
[[theValue(helper.tesseract.sourceResolution) should] beInTheIntervalFrom:theValue(kG8MinCredibleResolution)
139+
to:theValue(kG8MaxCredibleResolution)];
140+
});
141+
128142
it(@"Should draw blocks on image", ^{
129143
[helper recognizeImage];
130144

0 commit comments

Comments
 (0)