PDF OCR#
The PDF OCR (optical character recognition) step converts images of text embedded in PDF files into machine-encoded text. Images of text are typically found in PDFs of scanned documents.
Overview#
The PDF OCR step extracts text from files of MIME type application/pdf that don’t contain any machine-readable text.
The Content Augmentation step needs to run before and the Content Extraction (formerly Content Conversion) step after the PDF OCR step.
Configuration#
Enabling redo_ocr or force_ocr will apply the corresponding settings to the tool used for OCR. More information about these settings can be found in the ocrmypdf documentation.
Field |
Default |
Description |
|
True |
Replace original PDF file with a PDF file containing the extracted text overlay. The original file stays in storage until the ingester has finished the batch of the item, and a rerun from raw data processes the OCRed copy. See Stored Files. |
|
1800 |
Maximum wall-clock time in seconds for the OCR of one PDF file. The CPU budget and the no-progress limit stop an OCR that does not finish long before that. See OCR Limits. |
|
30 |
CPU seconds per page that the OCR of a PDF file can use, on top of 60 CPU seconds for the file, before it is stopped. |
|
(empty) |
Number of OCR jobs that run in parallel for each PDF file, as a whole number from 1 to 256. If empty, the CPUs that the ingester may use, divided by the threads per page that |
|
(empty) |
Minimum resolution, in DPI, at which page images are OCRed, as a whole number from 1 to 600, for example |
|
False |
Straightens each page before OCR. OCR takes longer, and the OCRed PDF file holds the straightened page images. Not applied with |
|
False |
Applies OCR on every page, including those that already have text. It replaces any existing text layers with OCR text. Aims to create a single, clean, and hopefully accurate text layer, in the expense of potentially increased file size and processing time. |
|
False |
Applies OCR on every page, including those that already have text. It layers new OCR text over any existing text, without replacing it. It ensures that all the elements in the PDF are OCR’d, however, it can significantly increase file size, and increase processing time. Force OCR takes precedense over Redo OCR when both options are enabled. |
|
False |
Annotate document with informative OCR confidence score. Enabling this may slow down OCR processing and even influence its results. Unless explictly needed, it is recommended to leave this disabled. |
|
True |
When OCR fails for a PDF file, the step logs the error, leaves the file unchanged, and succeeds. Set to |
|
(empty/eng) |
Tesseract language code for OCR text recognition. Defaults to English ( |
Parallel OCR Jobs#
By default, cpu_jobs is empty, and each PDF file is OCRed with as many parallel jobs as the CPUs that the ingester may use, divided by the threads per page that the OMP_THREAD_LIMIT environment variable of the ingester sets, if it is set to a number, and at least 1. The CPUs that the ingester may use are those of its container or CPU set, and no more than its CPU limit, rounded up. This keeps an OCR run from starting more processes and threads than those CPUs, which would make it block other processes and use more memory. The OCR tool on its own starts one job for every CPU of the server, also in a container with fewer CPUs. The default is at most 256, the most the OCR tool accepts. If the CPU limit of the ingester cannot be read, the default follows the CPUs of its container or CPU set alone, and the log says so with a warning, once for each process of the ingester that runs a processor. Set cpu_jobs in that case. To set another number of parallel jobs, set cpu_jobs to a whole number from 1 to 256. To return to the default, clear the value.
The value limits the parallel jobs of each OCR run, not the CPU use of the server. Each PDF file is OCRed in its own run, and several items can be processed at the same time, so the total CPU use can be higher than cpu_jobs cores.
Fewer jobs can make the OCR of a PDF file take longer. ocr_timeout stays unchanged when you set cpu_jobs.
With cpu_jobs below the default, a long scan also takes a smaller share of the OCR capacity, so more long scans run at the same time, each on fewer CPUs, as described in Concurrent OCR Runs. Each of them still needs the memory of a long scan.
If cpu_jobs is set to anything other than a whole number from 1 to 256, for example 0, -1, or 2.5, the step OCRs no file and reports an error for every item that reaches it, including items without PDF files, whatever the value of ignore_ocr_errors. The error log names cpu_jobs and the value. The items are then retried as described in Reporting OCR Errors, but retries cannot fix an invalid value. Correct or clear cpu_jobs.
Concurrent OCR Runs#
The ingester processes several items at the same time: each of its processors up to the workers of its [processor] section, 3 in the shipped configuration (see Pipeline Overview), whatever the number of its CPUs. A long scan OCRs as many pages at the same time as the CPUs that the ingester may use, while a PDF file of one page uses one CPU. More long scans at the same time than about half the CPUs only slow each other down, until they reach their ocr_timeout. The CPUs that the ingester may use are those of its container or CPU set, and no more than its CPU limit, rounded up.
By default, the ingester therefore shares its CPUs among the OCR runs of all its processors as follows:
Each OCR run takes a share of the OCR capacity, counted in OCR units: one for each CPU it can keep busy, which with one thread per page, the default, is the pages it can OCR at the same time, at most the CPUs. A PDF file of one page takes one unit. A PDF file whose pages cannot be counted, for example because it is damaged or needs a password, takes the share of a long scan.
The capacity is the CPUs divided by 2, at least 1, times the share of a long scan. With long scans only, that many run at the same time.
Shorter PDF files use the rest. For example, with 2 CPUs, one long scan runs alone, or two PDF files of one page run at the same time. With 4 CPUs, two long scans run at the same time, or one long scan and three PDF files of one page.
At most as many OCR runs as CPUs run at the same time, or more if
pdf_ocr_max_concurrent_runsis higher, because each OCR run needs memory, about 250 MB for a photo of one page.OCR runs start in the order in which they asked, so a long scan is not held back by shorter PDF files that arrive after it. A short PDF file can wait while a long scan before it waits for CPUs.
A PDF file of two or more pages that already has text, such as one created from an office document or one OCRed before, does not wait. Unless
force_ocrorredo_ocrisTrue, the OCR tool skips a PDF file with text on any page and leaves it unchanged. The step therefore first asks the OCR tool, on one CPU and without a share of the capacity, whether it would skip the file, and skips it if so. The OCR tool reads the file without OCRing any page, which takes about half a second for a file of a few pages and less than a second for 300 pages. If it has no answer within 20 seconds, or fails, the file waits for its share like any other, and the step logs the reason. A PDF file of one page takes one unit and is skipped by the OCR tool when its turn comes.A tagged PDF file, as office applications usually create, does not wait either. Unless
force_ocrorredo_ocrisTrue, the OCR tool refuses a PDF file that is marked as tagged, whatever its pages hold, and the step skips it and leaves it unchanged, as it does a PDF file with text. The step finds the mark without reading any page, and asks the OCR tool first, as for a file with text, whatever the number of pages. If the OCR tool fails the file for another reason, for example a language it does not have, the file fails as it would without the limit.
Other pipeline steps are not limited.
A PDF file whose OCR waits for its turn does not use its ocr_timeout or its OCR Limits while it waits, and does not use temporary storage yet. A wait of 10 seconds or more is logged while it lasts, after 10, 20, 40 seconds and so on, at least every 10 minutes, and again when the OCR starts, for example:
PDF OCR of 'storage://...': still waiting after 20 s for 2 of 2 OCR units
PDF OCR of 'storage://...': waited 42.0 s for 2 of 2 OCR units
If the workflow runs the step with "execution": "batch", the wait counts against the step’s timeout_seconds, as the OCR itself does.
The ingester keeps track of the OCR runs in the slots directory of the pdf_ocr directory in the current_state_directory of its [processor] section, which only the user of the ingester can access. If the step cannot use that directory, it runs the OCR without the limit and logs a warning, once for each process of the ingester, when that process first OCRs a PDF file. Items without PDF files do not use the slots directory. If the slots directory or its files fail again in the same process, each further PDF file that runs without the limit is also logged at INFO. If the pdf_ocr directory itself cannot be used, the two warnings about the slots and the temporary storage, once for each process, are the only lines. The limit applies to the processes of one ingester container, or of one host for an RPM installation. As for the temporary storage described below, the current_state_directory must belong to one host: the capacity counts the CPUs of that host, so hosts that share the directory would share the capacity of one.
To set another number of long scans at the same time, set pdf_ocr_max_concurrent_runs in the [pipeline] section of /etc/squirro/common.ini to a whole number, or to 0 for no limit, and restart the sqingesterd service as described on the Services page. For Docker-based deployments, set the SQ_PIPELINE_PDF_OCR_MAX_CONCURRENT_RUNS environment variable instead. The number replaces the CPUs divided by 2 in the capacity, and shorter PDF files still use the rest. Any other value makes the step report an error for every item that reaches it, until it is corrected. The setting applies to all projects of the instance.
[pipeline]
pdf_ocr_max_concurrent_runs = 4
Oversampling and Deskewing#
Low-resolution or crooked scans can lose characters in OCR, and digits, such as those of amounts, are often the first to go. The text around them can still look correct. Two options can recover such characters. Both are off by default, and both make OCR take longer.
oversample_dpisets the minimum resolution at which page images are OCRed. Page images with a lower resolution are upsampled to it before OCR, and page images that already have that resolution or more are OCRed as they are.300is a common value for scanned text.deskewstraightens each page before OCR. The angle is detected from the lines of text, so a page with only a few lines can stay as it is.
Measure the effect on a sample of your own documents before you turn on either option for a whole project, as the gain and the cost depend on the scans. Either option can also make other parts of the OCR text worse while it recovers characters, for example deskew for photos of pages taken with a phone.
Cost#
OCR time
Upsampling multiplies the pixels to OCR by the square of the factor: from 100 to 300 DPI, nine times as many. On low-resolution scans, OCR then needs about two to three times the CPU time per page, and more with
deskewor on noisy color scans.deskewalone adds a smaller amount of time to every page.A PDF file whose OCR takes longer than
ocr_timeoutis left unchanged, or fails the step ifignore_ocr_errorsisFalse. Increaseocr_timeoutfor long documents when you turn onoversample_dpi. As every OCR run needs more CPU time, the same server also OCRs fewer pages per hour.OCRed PDF file
Without
deskewandforce_ocr, the OCRed PDF file keeps the original page images, andoversample_dpionly changes the image that is OCRed. Withdeskew, as withforce_ocr, the OCRed PDF file holds the page images as they were OCRed: straightened, and at theoversample_dpiresolution if that is set. The OCRed PDF file can then be several times larger than the original. This applies to the stored file ifreplace_fileisTrue.Temporary storage
The intermediate files of each OCR run grow with the resolution and with the straightened page images. With both options, a scan of about 50 pages can need more than 1 GB of temporary storage while it is OCRed. See Temporary Storage.
Redo OCR and Force OCR#
The OCR tool does not support deskew together with redo_ocr. If both are True and force_ocr is False, the step OCRs every PDF file without deskewing, as with redo_ocr alone, and logs a warning that names both options for each PDF file. force_ocr takes precedence over redo_ocr, so if force_ocr is True, deskew applies. oversample_dpi applies with either option.
Invalid Values#
If oversample_dpi or deskew has an invalid value, the step OCRs no file and reports an error for every item that reaches it, including items without PDF files, whatever the value of ignore_ocr_errors. A value is invalid in these cases:
oversample_dpiis anything other than a whole number from 1 to 600, for example0,601, or300dpi. An empty value and the JSON valuenullare valid and mean the same as leaving the option out.deskewis anything other than the JSON valuestrue,false, ornull, for example the text"true".nullmeans the same asfalse.
The error log names the option and the value. The items are then retried as described in Reporting OCR Errors, but retries cannot fix an invalid value. Correct or clear the option.
Reporting OCR Errors#
By default, a PDF file that cannot be OCRed is left unchanged and the step succeeds. If ignore_ocr_errors is False, the step reports an error for the item instead when one of the following happens to a file:
The file is a PDF file and OCR fails, for example because the file is password protected or one of the OCR Limits is reached.
The file content cannot be read from storage, and the file is a PDF file or of unknown type. This covers read errors that the storage reports as file errors, such as a missing file in the local file storage. Other read errors, such as errors returned by the Amazon S3 service, are not handled by the step: the item is retried, whatever the value of
ignore_ocr_errors.replace_fileisTrueand the OCRed file cannot be stored.
PDF files that already contain text, tagged PDF files, PDF files in which OCR finds no text, and files that are not PDF files are not failures.
When the step reports an error, the item keeps its original PDF files in storage, its file URLs, and its keywords. The OCRed files that the step already stored for the item are deleted after the ingester finishes the batch, because no item references them, as described in Stored Files. The item is retried with increasing waits, and every attempt reads the original files from storage again and repeats the OCR work done before the failure, so an item that keeps failing adds OCR processing time and cost on each attempt.
With the default max_retries = 10 in the [processor] section of ingester.ini, an item whose PDF file keeps failing, such as a password-protected one, moves to the failed batch after 11 attempts over about a day.
If ignore_ocr_errors is False and replace_file is True, the step stores the OCRed files of an item only after every PDF file of the item is OCRed.
Stored Files#
If replace_file is True, the step stores the OCRed copy of each PDF file next to the original file, and the item refers to the OCRed copy. The original file stays in storage until the ingester has finished the batch of the item, that is, until the item is indexed, queued for a retry, moved to the failed batches, or handed to another workflow by the Change Pipeline step. The ingester then deletes the original file. If replace_file is False, the step deletes no file.
If the ingester stops while it processes a batch, for example because its process is killed when memory runs out, the batch is processed again from the original files, and the OCRed copies that the stopped run made are deleted once nothing uses them. A retry of a failed batch deletes the original files of the items it indexes, as a batch that did not fail does, unless kept data still refers to them, as described below.
Some files stay in storage when the ingester cannot tell whether something still refers to them. For example:
an OCRed copy of a run that stopped right after it stored the copy, or whose items were then moved to the failed batches or continued in another batch;
one OCRed copy of a PDF file when a retry of the failed batches and a rerun of the same data source from raw data process the same batch at the same time;
the original file when both of those runs stop, because the ingester queues their batches again under one name and only one of them runs again. The same happens if you start the retry and the rerun within the same hour, before the ingester picks up the first of them;
the files that a finished batch made obsolete, if the process stops at the exact moment it records that the batch finished.
If the ingester stops after it finished a batch but before it deleted the files, the periodic cleanup deletes them later. The clean_up_interval_in_hours setting in the [ingester] section of ingester.ini sets how often it runs, every hour by default. If the file storage reports an error when a file is deleted, the cleanup tries again each time it runs, until the retention of failed batches has passed (days_to_retain_failed_batches and hours_to_retain_failed_batches, 30 days by default). It then logs an error that names the files it keeps. The local file storage does not report such an error: it logs a warning, and the file stays in storage.
An item that a step removes from the batch before the Indexing step, as the Deduplication step does with duplicate items, keeps its original file, and its OCRed copy is deleted. An item that a step after the Indexing step removes keeps the OCRed copy it was indexed with. An item that the Change Pipeline step hands to another workflow keeps its OCRed copy. Only the Change Pipeline step counts as handing items on: if a pipelet before the Indexing step sends items elsewhere and then removes them from the batch, what it sent refers to an OCRed copy that is deleted.
If keep_processed_data is true in the [content_filesystem_stream] section of /etc/squirro/common.ini, which is the default, the ingester keeps the data of each batch that a data loader delivered once it has processed the batch, so that you can rerun the pipeline workflow from raw data, as described on the Pipeline Reruns page. A rerun from raw data keeps the data again. The batches that the ingester creates to retry items, failed batches that you retry, and the batches that a rerun from index or the Change Pipeline step creates are not kept. For each PDF file that the step replaced, the kept data refers to the OCRed copy that the item was indexed with, queued for a retry with, moved to the failed batches with, or handed to another workflow with. The original file is deleted as described above and is not kept for a rerun. In rare cases, the ingester cannot tell which OCRed copy belongs to an item, for example if a step before the PDF OCR step changes the item ID and several OCRed copies replaced the same original file. It then keeps the original file in storage, and the kept data refers to it.
A rerun from raw data of kept data therefore starts from the OCRed copy, not from the original file:
Unless
redo_ocrorforce_ocrisTrue, the step skips the OCRed copy because it already has text, and the rerun indexes the item with the same copy. A changed OCR setting, for example another OCR language, has no effect on such a rerun.With
redo_ocrorforce_ocr, the step OCRs the OCRed copy again, and the rerun indexes the item with the new copy and deletes the previous one. If the rerun does not index the item, for example because the Indexing step fails it and it is queued for a retry or moved to the failed batches, the previous copy stays, because the index and the kept data still refer to it. The next rerun that indexes the item deletes it. The OCR works on the output of the previous run, not on the original file: withforce_ocr, each rerun OCRs the page images that the previous run produced.Data that earlier versions of the ingester kept still refers to the original files that they deleted. A rerun from raw data of such data fails for the items whose PDF files were OCRed. To process those items again, load them again from their data source.
With kept data, a file that an item takes to a retry, to the failed batches, or to another workflow stays in storage permanently, so that neither a rerun from raw data nor the later batch removes a file that the other still refers to. This also applies if only another workflow has the PDF OCR step and the item reaches it, directly or from a retry or a failed batch: the original file that the item takes there stays, and the kept data still refers to it. How many extra files stay depends on the OCR mode and on which workflows have the PDF OCR step:
Without
redo_ocrandforce_ocr, an item that takes its OCRed copy to a later batch leaves no extra file, because no run replaces that copy. An item that takes its original file, for example because its OCR failed and it was queued for a retry, leaves the original file and, for each run in which it leaves with that file, the OCRed copy that its retry indexed, once a rerun from raw data indexes the item again. For example, an item whose OCR fails once leaves two extra files for each PDF file, and one more each time its OCR fails again on a rerun.With
redo_ocrorforce_ocr, every run OCRs the copy again, and the replaced copies that an item took to a later batch stay. An item that leaves the batch once leaves up to two extra files for each PDF file. For an item that leaves the batch on every run, each rerun from raw data adds two files for each PDF file if the item is queued for a retry or handed to a workflow that also has the PDF OCR step, and one file if it is handed to a workflow without that step. Storage then grows with every rerun, for example for a pipeline workflow whose Change Pipeline step hands on every item after the PDF OCR step.If only the other workflow has the PDF OCR step, every rerun from raw data hands on the original file again, and the other workflow stores a new OCRed copy of it, in either OCR mode. Each rerun then adds one file for each PDF file: the copy that the other workflow indexed before.
If the ingester cannot update the kept data of a batch, for example because the disk is full, it keeps the data unchanged and deletes none of the files that the batch made obsolete, original files included, so that a rerun from raw data still finds every file. The ingester then logs the error Could not rewrite data file … with the path of the data file.
Each data directory of the ingester, /var/lib/squirro/inputstream by default, must belong to one ingester service. If several ingester services share a data directory, for example on a network file system, each one takes the batches that the others are processing for abandoned and processes them again at the same time. With the PDF OCR step, an index document can then refer to an OCRed copy that has already been deleted, so the PDF file of that item cannot be opened.
The ingester records which stored files each batch creates and replaces in the stored_file_ledgers directory inside each of its data directories, which it creates itself, and keeps the record of a batch while the batch can still be processed again. The ingester must be able to write to that directory. If it cannot, a batch with the PDF OCR step that has to queue an item for a retry or hand it to another workflow is processed again on every pass, and each pass leaves one more OCRed copy in storage. The ingester logs the error Unable to write the stored-file ledger … with the path of the ledger file. Once the directory can be written again, the batch finishes on its next pass.
Peak use of the file storage includes both the original and the OCRed copy of every PDF file in the batches that the ingester is processing. The temporary storage described in Temporary Storage is not affected.
Temporary Storage#
The PDF OCR step writes each PDF file and its intermediate files to temporary storage while it processes the file. The space needed grows with the size and page count of the PDF file: a scan of 200 pages needed about 0.9 GB while it was OCRed, a photo of one page about 60 MB, and a clean scan of 10 pages about 6 MB. PDF files that are OCRed at the same time need that space at the same time. By default, the step uses the pdf_ocr directory in the current_state_directory of the [processor] section of the ingester configuration, and creates it if needed, but not the current_state_directory itself. For an RPM installation, that is the /var/lib/squirro/ingester/pdf_ocr directory by default, as /etc/squirro/ingester.ini sets current_state_directory = /var/lib/squirro/ingester. Docker-based deployments keep that directory on a disk-backed volume, while the /tmp directory of their containers is a small file system in memory. If the current_state_directory does not exist, if the step cannot create or write the pdf_ocr directory, or if the directory belongs to another user, can be written by other users or is a symbolic link, it uses the default temporary directory of the service that runs the pipeline instead and logs a warning, once for each process of the service, when that process first OCRs a PDF file. Items without PDF files do not use the directory.
The step makes the pdf_ocr directory accessible to the user of the ingester only. If a process of the ingester is killed while it OCRs a PDF file, for example by the operating system when memory runs out, the files of that OCR stay in the pdf_ocr directory, and a restart does not remove them. The next OCR run removes them and logs how many directories it removed. It never removes the files of an OCR run whose process of the ingester is still running. The OCR tool that a killed process started stops within seconds, so it does not keep using CPUs, and its result is lost. Only a copy of the killed process that native code, for example a library that a pipelet uses, created during the OCR keeps the OCR tool running until that copy ends.
While a run removes the files that killed processes left, which takes moments, other OCR runs wait before they write their own files to the pdf_ocr directory. A wait of 10 seconds or more is logged as a warning while it lasts. A run waits at most 5 minutes, the time that the step allows an OCR run without progress. After that, for example if the disk stops responding, the OCR of that PDF file fails and the file is left unchanged. Unlike when the step cannot create or write the pdf_ocr directory, it does not use the default temporary directory instead. The error log names the .staging.lock file in the pdf_ocr directory. If ignore_ocr_errors is False, the step reports an error for the item, which is then retried as described in Reporting OCR Errors.
The current_state_directory must belong to one host. The lock that marks the files of a running OCR as in use works between the processes of one host only. If the ingesters of several hosts share the directory, for example on a network file system, an OCR run on one host can remove the files of an OCR that is still running on another. Give each host its own current_state_directory.
To keep all temporary files of the PDF OCR step in a dedicated location, for example a larger volume, set pdf_ocr_temp_directory to the absolute path of that location in the [pipeline] section of /etc/squirro/common.ini. Leave the value empty to use the default location.
[pipeline]
pdf_ocr_temp_directory = /data/pdf-ocr-tmp
For Docker-based deployments, set the SQ_PIPELINE_PDF_OCR_TEMP_DIRECTORY environment variable instead of editing common.ini directly.
Restart the sqingesterd service for the change to take effect. For instructions, see the Services page.
The setting applies to all projects of the instance. It is not part of the step configuration.
The step removes its temporary files from the directory after it processes each PDF file. The removal is best effort, so files can remain in the directory. Monitor the free space of the directory.
Unlike in the default location, the step does not remove the files that killed processes leave in this directory, because other services or hosts can share it. To remove them, stop the sqingesterd service and delete the directories whose names start with sq_ocr.
Note
The directory must already exist and be writable by the service that runs the pipeline. The step does not create the directory and does not fall back to the default location. If the directory is missing or not writable, OCR fails and the PDF files are left unchanged. The error log names the pdf_ocr_temp_directory setting and the path. By default, the step tries every PDF file. If ignore_ocr_errors is False, the step stops at the first PDF file and reports an error for the item, which is then retried as described in Reporting OCR Errors.
CPU Priority#
By default, the PDF OCR step lowers the CPU priority of an OCR run in two steps as the run uses CPU time, so that while the CPUs are busy, PDF files that need little OCR are not queued behind long or hard ones. A lowered run still uses every CPU that nothing else needs, and its OCR result does not change.
The lower priority applies only against other processes in the same container, or, on a server without containers, in the same service if the service has its own share of CPU time, such as its own control group or session. Otherwise, it applies against all programs on the server, which then get CPU time before a lowered OCR run. It does not share CPU time between containers or Kubernetes pods.
If PDF files that need little CPU time arrive without pause, they can delay a lowered run up to ocr_timeout, where it fails. If long PDF files then reach ocr_timeout, increase it or turn off the lower priority for the whole instance, which leaves the OCR Limits unchanged: set pdf_ocr_lower_priority to false in the [pipeline] section of /etc/squirro/common.ini, or the SQ_PIPELINE_PDF_OCR_LOWER_PRIORITY environment variable in Docker-based deployments, and restart the sqingesterd service as described on the Services page.
[pipeline]
pdf_ocr_lower_priority = false
Each step is logged at the INFO level with the PDF file and the CPU time of the run when it was lowered, for example:
PDF OCR of 'storage://...': CPU priority step at 16.2 CPU-seconds to niceness 10 applied
If the priority cannot be lowered, the step logs a warning that ends with its OCR continues at the priority it has, once for that OCR run. The OCR result is not affected.
Note
The setting accepts true or false, and also yes or no, on or off, and 1 or 0, in any letter case. An empty value means the default, true, as when the setting is absent; for Docker-based deployments, that includes SQ_PIPELINE_PDF_OCR_LOWER_PRIORITY set to an empty value. If the setting has any other value, the step OCRs no file and reports an error for every item that reaches it, whatever the value of ignore_ocr_errors. The error log names pdf_ocr_lower_priority and the value. The items are then retried as described in Reporting OCR Errors, but retries cannot fix an invalid value. Correct the setting and restart the sqingesterd service.
Text Recognition Threads#
By default, the PDF OCR step recognizes the text of each page with one thread. More threads per page usually do not make a run faster, and each thread needs a CPU of its own. An OCR run recognizes several pages at the same time, so the threads of a run are the pages that run at the same time, multiplied by the threads per page. When pages use more than one thread and the threads of all OCR runs exceed the CPUs that the ingester may use, the threads mostly wait for each other. The OCR then uses far more CPU time and can stall for minutes or lose the text of some pages. With one thread per page, pages that run at the same time only share the CPUs. To use more threads, set the OMP_THREAD_LIMIT environment variable of the sqingesterd service, or of its container in Docker-based deployments, to a number, for example 2, and restart the service. The parallel jobs of each OCR run then follow that number by default, as described in Parallel OCR Jobs. If you set cpu_jobs, keep cpu_jobs multiplied by that number at or below the CPUs that the ingester may use. The default keeps this for one OCR run at a time. When the ingester OCRs several PDF files at the same time with more than one thread per page, their threads together can still exceed the CPUs, so keep one thread per page unless the ingester OCRs one PDF file at a time. Otherwise, pages can lose their text without a stop in the log, as described in OCR Limits. If the variable is set to a number, the OCR uses that number. The variable applies to all projects of the instance.
OCR Limits#
Three limits stop the OCR of a PDF file that does not finish, for example one that keeps using CPU time or one that waits for something that never happens. They apply whether the CPU priority of the OCR is lowered or not.
CPU budget
The OCR of a PDF file can use 60 CPU seconds plus
ocr_cpu_per_pageCPU seconds per page, 30 by default, so 360 CPU seconds for a 10-page PDF file. The budget does not grow with the threads per page of Text Recognition Threads. Scans and photos of pages need far less. If the PDF file cannot be read to count its pages, the OCR has no CPU budget. To turn off the CPU budget, setocr_cpu_per_pageto0. Any value other than a whole number of 0 or more, such as0.5or-1, means the default.No progress
An OCR that uses no CPU time for 5 minutes is stopped. An OCR with a lowered priority on busy CPUs still uses some CPU time and is not stopped.
Wall-clock time
ocr_timeoutstops an OCR after that many seconds, 1800 (30 minutes) by default, whatever CPU time it used.
A PDF file whose OCR is stopped by any of them is handled as when ocr_timeout is reached: it is left unchanged, or fails the step if ignore_ocr_errors is False. For the CPU budget and the no-progress limit, the error log names the PDF file, the limit, the CPU time used, the budget and how it was calculated, the number of pages, and the wall-clock time, for example:
PDF OCR of 'storage://...' stopped by its CPU budget: 1260.0 CPU-seconds used, budget 1260 CPU-seconds (60 + 30 per page), 40 pages, 412.3 s wall time
The CPU budget follows the number of pages, not their size. A page with a very large image, such as a photo of 100 megapixels or more, can need more than the budget for one page and is then stopped. If the log shows such stops for PDF files that you need, increase ocr_cpu_per_page for the workflow.
More threads per page use more CPU time for the same text, and the CPU budget does not grow with them. If the log shows CPU budget stops after you set OMP_THREAD_LIMIT above 1, increase ocr_cpu_per_page. When OCR runs with more than one thread per page share the CPUs, their threads wait for each other and use CPU time without progress. The CPU budget stops such a run only when it has used the budget of the whole PDF file. The OCR tool, however, stops the text recognition of each page after 3 minutes on its own. It then keeps that page without its text and reports the OCR as successful, and the step logs no stop. In a PDF file of many pages, pages can reach these 3 minutes long before the file has used its budget, already with two such runs on two CPUs. Keep one thread per page unless the ingester OCRs one PDF file at a time.
Workflows saved with a value for ocr_timeout keep that value, also the former default of 60 seconds. With 60 seconds, the no-progress limit, which waits 5 minutes, never stops an OCR, and the CPU budget only stops one that uses its whole budget within those 60 seconds, which for a one-page PDF file takes more than one CPU. To use the current default, enter 1800 for ocr_timeout in the PDF OCR step of the workflow and save it. A workflow that also saved the list of step options can still show 60 as the default of ocr_timeout in the Pipeline Editor, and no ocr_cpu_per_page option. The step uses the values in the configuration of the workflow either way, so to set ocr_cpu_per_page in such a workflow, add it to the configuration of its PDF OCR step.