Input Image

In order to use many of the Trueface AI inference functions (face detection, face recognition, etc), you must first preprocess your image. Once you have preprocessed your image, you are ready to start calling the various Trueface AI inference functions.

class tfsdk.TFImage
TFImage.rotate(self: tfsdk.TFImage, rotation_flag: tfsdk.ROTATEFLAGS)None

Rotate the image.

Parameters

rotation_flag - The tfsdk.ROTATEFLAGS rotation flag.

TFImage.resize(self: tfsdk.TFImage, width: int, height: int)None

Resize the image to the target size. Adds padding to the bottom and right if required in order to maintain aspect ratio.

Parameters
  • width – The target width.

  • height – The target height.

TFImage.save_image(self: tfsdk.TFImage, filepath: str)None

Save the preprocessed image to disk.

Parameters

filepath - the filepath where the image should be saved, including the image extension.

TFImage.get_height(self: tfsdk.TFImage)int

Get the image height in pixels.

Returns

Returns the image height in pixels.

TFImage.get_width(self: tfsdk.TFImage)int

Get the image width in pixels.

Returns

Returns the image width in pixels.

TFImage.as_numpy_array(self: tfsdk.TFImage)numpy.ndarray[numpy.uint8]

Get the image as a numpy array suitable for use with opencv.

Note

This returns the image in BGR format

See also

get_data()

Returns

Returns the image as a numpy array

TFImage.get_data(self: tfsdk.TFImage)numpy.ndarray[numpy.uint8]

Get the image as a numpy array

Note

This returns the image in RGB format

See also

as_numpy_array()

Returns

Returns the image as a numpy array

SDK.preprocess_image(*args, **kwargs)

Overloaded function.

  1. preprocess_image(self: tfsdk.SDK, pixel_array: numpy.ndarray[numpy.uint8], width: int, height: int, color_code: tfsdk.COLORCODE) -> Tuple[tfsdk.ERRORCODE, tfsdk.TFImage]

    Preprocess the image to be used by other methods. Loads an image from the given pixel array in memory. Creates a copy of the underlying data. Note, it is highly encouraged to check the return value from setImage before proceeding. If the license is invalid, the tfsdk.ERRORCODE.INVALID_LICENSE error will be returned.

    Parameters
    • pixel_array - decoded pixel array in memory.

    • width - the image width in pixels.

    • height - the image height in pixels.

    • color_code - the pixel array color code, see COLORCODE.

    Returns

    Error code, see ERRORCODE, and TFImage.

  2. preprocess_image(self: tfsdk.SDK, pointer: int, width: int, height: int, color_code: tfsdk.COLORCODE, stride: int) -> Tuple[tfsdk.ERRORCODE, tfsdk.TFImage]

    Preprocess the image to be used by other methods. Load an image from the given memory pointer. The underlying data is copied. Note, it is highly encouraged to check the return value from setImage before proceeding. If the license is invalid, the tfsdk.ERRORCODE.INVALID_LICENSE error will be returned.

    Parameters
    • pointer - pointer to decoded pixel array in memory.

    • width - the image width in pixels.

    • height - the image height in pixels.

    • color_code - the pixel array color code, see COLORCODE.

    • stride - the distance between image array rows in bytes, also known as step size (used only for pointers to the GPU memory).

    Returns

    Error code, see ERRORCODE, and TFImage.

  3. preprocess_image(self: tfsdk.SDK, image_filepath: str) -> Tuple[tfsdk.ERRORCODE, tfsdk.TFImage]

    Preprocess the image to be used by the other methods. Load an image from the given JPEG, JPG, PNG, BMP, or TIFF file. Note, it is highly encouraged to check the return value from setImage before proceeding. If the license is invalid, the tfsdk.ERRORCODE.INVALID_LICENSE error will be returned.

    Parameters

    image_filepath - the path to the image. Must not contain “~”. Can be a relative path.

    Returns

    Error code, see ERRORCODE, and TFImage.

  4. preprocess_image(self: tfsdk.SDK, encoded_image_buffer: List[int]) -> Tuple[tfsdk.ERRORCODE, tfsdk.TFImage]

    Preprocess the image to be used by the other methods. Load an image from an encoded image buffer, supports JPG, PNG, BMP, and TIFF. Note, it is highly encouraged to check the return value from setImage before proceeding. If the license is invalid, the tfsdk.ERRORCODE.INVALID_LICENSE error will be returned.

    Parameters

    encoded_image_buffer - encoded image buffer, supports JPG, PNG, BMP, and TIFF.

    Returns

    Error code, see ERRORCODE, and TFImage.

You can set the image using an OpenCV cv::Mat by passing the image buffer as follows:

1 cv_img = cv2.imread(img_path)
2 res, image = sdk.preprocess_image(cv_img, cv_img.shape[1], cv_img.shape[0], tfsdk.COLORCODE.bgr)
SDK.get_face_image_rotation(self: tfsdk.SDK, tf_image: tfsdk.TFImage)Tuple[tfsdk.ERRORCODE, tfsdk.ROTATEFLAGS]

Detect the orientation of a face image. Returns the rotation required to achieve neutral orientation. As this method does add overhead, we advise only adding it to your pipeline when doing offline processing (ex. reading a database of ID images where some ID images may not be oriented correctly). When consuming a video stream, this method is not necessary as you can guarantee all the frames will have the same orientation.

Parameters

tf_image – the input tfsdk.TFImage, returned by tfsdk.SDK.preprocess_image().

Returns

The ERRORCODE and the tfsdk.ROTATEFLAGS, representing the rotation required in order to achieve neutral orientation. This can be passed directly to the tfsdk.Image.rotate() method in order to correctly rotate the image.

class tfsdk.COLORCODE

Members:

bgr

rgb

bgra

rgba

gray

yuv_i420

yuv_nv12

class tfsdk.ROTATEFLAGS

Members:

ROTATE_0 : Do not rotate.

ROTATE_90_CLOCKWISE : Rotate 90 degrees clockwise.

ROTATE_180 : Rotate 180 degrees clockwise.

ROTATE_90_COUNTERCLOCKWISE : Rotate 270 degrees clockwise.