Alternat Reference

The alternat reference guide will walk through the implementation for both Generation and Collection, the alternat library, and the app.

Generation - Analyzer

Google Analyzer

class alternat.generation.google.analyze.AnalyzeImage[source]

Bases: alternat.generation.base.analyzer.AnalyzeImageBase

Google Analyzer driver class.

Parameters

AnalyzeImageBase ([type]) – Driver base class.

describe_image(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/alternat/envs/v0.1.4/lib/python3.8/site-packages/PIL/Image.py'>)[source]

Describe image (used for captioning) - Not availble in Google Computer Vision API

Parameters

image (PIL_IMAGE) – [description]

extract_labels(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/alternat/envs/v0.1.4/lib/python3.8/site-packages/PIL/Image.py'>)[source]

Extract labels of image using Google Computer Vision API.

Parameters

image (PIL_IMAGE) – PIL Image object.

Raises

Exception – Google Cloud specific error messages based on request.

handle(image_path: str = None, base64_image: str = None, actions: list = None) → dict[source]

Entry point for the driver. Implements all the action and generates data for rule engine.

Parameters
  • image_path (str, optional) – Path to image on disk, defaults to None

  • base64_image (str, optional) – Base64 image string, defaults to None

  • actions (list, optional) – list of actions to run, defaults to None (all actions execute)

Returns

[description]

Return type

dict

ocr_analysis(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/alternat/envs/v0.1.4/lib/python3.8/site-packages/PIL/Image.py'>)[source]

Does OCR analysis using Google Computer Vision API. Also runs the alternat clustering rule if app is configured for it.

Parameters

image (PIL_IMAGE) – PIL Image object.

set_environment_variables()[source]

Sets environment variable GOOGLE_APPLICATION_CREDENTIALS based on config.


Azure Analyzer

class alternat.generation.microsoft.analyze.AnalyzeImage[source]

Bases: alternat.generation.base.analyzer.AnalyzeImageBase

Azure / Microsoft Analyzer driver class.

Parameters

AnalyzeImageBase ([type]) – Driver base class.

describe_image(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/alternat/envs/v0.1.4/lib/python3.8/site-packages/PIL/Image.py'>)[source]

Describe image using Azure Vision API.

Parameters

image (PIL_Image) – PIL Image object

extract_labels(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/alternat/envs/v0.1.4/lib/python3.8/site-packages/PIL/Image.py'>)[source]

Extract labels of image using Azure Vision API.

Parameters

image (PIL_Image) – PIL Image object.

handle(image_path: str = None, base64_image: str = None, actions: list = None) → dict[source]

Entry point for the driver. Implements all the action and generates data for rule engine.

Parameters
  • image_path (str, optional) – Path to image on disk, defaults to None

  • base64_image (str, optional) – Base64 image string, defaults to None

  • actions (list, optional) – list of actions to run, defaults to None (all actions execute)

Returns

[description]

Return type

dict

is_clean(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/alternat/envs/v0.1.4/lib/python3.8/site-packages/PIL/Image.py'>) → bool[source]

Check if the image has proper resolution, and is clean.

:param image:PIL Image object. :type image: PIL_Image :return: [description] :rtype: bool

modifyBoundingBoxData(bounding_box: list)[source]

Transform bounding box data as per the convention. Azure API return bounding box info in the format [left, top, right, top, right, bottom, left, bottom] which is transformed to format [{x: left, y: top}, {x: right, y: top}, {x: right, y: bottom}, {x: left, y: bototm}].

Parameters

bounding_box (list) – Bounding box data form Azure API.

Returns

[description]

Return type

[type]

ocr_analysis(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/alternat/envs/v0.1.4/lib/python3.8/site-packages/PIL/Image.py'>)[source]

Does OCR Analysis using Azure Vision API.

Parameters

image (PIL_Image) – PIL Image object.

resize_image(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/alternat/envs/v0.1.4/lib/python3.8/site-packages/PIL/Image.py'>)[source]

Resize image (maintaining aspect ratio) if width / height > 5000 pixels (API constrain from Azure)

Parameters

image (PIL_Image) – [description]


Opensource Analyzer

class alternat.generation.opensource.analyze.AnalyzeImage[source]

Bases: alternat.generation.base.analyzer.AnalyzeImageBase

Opensource driver class.

Parameters

AnalyzeImageBase ([type]) – Driver base class.

describe_image(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/alternat/envs/v0.1.4/lib/python3.8/site-packages/PIL/Image.py'>)[source]

Describe image using open source solution. Not implemented right now.

Parameters

image (PIL_Image) – PIL Image object

extract_labels(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/alternat/envs/v0.1.4/lib/python3.8/site-packages/PIL/Image.py'>)[source]

Extract labels of image using open source solution. Not implemented righ now.

Parameters

image (PIL_Image) – PIL Image object.

handle(image_path: str = None, base64_image: str = None, actions: list = None) → dict[source]

Entry point for the driver. Implements all the action and generates data for rule engine.

Parameters
  • image_path (str, optional) – Path to image on disk, defaults to None

  • base64_image (str, optional) – Base64 image string, defaults to None

  • actions (list, optional) – list of actions to run, defaults to None (all actions execute)

Returns

[description]

Return type

dict

modifyBoundingBoxData(bounding_box: list)[source]

Transform bounding box data as per the convention. EasyOCR return bounding box info in the format [left, top, right, top, right, bottom, left, bottom] which is transformed to format [{x: left, y: top}, {x: right, y: top}, {x: right, y: bottom}, {x: left, y: bototm}].

Parameters

bounding_box (list) – Bounding box data form EasyOCR.

Returns

[description]

Return type

[type]

ocr_analysis(image: <module 'PIL.Image' from '/home/docs/checkouts/readthedocs.org/user_builds/alternat/envs/v0.1.4/lib/python3.8/site-packages/PIL/Image.py'>)[source]

Does OCR Analysis using EasyOCR.

Parameters

image (PIL_Image) – PIL Image object.

Generation - Rule Engine

OCR Rule

class alternat.generation.rules.ocr_handler.OCRDataHandler(input_data, confidence_threshold: float = None, ocr_filter_threshold: float = None)[source]

Bases: alternat.generation.base.action_data_handler.ActionDataHandler

Rule for processing OCR data from driver.

Parameters

ActionDataHandler ([type]) – Base class for rule.

Initialize the handler with input data and confidence threshold (if available)

Parameters
  • input_data ([type]) – Data from driver.

  • confidence_threshold (float, optional) – Confidence threshold to filter OCR with low threshold, defaults to None (Driver config default)

  • ocr_filter_threshold (float, optional) – Confidence threshold to filter OCR data based on line height ratio to image height.

apply(interim_result: dict) → dict[source]

Process intermin result from previous rules in the chain and run OCR rule.

Parameters

interim_result (dict) – Intermediate results from previous rules in the chain.

Returns

[description]

Return type

dict

has_data() → bool[source]

Checks whethere OCR data is avalible in the input data.

Returns

[description]

Return type

bool

process_ocr() → dict[source]

Process the OCR data from the driver and filter it on the basis of line confidence threshold value and the ratio of line height to image height. Based on the configuration also invokes alternat clustering implementation (default to True)

Returns

[description]

Return type

dict


Caption Rule

class alternat.generation.rules.caption_handler.CaptionDataHandler(input_data: dict, confidence_threshold: float = None)[source]

Bases: alternat.generation.base.action_data_handler.ActionDataHandler

Rule for processing caption data from driver.

Parameters

ActionDataHandler ([type]) – Base class for rule.

Initialize the handler with input data and confidence threshold (if available)

Parameters
  • input_data (dict) – Data from driver.

  • confidence_threshold (float, optional) – Confidence threshold to filter captions with low threshold, defaults to None (Driver config default)

apply(interim_result: dict) → dict[source]

Process intermin result from previous rules in the chain and run caption rule.

Parameters

interim_result (dict) – Intermediate results from previous rules in the chain.

Returns

[description]

Return type

dict

has_data() → bool[source]

Checks whethere caption data is avalible in the input data.

Returns

[description]

Return type

bool


Label Rule

class alternat.generation.rules.label_handler.LabelDataHandler(input_data, confidence_threshold: float = None)[source]

Bases: alternat.generation.base.action_data_handler.ActionDataHandler

Rule for processing label data from driver.

Parameters

ActionDataHandler ([type]) – Base class for rule.

Initialize the handler with input data and confidence threshold (if available)

Parameters
  • input_data ([type]) – Data from driver.

  • confidence_threshold (float, optional) – Confidence threshold to filter labels with low threshold, defaults to None (Driver config default)

apply(interim_result: dict) → dict[source]

Process intermin result from previous rules in the chain and run label rule.

Parameters

interim_result (dict) – Intermediate results from previous rules in the chain.

Returns

[description]

Return type

dict

has_data() → bool[source]

Checks whethere label data is avalible in the input data.

Returns

[description]

Return type

bool

Library

class alternat.generation.generator.Drivers[source]

Bases: object

Driver name for alternat Library.

GOOGLE = 'google'
MICROSOFT = 'azure'
OPEN = 'opensource'
class alternat.generation.generator.Generator(driver_name: str = None)[source]

Bases: object

Generator class to implement alternat Library.

Raises
  • InvalidGeneratorDriver – Driver Invalid

  • InvalidGeneratorDriver – Driver Invalid

  • InvalidGeneratorDriver – Driver Invalid

  • OutputDirPathNotGiven – Output director path is not given.

Returns

[description]

Return type

[type]

Initializes generator with driver to use.

Parameters

driver_name (str, optional) – Name of the driver], defaults to None (opensource)

Raises

InvalidGeneratorDriver – Driver name is invalid or not implemented.

ALLOWED_DRIVERS = ['opensource', 'azure', 'google']
DEFAULT_DRIVER = 'opensource'
generate_alt_text_from_base64(base64_image: str)[source]

Generates alt-text from base64 image string.

Parameters

base64_image (str) – base64 image string

Returns

[description]

Return type

[type]

generate_alt_text_from_file(input_image_path: str, output_dir_path: str)[source]

Generates alt-text from file on disk.

Parameters
  • input_image_path (str) – Path to image to be processed.

  • output_dir_path (str) – Path to directory where the results needs to be saved.

Returns

[description]

Return type

[type]

get_config()[source]

Get the generator level config in the form of JSON.

Returns

[description]

Return type

[type]

get_current_driver()[source]

Get the current driver.

Returns

[description]

Return type

[type]

get_driver_config()[source]

Get the driver config in the form of JSON. Retreives public members [name: value] pair from the driver config class.

Returns

[description]

Return type

[type]

set_config(conf)[source]

Sets the generator level configuration parameters passed via JSON.

Parameters

conf ([type]) – Generator configuration parameters with values.

set_driver_config(conf: dict)[source]

Sets the driver config parameters using the JSON passed. There is one-to-one mapping between key in json and driver class public members.

Parameters

conf (dict) – Configuration JSON to set the driver configuration.

class alternat.collection.collector.Collector[source]

Bases: object

process(url: str, output_dir_path: str, download_recursive: bool = False, collect_using_apify: bool = False)[source]

Collects image from the url into the output directory

Parameters
  • url (str) – [description]

  • output_dir_path (str) – [description]

  • download_recursive (bool, optional) – [description], defaults to False

  • collect_using_apify (bool, optional) – [description], defaults to False