Samples

Methods to generate sample images (random noise) and load an image from disk.
annot = json.load(open('../data/annots.json'))
annot
[{'x_min': 130, 'y_min': 63, 'x_max': 225, 'y_max': 180, 'label': 'clock'},
 {'x_min': 13, 'y_min': 158, 'x_max': 90, 'y_max': 213, 'label': 'frame'}]

Rescale the annotations to match the new image size. Can individually process dicts.

orig_sz=(256, 256)
new_sz=(200, 200)
_scale_annots_dict(annot[0], new_sz=new_sz, orig_sz=orig_sz)
{'x_min': 102, 'y_min': 49, 'x_max': 176, 'y_max': 141, 'label': 'clock'}

Processes lists of dicts

_get_scaled_annots(annot, new_sz, orig_sz)
[{'x_min': 102, 'y_min': 49, 'x_max': 176, 'y_max': 141, 'label': 'clock'},
 {'x_min': 10, 'y_min': 123, 'x_max': 70, 'y_max': 166, 'label': 'frame'}]

Also works with lists with labels.

_get_scaled_annots([[100, 150, 180, 256, 'hat'], [100, 120, 256, 200, 'shirt']], new_sz, orig_sz)
[{'x_min': 78, 'y_min': 117, 'x_max': 141, 'y_max': 200, 'label': 'hat'},
 {'x_min': 78, 'y_min': 94, 'x_max': 200, 'y_max': 156, 'label': 'shirt'}]
_get_scaled_annots([[100, 150, 180, 256], [100, 120, 256, 200]], new_sz, orig_sz) # without labels
[{'x_min': 78, 'y_min': 117, 'x_max': 141, 'y_max': 200},
 {'x_min': 78, 'y_min': 94, 'x_max': 200, 'y_max': 156}]
cv2 = _require_cv2()
image_path = '../data/image.jpg' if os.path.exists('../data/image.jpg') else 'data/image.jpg'
cv2.cvtColor(cv2.imread(image_path), cv2.COLOR_BGR2RGB).shape
(256, 256, 3)
img, annots, logits, color = _get_example(
    image_sz=orig_sz,
    feature_sz=(10, 10),
    pth="../data",
    img_fn="image.jpg",
    load_ann=True,
    ann_fn="annots.json",
)
annots # will not change as the same size is kept
[{'x_min': 130, 'y_min': 63, 'x_max': 225, 'y_max': 180, 'label': 'clock'},
 {'x_min': 13, 'y_min': 158, 'x_max': 90, 'y_max': 213, 'label': 'frame'}]
json.load(open('../data/annots.json'))
[{'x_min': 130, 'y_min': 63, 'x_max': 225, 'y_max': 180, 'label': 'clock'},
 {'x_min': 13, 'y_min': 158, 'x_max': 90, 'y_max': 213, 'label': 'frame'}]
img, annots, logits, color = _get_example(
    image_sz=new_sz, # change image size
    feature_sz=(10, 10),
    pth="../data",
    img_fn="image.jpg",
    load_ann=True,
    ann_fn="annots.json",
)
annots
[{'x_min': 102, 'y_min': 49, 'x_max': 176, 'y_max': 141, 'label': 'clock'},
 {'x_min': 10, 'y_min': 123, 'x_max': 70, 'y_max': 166, 'label': 'frame'}]

source

get_given_array

def get_given_array(
    image_arr, **kwargs
):

Get the image_array setup for visualisation. :param image_arr: image nparray :return: reference to protected _get_given_array()


source

get_example

def get_example(
    image_sz:tuple, **kwargs
):

Get an example image from the pth given for some image size for a feature size :param image_sz: required image size (will resize the original image) :return: reference to protected _get_example()