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

Example - Output

This example shows how to call APIs of multiple output ends. Before using the APIs, ensure that all output ends are connected and available. If an output end does not meet the requirements, comment out or delete the corresponding code in the sample code, and run the sample code. The following is an example of the output module:
#! /usr/bin/python3.7
import hilens
import cv2
import numpy as np
import wave

def run():
    # Show the display connected to the HDMI.
    # Currently, only one channel of data can be displayed on the HDMI. If multiple skills are displayed on the HDMI at the same time, an error is reported.
    disp0 = hilens.Display(hilens.HDMI)
    # Push streams to the server whose address is rtmp://192.168.1.1/stream.
    disp1 = hilens.Display(hilens.RTMP, "rtmp://192.168.1.1/stream")
    # Write the video to a file. The file generated by hilens.H264_FILE is the raw video stream file encoded using H.264.
    # The file size is not limited. It is recommended that this command be used only for debugging.
    disp2 = hilens.Display(hilens.H264_FILE, hilens.get_workspace_path() + "video.h264")
    # hilens.get_workspace_path() returns the skill workspace directory. For details, see the resource management module.

    # Construct a local camera video collector.
    cap = hilens.VideoCapture()

    # Display the video to the HDMI display device.
    disp0.show(cap.read())

    # Upload video.h264 to OBS.
    # Generate a video file in H.264 format.
    disp2.show(cap.read())
    # Upload the file to OBS.
    hilens.upload_file_to_obs("video", hilens. get_workspace_path() + "video.h264", "write")
 
    # Append 1234 to the test4 file of OBS.
    hilens.upload_buffer_to_obs("test4", "1234", "append")

    # Upload images to OBS using the data in the buffer.
    # Convert the images to the BGR format.
    frame = cap.read()
    img_bgr = cv2.cvtColor(frame, cv2.COLOR_YUV2BGR_NV21)
    # Encode the current images in JPG format.
    img_encode = cv2.imencode(".jpg", img_bgr)[1]
    # Use upload_bufer to upload the images in the buffer. The image format must be the same as the encoding format.
    hilens.upload_bufer("img.jpg", img_encode, "write")

    # Play an audio file.
    hilens.play_aac_file("test.aac", 6)
    
if __name__ == '__main__':
    hilens.init("hello")
    run()
    hilens.terminate()