Skip to content

PLUS Examples in Python code

Kneron PLUS is an API-based software library, which allows users to control their AI devices.

In the Introduction, we have introduced the concept of inference flow containing these three main executions: Data Pre-processing, Inference, and Post-Processing.

Based on where the pre-processing is executed, there are two types of inference API that are provided in Kneron PLUS:

PLUS Examples in Python code provided in KNEO Pi are limited to these two usages. If users wish to explore further usage of Kneron PLUS, please refer to this documentation site for more information.


Examples

ScanDevices

This example shows how to use PLUS API to search the Kneron AI devices that are connected to KNEO Pi.

Even without using an external NPU device, users will still see KL730's NPU virtualized as a USB device in this example.

[root@kneo-pi plus_python]# python ScanDevices.py
scanning kneron devices ...
number of Kneron devices found: 1
listing devices infomation as follows:

[0] USB scan index: '0'
[0] USB port ID: '5'
[0] Product ID: '0x732 (KL730)'
[0] USB link speed: 'UsbSpeed.KP_USB_SPEED_SUPER'
[0] USB port path: '5-1'
[0] KN number: '0x9011004'
[0] Connectable: 'True'
[0] Firmware: 'KDP2 Loader/L'

KL730DemoGenericImageInference

This example shows the usage of the API kp.inference.generic_image_inference_send() and kp.inference.generic_image_inference_receive().

Please refer to this documentation site for detailed information.

[root@kneo-pi plus_python]# python KL730DemoGenericImageInference.py
[Connect Device]
 - Success
[Set Device Timeout]
 - Success
[Upload Firmware]
 - Success
[Upload Model]
 - Success
[Read Image]
 - Success
[Starting Inference Work]
 - Starting inference loop 10 times
 - ..........
[Retrieve Inference Node Output ]
 - Success
[Result]
[{
    "name": "output",
    "shape": [
        1,
        255,
        80,
        80
    ],
    "channels_ordering": "ChannelOrdering.KP_CHANNEL_ORDERING_CHW",
    "num_data": 1632000,
    "ndarray": [
        ...
    ]
}, {
    "name": "618",
    "shape": [
        1,
        255,
        40,
        40
    ],
    "channels_ordering": "ChannelOrdering.KP_CHANNEL_ORDERING_CHW",
    "num_data": 408000,
    "ndarray": [
        ...
    ]
}, {
    "name": "620",
    "shape": [
        1,
        255,
        20,
        20
    ],
    "channels_ordering": "ChannelOrdering.KP_CHANNEL_ORDERING_CHW",
    "num_data": 102000,
    "ndarray": [
        ...
    ]
}]

KL730DemoGenericDataInference

This example shows the usage of the API kp.inference.generic_data_inference_send() and kp.inference.generic_data_inference_receive().

Please refer to this documentation site for detailed information.

Info

This example also shows how to implement a post-process for the YOLO raw output data.

[root@kneo-pi plus_python]# python KL730DemoGenericDataInference.py
[Connect Device]
 - Success
 - Success
[Set Device Timeout]
 - Success
[Upload Model]
 - Success
[Prepare NPU Inference Data]
 - Success
[Starting Inference Work]
 - Starting inference loop 100 times
 - ....................................................................................................
[Retrieve Inference Node Output ]
 - Success
[Yolo V5 Post-Processing]
 - Success
[Result]
{
    "class_count": 80,
    "box_count": 12,
    "box_list": {
        "0": {
            "x1": 183,
            "y1": 260,
            "x2": 398,
            "y2": 594,
            "score": 0.8837,
            "class_num": 0
        },

        ...
    }
}

KL730DemoCamGenericImageInferenceDropFrame

This example demonstrates how to get images from a webcam using OpenCV and display them with bounding boxes, which are generated through post-processing from the raw inference output.

The default device path of the webcam is set to /dev/video0, but it may not be the same every time due to the timing when the webcam is connected to KNEO Pi.

To search the device path of the webcam, please use the command below:

$ v4l2-ctl --list-devices

[root@kneo-pi ~]# v4l2-ctl --list-devices
vpl_voc2.0 (platform:vpl_voc):
        /dev/video2
        /dev/video3
        /dev/video4
        /dev/video5
        /dev/video6
        /dev/video7
        /dev/video8
        /dev/video9

USB2.0 Camera: USB2.0 Camera (usb-xhci-hcd.1.auto-1):
        /dev/video0
        /dev/video1
        /dev/media0

Info

This example also shows how to implement a post-process for the raw YOLO output data.

[root@kneo-pi plus_python]# python KL730DemoCamGenericImageInferenceDropFrame.py
[Connect Device]
 - Success
[Set Device Timeout]
 - Success
[Upload Firmware]
 - Success
[Upload Model]
 - Success
[Model NEF Information]
{
    ...
}
[Configure Inference Settings]
 - Success
[Starting Inference Work]
 - Starting inference

KL730End2EndTutorialYoloV7

This example provides how to run the example model generated from end to end model convert tutorial. To see the yolov7 model conversion tutorial, check Create your own model - yolov7.

Info

For an easier demo, we have extracted some post-processing code from the original yolov7 repository and included it in this example code.

[root@kneo-pi plus_python]# python KL730End2EndTutorialYoloV7.py
[Connect Device]
 - Success
[Upload Firmware]
 - Success
[Upload Model]
 - Success
[Starting Inference Work]
 - Success
[Retrieve Inference Node Output ]
 - Success
[Post Process]
save result to /media/eric/data/misc/kneopi-e2e-example/kneopi-examples/kneopi_on_board_inf.png

KL730End2EndTutorialPidnet

This example provides how to run the example model generated from end to end model convert tutorial. To see the PIDNet model conversion tutorial, check Create your own model - pidnet.

Info

For an easier demo, we have extracted some post-processing code from the original PIDNet repository and included it in this example code.

[root@kneo-pi plus_python]# python KL730End2EndTutorialPidnet.py
[Connect Device]
 - Success
[Upload Firmware]
 - Success
[Upload Model]
 - Success
[Starting Inference Work]
 - Success
[Retrieve Inference Node Output ]
 - Success
[Post Process]
save result to /***YOUR_PATH***/kneopi-examples/kneopi_on_board_inf.png