def test_opencv_frame(): # Create InferServer infer_server = cnis.InferServer(dev_id=0) session_desc = cnis.SessionDesc() session_desc.name = "test_session" session_desc.model = infer_server.load_model(utils.model_dir) # Set OpencvPreproc session_desc.preproc = cnis.PreprocessorHost() session_desc.set_preproc_func(cnis.OpencvPreproc(dst_fmt=cnis.VideoPixelFmt.BGRA, keep_aspect_ratio=False).execute) session_desc.show_perf = False # create synchronous session session = infer_server.create_sync_session(session_desc) # Request opencv frame input_pak = prepare_input() output = cnis.Package(1, utils.tag) status = cnis.Status.SUCCESS assert infer_server.request_sync(session, input_pak, status, output) assert status == cnis.Status.SUCCESS # Request opencv frame with roi input_pak = prepare_input() cv_frame = input_pak.data[0].get_cv_frame() cv_frame.roi = cnis.BoundingBox(x=0, y=0, w=0.5, h=0.5) output = cnis.Package(1, utils.tag) status = cnis.Status.SUCCESS assert infer_server.request_sync(session, input_pak, status, output) assert status == cnis.Status.SUCCESS # Destroy session assert infer_server.destroy_session(session)
def test_python_preprocess(): """Test custom preprocess function (written in python)""" # Create InferServer infer_server = cnis.InferServer(dev_id=0) session_desc = cnis.SessionDesc() session_desc.name = "test_session" # Load model session_desc.model = infer_server.load_model(utils.model_dir) # Create PreprocessorHost and set custom preprocess function to description session_desc.preproc = cnis.PreprocessorHost() custom_python_preproc = CustomPreprocess() session_desc.set_preproc_func(custom_python_preproc.execute) session_desc.show_perf = False # Create synchronous session session = infer_server.create_sync_session(session_desc) # Create a package input_pak = cnis.Package(1, utils.tag) output = cnis.Package(1, utils.tag) status = cnis.Status.SUCCESS # Request assert infer_server.request_sync(session, input_pak, status, output) assert status == cnis.Status.SUCCESS assert custom_python_preproc.called # Destroy session assert infer_server.destroy_session(session)
def opencv_preproc(self, session_desc): """Set OpenCV preproc to session description""" session_desc.preproc = cnis.PreprocessorHost() if self.core_ver == cnis.CoreVersion.MLU220 or self.core_ver == cnis.CoreVersion.MLU270: session_desc.set_preproc_func( cnis.OpencvPreproc(dst_fmt=cnis.VideoPixelFmt.BGRA, keep_aspect_ratio=False).execute) else: session_desc.set_preproc_func( cnis.OpencvPreproc(dst_fmt=cnis.VideoPixelFmt.RGB24, keep_aspect_ratio=True).execute)
def test_session(): infer_server = cnis.InferServer(dev_id=0) session_desc = cnis.SessionDesc() session_desc.name = "test_session" session_desc.model = infer_server.load_model(utils.model_dir) session_desc.strategy = cnis.BatchStrategy.DYNAMIC session_desc.preproc = cnis.PreprocessorHost() session_desc.set_preproc_func(CustomPreprocess().execute) session_desc.postproc = cnis.Postprocessor() session_desc.set_postproc_func(CustomPostprocess().execute) session_desc.host_input_layout = cnis.DataLayout( cnis.DataType.UINT8, cnis.DimOrder.NHWC) session_desc.host_output_layout = cnis.DataLayout( cnis.DataType.FLOAT32, cnis.DimOrder.NHWC) session_desc.batch_timeout = 1000 session_desc.priority = 0 session_desc.engine_num = 2 session_desc.show_perf = False session = infer_server.create_sync_session(session_desc) assert session assert infer_server.destroy_session(session) class TestObserver(cnis.Observer): """To receive results from InferServer, we define a class TestObserver which inherits from cnis.Observer. After a request is sent to InferServer and is processed by InferServer, the response_func API will be called with status, results and user data. """ def __init__(self): super().__init__() def response_func(self, status, data, user_data): # Empty, just for testing session creation pass obs = TestObserver() session = infer_server.create_session(session_desc, obs) assert session assert infer_server.destroy_session(session)