Reference
OCR class¤
Performs OCR on a given image, saves an image with boxes around the words, and converts the extracted text to an MP3 file.
Add Tesseract OCR's installation location in PATH for functions using it to work.
Args:
preprocess:
Set True if the image is a real life photo of some large meaningful (page of
a book). Usually set to False when OCRing using `ocr_meaningful_text` to
preprocess the image.
Set False if the image is a scanned photo (an e-book). It will not be
pre-processed before OCRing.
Use the `Preprocessor` class manually to have more control!
path:
Path of the image to be used.
Examples:
>>> import sys
>>> sys.displayhook = lambda x: None
>>> import ocred
>>> ocr = ocred.OCR(
... False, # preprocess -> to preprocess the image
... "./images/Page.png"
... )
>>> ocr.ocr_meaningful_text(save_output=True)
Source code in ocred/ocr.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
|
ocr_meaningful_text(*, tesseract_config='-l eng --oem 1', preserve_orientation=False, save_output=False)
¤
Performs OCR on long meaningful text documents and saves the image with boxes around the words. For example - books, PDFs etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
tesseract_config |
str | None
|
Configuration passed down to the Tesseract OCR Engine. |
'-l eng --oem 1'
|
preserve_orientation |
bool | None
|
Preserves the orientation of OCRed text. |
False
|
save_output |
bool | None
|
Saves the text to |
False
|
Returns:
Name | Type | Description |
---|---|---|
text |
str
|
The extracted text. |
Source code in ocred/ocr.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
ocr_sparse_text(*, languages=['en', 'hi'], decoder='greedy', save_output=False)
¤
Performs OCR on sparse text and saves the image with boxes around the words. This method can be used to OCR documents in which the characters don't form any proper/meaningful sentences, or if there are very less meaningful sentences, for example - bills, sign-boards etc.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
languages |
list[str] | None
|
A list of languages that the signboard possible has. Note: Provide only the languages that are present in the image, adding additional languages misguides the model. |
['en', 'hi']
|
decoder |
str | None
|
If the document has a larger number of meaningful sentences then use "beamsearch". For most of the cases "greedy" works very well. |
'greedy'
|
save_output |
bool | None
|
Saves the text to |
False
|
Returns:
Name | Type | Description |
---|---|---|
text |
str
|
The extracted text. |
detailed_text |
Any
|
Text with extra information (returned by easyocr.Reader.readtext()). |
Source code in ocred/ocr.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
process_extracted_text_from_invoice()
¤
This method processes the extracted text from invoices, and returns some useful information.
Returns:
Name | Type | Description |
---|---|---|
extracted_info |
dict[str, Any]
|
The extracted information. |
Source code in ocred/ocr.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
|
save_output()
¤
Saves the extracted text in the output.txt
file.
Source code in ocred/ocr.py
281 282 283 284 285 286 287 |
|
text_to_speech(*, lang='en')
¤
Danger
Deprecated since version v0.2.0. Instead, use gTTS manually.
Converts the extracted text to speech and save it as an MP3 file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lang |
str | None
|
Language of the processed text. |
'en'
|
Source code in ocred/ocr.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
|
Preprocessor class¤
Preprocesses an image and makes it ready for OCR.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image |
str | NDArray[int64] | NDArray[float64]
|
Path of the image or a numpy array. |
required |
Examples:
>>> import sys
>>> sys.displayhook = lambda x: None
>>> import cv2
>>> from scipy import ndimage
>>> from ocred import Preprocessor
>>> # scan the image and copy the scanned image
>>> preprocessed = Preprocessor("images/CosmosTwo.jpg")
>>> # scan the image and copy the scanned image
>>> preprocessed.scan()
>>> orig = preprocessed.img.copy()
>>> # remove noise
>>> preprocessed.remove_noise()
>>> # thicken the ink to draw Hough lines better
>>> preprocessed.thicken_font()
>>> # calculate the median angle of all the Hough lines
>>> _, median_angle = preprocessed.rotate()
>>> # rotate the original scanned image
>>> rotated = ndimage.rotate(orig, median_angle)
>>> # remove noise again
>>> preprocessed = Preprocessor(rotated)
>>> preprocessed.remove_noise()
>>> cv2.imwrite("preprocessed.png", preprocessed.img)
True
Source code in ocred/preprocessing.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
remove_noise(*, save=False, inplace=None, iterations=1, overriden_image=None)
¤
Removes noise from an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
save |
bool | None
|
Saves the resultant image. |
False
|
iterations |
int | None
|
Number of times the image is processed. |
1
|
inplace |
bool | None | None
|
Danger Deprecated since version v0.3.0. Was intended to edit the image inplace, but never actually worked. |
None
|
overriden_image |
NDArray[int64] | NDArray[float64] | None
|
Danger Deprecated since version v0.3.0. Was used to pass a new image to the method but was redundant and buggy. |
None
|
Returns:
Name | Type | Description |
---|---|---|
noise_free_image |
NDArray[int64] | NDArray[float64]
|
The noise free image. |
Source code in ocred/preprocessing.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|
rotate(*, save=False, inplace=None, overriden_image=None)
¤
Rotates an image for a face-on view (view from the top).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
save |
bool | None
|
Saves the resultant image. |
False
|
inplace |
bool | None | None
|
Danger Deprecated since version v0.3.0. Was intended to edit the image inplace, but never actually worked. |
None
|
overriden_image |
NDArray[int64] | NDArray[float64] | None
|
Danger Deprecated since version v0.3.0. Was used to pass a new image to the method but was redundant and buggy. |
None
|
Returns:
Name | Type | Description |
---|---|---|
rotated_image |
NDArray[int64] | NDArray[float64]
|
The rotated image. |
median_angle |
float
|
The angly by which it is rotated. |
Source code in ocred/preprocessing.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
|
scan(*, save=False, inplace=None, overriden_image=None)
¤
Transforms an image/document view into B&W view (proper scanned colour scheme).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
save |
bool | None
|
Saves the resultant image. |
False
|
inplace |
bool | None | None
|
Danger Deprecated since version v0.3.0. Was intended to edit the image inplace, but never actually worked. |
None
|
overriden_image |
NDArray[int64] | NDArray[float64] | None
|
Danger Deprecated since version v0.3.0. Was used to pass a new image to the method but was redundant and buggy. |
None
|
Returns:
Name | Type | Description |
---|---|---|
scanned_image |
NDArray[int64] | NDArray[float64]
|
The scanned image. |
Source code in ocred/preprocessing.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
|
thicken_font(*, save=False, inplace=None, iterations=2, overriden_image=None)
¤
Thickens the ink of an image.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
save |
bool | None
|
Saves the resultant image. |
False
|
iterations |
int | None
|
Number of times the image is processed. |
2
|
inplace |
bool | None | None
|
Danger Deprecated since version v0.3.0. Was intended to edit the image inplace, but never actually worked. |
None
|
overriden_image |
NDArray[int64] | NDArray[float64] | None
|
Danger Deprecated since version v0.3.0. Was used to pass a new image to the method but was redundant and buggy. |
None
|
Returns:
Name | Type | Description |
---|---|---|
thickened_image |
NDArray[int64] | NDArray[float64]
|
The thickened image. |
Source code in ocred/preprocessing.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|