Updated on 2022-02-22 GMT+08:00

Example - Preprocessing

The following is an example of the preprocessing module:

import hilens
import cv2
import numpy as np

def run():
    # Construct a camera.
    cap = hilens.VideoCapture()
    # Obtain a frame of image. The image obtained by the built-in camera is in YUV format.
    # The default resolution of the built-in camera is 720p. Therefore, the YUV image size is (720 x 3/2,1280).
    frame = cap.read()

    # Convert the color format of the image. The YUV-to-BGR conversion needs to be implemented using OpenCV.
    image_bgr = cv2.cvtColor(image_yuv, cv2.COLOR_YUV2BGR_NV21)

     # Convert the color format of the image. The BGR/RGB format can be converted to the YUV format by calling hilens.cvt_color.
    image_yuv = hilens.cvt_color(image_bgr, hilens.BGR2YUV_NV21)

    # Construct a preprocessor that supports only YUV_NV21/NV12 images.
    proc = hilens.Preprocessor()
     # Adjust the image size.
    resized = proc.resize(image_yuv, 640, 480,0)
    # Crop the image.
    croped = proc.crop(image_yuv, 10, 20, 64, 64, 0)
    
    # Other processing
    pass

if __name__ == '__main__':
    hilens.init("hello")
    run()
    hilens.terminate()