You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: vignettes/intro.Rmd
+77-28Lines changed: 77 additions & 28 deletions
Original file line number
Diff line number
Diff line change
@@ -23,17 +23,26 @@ if (grepl("tesseract.Rcheck", getwd())) {
23
23
}
24
24
```
25
25
26
-
The tesseract package provides R bindings [Tesseract](https://github.com/tesseract-ocr/tesseract): a powerful optical character recognition (OCR) engine that supports over 100 languages. The engine is highly configurable in order to tune the detection algorithms and obtain the best possible results.
26
+
The tesseract package provides R bindings [Tesseract](https://github.com/tesseract-ocr/tesseract):
27
+
a powerful optical character recognition (OCR) engine that supports over 100
28
+
languages. The engine is highly configurable in order to tune the detection
29
+
algorithms and obtain the best possible results.
27
30
28
-
Keep in mind that OCR (pattern recognition in general) is a very difficult problem for computers. Results will rarely be perfect and the accuracy rapidly decreases with the quality of the input image. But if you can get your input images to reasonable quality, Tesseract can often help to extract most of the text from the image.
31
+
Keep in mind that OCR (pattern recognition in general) is a very difficult
32
+
problem for computers. Results will rarely be perfect and the accuracy rapidly
33
+
decreases with the quality of the input image. But if you can get your input
34
+
images to reasonable quality, Tesseract can often help to extract most of the
35
+
text from the image.
29
36
30
37
## Extract Text from Images
31
38
32
-
OCR is the process of finding and recognizing text inside images, for example from a screenshot, scanned paper. The image below has some example text:
39
+
OCR is the process of finding and recognizing text inside images, for example
40
+
from a screenshot, scanned paper. The image below has some example text:
33
41
34
42

35
43
36
-
The `ocr()` function extracts text from an image file. After indicating the engine for the language, it will return the text found in the image:
44
+
The `ocr()` function extracts text from an image file. After indicating the
45
+
engine for the language, it will return the text found in the image:
37
46
38
47
```{r}
39
48
library(cpp11tesseract)
@@ -43,7 +52,8 @@ text <- ocr(file, engine = eng)
43
52
cat(text)
44
53
```
45
54
46
-
The `ocr_data()` function returns all words in the image along with a bounding box and confidence rate.
55
+
The `ocr_data()` function returns all words in the image along with a bounding
56
+
box and confidence rate.
47
57
48
58
```{r}
49
59
results <- ocr_data(file, engine = eng)
@@ -52,15 +62,21 @@ results
52
62
53
63
## Language Data
54
64
55
-
The tesseract OCR engine uses language-specific training data in the recognize words. The OCR algorithms bias towards words and sentences that frequently appear together in a given language, just like the human brain does. Therefore the most accurate results will be obtained when using training data in the correct language.
65
+
The tesseract OCR engine uses language-specific training data in the recognize
66
+
words. The OCR algorithms bias towards words and sentences that frequently
67
+
appear together in a given language, just like the human brain does. Therefore
68
+
the most accurate results will be obtained when using training data in the
69
+
correct language.
56
70
57
71
Use `tesseract_info()` to list the languages that you currently have installed.
58
72
59
73
```{r}
60
74
tesseract_info()
61
75
```
62
76
63
-
By default the R package only includes English training data. Windows and Mac users can install additional training data using `tesseract_download()`. Let's OCR a screenshot from Wikipedia in Simplified Chinese.
77
+
By default the R package only includes English training data. Windows and Mac
78
+
users can install additional training data using `tesseract_download()`. Let's
79
+
OCR a screenshot from Wikipedia in Simplified Chinese.
64
80
65
81

66
82
@@ -89,20 +105,34 @@ cat(text2)
89
105
90
106
## Preprocessing with Magick
91
107
92
-
The accuracy of the OCR process depends on the quality of the input image. You can often improve results by properly scaling the image, removing noise and artifacts or cropping the area where the text exists. See [tesseract wiki: improve quality](https://tesseract-ocr.github.io/tessdoc/ImproveQuality) for important tips to improve the quality of your input image.
93
-
94
-
The awesome [magick](https://cran.r-project.org/package=magick/vignettes/intro.html) R package has many useful functions that can be use for enhancing the quality of the image. Some things to try:
95
-
96
-
- If your image is skewed, use `image_deskew()` and `image_rotate()` make the text horizontal.
97
-
-`image_trim()` crops out whitespace in the margins. Increase the `fuzz` parameter to make it work for noisy whitespace.
98
-
- Use `image_convert()` to turn the image into greyscale, which can reduce artifacts and enhance actual text.
99
-
- If your image is very large or small resizing with `image_resize()` can help tesseract determine text size.
100
-
- Use `image_modulate()` or `image_contrast()` or `image_contrast()` to tweak brightness / contrast if this is an issue.
108
+
The accuracy of the OCR process depends on the quality of the input image. You
109
+
can often improve results by properly scaling the image, removing noise and
110
+
artifacts or cropping the area where the text exists.
111
+
See [tesseract wiki: improve quality](https://tesseract-ocr.github.io/tessdoc/ImproveQuality)
112
+
for important tips to improve the quality of your input image.
113
+
114
+
The awesome [magick](https://cran.r-project.org/package=magick/vignettes/intro.html)
115
+
R package has many useful functions that can be use for enhancing the quality of
116
+
the image. Some things to try:
117
+
118
+
- If your image is skewed, use `image_deskew()` and `image_rotate()` make the
119
+
text horizontal.
120
+
-`image_trim()` crops out whitespace in the margins. Increase the `fuzz`
121
+
parameter to make it work for noisy whitespace.
122
+
- Use `image_convert()` to turn the image into greyscale, which can reduce
123
+
artifacts and enhance actual text.
124
+
- If your image is very large or small resizing with `image_resize()` can help
125
+
tesseract determine text size.
126
+
- Use `image_modulate()` or `image_contrast()` or `image_contrast()` to tweak
127
+
brightness / contrast if this is an issue.
101
128
- Try `image_reducenoise()` for automated noise removal. Your mileage may vary.
102
-
- With `image_quantize()` you can reduce the number of colors in the image. This can sometimes help with increasing contrast and reducing artifacts.
103
-
- True imaging ninjas can use `image_convolve()` to use custom [convolution methods](https://ropensci.org/technotes/2017/11/02/image-convolve/).
129
+
- With `image_quantize()` you can reduce the number of colors in the image.
130
+
This can sometimes help with increasing contrast and reducing artifacts.
131
+
- True imaging ninjas can use `image_convolve()` to use custom
Below is an example OCR scan. The code converts it to black-and-white and resizes + crops the image before feeding it to tesseract to get more accurate OCR results.
134
+
Below is an example OCR scan. The code converts it to black-and-white and
135
+
resizes + crops the image before feeding it to tesseract to get more accurate OCR results.
106
136
107
137

108
138
@@ -123,7 +153,9 @@ cat(text)
123
153
124
154
## Read from PDF files
125
155
126
-
If your images are stored in PDF files they first need to be converted to a proper image format. We can do this in R using the `pdf_convert` function from the pdftools package. Use a high DPI to keep quality of the image.
156
+
If your images are stored in PDF files they first need to be converted to a
157
+
proper image format. We can do this in R using the `pdf_convert` function from
158
+
the `cpp11poppler` package. Use a high DPI to keep quality of the image.
127
159
128
160
```{r, eval=require(cpp11poppler)}
129
161
library(cpp11poppler)
@@ -135,24 +167,32 @@ cat(text)
135
167
136
168
## Tesseract Control Parameters
137
169
138
-
Tesseract supports hundreds of "control parameters" which alter the OCR engine. Use `tesseract_params()` to list all parameters with their default value and a brief description. It also has a handy `filter` argument to quickly find parameters that match a particular string.
170
+
Tesseract supports hundreds of "control parameters" which alter the OCR engine
171
+
Use `tesseract_params()` to list all parameters with their default value and a
172
+
brief description. It also has a handy `filter` argument to quickly find
173
+
parameters that match a particular string.
139
174
140
175
```{r}
141
176
# List all parameters with *colour* in name or description
142
177
tesseract_params("colour")
143
178
```
144
179
145
-
Do note that some of the control parameters have changed between Tesseract engine 3 and 4.
180
+
Do note that some of the control parameters have changed between Tesseract
181
+
engine 3 and 4.
146
182
147
183
```{r}
148
184
tesseract_info()["version"]
149
185
```
150
186
151
187
### Whitelist / Blacklist characters
152
188
153
-
One powerful parameter is `tessedit_char_whitelist` which restricts the output to a limited set of characters. This may be useful for reading for example numbers such as a bank account, zip code, or gas meter.
189
+
One powerful parameter is `tessedit_char_whitelist` which restricts the output
190
+
to a limited set of characters. This may be useful for reading for example
191
+
numbers such as a bank account, zip code, or gas meter.
154
192
155
-
The whitelist parameter works for all versions of Tesseract engine 3 and also engine versions 4.1 and higher, but unfortunately it did not work in Tesseract 4.0.
193
+
The whitelist parameter works for all versions of Tesseract engine 3 and also
194
+
engine versions 4.1 and higher, but unfortunately it did not work in Tesseract
195
+
4.0.
156
196
157
197

158
198
@@ -182,7 +222,10 @@ cat(text)
182
222
183
223
## Best versus Fast models
184
224
185
-
In order to improve the OCR results, Tesseract has two variants of models that can be used. The `tesseract_download()` can download the 'best' (but slower) model, which increases the accuracy. The 'fast' (but less accurate) model is the default.
225
+
In order to improve the OCR results, Tesseract has two variants of models that
226
+
can be used. The `tesseract_download()` can download the 'best' (but slower)
227
+
model, which increases the accuracy. The 'fast' (but less accurate) model is the
The `tesseract_contributed_download()` function can download contributed models. For example, the `grc_hist` model is useful for Polytonic Greek. Here is an example from Sophocles' Ajax (source: [Ajax Multi-Commentary](https://github.com/AjaxMultiCommentary))
248
+
The `tesseract_contributed_download()` function can download contributed models.
249
+
For example, the `grc_hist` model is useful for Polytonic Greek. Here is an
250
+
example from Sophocles' Ajax (source: [Ajax Multi-Commentary](https://github.com/AjaxMultiCommentary))
One way to organize the output is to split the text before the first digit on each line.
310
+
One way to organize the output is to split the text before the first digit on
311
+
each line.
266
312
267
313
```{r}
268
314
text <- strsplit(text, "\n")[[1]]
@@ -298,4 +344,7 @@ for (i in seq_along(text)) {
298
344
head(df)
299
345
```
300
346
301
-
The result is not perfect (e.g. I still need to change "Gross National Product in Constant" to add the "(1958) Dollars"), but neither is Textract's and it requires to write a more complex loop to organize the data. Certainly, this can be simplified by using the Tidyverse.
347
+
The result is not perfect (e.g. I still need to change "Gross National Product
348
+
in Constant" to add the "(1958) Dollars"), but neither is Textract's and it
349
+
requires to write a more complex loop to organize the data. Certainly, this can
0 commit comments