QGIS processing algorithm which recognizes text from raster images inside input polygon features and saves as attribute value of output layer.
PW_OCR_ADVENCED script processes in different way than PW_OCR. Check specifications of both scripts to choose better one for your applications.
If usage of the script leads to a scientific publication, please acknowledge this fact by citing:
Graszka, O. (2021). Automatyzacja procesu rozpoznawania i weryfikacji nazw geograficznych ze źródeł historycznych na przykładzie Słownika geograficznego Królestwa Polskiego. W T. Epsztein (red.), Od Słownika geograficznego Królestwa Polskiego do map topograficznych Wojskowego Instytutu Geograficznego (s. 23–32).
PW_OCR script usues Pytesseract library and requires its installation. After installation you have to update path to your Tesseract directory at the beginning of the script.
// path to your tesseract installation directory.
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
You may set Pytesseract configuration (page segmentation mode and OCR engine model) using comboboxes of script graphical interface, but if you want to use language other than polish, you have to edit line below in the code:
data = pytesseract.image_to_data(Raster_lyr.source(), lang='pol', config=self.config, output_type=Output.DICT)
This algorithm iterates over all input raster layers and processes them according to the scheme below:
-
Recognizing all words on the sheet and returning table with their pixel coordinates , width, height, confidence of recognition and recognized text (Pytesseract library).
-
Itarating over all features overlaying raster and collecting all words which centroids are inside feature boundaries.
-
Merging sentences from all collected for each feature words.
-
Adding recognized text (sentence) as attribute value to output feature field.
-
Adding confidence of recognition as list of percentage values for each word to confidence output field.
Cekcyn Polski
->[96,71]
This script works properly only if edges of rectified rasters are paralell to axes of QGIS project coordinate reference system.
Text output field
Confidence output field
Run for all raster layers
Input raster layer
In case of multiband raster images, the only first band will be used.
Page Segmentation Mode
OCR Engine Model
Add words recognized with zero confidence
Output layer