Updated on 2022-08-19 GMT+08:00

Writing the Code

Huawei HiLens provides a HiLens Framework, which encapsulates bottom-layer APIs to implement common management functions, enabling you to easily develop skills and the AI ecosystem. For details about the guide and APIs, see the HiLens Developer Guide.

HiLens Framework Sample Code

HiLens Kit devices run on the HiLens Framework. Developers can directly call APIs of the HiLens Framework when compiling logic code for skill development.

The following is an example of using the HiLens Framework to obtain the camera content and perform simple processing.

  • You need to fill in the code execution file during skill development.
  • Firmware 1.1.2 or later supports configuring Python dependencies for skills. During skill development, you can configure Python dependencies for a skill as required. For details, see How Do I Configure Python Dependencies for a Skill.
#!/usr/bin/python3
# skillframework 1.0.0 python demo

import hilens                                                                   # Import the HiLens package.
import cv2                                                                      # Import the opencv.

def main():
    hilens.init("hello")                                                        # Initialize the skill.
    model = hilens.Model(hilens.get_model_dir() + 
                "faceDetection.om")                                             # Construct the model manager.
    disp = hilens.Display(hilens.HDMI)                                          # Construct the display.
    cap = hilens.VideoCapture()                                                 # Construct the camera.
    proc = hilens.Preprocessor()                                                # Construct the preprocessor.

    hilens.set_log_level(hilens.DEBUG)                                          # Set the log level.
    hilens.Info("This is a skillframework python demo")                         # Print the log.

    for i in range(10):
        frame = cap.read()                                                      # Read a frame of image.
        rframe = proc.resize(frame, 480, 480, 1)                                # Zoom in/out
        inputs = [rframe.flatten()]                                             # Construct the model input.
        outputs = model.infer(inputs)                                           # Inference
        hilens.Info("outputs len: %d" % len(outputs))                           # Print the output size.
        for o in outputs:
            hilens.Info("output shape: " + str(o.shape))
        # post process is ignored
        disp.show(frame)                                                        # Output the image to the display.

    hilens.terminate()                                                          # Destroy the resource.

if __name__ == "__main__":
    main()