def test_build_failed(self): conf_file = test_config.TEST_DATA_DIR + "/py_config.toml" driver_dir = test_config.TEST_DRIVER_DIR txt = r""" [driver] dir=["{}"] skip-default=true [log] level="ERROR" [graph] graphconf = '''digraph demo {{ notexist[type=flowunit, flowunit=notexist, device=cpu] }}''' format = "graphviz" """.format(driver_dir) conf = modelbox.Configuration() flow = modelbox.Flow() ret = flow.init("graph", txt) if ret == False: self.assertTrue(ret) self.assertTrue(ret) ret = flow.build() if ret == False: modelbox.error(ret) self.assertFalse(ret)
def test_callback(self): desc = modelbox.FlowGraphDesc() self.assertNotEqual(desc, None) config = modelbox.Configuration() config.set("graph.queue_size", "32") config.set("graph.queue_size_external", "1000") config.set("graph.batch_size", "16") config.set("drivers.skip-default", "true") config.set("drivers.dir", [test_config.TEST_DRIVER_DIR]) desc.init(config) resize_output = desc.addnode("resize", { "width": "256", "height": "256" }, {}) out_image = resize_output.get_nodedesc("out_image") callback_output = desc.addnode(callback_func, ["In_1"], ["Out_1"], {"In_1": out_image}) desc.bindinput(resize_output, "__default__inport__") desc.bindoutput(callback_output, "__default__outport__") flow = modelbox.Flow() flow.init(desc) flow.build() flow.run_async() retval = modelbox.Status() flow.wait(1000, retval)
def test_config_set_get(self): c = modelbox.Configuration() c.set("1", 1) c.set("2", 1.2) c.set("3", 1.3) c.set("4", False) c.set("5", True) c.set("6", "test") self.assertEqual(c.get_int("1"), 1) self.assertEqual(c.get_float("2"), 1.2) self.assertEqual(c.get_float("3"), 1.3) self.assertEqual(c.get_bool("4"), False) self.assertEqual(c.get_bool("5"), True) self.assertEqual(c.get_string("6"), "test")
def test_api_graph(self): engine = modelbox.FlowGraphDesc() self.assertNotEqual(engine, None) config = modelbox.Configuration() config.set("graph.queue_size", "32") config.set("graph.queue_size_external", "1000") config.set("graph.batch_size", "16") config.set("drivers.skip-default", "true") config.set("drivers.dir", [test_config.TEST_DRIVER_DIR]) engine.init(config) video_demuxer_output = engine.addnode("video_demuxer", {}, {}) engine.bindinput(video_demuxer_output, "in_video_url") engine.bindoutput(video_demuxer_output, "out_video_packet") flow = modelbox.Flow() flow.init(engine) flow.build() flow.run_async() retval = modelbox.Status() ret = flow.wait(1000, retval)
def test_flow_op_thread(self): conf_file = test_config.TEST_DATA_DIR + "/py_op_config.toml" driver_dir = test_config.TEST_DRIVER_DIR with open(conf_file, "w") as out: txt = r""" [driver] dir=["{}", "{}"] skip-default=true [log] level="INFO" [graph] graphconf = '''digraph demo {{ python_image[type=flowunit, flowunit=python_image, device=cpu, deviceid=0, label="<image_out/out_1>", batch_size = 10] python_resize[type=flowunit, flowunit=python_resize, device=cpu, deviceid=0, label="<resize_in> | <resize_out>"] python_brightness[type=flowunit, flowunit=python_brightness, device=cpu, deviceid=0, label="<brightness_in> | <brightness_out>", brightness = 0.1] python_show[type=flowunit, flowunit=python_show, device=cpu, deviceid=0, label="<show_in>", is_save = false] python_image:"image_out/out_1" -> python_resize:resize_in python_resize:resize_out -> python_brightness:brightness_in python_brightness:brightness_out -> python_show:show_in }}''' format = "graphviz" """.format(driver_dir, test_config.TEST_DATA_DIR + "/python_op") out.write(txt) conf = modelbox.Configuration() flow1 = modelbox.Flow() flow2 = modelbox.Flow() t1 = threading.Thread(target=self.thread_func, args=(flow1, conf_file,)) t2 = threading.Thread(target=self.thread_func, args=(flow2, conf_file,)) t1.setDaemon(True) t2.setDaemon(True) t1.start() t2.start() t1.join() t2.join() os.remove(conf_file)
def test_dynamic_graph(self): engine = modelbox.ModelBoxEngine() self.assertNotEqual(engine, None) config = modelbox.Configuration() config.set("graph.queue_size", "32") config.set("graph.queue_size_external", "1000") config.set("graph.batch_size", "16") config.set("drivers.skip-default", "true") config.set("drivers.dir", [test_config.TEST_DRIVER_DIR]) engine.init(config) input_stream = engine.create_input({"input"}) source_url = f'{test_config.TEST_ASSETS}/video/jpeg_5s_480x320_24fps_yuv444_8bit.mp4' input_stream.setmeta("source_url", source_url) input_stream.close() video_demuxer_output = engine.execute("video_demuxer", {}, input_stream) frame_num = 0 for packet in video_demuxer_output: frame_num = frame_num + 1 engine.shutdown()
def test_flow_op(self): conf_file = test_config.TEST_DATA_DIR + "/py_op_config.toml" driver_dir = test_config.TEST_DRIVER_DIR with open(conf_file, "w") as out: txt = r""" [driver] dir=["{}", "{}"] skip-default=true [log] level="INFO" [graph] graphconf = '''digraph demo {{ python_image[type=flowunit, flowunit=python_image, device=cpu, deviceid=0, label="<image_out/out_1>", batch_size = 10] python_resize[type=flowunit, flowunit=python_resize, device=cpu, deviceid=0, label="<resize_in> | <resize_out>"] python_brightness[type=flowunit, flowunit=python_brightness, device=cpu, deviceid=0, label="<brightness_in> | <brightness_out>", brightness = 0.1] python_show[type=flowunit, flowunit=python_show, device=cpu, deviceid=0, label="<show_in>", is_save = true] python_image:"image_out/out_1" -> python_resize:resize_in python_resize:resize_out -> python_brightness:brightness_in python_brightness:brightness_out -> python_show:show_in }}''' format = "graphviz" """.format(driver_dir, test_config.TEST_DATA_DIR + "/python_op") out.write(txt) conf = modelbox.Configuration() flow = modelbox.Flow() ret = flow.init(conf_file) os.remove(conf_file) if ret == False: modelbox.error(ret) self.assertTrue(ret) ret = flow.build() self.assertTrue(ret) ret = flow.run_async() self.assertTrue(ret) retval = modelbox.Status() ret = flow.wait(0, retval) self.assertEqual(retval, modelbox.Status.StatusCode.STATUS_STOP)
def test_flow_for_buffer(self): conf_file = test_config.TEST_DATA_DIR + "/py_op_config.toml" driver_dir = test_config.TEST_DRIVER_DIR with open(conf_file, "w") as out: txt = r""" [driver] dir=["{}", "{}"] skip-default=true [log] level="INFO" [graph] graphconf = '''digraph demo {{ input1[type=input] python_buffer[type=flowunit, flowunit=python_buffer, device=cpu, deviceid=0, label="<buffer_in> | <buffer_out>", buffer_config = 0.2] output1[type=output] input1 -> python_buffer:buffer_in python_buffer:buffer_out -> output1 }}''' format = "graphviz" """.format(driver_dir, test_config.TEST_DATA_DIR + "/python_op") out.write(txt) conf = modelbox.Configuration() flow = modelbox.Flow() ret = flow.init(conf_file) os.remove(conf_file) if ret == False: modelbox.error(ret) self.assertTrue(ret) ret = flow.build() self.assertTrue(ret) ret = flow.run_async() self.assertTrue(ret) img = cv2.imread(test_config.TEST_SOURCE_DIR + "/../src/python/test/data/liu-x-160.jpg") img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) with Image.open(test_config.TEST_SOURCE_DIR + "/../src/python/test/data/liu-x-160.jpg") as img: img_np = np.array(img) extern_data_map = flow.create_external_data_map() buffer_list = extern_data_map.create_buffer_list() buffer_list.push_back(img_np) extern_data_map.send("input1", buffer_list) extern_data_map.shutdown() buffer_list_map = modelbox.ExtOutputBufferList() ret = extern_data_map.recv(buffer_list_map) self.assertTrue(ret) result_buffer_list = buffer_list_map.get_buffer_list("output1") for i in range(result_buffer_list.size()): buffer = result_buffer_list[i] np_image = np.array(buffer, copy= False) image = Image.fromarray(np_image) with Image.open(test_config.TEST_SOURCE_DIR + "/../src/python/test/data/liu-x-160.jpg") as check_image: try: check_image_np = np.array(check_image) diff = ImageChops.difference(image, Image.fromarray(check_image_np)) self.assertEqual(diff.getbbox(), None) except ValueError as e: flow.stop() self.assertTrue(False) data_type = buffer.get("type") if data_type != modelbox.Buffer.ModelBoxDataType.UINT8: return modelbox.Status(modelbox.Status.StatusCode.STATUS_SHUTDOWN, "invalid type") float_test = buffer.get("float_test") if float_test != 0.5: return modelbox.Status(modelbox.Status.StatusCode.STATUS_SHUTDOWN, "invalid float test") string_test = buffer.get("string_test") if string_test != "TEST": return modelbox.Status(modelbox.Status.StatusCode.STATUS_SHUTDOWN, "invalid string test") int_test = buffer.get("int_test") if int_test != 100: return modelbox.Status(modelbox.Status.StatusCode.STATUS_SHUTDOWN, "invalid int test") bool_test = buffer.get("bool_test") if bool_test != False: return modelbox.Status(modelbox.Status.StatusCode.STATUS_SHUTDOWN, "invalid bool test") int_list = buffer.get("list_int_test") if int_list != [1, 1, 1]: return modelbox.Status(modelbox.Status.StatusCode.STATUS_SHUTDOWN, "invalid int list") float_list = buffer.get("list_float_test") if float_list != [0.1, 0.2, 0.3]: return modelbox.Status(modelbox.Status.StatusCode.STATUS_SHUTDOWN, "invalid float list") bool_list = buffer.get("list_bool_test") if bool_list != [False, False, True]: return modelbox.Status(modelbox.Status.StatusCode.STATUS_SHUTDOWN, "invalid bool list") string_list = buffer.get("list_string_test") if string_list != ["TEST1", "TEST2", "TEST3"]: return modelbox.Status(modelbox.Status.StatusCode.STATUS_SHUTDOWN, "invalid string list") try: dict_test = buffer.get("map_test") except ValueError as err: modelbox.info(str(err)) else: flow.stop() self.assertTrue(False) flow.stop()
def test_flow_op_ext(self): conf_file = test_config.TEST_DATA_DIR + "/py_op_config.toml" driver_dir = test_config.TEST_DRIVER_DIR with open(conf_file, "w") as out: txt = r""" [driver] dir=["{}", "{}"] skip-default=true [log] level="INFO" [graph] graphconf = '''digraph demo {{ input1[type=input] python_resize[type=flowunit, flowunit=python_resize, device=cpu, deviceid=0, label="<resize_in> | <resize_out>"] python_brightness[type=flowunit, flowunit=python_brightness, device=cpu, deviceid=0, label="<brightness_in> | <brightness_out>", brightness = 0.1] output1[type=output] input1 -> python_resize:resize_in python_resize:resize_out -> python_brightness:brightness_in python_brightness:brightness_out -> output1 }}''' format = "graphviz" """.format(driver_dir, test_config.TEST_DATA_DIR + "/python_op") out.write(txt) conf = modelbox.Configuration() flow = modelbox.Flow() ret = flow.init(conf_file) os.remove(conf_file) if ret == False: modelbox.error(ret) self.assertTrue(ret) ret = flow.build() self.assertTrue(ret) ret = flow.run_async() self.assertTrue(ret) img = cv2.imread(test_config.TEST_SOURCE_DIR + "/../src/python/test/data/liu-x-160.jpg") img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) extern_data_map = flow.create_external_data_map() buffer_list = extern_data_map.create_buffer_list() im_array = np.asarray(img_rgb[:,:]) buffer_list.push_back(im_array) extern_data_map.send("input1", buffer_list) extern_data_map.shutdown() buffer_list_map = modelbox.ExtOutputBufferList() ret = extern_data_map.recv(buffer_list_map) self.assertTrue(ret) result_buffer_list = buffer_list_map.get_buffer_list("output1") for i in range(result_buffer_list.size()): aa = result_buffer_list[i] np_image = np.array(aa, copy= False) image = Image.fromarray(np_image) with Image.open(test_config.TEST_SOURCE_DIR + "/../src/python/test/data/python_test_show_out.png") as check_image: try: diff = ImageChops.difference(image, check_image) self.assertEqual(diff.getbbox(), None) except ValueError as e: self.assertTrue(False) flow.stop()