Utilities

Here you will find all those functionalities that do not fit in the rest of the modules.

Annotations Parser

class py2vision.utils.annotations_parser.AnnotationsFormat[source]

The AnnotationsFormat Interface declares a set of visiting methods that correspond to Parser classes. The signature of a visiting method allows the visitor to identify the exact class of the Parser that it’s dealing with.

class py2vision.utils.annotations_parser.Parser[source]

The Parser interface declares an parse method that should take the base AnnotationsFormat interface as an argument.

class py2vision.utils.annotations_parser.XmlParser[source]

Each Concrete Parser must implement the parse method in such a way that it calls the annotationsFormat’s method corresponding to the Parser’s class.

parse(anno: py2vision.utils.annotations_parser.AnnotationsFormat, xml_path, annotations_output_name, classes_output_name, image_path, work_dir=None, print_output=False)[source]

This method convert annotations from COCO or PASCAL VOC dataset in xml format to be compatible with an especific network model. Exporting a text file for annotations and a text file for classes names

Parameters:
  • xml_path – a string with the full path of xml annotations.
  • annotations_output_name – a string with the name of annotations file that will be generated.
  • classes_output_name – a string with the name of classes file that will be generated.
  • image_path – a full path where the images are saved.
  • work_dir – a path where the annotations and classes files will be saved, if is None these will be saved in current directory.
  • print_output – a boolean to print in console each annotation line
class py2vision.utils.annotations_parser.YoloV3AnnotationsFormat[source]

Get a group of xml annotations to transform in a .txt file compatible with YoloV3 dataset

Example of code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from py2vision.utils.annotations_parser import XmlParser, YoloV3AnnotationsFormat

anno_out_file = "test_anno_file"
xml_path = "tests/test_dataset/annotations"
classes_out_file = "test_classes"
work_dir = "tests"
images_path = "tests/test_dataset/images"

parser = XmlParser()
anno_format = YoloV3AnnotationsFormat()
parser.parse(anno_format, xml_path, anno_out_file, classes_out_file, images_path, work_dir)

Annotations Helper

class py2vision.utils.annotations_helper.AnnotationsHelper(annotations_path)[source]

This help to split annotations in .txt format.

Parameters:annotations_path – a path with a .txt annotations file.
export(data, file_path)[source]

To export a dataframe like a .txt file.

Parameters:
  • data – a pandas dataframe
  • file_path – output file path
shuffle()[source]

shuffle annotations

split(train_percentage=0.8, random_state=25)[source]

Split annotations in two dataframes in reference of a percentage.

Parameters:
  • train_percentage – a float between (0, 1) that corresponds with train data proportion.
  • random_state – int, array-like, BitGenerator, np.random.RandomState
Returns:

A tuple where the first element is train data (DataFrame) and the second is test data (DataFrame)

Example of code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from py2vision.utils.annotations_parser import XmlParser, YoloV3AnnotationsFormat
from py2vision.utils.annotations_helper import AnnotationsHelper

anno_out_file = "annotations_formated"
xml_path = "tests/test_dataset/annotations"
classes_file = "test_dataset_generator"
work_dir = "tests/test_dataset/to_generator_test"
images_path = "tests/test_dataset/images"

try:
    os.mkdir(work_dir)
except:
    pass

#create annotations formated
parser = XmlParser()
anno_format = YoloV3AnnotationsFormat()
parser.parse(anno_format, xml_path, anno_out_file, classes_file, images_path, work_dir)

training_percen = 0.8
anno_out_full_path = os.path.join(work_dir, "{}.txt".format(anno_out_file))
anno_helper = AnnotationsHelper(anno_out_full_path)

train, test = anno_helper.split(training_percen)
anno_helper.export(train, os.path.join(work_dir, "train.txt"))
anno_helper.export(test, os.path.join(work_dir, "test.txt"))

Draw functions

py2vision.utils.draw.draw_bbox(image, bboxes, class_file_name, show_label=True, show_confidence=True, text_colors=(255, 255, 0), rectangle_colors='', tracking=False, homogeneous_points=None)[source]

Draw bounding boxes on images

Parameters:
  • image – an array which correspond with an image
  • bboxes – their bounding boxes.
  • class_file_name – a path with a .txt file where the classes are saved.
  • show_label – a boolean to show or hide object label.
  • show_confidence – a boolean to show or hide confidence level.
  • text_colors – a tuple that represents (R, G, B) colors.
  • rectangle_colors – if this parameter is a string empty bounding box colors will be assing by default, however if rectangle_colors is a tuple like: (R, G, B) that will be bounding box colors.
  • homogeneous_points – an array with dimensions n x 4 where each row is like (X, Y, Z, W). However if is None it won’t be drawed.
Returns:

An image with bounding boxes and homogeneous coordinates.

py2vision.utils.draw.draw_lines(img1, img2, lines, pts1, pts2)[source]

img1 - image on which we draw the epilines for the points in img2 lines - corresponding epilines

Label utils

py2vision.utils.label_utils.class2index(class_name, classes: list)[source]

Convert class name (string) to index (int)

py2vision.utils.label_utils.index2class(index, classes: list)[source]

Convert index (int) to class name (string)

py2vision.utils.label_utils.label_map(labels, dst_path=None, name='label_map')[source]

An easy way to convert classes names and ids to a .pbtxt file compatible with tensorflow models API.

Parameters:
  • labels – a list, which each element is a dictionary with two keys ‘name’ and ‘id’.
  • dst_path – a path where the file will be saved.
  • name – the name of the file, with which it will save the .pbtxt
Returns:

a string where the file was saved.

Raises:
  • Exception – When someone element in internal dictionaries have another keys different of name and id.
  • TypeError – when labels aren’t list type
  • ValueError – when labels are empty
py2vision.utils.label_utils.read_class_names(class_file_name)[source]

loads class name from a file to a dict