Image Decoding

Function Description

Decodes the input image data and converts the local image into the Image class for subsequent preprocessing and inference services. Currently, .jpeg and .png images are supported.

For details about the API, see decode.

API Call Process

Prepare a local image file to be decoded, initialize the ImageProcessor class, and call the decode API of the ImageProcessor class to decode and return the output Image object.

The process of calling image decoding APIs is as follows:
Figure 1 Process of calling image decoding APIs

Key steps are demonstrated as follows:

  1. Perform global initialization by calling mx_init().
  2. Initialize ImageProcessor.

    Construct the ImageProcessor object. You need to specify the device ID during the construction.

  3. Decode the image by calling decode.

    Pass parameters in a proper format to the decode API.

  4. Perform deinitialization by calling mx_deinit().

Sample Code

The following is a code example of key steps of functions and features, which is for reference only and cannot be directly copied for execution.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from mindx.sdk import base  
from mindx.sdk.base import ImageProcessor, Image

def process():
    # Image decoding
    # Initialize the ImageProcessor object.
    imageProcessor = ImageProcessor(device_id)
    image_path = "test_image.jpg"
    # Read the image path for decoding. The decoding format is nv12 (YUV_SP_420).
    decoded_image = imageProcessor.decode(image_path, base.nv12)

if __name__ == "__main__":
    base.mx_init()    # Initialize resources.
    process()
    base.mx_deinit()  # Deinitialize resources.