image_processing package

Submodules

image_processing.barcode_manager module

A class that is responsible for detecting a barcode and extracting information from said barcode.

class image_processing.barcode_manager.BarCodeManager[source]

Bases: object

The BarCode Manager is responsible for: 1. Detecting the barcode. 2. Extracting any information on the detected barcode. 3. Applying blurring to the detected barcode to reduce noise.

apply_barcode_blur(image, box, dilation_intensity=2)[source]

This function applies blurring to a detected barcode region to reduce noise in the image. The barcode region is first extracted, then blurring is applied, after blurring is applied the blurred out barcode is reapplied to the original image.

Args:

image (:obj:’OpenCV image’): Image containing a Barcode. box: The box is an integer list containing the box region coordinates of the barcode location. dilation_intensity (int): Indicates the intensity by which the barcode should be dilated.

The greater the intensity the greater the extent of dilation. Greater intensity leads to reduce of speed with every iterations.
Returns:
obj:’OpenCV image’: The Original image with blurring applied to the barcode region in the image.
detect(image)[source]

This function detects a region containing a barcode if a barcode is present in the image passed. Barcodes supported:

EAN UPC Code 39 Code 93 Code 128 ITF

For more information on Barcode types: https://www.scandit.com/types-barcodes-choosing-right-barcode/

Args:
image (:obj:’OpenCV image’): Image containing the potential barcode.
Returns:
boolean: A value of True is returned if a barcode was detected.
If however a barcode was not detected a value of false is returned.
obj:’OpenCV image’: If a Barcode was successfully detected the detected barcode is returned.
If a barcode was not detected return the original image.

integer list: This list contains the box coordinates for the region in which the barcode resides.

Raises:
TypeError: If a none numpy array value has been passed
Todo:
Find a way to support PDF417 format.
get_barcode_info(image)[source]

This function returns scanned barcode information.

Args:
image (:obj:’OpenCV image’): Image containing a Barcode.
Returns:
boolean: A value of True if the function was able to extract information from the barcode.
If no information was extracted from the barcode a value of False is returned.
string: A UTF-8 String containing the information extracted from the Barcode.
If no information was extracted from the barcode a empty string is returned.

obj:’OpenCV image’: A copy of the original image.

Todo:
Find a way to support PDF417 format.

image_processing.context_manager module

This file contains the logic used to manage the various ID contexts. Should ideally be extended to do more than it currently does.

class image_processing.context_manager.ContextManager[source]

Bases: object

A class responsible for managing and maintaining the various ID contexts.

Attributes:
_sa_id_card (IDContext): A South African ID card context. _sa_id_book (IDContext): A South African ID book context. _sa_id_book_old (IDContext): An old South African ID book context. _up_card (IDContext): A University of Pretoria staff/student card context.
get_id_context(id_type)[source]

Returns an ID context based on the ID type that is passed in as an arg.

Args:
id_type (str): A string indicating a type of ID.
Returns:
(IDContext): An IDContext object determined by the ID type passed in as an arg. (None): In the event that the ID type is unrecognisable.

image_processing.controllers module

Handles all requests relevant to the extraction service of the API.

image_processing.controllers.extract_all()[source]

Sample function to extract face and text from image received URL: http://localhost:5000/extractAll

image_processing.controllers.extract_face()[source]

Sample function to extract face from image received URL: http://localhost:5000/extractFace

image_processing.controllers.extract_text()[source]

Sample function to extract text from image received. URL: http://localhost:5000/extractText

image_processing.controllers.face_extraction_response(use_io, image, text_extract_result=None)[source]

This function converts the extracted cv2 image and converts it to a jpg image. Furthermore, the jpg image is converted to Base64 jpg type and returned. If text extraction results are provided the response will contain the data of text extraction result as well.

Args:
use_io (boolean): Whether or not images should be written to disk image: The cv2 (numpy) image that should be converted to jpg text_extract_result (dict) the extracted text results
Returns:
(:obj:’Response’): The response object that contains the information for HTTP transmission

image_processing.sample_extract module

This class wraps all the functionality required to extract text from an image.

class image_processing.sample_extract.FaceExtractor[source]

Bases: object

The FaceExtractor extracts the face region for the image passed.

extract(img, use_io)[source]

This function is a sample that demonstrates how the face would be extracted.

Args:
img: The image of the ID that contains the face that must be extracted. use_io (boolean): Whether or not images should be written to disk
Returns:
image: The extracted and aligned facial image.
class image_processing.sample_extract.TextExtractor(preferences)[source]

Bases: object

The TextExtractor extracts text from the ID image.

extract(img)[source]

This function is a sample that demonstrates how text would be extracted

Args:
img: The image of the ID that contains the text to be extracted
Returns:
id_details (obj): The extracted information

image_processing.simplification_manager module

Wraps functions that are used to simplify or ease the image processing process.

class image_processing.simplification_manager.SimplificationManager[source]

Bases: object

The Simplification manger is used to remove unwanted content in an image thus simplifying process like OCR and facial comparisons.

perspectiveTransformation(image, use_io)[source]

The perspective transformation takes the image passed and applies edge detection and a function to detect the contours of a identification document. If contours of an identification document is detected the image is converted from a non-perspective view to an perspective view.

Args:
image (:obj:’OpenCV image’): Image containing a identification document use_io (boolean): Whether or not to write images to disk
Returns:
obj:’OpenCV image’: Returns as warped image where just the identification document
is present and the identification document is now in a perspective view.
Raises:
TypeError: If a parameter is passed that is not of type Numpy array.

image_processing.text_cleaner module

This file contains the logic used to manage the removal of characters from an input string.

class image_processing.text_cleaner.TextCleaner[source]

Bases: object

This class is encapsulates the logic required to clean the OCR output string produced from an image of an ID.

Attributes:
_deplorables (list): A list of strings that contain characters that is to be filtered out from the OCR output
string during string cleaning.
clean_up(in_string, deplorables=None)[source]

This function serves to receive an input string, clean it up through removing undesirable characters and unnecessary whitespace, and to return the cleaned string.

Args:
in_string (str): The input string that is to be cleaned. deplorables (list, Optional): A list of characters that are to be filtered from the input string.
Returns:
str: A string that has been stripped of undesirable characters and unnecessary whitespace.
Raises:
TypeError: If in_string is not a string. TypeError: If deplorables is not a list of strings.

Module contents