straditize.colnames module¶
Module for text recognition
Disclaimer
Copyright (C) 2018-2019 Philipp S. Sommer
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
Classes
A bounding box for a column name |
|
|
A class to recognize the text in an image |
-
class
straditize.colnames.
Bbox
[source]¶ Bases:
straditize.colnames._Bbox
A bounding box for a column name
Create new instance of _Bbox(x, y, w, h)
Attributes
The bottom of the box
A list
[x, y, width, height]
A np.ndarray of shape (4, 2) with the corners of the box
The extents necessary for PIL.Image.crop
A list
[x0, x1, y0, y1]
withx0 <= x1
andy0 <= y1
The (positive) height
The left edge of the box
The right edge of the box
The top of the box
The (positive) width
The left edge
The right edge
The lower (bottom) edge
The upper (top) edge
Methods
from_dict
(d)Construct a box from the dictionary
-
property
bottom
¶ The bottom of the box
-
property
bounds
¶ A list
[x, y, width, height]
-
property
corners
¶ A np.ndarray of shape (4, 2) with the corners of the box
-
property
crop_extents
¶ The extents necessary for PIL.Image.crop
-
property
extents
¶ A list
[x0, x1, y0, y1]
withx0 <= x1
andy0 <= y1
-
property
height
¶ The (positive) height
-
property
left
¶ The left edge of the box
-
property
right
¶ The right edge of the box
-
property
top
¶ The top of the box
-
property
width
¶ The (positive) width
-
property
x0
¶ The left edge
-
property
x1
¶ The right edge
-
property
y0
¶ The lower (bottom) edge
-
property
y1
¶ The upper (top) edge
-
property
-
class
straditize.colnames.
ColNamesReader
(image, bounds, rotate=45, mirror=False, flip=False, highres_image=None, data_ylim=None)[source]¶ Bases:
object
A class to recognize the text in an image
This object handles the column names in the
column_names
attribute. It also implements several algorithms to automatically read in the column names using the tesserocr package. In particular these are therecognize_text()
method to read in one small image and thefind_colnames()
method to find the column names automatically.- Parameters
image (PIL.Image.Image) – The RGBA image that has the same shape as the original stratigraphic diagram
bounds (np.ndarray of shape (N, 2)) – The boundaries for each column. These are essential for the
find_colnames()
and thehighlight_column()
methodsrotate (float) – An angle between 0 and 90 that corresponds to the rotation of the column names
mirror (bool) – If True, the image is mirrored (horizontally)
flip (bool) – If True, the image is flipped (vertically)
highres_image (PIL.Image.Image) – A high resolution version of the image with the same width-to-height ratio
data_ylim (tuple (y0, y1)) – The vertical data limits of the data part that should be ignored in the
find_colnames()
method if theignore_data_part
is True
Methods
close
()Close the column names reader
create_variable
(ds, vname, data, **kwargs)Insert the data into a variable in an
xr.Dataset
find_colnames
([extents])Find the names for the columns using tesserocr
from_dataset
(ds)Create a
ColNamesReader
for a xarray.Datasetget_colpic
(x0, y0, x1, y1)Extract the picture of the column name
highlight_column
(col, ax)Highlight the column in the given axes displaying the
navigate_to_col
(col, ax)Navigate to the specified column
recognize_text
(image)Recognize the text in an image using tesserocr
rotate_image
(image)Modify an image with
rotate
,flip
,mirror
to_dataset
([ds])All the necessary data as a
xarray.Dataset
transform_point
(x, y[, invert, image])Transform a point between un-rotated and rotated coordinate system
Attributes
The pictures of the column names
The names of the columns
The vertical data limits of the data part that shall be exluded in the
The
image
attribute with higher resolution and with masked out data part if theignore_data_part
attribute is True and thedata_ylim
attribute is not None.Boolean flag.
The RGBA
PIL.Image.Image
that stores the column namesdict() -> new empty dictionary
The rotated
image
based on therotate_image()
method-
property
colpics
¶ The pictures of the column names
-
property
column_names
¶ The names of the columns
-
create_variable
(ds, vname, data, **kwargs)[source]¶ Insert the data into a variable in an
xr.Dataset
-
data_ylim
= None¶ The vertical data limits of the data part that shall be exluded in the
highres_image
if theignore_data_part
is True
-
find_colnames
(extents=None)[source]¶ Find the names for the columns using tesserocr
- Parameters
extents (list of floats (x0, y0, x1, y1)) – The extents to crop the
rotated_image
. We only look for column names in this image- Returns
dict – A mapping from column number to a string (the column name)
dict – A mapping from column number to a
PIL.Image.Image
(the image of the column name)dict – A mapping from column number to a
Bbox
(the bounding box of the corresponding column name)
-
classmethod
from_dataset
(ds)[source]¶ Create a
ColNamesReader
for a xarray.Dataset- Parameters
ds (xarray.Dataset) – The dataset as obtained from the
to_dataset()
method
-
get_colpic
(x0, y0, x1, y1)[source]¶ Extract the picture of the column name
- Parameters
- Returns
The part of the rotated
highres_image
cropped out from the given parameters- Return type
-
highlight_column
(col, ax)[source]¶ Highlight the column in the given axes displaying the
rotated_image
This method draws a rotated rectangle highlighting the given column col in the given ax.
- Parameters
col (int) – The column number
ax (matplotlib.axes.Axes) – The matplotlib axes on which to plot the rectangle. This ax is expected to show the
rotated_image
-
property
highres_image
¶ The
image
attribute with higher resolution and with masked out data part if theignore_data_part
attribute is True and thedata_ylim
attribute is not None. The data part is then set to white with 0 alpha
-
ignore_data_part
= True¶ Boolean flag. If True, the data part is masked out in the
highres_image
-
image
= None¶ The RGBA
PIL.Image.Image
that stores the column names
Navigate to the specified column
Change the x- and y-limits of the ax to display the given col based on the
column_bounds
- Parameters
col (int) – The column number
ax (matplotlib.axes.Axes) – The matplotlib axes for which to update the limits. This ax is expected to show the
rotated_image
-
nc_meta
= {'colname': {'dims': 'column', 'long_name': 'Name of the columns'}, 'colnames_bounds': {'dims': ('column', 'limit'), 'long_name': 'The boundaries of the columns for the column names reader', 'units': 'px'}, 'colnames_hr_image': {'dims': ('ycolname_hr', 'xcolname_hr', 'rgba'), 'long_name': 'Highres image for column names reader', 'units': 'color'}, 'colnames_image': {'dims': ('ycolname', 'xcolname', 'rgba'), 'long_name': 'RGBA images for column names reader', 'units': 'color'}, 'colpic': {'dims': ('column', 'colpic_y', 'colpic_x', 'rgba'), 'long_name': 'The pictures of the column names', 'units': 'color'}, 'colpic_extents': {'dims': ('column', 'limit'), 'long_name': 'The limits of the column names pictures', 'units': 'px'}, 'flip_colnames': {'dims': (), 'long_name': 'Flip the column names picture (vertically)'}, 'mirror_colnames': {'dims': (), 'long_name': 'Mirror the column names picture (horizontally)'}, 'rotate_colnames': {'dims': (), 'long_name': 'The rotation angle for column names'}}¶
-
recognize_text
(image)[source]¶ Recognize the text in an image using tesserocr
This method uses the
tesserocr.image_to_text()
to read in the text in a given image- Parameters
image (PIL.Image.Image) – The image to read in
- Returns
The text found in it without newline characters
- Return type
-
rotate_image
(image)[source]¶ Modify an image with
rotate
,flip
,mirror
This method rotated, mirrors and/or flips the given image based on the
rotate
,mirror
andflip
attributes- Parameters
image (PIL.Image.Image) – The source image
- Returns
The target image
- Return type
-
property
rotated_image
¶ The rotated
image
based on therotate_image()
method
-
to_dataset
(ds=None)[source]¶ All the necessary data as a
xarray.Dataset
- Parameters
ds (xarray.Dataset) – The dataset in which to insert the data. If None, a new one will be created
- Returns
Either the given ds or a new
xarray.Dataset
instance- Return type
-
transform_point
(x, y, invert=False, image=None)[source]¶ Transform a point between un-rotated and rotated coordinate system
- Parameters
x (float) – The x-coordinate of the point in the source coordinate system
y (float) – The y-coordinate of the point in the source coordinate system
invert (bool) – If True, the source coordinate system is the rotated one (i.e. this method transform from the
rotated_image
to the coordinate system of theimage
), other wise from theimage
to therotated_image
image (PIL.Image.Image) – The unrotated source image. If None, the
image
is used. This image defines the source coordinate system (or the target coordinate system if invert is True)
- Returns
float – The transformed x-coordinate
float – The transformed y-coordinate