image_preprocessing package

Submodules

image_preprocessing.blur_manager module

Wraps all the functionality required for applying blurring techniques to an image.

class image_preprocessing.blur_manager.BlurManager(blur_type, kernel_size)[source]

Bases: object

The blur is responsible for applying different blur techniques to the images passed.

apply(image)[source]

This performs the blurring.

Args:
image: The image to be blurred.
Raises:
NameError: If invalid blur type is provided i.e. Normal, Gaussian or Median.
Returns:
obj:’OpenCV image’: The blurred image.
blur(image, blur_kernel=[(3, 3)])[source]

This function applies basic blurring to the image passed.

Args:
image (:obj:’OpenCV image’): Image to which basic blurring should be applied to. blur_kernel (Integer list): Represent the kernel dimension by which basic blurring should be applied to. Integer list: Represent the kernel dimension by which basic blurring should be applied to.
Raises:
ValueError: If a blur_kernel with an invalid length is provided. TypeError: If a blur_kernel is not of type list.
Returns:
obj:’OpenCV image’: A modified copy of the image where basic blurring was applied to the image.
gaussianBlur(image, blur_kernel=[(7, 7)])[source]

This function applies Gaussian blurring to the image passed. Args:

image (:obj:’OpenCV image’): Image to which Gaussian blurring should be applied to. blur_kernel (Integer list): Represent the kernel dimension by which basic blurring should be applied to. Integer list: Represent the kernel dimension by which basic blurring should be applied to.
Raises:
ValueError: If a blur_kernel with an invalid length is provided. TypeError: If a blur_kernel is not of type list.
Returns:
obj:’OpenCV image’: A modified copy of the image where Gaussian blurring was applied to the image.
medianBlur(image, blur_kernel=[3])[source]

This function applies Median blurring to the image passed.

Args:
image (:obj:’OpenCV image’): Image to which Median blurring should be applied to. blur_kernel (Integer array): Represent the kernel dimension by which median blurring should be applied to. Integer array: Represent the kernel dimension by which median blurring should be applied to.
Raises:
TypeError: If a blur_kernel is not of type list. ValueError: If a blur_kernel with an invalid length is provided.
Returns:
obj:’OpenCV image’: A modified copy of the image where Median blurring was applied to the image.

image_preprocessing.build_director module

This class is responsible for controlling the PipelineBuilder.

class image_preprocessing.build_director.BuildDirector[source]

Bases: object

The BuildDirector constructs the Pipeline using the PipelineBuilder

static construct_face_extract_pipeline()[source]

This function constructs the pipeline for face extraction. This includes building different managers with their specific parameters. These managers will be called within the pipeline when executed.

Returns:
:Pipeline (Constructed pipeline)
static construct_text_extract_pipeline(preferences, identification_type)[source]

This function constructs the pipeline for text extraction. This includes building different managers with their specific parameters. These managers will be called within the pipeline when executed.

Args:

preferences (dict): User-specified techniques to use in pipeline. identification_type (string): Contains the type of identification, this is used

to determine which techniques are used.
Returns:
:Pipeline (Constructed pipeline)

image_preprocessing.color_manager module

Wraps all the functionality required to manipulate the color channels in an image.

class image_preprocessing.color_manager.ColorManager(color_extraction_type, channel='green', kernel_size=(13, 7))[source]

Bases: object

The Color manager is responsible for applying several color management techniques to the image passed.

apply(image)[source]

This performs the specified processing technique.

Args:
image: The image to which the technique must be applied.
Raises:
NameError: If invalid color extraction type is provided i.e.
histogram, extract, black hat or top hat (white hat).
Returns:
obj:’OpenCV image’: The modified image
blackHat(image, rect_kernel_size=(13, 7))[source]

This function applies blackhat color changes to the image passed.

Args:
image (:obj:’OpenCV image’): Image to which black hat color changes should be
applied to
rect_kernel_size (list): Represent the kernel dimension by which blackHat morphology
changes should be applied to.
Raises:
TypeError: If the kernel size type is not a tuple. ValueError: If the kernel size tuple contains more than 2 items.
Returns:
obj:’OpenCV image’: A modified copy of the image where blackHat morphology was
applied to an image.
extractChannel(image, image_channel='green')[source]

This function extracts a selected color channel from an image.

Args:
image (:obj:’OpenCV image’): Image to which image channel should be removed. str: Color that should be removed valid color red, green, blue.
Raises:
NameError: If invalid colour is selected i.e. not red, green, blue
Returns:
obj:’OpenCV image’: A copy of the image passed but with a color channel removed.
static histEqualisation(image)[source]

This function applies histogram equalisation to the image passed.

Args:
image (:obj:’OpenCV image’): Image to which histogram equalisation should be applied to.
Returns:
obj:’OpenCV image’: The Histogram equalised image.
topHat(image, rect_kernel_size=(13, 7))[source]

This function applies tophat color changes to the image passed.

Args:
image (:obj:’OpenCV image’): Image to which top hat color changes should be
applied to.
rect_kernel_size (list): Represents the kernel dimension by which topHat morphology
changes should be applied to.
Raises:
TypeError: If the kernel size type is not a tuple. ValueError: If the kernel size tuple contains more than 2 items.
Returns:
obj:’OpenCV image’: A modified copy of the image where topHat morphology was
applied to an image.

image_preprocessing.face_manager module

Wraps all the functionality necessary for extracting a face from an image.

class image_preprocessing.face_manager.FaceDetector(shape_predictor_path)[source]

Bases: object

The FaceDetector class is responsible for 1. Detecting the face. 2. Extracting a face from an image. 3. Applying blurring on a detected face in an image.

blur_face(image)[source]

This function find the faces and apply a blurring effect on the detected region. After the region has been blurred, the blurred region is reapplied to the original image. Blurring the face is implemented as a method in the attempt to reduce noise when extracting text from the image later in the image pipeline.

Args:
image (:obj:’OpenCV image’): Image containing the face we need to detect and blur.
Raises:
ValueError: If no face can be detected.
Returns:
obj:’OpenCV image’: A copy of the original image is returned but with the applied blurring to the face region.
detect(image)[source]

This function detects the face in the image passed. By making use of the dlib HOG feature image_preprocessing and linear classifier for frontal face detection we are able to detect the face with less false-positive results and without a major time penalty. More Information dlib frontal_face detection: http://dlib.net/imaging.html#get_frontal_face_detector

A check will be done to see if a face is present in the image. If a face is not detected in the image the execution should log that the face was not found and continue with execution. This is due to the fact that face detection might not be critical to a function (like with text extraction) and rather be used to increase accuracy.

Args:
image (:obj:’OpenCV image’): Image containing the face we need to detect.
Raises:
ValueError: If no face can be detected.
Returns:
list(integer): This list contains the box coordinates for the region in which the face resides.
extract_face(image)[source]

This function finds a face in the image passed and is optimised to align the face before being returned.

Args:
image (:obj:’OpenCV image’): Image containing the face we need to detect and extract.
Raises:
ValueError: If no face can be detected.
Returns:
obj:’OpenCV image’: Any background and unnecessary components are removed and only the aligned face is returned. obj:’OpenCV image’: A copy of the original image is returned.

image_preprocessing.pipeline module

This is an object that handles the entire process of extracting data from an image from a high-level perspective.

class image_preprocessing.pipeline.Pipeline(blur_manager=None, color_manager=None, face_detector=None, threshold_manager=None)[source]

Bases: object

The Pipeline will perform all necessary processing on the image and is built by the PipelineBuilder.

Attributes:
blur_manager (:BlurManager): The BlurManager that is used in this pipeline. color_manager (:ColorManager): The ColorManager that is used in this pipeline. face_detector (:FaceDetector): The FaceDetector that is used in this pipeline. threshold_manager (:ThresholdManager): The ThresholdManager that is used in this pipeline.
process_face_extraction(image)[source]

This function applies all the processing needed to extract a face from a image.

Args:
image (:obj:’OpenCV image’): Image to which processing should be applied to.
Returns:
image (:obj:’OpenCV image’): The processed image.
process_text_extraction(useIO, image, remove_face=False)[source]

This function applies all the processing needed to extract text from a image.

Args:

useIO (boolean): Whether or not to write images to disk image (:obj:’OpenCV image’): Image to which processing should be applied to. remove_face :boolean: If the remove face flag is set to true extra processes will

be activated during the pre-processing phase to remove the face from the image.
Returns:
image: The processed image.

image_preprocessing.pipeline_builder module

The class that is responsible for creating a Pipeline. This class is the Builder of the Builder design pattern.

class image_preprocessing.pipeline_builder.PipelineBuilder[source]

Bases: object

The PipelineBuilder will assemble all the parts of the Pipeline

get_result()[source]

This function returns the fully-assembled pipeline

Returns:
:Pipeline (Assembled pipeline)
set_blur_manager(value)[source]

This function adds the specified blur manager to the pipeline

Args:
value (:BlurManager): BlurManager object to be added
Returns:
None
set_color_manager(value)[source]

This function adds the specified color manager to the pipeline

Args:
value (:ColorManager): ColorManager object to be added
Returns:
None
set_face_detector(value)[source]

This function adds the specified face detector to the pipeline

Args:
value (:FaceDetector): FaceDetector object to be added
Returns:
None
set_threshold_manager(value)[source]

This function adds the specified threshold manager to the pipeline

Args:
value (:ThresholdManager): ThresholdManager object to be added
Returns:
None

image_preprocessing.template_matching module

Wraps the functionality required to dynamically deduce the type of identification documentation that has been provided in an image.

class image_preprocessing.template_matching.TemplateMatching[source]

Bases: object

The TemplateMatching class receives template images to identify the type of identification that is used in the image. Thus you provide it with templates and it will identify whether you used an id card, id book etc.

identify(source)[source]

This function identifies the src image by searching for the templates provided.

Args:
source (Image) : The image that needs to be identified
Returns:
string : Returns a string if no type could be identified, None is returned
Example:

identify(args[“image”]]) !

image_preprocessing.thresholding_manager module

Wraps all the functions related to applying thresholding techniques to an image.

class image_preprocessing.thresholding_manager.ThresholdingManager(thresholding_type)[source]

Bases: object

The Thresholding manager is responsible for applying the different types of thresholding techniques.

static adaptiveThresholding(image)[source]

This function applies a simple adaptive thresholding to the image passed.

Args:
image (:obj:’OpenCV image’): Image to which thresholding should be applied.
Raises:
TypeError: If a parameter is passed that is not of type Numpy array.
Returns:
obj:’OpenCV image’: The Threshold image.
apply(image)[source]

This performs the thresholding based on the predefined technique.

Args:
image: The image to which the thresholding must be applied.
Raises:
NameError: If invalid thresholding type is provided. i.e. Adaptive or Otsu.
Returns:
obj:’OpenCV image’: The threshold image.
static otsuThresholding(image)[source]

This function applies a simple Binary Inverse Otso thresholding to the image passed.

Args:
image (:obj:’OpenCV image’): Image to which thresholding should be applied.
Raises:
TypeError: If a parameter is passed that is not of type Numpy array.
Returns:
obj:’OpenCV image’: The Threshold image.

Module contents