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:
int - The image height in pixels.
- TFImage.get_width(self: tfsdk.TFImage) int
Get the image width in pixels.
- Returns:
int - 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
- Returns:
numpy.ndarray[numpy.uint8] - 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
- Returns:
numpy.ndarray[numpy.uint8] - The image as a numpy array
- SDK.preprocess_image(*args, **kwargs)
Overloaded function.
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:
tfsdk.ERRORCODE
- The error code.tfsdk.TFImage
- The preprocesses image.
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:
tfsdk.ERRORCODE
- The error code.tfsdk.TFImage
- The preprocesses image.
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:
tfsdk.ERRORCODE
- The error code.tfsdk.TFImage
- The preprocesses image.
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:
tfsdk.ERRORCODE
- The error code.tfsdk.TFImage
- The preprocesses image.
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 bytfsdk.SDK.preprocess_image()
.- Returns:
tfsdk.ERRORCODE
- The error code.tfsdk.ROTATEFLAGS
- The rotation required in order to achieve neutral orientation. This can be passed directly to thetfsdk.TFImage.rotate()
method in order to correctly rotate the image.
- SDK.get_face_image_rotations(self: tfsdk.SDK, tf_images: List[tfsdk.TFImage]) Tuple[tfsdk.ERRORCODE, List[tfsdk.ROTATEFLAGS]]
Detect the orientation of a list of face images. 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. This batch processing method increases throughput when using GPU inference.
- Parameters:
tf_images - The list of input
tfsdk.TFImage
.- Returns:
tfsdk.ERRORCODE
- The error code.List[
tfsdk.ROTATEFLAGS
] - The rotations required in order to achieve neutral orientation. These rotations can be passed directly to thetfsdk.TFImage.rotate()
method in order to correctly rotate the images.
- 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.