### Clone dhSegment Repository Source: https://dhsegment.readthedocs.io/en/latest/start/demo.html Clone the dhSegment repository to your local machine to get started. ```bash git clone https://github.com/dhlab-epfl/dhSegment.git ``` -------------------------------- ### get_classes_color_from_file_multilabel Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Gets classes and code labels from a text file for multi-label scenarios. ```APIDOC ## get_classes_color_from_file_multilabel ### Description Get classes and code labels from txt file for multi-label scenarios. ### Parameters * **...**: Parameters for the function. ``` -------------------------------- ### Define Class RGB Values Source: https://dhsegment.readthedocs.io/en/latest/intro/intro.html This snippet shows how to define the RGB values for different classes in a text file. Each line corresponds to a class, starting with class 0. ```text 0 255 0 255 0 0 0 0 255 ``` -------------------------------- ### Get Annotations Per File Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Retrieves annotations specific to a given file from VIA JSON content. ```APIDOC ## get_annotations_per_file ### Description From VIA json content, get annotations relative to the given name_file. ### Parameters - **via_dict** (`dict`) – VIA annotations content (originally json) - **name_file** (`str`) – the file to look for (it can be a iiif path or a file path) ### Return type `dict` ### Returns dict ``` -------------------------------- ### Get VIA Attributes Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Extracts attributes from annotated data and returns a list of VIAttribute objects. ```APIDOC ## get_via_attributes ### Description Gets the attributes of the annotated data and returns a list of VIAttribute. ### Parameters - **annotation_dict** (`dict`) – json content of the VIA exported file - **via_version** (`int`) – either 1 or 2 (for VIA v 1.0 or VIA v 2.0) ### Return type `List`[`VIAttribute`] ### Returns A list containing VIAttributes ``` -------------------------------- ### Download Provided Model Source: https://dhsegment.readthedocs.io/en/latest/start/demo.html Download the provided pre-trained model and unzip it into the demo/model directory. This is recommended to skip the time-consuming training process. ```bash cd demo/ wget https://github.com/dhlab-epfl/dhSegment/releases/download/v0.2/model.zip unzip model.zip cd .. ``` -------------------------------- ### Run Training Script Source: https://dhsegment.readthedocs.io/en/latest/start/training.html Execute the training script using the 'sacred' package by providing a configuration file. ```bash python train.py with ``` -------------------------------- ### Download and Unzip Annotated Dataset Source: https://dhsegment.readthedocs.io/en/latest/start/demo.html Download the annotated dataset and unzip it into the demo/pages directory. This dataset contains pre-organized folders for training, validation, and testing. ```bash cd demo/ wget https://github.com/dhlab-epfl/dhSegment/releases/download/v0.2/pages.zip unzip pages.zip cd .. ``` -------------------------------- ### Download Pretrained ResNet Model Source: https://dhsegment.readthedocs.io/en/latest/start/demo.html Download the pre-trained weights for ResNet. This step is only necessary if you plan to train the model from scratch. ```python cd pretrained_models/ python download_resnet_pretrained_model.py cd .. ``` -------------------------------- ### Run Demo Script Source: https://dhsegment.readthedocs.io/en/latest/start/demo.html Execute the main demo script to perform page document extraction. Results will be saved in the demo/processed_images directory. ```python python demo.py ``` -------------------------------- ### TextLine Instance Methods Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Methods for manipulating and serializing TextLine objects. ```APIDOC ## TextLine Instance Methods ### `scale_baseline_points` Scales the points of the baseline by a factor ratio. **Parameters** * **ratio** (`float`) – factor to rescale the baseline coordinates ### `to_dict` Converts a TextLine object to a dictionary. **Parameters** * **non_serializable_keys** (`List`[`str`]) – list of keys that can’t be directly serialized and that need some internal serialization **Returns** a dictionary with the atributes of the object serialized ### `to_xml` Converts a TextLine object to a XML structure. **Parameters** * **name_element** – name of the object (optional) **Returns** a etree structure ``` -------------------------------- ### TrainingParams Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Parameters to configure the training process. ```APIDOC ## TrainingParams ### Description Parameters to configure the training process. ### Parameters * **kwargs**: Arbitrary keyword arguments. ``` -------------------------------- ### Class Definition File Format Source: https://dhsegment.readthedocs.io/en/latest/start/training.html Defines the RGB color codes for each segmentation class in the 'classes.txt' file. Each class must have a unique color. ```text classes.txt 0 0 0 0 255 0 ... ``` -------------------------------- ### Training Parameters Class Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Class for configuring the training process. ```APIDOC ## TrainingParams(_**kwargs_) Parameters to configure the training process. ### Variables - **n_epochs** (int): Number of epochs for training. - **evaluate_every_epoch** (int): The model will be evaluated every n epochs. - **learning_rate** (float): The starting learning rate value. - **exponential_learning** (bool): Option to use exponential learning rate. - **batch_size** (int): Size of batch. - **data_augmentation** (bool): Option to use data augmentation (by default is set to False). - **data_augmentation_flip_lr** (bool): Option to use image flipping in right-left direction. - **data_augmentation_flip_ud** (bool): Option to use image flipping in up down direction. - **data_augmentation_color** (bool): Option to use data augmentation with color. - **data_augmentation_max_rotation** (float): Maximum angle of rotation (in radians) for data augmentation. - **data_augmentation_max_scaling** (float): Maximum scale of zooming during data augmentation (range: [0,1]). - **make_patches** (bool): Option to crop image into patches. This will cut the entire image in several patches. - **patch_shape** (tuple): Shape of the patches. - **input_resized_size** (int): Size (in pixel) of the image after resizing. The original ratio is kept. If no resizing is wanted, set it to -1. - **weights_labels** (list): Weight given to each label. Should be a list of length = number of classes. - **training_margin** (int): Size of the margin to add to the images. This is particularly useful when training with patches. - **local_entropy_ratio** (float): - **local_entropy_sigma** (float): - **focal_loss_gamma** (float): Value of gamma for the focal loss. See paper : https://arxiv.org/abs/1708.02002 ### Methods - `check_params()`: Checks if there is no parameter inconsistency. Returns None. ``` -------------------------------- ### Miscellaneous Helpers Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html A collection of general-purpose utility functions for file handling and data manipulation. ```APIDOC ## Miscellaneous helpers - `parse_json(filename)`: Parses JSON data from a file. - `dump_json(filename, dict)`: Dumps a dictionary to a JSON file. - `load_pickle(filename)`: Loads data from a pickle file. - `dump_pickle(filename, obj)`: Dumps an object to a pickle file. - `hash_dict(params)`: Hashes a dictionary. ``` -------------------------------- ### TextLine Class Methods Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Methods for creating TextLine objects from various formats. ```APIDOC ## TextLine Class Methods ### `from_array` Creates a TextLine object from array inputs. ### `from_dict` Creates a TextLine object from a serialized dictionary. **Parameters** * **dictionary** (`dict`) – serialized dictionary **Returns** `TextLine` ### `from_xml` Creates a TextLine object from an XML structure. **Parameters** * **etree_element** (`Element`) – a xml etree **Returns** a dictionary with keys ‘id’ and ‘coords’ ``` -------------------------------- ### TextRegion Instance Methods Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Methods for manipulating and serializing TextRegion objects. ```APIDOC ## TextRegion Instance Methods ### `sort_text_lines` Sorts `TextLine` from top to bottom according to their mean y coordinate (centroid). **Parameters** * **top_to_bottom** (`bool`) – order lines from top to bottom of image, default=True **Returns** `None` ### `to_dict` Converts a TextRegion object to a dictionary. **Parameters** * **non_serializable_keys** (`List`[`str`]) – list of keys that can’t be directly serialized and that need some internal serialization **Returns** a dictionary with the atributes of the object serialized ### `to_xml` Converts a TextRegion object to a XML structure. **Parameters** * **name_element** – name of the object (optional) **Returns** a etree structure ``` -------------------------------- ### WorkingItem Class Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html A container for annotated images within the VIA format. ```APIDOC ## WorkingItem Class ### Description A container for annotated images. This class holds information about an image and its associated annotations in the VIA format. ### Parameters - **collection** (str) - name of the collection - **image_name** (str) - name of the image - **original_x** (int) - original image x size (width) - **original_y** (int) - original image y size (height) - **reduced_x** (int) - resized x size - **reduced_y** (int) - resized y size - **iiif** (str) - iiif url - **annotations** (dict) - VIA ‘region_attributes’ ### Properties - **collection** (str) - Alias for field number 0 - **image_name** (str) - Alias for field number 1 - **original_x** (int) - Alias for field number 2 - **original_y** (int) - Alias for field number 3 - **reduced_x** (int) - Alias for field number 4 - **reduced_y** (int) - Alias for field number 5 - **iiif** (str) - Alias for field number 6 - **annotations** (dict) - Alias for field number 7 ``` -------------------------------- ### dh_segment.io.serving_input_image Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Generates serving input for image-based input. ```APIDOC ## dh_segment.io.serving_input_image ### Description Generates serving input for image-based input. ### Returns fn - Serving input function. ``` -------------------------------- ### Create and Activate Virtual Environment Source: https://dhsegment.readthedocs.io/en/latest/start/install.html Use these commands to create a new conda environment named 'dh_segment' with Python 3.6 and then activate it. This isolates project dependencies. ```bash conda create -n dh_segment python=3.6 source activate dh_segment ``` -------------------------------- ### create_via_annotation_single_image Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Creates a VIA annotation dictionary for a single image. ```APIDOC ## create_via_annotation_single_image ### Description Returns a dictionary item {key: annotation} in VIA format to further export to .json file. ### Parameters - **img_filename** (str) - path to the image - **via_regions** (List[dict]) - regions in VIA format (output from `create_via_region_from_coordinates`) - **file_attributes** (Optional[dict]) - file attributes (usually None) ### Return Type `Dict[str, dict]` Returns a dictionary item with key and annotations in VIA format. ``` -------------------------------- ### CSV Input Data Format Source: https://dhsegment.readthedocs.io/en/latest/start/training.html Use this format when providing training data via a CSV file, listing pairs of original and label image filenames. ```text mypath/myfolder/original_image_filename1,mypath/myfolder/label_image_filename1 mypath/myfolder/original_image_filename2,mypath/myfolder/label_image_filename2 ``` -------------------------------- ### dh_segment.io.load_and_resize_image Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Loads an image from its filename and resizes it. ```APIDOC ## dh_segment.io.load_and_resize_image ### Description Loads an image from its filename and resizes it to the desired output size. ### Parameters * **filename** (str) - Required - String tensor. * **channels** (int) - Required - Number of channels for the decoded image. * **size** (Optional[int]) - Optional - Number of desired pixels in the resized image, tf.Tensor or int (None for no resizing). * **interpolation** (str) - Optional - Interpolation method. * **return_original_shape** (bool) - Optional - Returns the original shape of the image before resizing if this flag is True. ### Returns tensorflow.Tensor - The loaded and resized image. ``` -------------------------------- ### Image and Class Conversion Utilities Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Functions for converting between image representations and class labels. ```APIDOC ## Image and Class Conversion Utilities - `label_image_to_class(label_image, classes_file)`: Converts a label image to class format. Returns tensorflow.Tensor. - `class_to_label_image(class_label, classes_file)`: Converts class labels to a label image. Returns tensorflow.Tensor. - `multilabel_image_to_class(label_image, classes_file)`: Combines image annotations with classes info of the txt file to create the input label for the training. Parameters: - **label_image** (tensorflow.Tensor): Annotated image [H,W,Ch] or [B,H,W,Ch] (Ch = color channels). - **classes_file** (str): The filename of the txt file containing the class info. Returns: tensorflow.Tensor: [H,W,Cl] or [B,H,W,Cl] (Cl = number of classes). - `multiclass_to_label_image(class_label_tensor, classes_file)`: Converts multi-class labels to a label image. Returns tensorflow.Tensor. - `get_classes_color_from_file(classes_file)`: Retrieves class colors from a file. Returns ndarray. - `get_n_classes_from_file(classes_file)`: Retrieves the number of classes from a file. Returns int. - `get_classes_color_from_file_multilabel(classes_file)`: Get classes and code labels from txt file. This function deals with the case of elements with multiple labels. Parameters: - **classes_file** (str): File containing the classes (usually named _classes.txt_). Returns: Tuple[ndarray, array]: A tuple containing class colors and code labels. ``` -------------------------------- ### TextRegion Class Methods Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Methods for creating TextRegion objects from various formats. ```APIDOC ## TextRegion Class Methods ### `from_dict` Creates a TextRegion object from a serialized dictionary. **Parameters** * **dictionary** (`dict`) – serialized dictionary **Returns** `TextRegion` ### `from_xml` Creates a TextRegion object from an XML structure. **Parameters** * **etree_element** – a xml etree **Returns** a dictionary with keys ‘id’ and ‘coords’ ``` -------------------------------- ### dh_segment.io.serving_input_filename Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Generates serving input for filename-based input. ```APIDOC ## dh_segment.io.serving_input_filename ### Description Generates serving input for filename-based input. ### Parameters * **resized_size** (int) - The desired size for resizing. ### Returns fn - Serving input function. ``` -------------------------------- ### Load Annotation Data Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Loads the content of VIA annotation files. ```APIDOC ## load_annotation_data ### Description Load the content of via annotation files. ### Parameters - **via_data_filename** (`str`) – via annotations json file - **only_img_annotations** (`bool`) – load only the images annotations (‘_via_img_metadata’ field) - **via_version** (`int`) – ### Return type `dict` ### Returns the content of json file containing the region annotated ``` -------------------------------- ### multilabel_image_to_class Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Combines image annotations with classes info to create the input label for training. ```APIDOC ## multilabel_image_to_class ### Description Combines image annotations with classes info of the txt file to create the input label for the training. ### Parameters * **label_image**: The input label image. * **...**: Additional parameters. ``` -------------------------------- ### Metrics Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Utility class for evaluation metrics. ```APIDOC ## Metrics ### Description Utility class for evaluation metrics. ``` -------------------------------- ### BaseElement Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Base class for page elements. This is an abstract class. ```APIDOC ## BaseElement ### Description Base page element class. (Abstract) ### Class Methods * **check_tag**(_tag_) * **full_tag**() - Returns the full tag of the element. ### Attributes * **tag** (str, optional) - The tag associated with the element. ``` -------------------------------- ### local_entropy Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Calculates the local entropy of a binary image. ```APIDOC ## local_entropy ### Parameters * **tf_binary_img** (tensorflow.Tensor) - The input binary image tensor. * **sigma** (float) - The sigma value for the Gaussian kernel, defaults to 3. ### Returns tensorflow.Tensor - The calculated local entropy. ``` -------------------------------- ### Model Parameter Classes Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Classes for defining model-specific parameters. ```APIDOC ## Model Parameter Classes ### VGG16ModelParams Parameters for the VGG16 model. - `CORRECTED_VERSION`: (None) - `INTERMEDIATE_CONV`: [[(256, 3)]] - `PRETRAINED_MODEL_FILE`: 'pretrained_models/vgg_16.ckpt' - `SELECTED_LAYERS_UPSCALING`: [True, True, True, True, False, False] - `UPSCALE_PARAMS`: [[(32, 3)], [(64, 3)], [(128, 3)], [(256, 3)], [(512, 3)], [(512, 3)]] ### ResNetModelParams Parameters for the ResNet model. - `CORRECT_VERSION`: False - `INTERMEDIATE_CONV`: None - `PRETRAINED_MODEL_FILE`: 'pretrained_models/resnet_v1_50.ckpt' - `SELECTED_LAYERS_UPSCALING`: [True, True, True, True, True] - `UPSCALE_PARAMS`: [(32, 0), (64, 0), (128, 0), (256, 0), (512, 0)] ### UNetModelParams Parameters for the UNet model. - `CORRECT_VERSION`: False - `INTERMEDIATE_CONV`: None - `PRETRAINED_MODEL_FILE`: None - `SELECTED_LAYERS_UPSCALING`: None - `UPSCALE_PARAMS`: None ### ModelParams(_**kwargs_) Parameters related to the model. - `check_params()`: Checks model parameters. ``` -------------------------------- ### get_n_classes_from_file_multilabel Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Retrieves the number of classes for multi-label scenarios from a file. ```APIDOC ## get_n_classes_from_file_multilabel ### Description Retrieves the number of classes for multi-label scenarios from a file. ### Parameters * **classes_file**: Path to the file containing class information. ### Returns * **int**: The number of classes. ``` -------------------------------- ### ModelParams Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Parameters related to the model configuration. ```APIDOC ## ModelParams ### Description Parameters related to the model configuration. ### Parameters * **kwargs**: Arbitrary keyword arguments. ``` -------------------------------- ### Create Masks from VIA Annotations Source: https://dhsegment.readthedocs.io/en/latest/start/annotating.html This script processes VGG Image Annotator (VIA) JSON annotations to generate individual image masks for each annotated region and attribute option. Ensure VIA annotations favor 'dropdown', 'checkbox', or 'radio' attribute types for easier parsing. ```python from dh_segment.io import via collection = 'mycollection' annotation_file = 'via_sample.json' masks_dir = '/home/project/generated_masks' images_dir = './my_images' # Load all the data in the annotation file # (the file may be an exported project or an export of the annotations) via_data = via.load_annotation_data(annotation_file) # In the case of an exported project file, you can set ``only_img_annotations=True`` # to get only the image annotations via_annotations = via.load_annotation_data(annotation_file, only_img_annotations=True) # Collect the annotated regions working_items = via.collect_working_items(via_annotations, collection, images_dir) # Collect the attributes and options if '_via_attributes' in via_data.keys(): list_attributes = via.parse_via_attributes(via_data['_via_attributes']) else: list_attributes = via.get_via_attributes(via_annotations) # Create one mask per option per attribute via.create_masks(masks_dir, working_items, list_attributes, collection) ``` -------------------------------- ### Tensorflow serving functions Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Functions for creating serving input receivers for TensorFlow models. ```APIDOC ## serving_input_filename ### Description Serving input function based on filename. ### Parameters - **resized_size**: Type and description not specified in source. ### Method Not applicable (Python function) ### Endpoint Not applicable (Python function) ``` ```APIDOC ## serving_input_image ### Description Serving input function based on image data. ### Parameters None specified in source. ### Method Not applicable (Python function) ### Endpoint Not applicable (Python function) ``` -------------------------------- ### thresholding Source: https://dhsegment.readthedocs.io/en/latest/reference/post_processing.html Computes the binary mask of the detected Page from the probabilities output by network. ```APIDOC ## thresholding ### Description Computes the binary mask of the detected Page from the probabilities output by network. ### Method `thresholding(probs, threshold=-1)` ### Parameters #### Path Parameters - **probs** (ndarray) - Required - array in range [0, 1] of shape HxWx2 - **threshold** (float) - Optional - threshold between [0 and 1], if negative Otsu’s adaptive threshold will be used ### Return type `ndarray` ### Returns binary mask ``` -------------------------------- ### cleaning_binary Source: https://dhsegment.readthedocs.io/en/latest/reference/post_processing.html Uses mathematical morphology to clean and remove small elements from binary images. ```APIDOC ## cleaning_binary ### Description Uses mathematical morphology to clean and remove small elements from binary images. ### Method `cleaning_binary(mask, kernel_size=5)` ### Parameters #### Path Parameters - **mask** (ndarray) - Required - the binary image to clean - **kernel_size** (int) - Optional - size of the kernel ### Return type `ndarray` ### Returns the cleaned mask ``` -------------------------------- ### Utility Functions Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Helper functions for processing XML and JSON data. ```APIDOC ## Utility Functions ### `get_unique_tags_from_xml_text_regions` Get a list of all the values of labels/tags from XML text regions. **Parameters** * **xml_filename** (`str`) – filename of the xml file * **tag_pattern** (`str`) – regular expression pattern to look for in TextRegion.custom_attribute **Returns** ### `json_serialize` Serialize a dictionary in order to export it. **Parameters** * **dict_to_serialize** (`dict`) – dictionary to serialize * **non_serializable_keys** (`List`[`str`]) – keys that are not directly seriazable sucha as python objects **Returns** `dict` – the serialized dictionnary ### `parse_file` Parses the files to create the corresponding `Page` object. The files can be a .xml or a .json. **Parameters** * **filename** (`str`) ``` -------------------------------- ### create_via_region_from_coordinates Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Formats coordinates into a VIA region dictionary. ```APIDOC ## create_via_region_from_coordinates ### Description Formats coordinates to a VIA region (dict). ### Parameters - **coordinates** (array) - (N, 2) coordinates (x, y) - **region_attributes** (dict) - dictionary with keys : name of labels, values : values of labels - **type_region** (str) - via region annotation type (‘rect’, ‘polygon’) ### Return Type `dict` Returns a region in VIA style (dict/json). ``` -------------------------------- ### predict_with_tiles Method Source: https://dhsegment.readthedocs.io/en/latest/reference/inference.html Performs prediction on an image by dividing it into tiles. This is useful for large images that cannot be processed in memory at once. ```APIDOC ## LoadedModel.predict_with_tiles ### Description Performs prediction on an image by dividing it into tiles. ### Parameters * **filename** (str) - The path to the image file. * **resized_size** (tuple, optional) - The target size to resize the image to before tiling. If None, the original size is used. * **tile_size** (int, optional) - The size of each tile. Defaults to 500. * **min_overlap** (float, optional) - The minimum overlap between adjacent tiles. Defaults to 0.2. * **linear_interpolation** (bool, optional) - Whether to use linear interpolation when combining tile predictions. Defaults to True. ``` -------------------------------- ### Point.cv2_to_point_list Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Converts an opencv-formatted set of coordinates to a list of Point. ```APIDOC ## Point.cv2_to_point_list ### Description Converts an opencv-formatted set of coordinates to a list of Point. ### Parameters #### Parameters - **cv2_array** (ndarray) - opencv-formatted set of coordinates, shape (N,1,2) ### Returns List[Point] ``` -------------------------------- ### PAGE XML and JSON import / export Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Classes and functions for parsing and serializing PAGE XML and JSON data. ```APIDOC ## PAGE.Point ### Description Represents a point with (x,y) coordinates. ### Parameters - **y**: Type and description not specified in source. - **x**: Type and description not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.Text ### Description Represents a text entity, typically produced by a transcription system. ### Parameters - **text_equiv**: Type and description not specified in source. - **alternatives**: Type and description not specified in source. - **score**: Type and description not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.Border ### Description Represents a region containing the page border. ### Parameters - **coords**: Type and description not specified in source. - **id**: Type and description not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.TextRegion ### Description Represents a region containing text lines. ### Parameters - **id**: Type and description not specified in source. - **coords**: Type and description not specified in source. - **text_lines**: Type and description not specified in source. - **...**: Additional parameters not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.TextLine ### Description Represents a region corresponding to a text line. ### Parameters - **id**: Type and description not specified in source. - **coords**: Type and description not specified in source. - **baseline**: Type and description not specified in source. - **text**: Type and description not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.GraphicRegion ### Description Represents a region containing simple graphics. ### Parameters - **id**: Type and description not specified in source. - **coords**: Type and description not specified in source. - **...**: Additional parameters not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.TableRegion ### Description Represents a region containing tabular data. ### Parameters - **id**: Type and description not specified in source. - **coords**: Type and description not specified in source. - **rows**: Type and description not specified in source. - **...**: Additional parameters not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.SeparatorRegion ### Description Represents lines separating columns or paragraphs. ### Parameters - **id**: Type and description not specified in source. - **coords**: Type and description not specified in source. - **...**: Additional parameters not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.GroupSegment ### Description Represents a set of regions that form a larger region (group). ### Parameters - **id**: Type and description not specified in source. - **coords**: Type and description not specified in source. - **segment_ids**: Type and description not specified in source. - **...**: Additional parameters not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.Metadata ### Description Represents metadata information. ### Parameters - **creator**: Type and description not specified in source. - **created**: Type and description not specified in source. - **...**: Additional parameters not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.Page ### Description Represents a class following the PAGE-XML object structure. ### Parameters - **kwargs**: Type and description not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.BaseElement ### Description Base class for PAGE elements. ### Parameters None specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.Region ### Description Base class for PAGE regions. ### Parameters - **id**: Type and description not specified in source. - **coords**: Type and description not specified in source. - **custom_attribute**: Type and description not specified in source. ### Method Not applicable (Python class) ### Endpoint Not applicable (Python class) ``` ```APIDOC ## PAGE.parse_file ### Description Parses a file to create the corresponding `Page` object. ### Parameters - **filename**: Type and description not specified in source. ### Method Not applicable (Python function) ### Endpoint Not applicable (Python function) ``` ```APIDOC ## PAGE.json_serialize ### Description Serializes a dictionary for export. ### Parameters - **dict_to_serialize**: Type and description not specified in source. - **...**: Additional parameters not specified in source. ### Method Not applicable (Python function) ### Endpoint Not applicable (Python function) ``` -------------------------------- ### Point Conversion Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Provides a class method to convert a list of Point objects into a list of coordinates. ```APIDOC ## _classmethod_`point_to_list`(_points_) ### Description Converts a list of Point to a list of coordinates. ### Parameters #### Path Parameters - **points** (List[Point]) - Required - list of Points ### Returns - **list** - list of shape (N,2) ``` -------------------------------- ### Point.list_from_xml Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Converts a PAGEXML-formatted set of coordinates to a list of Point. ```APIDOC ## Point.list_from_xml ### Description Converts a PAGEXML-formatted set of coordinates to a list of Point. ### Parameters #### Parameters - **etree_elem** (Element) - etree XML element containing a set of coordinates ### Returns List[Point] ``` -------------------------------- ### inference_u_net Source: https://dhsegment.readthedocs.io/en/latest/reference/network.html Performs inference using the U-Net model architecture. ```APIDOC ## inference_u_net ### Description Performs inference using the U-Net model architecture. ### Parameters - **images** (type) - Description - **params** (type) - Description - **num_classes** (type) - Description - **use_batch_norm** (bool) - Optional - Whether to use batch normalization. - **weight_decay** (float) - Optional - The weight decay factor. - **is_training** (bool) - Optional - Whether the model is in training mode. ### Return Type tensorflow.Tensor ``` -------------------------------- ### create_masks Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Creates binary masks for annotations from VIA data. ```APIDOC ## create_masks ### Description For each annotation, create a corresponding binary mask and resize it (h = 2000). Only valid for VIA 2.0. Several annotations of the same class on the same image produce one image with several masks. ### Parameters - **masks_dir** (str) - where to output the masks - **working_items** (List[WorkingItem]) - infos to work with - **via_attributes** (List[VIAttribute]) - VIAttributes computed by `get_via_attributes` function. - **collection** (str) - name of the collection - **contours_only** (bool) - creates the binary masks only for the contours of the object (thickness of contours : 20 px) ### Return Type `dict` Returns annotation_summary, a dictionary containing a list of labels per image. ``` -------------------------------- ### Point.list_to_point Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Converts a list of coordinates to a list of Point. ```APIDOC ## Point.list_to_point ### Description Converts a list of coordinates to a list of Point. ### Parameters #### Parameters - **list_coords** (list) - list of coordinates, shape (N, 2) ### Returns List[Point] ``` -------------------------------- ### get_classes_color_from_file Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Retrieves class colors from a file. ```APIDOC ## get_classes_color_from_file ### Description Retrieves class colors from a file. ### Parameters * **classes_file**: Path to the file containing class information. ### Returns * **ndarray**: An array containing class colors. ``` -------------------------------- ### Page Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Represents the entire page information, including image details, text regions, graphic regions, and metadata. Can be exported to PAGE-XML or JSON. ```APIDOC ## Page ### Description Class following PAGE-XML object. This class is used to represent the information of the processed image. It is possible to export this info as PAGE-XML or JSON format. ### Attributes * **image_filename** (str) - Filename of the image. * **image_width** (int) - Width of the original image. * **image_height** (int) - Height of the original image. * **text_regions** (list) - List of TextRegion objects. * **graphic_regions** (list) - List of GraphicRegion objects. * **page_border** (Border) - Border object defining the page boundaries. * **separator_regions** (list) - List of SeparatorRegion objects. * **table_regions** (list) - List of TableRegion objects. * **metadata** (Metadata) - Metadata object containing information about the image and process. * **line_groups** (list) - List of GroupSegment objects forming lines. ``` -------------------------------- ### Metadata Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Stores metadata information about the image processing, including creator, creation time, and comments. ```APIDOC ## Metadata ### Description Metadata information. ### Class Methods * **from_dict**(_dictionary_) - Creates a Metadata object from a dictionary. * **from_xml**(_e_) - Creates a Metadata object from an XML element. ### Methods * **to_dict**() - Converts the Metadata object to a dictionary. * **to_xml**() - Converts the Metadata object to an XML Element. ### Attributes * **creator** - Name of the process or person that created the exported file. * **created** - Time of creation of the file. * **last_change** - Time of last modification of the file. * **comments** - Comments on the process. * **tag** - 'Metadata' ``` -------------------------------- ### Input functions for tf.Estimator Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Provides input functions and data augmentation utilities for use with TensorFlow Estimators. ```APIDOC ## input_fn ### Description Input function for TensorFlow Estimators. ### Parameters - **input_data**: Type and description not specified in source. - **params**: Type and description not specified in source. - **...**: Additional parameters not specified in source. ### Method Not applicable (Python function) ### Endpoint Not applicable (Python function) ``` ```APIDOC ## data_augmentation_fn ### Description Applies data augmentation to both input images and label images. ### Parameters - **input_image**: Type and description not specified in source. - **label_image**: Type and description not specified in source. ### Method Not applicable (Python function) ### Endpoint Not applicable (Python function) ``` ```APIDOC ## extract_patches_fn ### Description Cuts a given image into patches. ### Parameters - **image**: Type and description not specified in source. - **patch_shape**: Type and description not specified in source. - **offsets**: Type and description not specified in source. ### Method Not applicable (Python function) ### Endpoint Not applicable (Python function) ``` ```APIDOC ## rotate_crop ### Description Rotates and crops the images. ### Parameters - **image**: Type and description not specified in source. - **rotation**: Type and description not specified in source. - **crop**: Type and description not specified in source. - **...**: Additional parameters not specified in source. ### Method Not applicable (Python function) ### Endpoint Not applicable (Python function) ``` ```APIDOC ## resize_image ### Description Resizes the image to a specified size. ### Parameters - **image**: Type and description not specified in source. - **size**: Type and description not specified in source. - **interpolation**: Type and description not specified in source. ### Method Not applicable (Python function) ### Endpoint Not applicable (Python function) ``` ```APIDOC ## load_and_resize_image ### Description Loads an image from its filename and resizes it to the desired output size. ### Parameters - **filename**: Type and description not specified in source. - **channels**: Type and description not specified in source. - **...**: Additional parameters not specified in source. ### Method Not applicable (Python function) ### Endpoint Not applicable (Python function) ``` -------------------------------- ### write_to_file Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Export Page object to json or page-xml format. Will assume the format based on the extension of the filename, if there is no extension will export as an xml file. ```APIDOC ## write_to_file ### Description Export Page object to json or page-xml format. Will assume the format based on the extension of the filename, if there is no extension will export as an xml file. ### Parameters #### Parameters - **filename** (str) - filename of the file to be exported - **creator_name** (str) - name of the creator (process or person) creating the file - **comments** (str) - optionnal comment to add to the metadata of the file. ### Return Type None ``` -------------------------------- ### draw_baselines Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Draws the TextLines' baselines on the image canvas. Coordinates can be autiscaled. ```APIDOC ## draw_baselines ### Description Given an image, draws the TextLines.baselines. ### Parameters * **img_canvas** (`ndarray`) – 3 channel image in which the region will be drawn. The image is modified inplace. * **color** (`Tuple`[`int`, `int`, `int`]) – (R, G, B) value color * **thickness** (`int`) – the thickness of the line * **endpoint_radius** (`int`) – the radius of the endpoints of line s(first and last coordinates of line) * **autoscale** (`bool`) – whether to scale the coordinates to the size of img_canvas. If True, it will use the dimensions provided in Page.image_width and Page.image_height to compute the scaling ratio ``` -------------------------------- ### LoadedModel Class Source: https://dhsegment.readthedocs.io/en/latest/reference/inference.html Loads an exported dhSegment model. This class is used to initialize a model for inference. ```APIDOC ## class dh_segment.inference.LoadedModel ### Description Loads an exported dhSegment model. ### Parameters * **model_base_dir** (str) - The model directory containing saved_model.{pb|pbtxt}. If not provided, it is assumed to be a TF exporter directory, and the latest export directory will be automatically selected. * **predict_mode** (str, optional) - Defines the input/output format of the prediction output. Defaults to 'filename'. * **num_parallel_predictions** (int, optional) - Limits the number of concurrent calls to predict to avoid Out-Of-Memory issues if predicting on GPU. Defaults to 2. ``` -------------------------------- ### VIAttribute Class Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html A container for VIA attributes, representing metadata for annotations. ```APIDOC ## VIAttribute Class ### Description A container for VIA attributes. This class holds information about annotation attributes like name, type, and options. ### Parameters - **name** (str) - The name of attribute - **type** (str) - The type of the annotation (dropdown, markbox, …) - **options** (list) - The options / labels possible for this attribute. ### Properties - **name** (str) - Alias for field number 0 - **type** (str) - Alias for field number 1 - **options** (list) - Alias for field number 2 ``` -------------------------------- ### Point.list_point_to_string Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Converts a list of Point to a string ‘x,y’. ```APIDOC ## Point.list_point_to_string ### Description Converts a list of Point to a string ‘x,y’. ### Parameters #### Parameters - **list_points** (List[Point]) - list of coordinates with Point format ### Returns a string with the coordinates ``` -------------------------------- ### multiclass_to_label_image Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Converts multi-class labels to a label image. ```APIDOC ## multiclass_to_label_image ### Description Converts multi-class labels to a label image. ### Returns * **tensorflow.Tensor**: The resulting label image. ``` -------------------------------- ### get_n_classes_from_file Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Retrieves the number of classes from a file. ```APIDOC ## get_n_classes_from_file ### Description Retrieves the number of classes from a file. ### Parameters * **classes_file**: Path to the file containing class information. ### Returns * **int**: The number of classes. ``` -------------------------------- ### Multilabel Class Definition Source: https://dhsegment.readthedocs.io/en/latest/start/training.html For multilabel classification, extend 'classes.txt' with an attribution code indicating which classes are assigned to each color. The attribution code has a length equal to the number of classes. ```text classes.txt 0 0 0 0 0 0 0 255 0 1 0 0 255 0 0 0 1 0 0 0 255 0 0 1 128 128 128 1 1 0 0 255 255 0 1 1 ``` -------------------------------- ### export_annotation_dict Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Exports an annotation dictionary to a file. ```APIDOC ## export_annotation_dict ### Description Exports an annotation dictionary to a specified file. ### Parameters - **annotation_dict** - The dictionary containing annotations to export. - **filename** (str) - The name of the file to save the annotations to. ``` -------------------------------- ### label_image_to_class Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Converts a label image to class labels. ```APIDOC ## label_image_to_class ### Description Converts a label image to class labels. ### Parameters * **label_image**: The input label image. * **classes_file**: Path to the file containing class information. ### Returns * **tensorflow.Tensor**: The resulting class labels. ``` -------------------------------- ### Parse VIA Attributes Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Parses a VIA attribute dictionary into a list of VIAttribute instances. ```APIDOC ## parse_via_attributes ### Description Parses the VIA attribute dictionary and returns a list of VIAttribute instances. ### Parameters - **via_attributes** (`dict`) – attributes from VIA annotation (‘_via_attributes’ field) ### Return type `List`[`VIAttribute`] ### Returns list of `VIAttribute` ``` -------------------------------- ### Point.array_to_point Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Converts an np.array to a list of Point. ```APIDOC ## Point.array_to_point ### Description Converts an np.array to a list of Point. ### Parameters #### Parameters - **array** (ndarray) - an array of coordinates. Must be of shape (N, 2) ### Returns list of Point ``` -------------------------------- ### collect_working_items Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Collects all WorkingItem objects from VIA annotations. ```APIDOC ## collect_working_items ### Description Given VIA annotation input, collect all info on WorkingItem object. This function will take care of separating images from local files and images from IIIF urls. ### Parameters - **via_annotations** (dict) - via annotations (‘regions’ field) - **collection_name** (str) - name of the collection - **images_dir** (Optional[str]) - directory where to find the images - **via_version** (int) - version of the VIA tool used to produce the annotations (1 or 2) ### Return Type `List[WorkingItem]` Returns a list of WorkingItem objects. ``` -------------------------------- ### load_pickle Source: https://dhsegment.readthedocs.io/en/latest/reference/utils.html Loads a Python object from a pickle file. This function is useful for deserializing objects that were previously saved using Python's pickle module. ```APIDOC ## load_pickle(filename) ### Description Loads and deserializes a Python object from a pickle file specified by `filename`. ### Parameters #### Path Parameters - **filename** (string) - Required - The path to the pickle file to be loaded. ``` -------------------------------- ### draw_text_regions Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Given an image, draws the TextRegions, either fills it (fill=True) or draws the contours (fill=False). The image is modified in-place. ```APIDOC ## draw_text_regions ### Description Given an image, draws the TextRegions, either fills it (fill=True) or draws the contours (fill=False). The image is modified in-place. ### Parameters #### Parameters - **img_canvas** (ndarray) - 3 channel image in which the region will be drawn. The image is modified inplace. - **color** (Tuple[int, int, int]) - (R, G, B) value color - **fill** (bool) - either to fill the region (True) of only draw the external contours (False) - **thickness** (int) - in case fill=True the thickness of the line - **autoscale** (bool) - whether to scale the coordinates to the size of img_canvas. If True, it will use the dimensions provided in Page.image_width and Page.image_height to compute the scaling ratio ``` -------------------------------- ### inference_vgg16 Source: https://dhsegment.readthedocs.io/en/latest/reference/network.html Performs inference using the VGG16 model architecture. ```APIDOC ## inference_vgg16 ### Description Performs inference using the VGG16 model architecture. ### Parameters - **images** (type) - Description - **params** (type) - Description - **num_classes** (type) - Description - **use_batch_norm** (bool) - Optional - Whether to use batch normalization. - **weight_decay** (float) - Optional - The weight decay factor. - **is_training** (bool) - Optional - Whether the model is in training mode. ### Return Type tensorflow.Tensor ``` -------------------------------- ### Export Annotations to JSON Source: https://dhsegment.readthedocs.io/en/latest/reference/io.html Exports VIA annotations to a JSON file. ```APIDOC ## export_annotations_to_json ### Description Exports the annotations to a json file. ### Parameters - **annotation_dict** (`dict`) – VIA annotations - **filename** (`str`) – filename to export the data (json file) ### Return type `None` ```