Module dvt.annotate

Annotators for extracting high-level metadata about the images in the input.

Expand source code
# -*- coding: utf-8 -*-
"""Annotators for extracting high-level metadata about the images in the input.
"""

from os.path import join

from cv2 import imwrite, resize, cvtColor, COLOR_BGR2HSV

from .abstract import ImageAnnotator
from .utils import _check_out_dir


class SizeAnnotator(ImageAnnotator):
    """Annotator for grabbing metadata about the images in the batch.

    Attributes:
        name (str): A description of the aggregator. Used as a key in the
            output data.
    """

    def annotate_image(self, img):
        """Annotate the batch of frames with the image annotator.

        Args:
            img (Array)

        Returns:
            A dictionary containing the height and width of the input image.
        """
        output = {"size": {"height": [img.shape[0]], "width": [img.shape[1]]}}
        return output


class AverageAnnotator(ImageAnnotator):
    """Annotator for grabbing"""

    def annotate_image(self, img):
        """Annotate the batch of frames with the image annotator.

        Args:
            img (Array)

        Returns:
            A dictionary containing the average value and saturation of the
            image.
        """
        img_hsv = cvtColor(img, COLOR_BGR2HSV)
        output = {
            "average": {
                "saturation": [img_hsv[:, :, 1].mean()],
                "val": [img_hsv[:, :, 2].mean()],
            }
        }
        return output


class ImwriteAnnotator(ImageAnnotator):
    """Annotator for saving still images from an input.

    The annotate method of this annotator does not return any data. It is
    useful only for its side effects.
    """

    def __init__(self, **kwargs):
        self.output_dir = _check_out_dir(kwargs["output_dir"])
        self.size = kwargs.get("size", None)

        super().__init__(**kwargs)

    def annotate_image(self, img, **kwargs):
        """Annotate the images."""
        opath = join(self.output_dir, kwargs.get("fname", "figure.png"))

        if self.size is not None:
            scale = img.shape[1] / self.size
            new_size = (int(img.shape[2] // scale), int(self.size))
            img_resize = resize(img, new_size)
            imwrite(filename=opath, img=img_resize)
        else:
            imwrite(filename=opath, img=img)

Functions

def cvtColor(src, code, dst, dstCn)

cvtColor(src, code[, dst[, dstCn]]) -> dst . @brief Converts an image from one color space to another. .
. The function converts an input image from one color space to another. In case of a transformation . to-from RGB color space, the order of the channels should be specified explicitly (RGB or BGR). Note . that the default color format in OpenCV is often referred to as RGB but it is actually BGR (the . bytes are reversed). So the first byte in a standard (24-bit) color image will be an 8-bit Blue . component, the second byte will be Green, and the third byte will be Red. The fourth, fifth, and . sixth bytes would then be the second pixel (Blue, then Green, then Red), and so on. .
. The conventional ranges for R, G, and B channel values are: . - 0 to 255 for CV_8U images . - 0 to 65535 for CV_16U images . - 0 to 1 for CV_32F images .
. In case of linear transformations, the range does not matter. But in case of a non-linear . transformation, an input RGB image should be normalized to the proper value range to get the correct . results, for example, for RGB \f$\rightarrow\f$ L*u*v* transformation. For example, if you have a . 32-bit floating-point image directly converted from an 8-bit image without any scaling, then it will . have the 0..255 value range instead of 0..1 assumed by the function. So, before calling #cvtColor , . you need first to scale the image down: . @code . img *= 1./255; . cvtColor(img, img, COLOR_BGR2Luv); . @endcode . If you use #cvtColor with 8-bit images, the conversion will have some information lost. For many . applications, this will not be noticeable but it is recommended to use 32-bit images in applications . that need the full range of colors or that convert an image before an operation and then convert . back. .
. If conversion adds the alpha channel, its value will set to the maximum of corresponding channel . range: 255 for CV_8U, 65535 for CV_16U, 1 for CV_32F. .
. @param src input image: 8-bit unsigned, 16-bit unsigned ( CV_16UC… ), or single-precision . floating-point. . @param dst output image of the same size and depth as src. . @param code color space conversion code (see #ColorConversionCodes). . @param dstCn number of channels in the destination image; if the parameter is 0, the number of the . channels is derived automatically from src and code. .
. @see @ref imgproc_color_conversions

def imwrite(filename, img, params)

imwrite(filename, img[, params]) -> retval . @brief Saves an image to a specified file. .
. The function imwrite saves the image to the specified file. The image format is chosen based on the . filename extension (see cv::imread for the list of extensions). In general, only 8-bit . single-channel or 3-channel (with 'BGR' channel order) images . can be saved using this function, with these exceptions: .
. - 16-bit unsigned (CV_16U) images can be saved in the case of PNG, JPEG 2000, and TIFF formats . - 32-bit float (CV_32F) images can be saved in PFM, TIFF, OpenEXR, and Radiance HDR formats; . 3-channel (CV_32FC3) TIFF images will be saved using the LogLuv high dynamic range encoding . (4 bytes per pixel) . - PNG images with an alpha channel can be saved using this function. To do this, create . 8-bit (or 16-bit) 4-channel image BGRA, where the alpha channel goes last. Fully transparent pixels . should have alpha set to 0, fully opaque pixels should have alpha set to 255/65535 (see the code sample below). . - Multiple images (vector of Mat) can be saved in TIFF format (see the code sample below). .
. If the format, depth or channel order is different, use . Mat::convertTo and cv::cvtColor to convert it before saving. Or, use the universal FileStorage I/O . functions to save the image to XML or YAML format. .
. The sample below shows how to create a BGRA image, how to set custom compression parameters and save it to a PNG file. . It also demonstrates how to save multiple images in a TIFF file: . @include snippets/imgcodecs_imwrite.cpp . @param filename Name of the file. . @param img (Mat or vector of Mat) Image or Images to be saved. . @param params Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, … .) see cv::ImwriteFlags

def resize(src, dsize, dst, fx, fy, interpolation)

resize(src, dsize[, dst[, fx[, fy[, interpolation]]]]) -> dst . @brief Resizes an image. .
. The function resize resizes the image src down to or up to the specified size. Note that the . initial dst type or size are not taken into account. Instead, the size and type are derived from . the src,dsize,fx, and fy. If you want to resize src so that it fits the pre-created dst, . you may call the function as follows: . @code . // explicitly specify dsize=dst.size(); fx and fy will be computed from that. . resize(src, dst, dst.size(), 0, 0, interpolation); . @endcode . If you want to decimate the image by factor of 2 in each direction, you can call the function this . way: . @code . // specify fx and fy and let the function compute the destination image size. . resize(src, dst, Size(), 0.5, 0.5, interpolation); . @endcode . To shrink an image, it will generally look best with #INTER_AREA interpolation, whereas to . enlarge an image, it will generally look best with c#INTER_CUBIC (slow) or #INTER_LINEAR . (faster but still looks OK). .
. @param src input image. . @param dst output image; it has the size dsize (when it is non-zero) or the size computed from . src.size(), fx, and fy; the type of dst is the same as of src. . @param dsize output image size; if it equals zero, it is computed as: . \f[\texttt{dsize = Size(round(fxsrc.cols), round(fysrc.rows))}\f] . Either dsize or both fx and fy must be non-zero. . @param fx scale factor along the horizontal axis; when it equals 0, it is computed as . \f[\texttt{(double)dsize.width/src.cols}\f] . @param fy scale factor along the vertical axis; when it equals 0, it is computed as . \f[\texttt{(double)dsize.height/src.rows}\f] . @param interpolation interpolation method, see #InterpolationFlags .
. @sa warpAffine, warpPerspective, remap

Classes

class AverageAnnotator

Annotator for grabbing

Expand source code
class AverageAnnotator(ImageAnnotator):
    """Annotator for grabbing"""

    def annotate_image(self, img):
        """Annotate the batch of frames with the image annotator.

        Args:
            img (Array)

        Returns:
            A dictionary containing the average value and saturation of the
            image.
        """
        img_hsv = cvtColor(img, COLOR_BGR2HSV)
        output = {
            "average": {
                "saturation": [img_hsv[:, :, 1].mean()],
                "val": [img_hsv[:, :, 2].mean()],
            }
        }
        return output

Ancestors

Methods

def annotate_image(self, img)

Annotate the batch of frames with the image annotator.

Args

img (Array)

Returns

A dictionary containing the average value and saturation of the image.

Expand source code
def annotate_image(self, img):
    """Annotate the batch of frames with the image annotator.

    Args:
        img (Array)

    Returns:
        A dictionary containing the average value and saturation of the
        image.
    """
    img_hsv = cvtColor(img, COLOR_BGR2HSV)
    output = {
        "average": {
            "saturation": [img_hsv[:, :, 1].mean()],
            "val": [img_hsv[:, :, 2].mean()],
        }
    }
    return output

Inherited members

class ImwriteAnnotator (**kwargs)

Annotator for saving still images from an input.

The annotate method of this annotator does not return any data. It is useful only for its side effects.

Expand source code
class ImwriteAnnotator(ImageAnnotator):
    """Annotator for saving still images from an input.

    The annotate method of this annotator does not return any data. It is
    useful only for its side effects.
    """

    def __init__(self, **kwargs):
        self.output_dir = _check_out_dir(kwargs["output_dir"])
        self.size = kwargs.get("size", None)

        super().__init__(**kwargs)

    def annotate_image(self, img, **kwargs):
        """Annotate the images."""
        opath = join(self.output_dir, kwargs.get("fname", "figure.png"))

        if self.size is not None:
            scale = img.shape[1] / self.size
            new_size = (int(img.shape[2] // scale), int(self.size))
            img_resize = resize(img, new_size)
            imwrite(filename=opath, img=img_resize)
        else:
            imwrite(filename=opath, img=img)

Ancestors

Methods

def annotate_image(self, img, **kwargs)

Annotate the images.

Expand source code
def annotate_image(self, img, **kwargs):
    """Annotate the images."""
    opath = join(self.output_dir, kwargs.get("fname", "figure.png"))

    if self.size is not None:
        scale = img.shape[1] / self.size
        new_size = (int(img.shape[2] // scale), int(self.size))
        img_resize = resize(img, new_size)
        imwrite(filename=opath, img=img_resize)
    else:
        imwrite(filename=opath, img=img)

Inherited members

class SizeAnnotator

Annotator for grabbing metadata about the images in the batch.

Attributes

name : str
A description of the aggregator. Used as a key in the output data.
Expand source code
class SizeAnnotator(ImageAnnotator):
    """Annotator for grabbing metadata about the images in the batch.

    Attributes:
        name (str): A description of the aggregator. Used as a key in the
            output data.
    """

    def annotate_image(self, img):
        """Annotate the batch of frames with the image annotator.

        Args:
            img (Array)

        Returns:
            A dictionary containing the height and width of the input image.
        """
        output = {"size": {"height": [img.shape[0]], "width": [img.shape[1]]}}
        return output

Ancestors

Methods

def annotate_image(self, img)

Annotate the batch of frames with the image annotator.

Args

img (Array)

Returns

A dictionary containing the height and width of the input image.

Expand source code
def annotate_image(self, img):
    """Annotate the batch of frames with the image annotator.

    Args:
        img (Array)

    Returns:
        A dictionary containing the height and width of the input image.
    """
    output = {"size": {"height": [img.shape[0]], "width": [img.shape[1]]}}
    return output

Inherited members