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

Example - Hard Example Upload

The following is an example of uploading a hard example:

import hilens
import cv2
import numpy as np

def run():
    # Construct a camera.
    cap = hilens.VideoCapture()

    disp = hilens.Display(hilens.HDMI)

    hard_sample = hilens.HardSample(0.5,0.5,1) # 1 and 2 is the algorithm used by the detection model.
     #hard_sample = hilens.HardSample(0.5,0.5,0) # 0 is the algorithm used by the classification model.

    hard_sample_flag = False # Whether hard sample upload configuration exists

    hard_sample_config = hilens.get_hard_sample_config() # Obtain hard example configurations.
    if not hard_sample_config:
        hilens.warning("hardSampleConfig is empty")
    else:
        hard_sample_flag = True
        data_count = hard_sample_config["hard_sample_setting"][0]["data_count"]
        data_current_count = hard_sample_config["hard_sample_setting"][0]["datacur_count"]
        upload_jpeg_url = hard_sample_config["hard_sample_setting"][0]["dataset_path"]
        model_name = hard_sample_config["hard_sample_setting"][0]["model_name"]
        camera_name = "default"
        if data_count > data_current_count:
            upload_flag = True # Whether to continue the upload
        else:
            upload_flag = False

    while True:
        # Obtain a frame of image.
        frame = cap.read()
        if hard_sample_flag:
            if upload_flag:
                if hard_sample.hard_sample_detection_filter([[0.,0.,1280.,720.,0.4,1]]): # The input of the detection algorithm is the post-processed bounding box. Each bounding box contains [xmin, ymin, xmax, ymax, conf, label] (including the confidence score and class labels).
                #if hard_sample.hard_sample_classification_filter([0.2, 0.2, 0.2, 0.2, 0.2],5):  # The input of the classification algorithm is the probability of each class, that is, the output of the model.
                    hard_sample.upload_jpeg(upload_jpeg_url, data_current_count, model_name, camera_name, frame)
                    data_current_count += 1
                    hard_sample_config["hard_sample_setting"][0]["datacur_count"] = data_current_count
                    if data_current_count == 1 or data_current_count == data_count:
                        if data_current_count == data_count:
                            upload_flag = False
                    hilens.set_hard_sample_config(hard_sample_config) # Update the hard example configuration file on the device.

        #Output the result to HDMI.
        disp.show(frame)

if __name__ == '__main__':
	#The value of hello must be the same as that of the check value in the basic information. For details, see the developer guide.
    hilens.init("hello")
    run()
    hilens.terminate()