def test_stateful_kernel(self): @cv.gapi.op('custom.sum', in_types=[cv.GArray.Int], out_types=[cv.GOpaque.Int]) class GSum: @staticmethod def outMeta(arr_desc): return cv.empty_gopaque_desc() @cv.gapi.kernel(GSum) class GSumImpl: last_result = 0 @staticmethod def run(arr): GSumImpl.last_result = sum(arr) return GSumImpl.last_result g_in = cv.GArray.Int() comp = cv.GComputation(cv.GIn(g_in), cv.GOut(GSum.on(g_in))) s = comp.apply(cv.gin([1, 2, 3, 4]), args=cv.compile_args(cv.gapi.kernels(GSumImpl))) self.assertEqual(10, s) s = comp.apply(cv.gin([1, 2, 8, 7]), args=cv.compile_args(cv.gapi.kernels(GSumImpl))) self.assertEqual(18, s) self.assertEqual(18, GSumImpl.last_result)
def test_age_gender_infer(self): # NB: Check IE if not cv.dnn.DNN_TARGET_CPU in cv.dnn.getAvailableTargets( cv.dnn.DNN_BACKEND_INFERENCE_ENGINE): return root_path = '/omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013' model_path = self.find_file( root_path + '.xml', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')]) weights_path = self.find_file( root_path + '.bin', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')]) img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')]) device_id = 'CPU' img = cv.resize(cv.imread(img_path), (62, 62)) # OpenCV DNN net = cv.dnn.readNetFromModelOptimizer(model_path, weights_path) net.setPreferableBackend(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE) net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU) blob = cv.dnn.blobFromImage(img) net.setInput(blob) dnn_age, dnn_gender = net.forward(net.getUnconnectedOutLayersNames()) # OpenCV G-API g_in = cv.GMat() inputs = cv.GInferInputs() inputs.setInput('data', g_in) outputs = cv.gapi.infer("net", inputs) age_g = outputs.at("age_conv3") gender_g = outputs.at("prob") comp = cv.GComputation(cv.GIn(g_in), cv.GOut(age_g, gender_g)) pp = cv.gapi.ie.params("net", model_path, weights_path, device_id) nets = cv.gapi.networks(pp) args = cv.compile_args(nets) gapi_age, gapi_gender = comp.apply(cv.gin(img), args=cv.compile_args( cv.gapi.networks(pp))) # Check self.assertEqual(0.0, cv.norm(dnn_gender, gapi_gender, cv.NORM_INF)) self.assertEqual(0.0, cv.norm(dnn_age, gapi_age, cv.NORM_INF))
def test_array_with_custom_type(self): @cv.gapi.op('custom.op', in_types=[cv.GArray.Any, cv.GArray.Any], out_types=[cv.GArray.Any]) class GConcat: @staticmethod def outMeta(arr_desc0, arr_desc1): return cv.empty_array_desc() @cv.gapi.kernel(GConcat) class GConcatImpl: @staticmethod def run(arr0, arr1): return arr0 + arr1 g_arr0 = cv.GArray.Any() g_arr1 = cv.GArray.Any() g_out = GConcat.on(g_arr0, g_arr1) comp = cv.GComputation(cv.GIn(g_arr0, g_arr1), cv.GOut(g_out)) arr0 = [(2, 2), 2.0] arr1 = [3, 'str'] out = comp.apply(cv.gin(arr0, arr1), args=cv.compile_args( cv.gapi.kernels(GConcatImpl))) self.assertEqual(arr0 + arr1, out)
def test_raise_in_outMeta(self): @cv.gapi.op('custom.op', in_types=[cv.GMat, cv.GMat], out_types=[cv.GMat]) class GAdd: @staticmethod def outMeta(desc0, desc1): raise NotImplementedError("outMeta isn't implemented") @cv.gapi.kernel(GAdd) class GAddImpl: @staticmethod def run(img0, img1): return img0 + img1 g_in0 = cv.GMat() g_in1 = cv.GMat() g_out = GAdd.on(g_in0, g_in1) comp = cv.GComputation(cv.GIn(g_in0, g_in1), cv.GOut(g_out)) img0 = np.array([1, 2, 3]) img1 = np.array([1, 2, 3]) with self.assertRaises(Exception): comp.apply(cv.gin(img0, img1), args=cv.compile_args(cv.gapi.kernels(GAddImpl)))
def test_invalid_outMeta(self): @cv.gapi.op('custom.op', in_types=[cv.GMat, cv.GMat], out_types=[cv.GMat]) class GAdd: @staticmethod def outMeta(desc0, desc1): # Invalid outMeta return cv.empty_gopaque_desc() @cv.gapi.kernel(GAdd) class GAddImpl: @staticmethod def run(img0, img1): return img0 + img1 g_in0 = cv.GMat() g_in1 = cv.GMat() g_out = GAdd.on(g_in0, g_in1) comp = cv.GComputation(cv.GIn(g_in0, g_in1), cv.GOut(g_out)) img0 = np.array([1, 2, 3]) img1 = np.array([1, 2, 3]) # FIXME: Cause Bad variant access. # Need to provide more descriptive error messsage. with self.assertRaises(Exception): comp.apply(cv.gin(img0, img1), args=cv.compile_args(cv.gapi.kernels(GAddImpl)))
def test_multiple_custom_kernels(self): sz = (3, 3, 3) in_mat1 = np.full(sz, 45, dtype=np.uint8) in_mat2 = np.full(sz, 50, dtype=np.uint8) # OpenCV expected = cv.mean(cv.split(cv.add(in_mat1, in_mat2))[1]) # G-API g_in1 = cv.GMat() g_in2 = cv.GMat() g_sum = cv.gapi.add(g_in1, g_in2) g_b, g_r, g_g = cv.gapi.split3(g_sum) g_mean = cv.gapi.mean(g_b) comp = cv.GComputation(cv.GIn(g_in1, g_in2), cv.GOut(g_mean)) pkg = cv.gapi_wip_kernels( (custom_add, 'org.opencv.core.math.add'), (custom_mean, 'org.opencv.core.math.mean'), (custom_split3, 'org.opencv.core.transform.split3')) actual = comp.apply(cv.gin(in_mat1, in_mat2), args=cv.compile_args(pkg)) self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_threshold(self): img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')]) in_mat = cv.cvtColor(cv.imread(img_path), cv.COLOR_RGB2GRAY) maxv = (30, 30) # OpenCV expected_thresh, expected_mat = cv.threshold(in_mat, maxv[0], maxv[0], cv.THRESH_TRIANGLE) # G-API g_in = cv.GMat() g_sc = cv.GScalar() mat, threshold = cv.gapi.threshold(g_in, g_sc, cv.THRESH_TRIANGLE) comp = cv.GComputation(cv.GIn(g_in, g_sc), cv.GOut(mat, threshold)) for pkg_name, pkg in pkgs: actual_mat, actual_thresh = comp.apply(cv.gin(in_mat, maxv), args=cv.compile_args(pkg)) # Comparison self.assertEqual(0.0, cv.norm(expected_mat, actual_mat, cv.NORM_INF), 'Failed on ' + pkg_name + ' backend') self.assertEqual(expected_mat.dtype, actual_mat.dtype, 'Failed on ' + pkg_name + ' backend') self.assertEqual(expected_thresh, actual_thresh[0], 'Failed on ' + pkg_name + ' backend')
def test_pipeline_with_custom_kernels(self): @cv.gapi.op('custom.resize', in_types=[cv.GMat, tuple], out_types=[cv.GMat]) class GResize: @staticmethod def outMeta(desc, size): return desc.withSize(size) @cv.gapi.kernel(GResize) class GResizeImpl: @staticmethod def run(img, size): return cv.resize(img, size) @cv.gapi.op('custom.transpose', in_types=[cv.GMat, tuple], out_types=[cv.GMat]) class GTranspose: @staticmethod def outMeta(desc, order): return desc @cv.gapi.kernel(GTranspose) class GTransposeImpl: @staticmethod def run(img, order): return np.transpose(img, order) img_path = self.find_file( 'cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')]) img = cv.imread(img_path) size = (32, 32) order = (1, 0, 2) # Dummy pipeline just to validate this case: # gapi -> custom -> custom -> gapi # OpenCV expected = cv.cvtColor(img, cv.COLOR_BGR2RGB) expected = cv.resize(expected, size) expected = np.transpose(expected, order) expected = cv.mean(expected) # G-API g_bgr = cv.GMat() g_rgb = cv.gapi.BGR2RGB(g_bgr) g_resized = GResize.on(g_rgb, size) g_transposed = GTranspose.on(g_resized, order) g_mean = cv.gapi.mean(g_transposed) comp = cv.GComputation(cv.GIn(g_bgr), cv.GOut(g_mean)) actual = comp.apply(cv.gin(img), args=cv.compile_args( cv.gapi.kernels(GResizeImpl, GTransposeImpl))) self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_age_gender_infer2_roi(self): # NB: Check IE if not cv.dnn.DNN_TARGET_CPU in cv.dnn.getAvailableTargets( cv.dnn.DNN_BACKEND_INFERENCE_ENGINE): return root_path = '/omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013' model_path = self.find_file( root_path + '.xml', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')]) weights_path = self.find_file( root_path + '.bin', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')]) device_id = 'CPU' rois = [(10, 15, 62, 62), (23, 50, 62, 62), (14, 100, 62, 62), (80, 50, 62, 62)] img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')]) img = cv.imread(img_path) # OpenCV DNN dnn_age_list = [] dnn_gender_list = [] for roi in rois: age, gender = self.infer_reference_network(model_path, weights_path, self.make_roi(img, roi)) dnn_age_list.append(age) dnn_gender_list.append(gender) # OpenCV G-API g_in = cv.GMat() g_rois = cv.GArrayT(cv.gapi.CV_RECT) inputs = cv.GInferListInputs() inputs.setInput('data', g_rois) outputs = cv.gapi.infer2("net", g_in, inputs) age_g = outputs.at("age_conv3") gender_g = outputs.at("prob") comp = cv.GComputation(cv.GIn(g_in, g_rois), cv.GOut(age_g, gender_g)) pp = cv.gapi.ie.params("net", model_path, weights_path, device_id) gapi_age_list, gapi_gender_list = comp.apply(cv.gin(img, rois), args=cv.compile_args( cv.gapi.networks(pp))) # Check for gapi_age, gapi_gender, dnn_age, dnn_gender in zip( gapi_age_list, gapi_gender_list, dnn_age_list, dnn_gender_list): self.assertEqual(0.0, cv.norm(dnn_gender, gapi_gender, cv.NORM_INF)) self.assertEqual(0.0, cv.norm(dnn_age, gapi_age, cv.NORM_INF))
def test_opaq_with_custom_type(self): @cv.gapi.op('custom.op', in_types=[cv.GOpaque.Any, cv.GOpaque.String], out_types=[cv.GOpaque.Any]) class GLookUp: @staticmethod def outMeta(opaq_desc0, opaq_desc1): return cv.empty_gopaque_desc() @cv.gapi.kernel(GLookUp) class GLookUpImpl: @staticmethod def run(table, key): return table[key] g_table = cv.GOpaque.Any() g_key = cv.GOpaque.String() g_out = GLookUp.on(g_table, g_key) comp = cv.GComputation(cv.GIn(g_table, g_key), cv.GOut(g_out)) table = {'int': 42, 'str': 'hello, world!', 'tuple': (42, 42)} out = comp.apply(cv.gin(table, 'int'), args=cv.compile_args( cv.gapi.kernels(GLookUpImpl))) self.assertEqual(42, out) out = comp.apply(cv.gin(table, 'str'), args=cv.compile_args( cv.gapi.kernels(GLookUpImpl))) self.assertEqual('hello, world!', out) out = comp.apply(cv.gin(table, 'tuple'), args=cv.compile_args( cv.gapi.kernels(GLookUpImpl))) self.assertEqual((42, 42), out)
def test_custom_size(self): sz = (100, 150, 3) in_mat = np.full(sz, 45, dtype=np.uint8) # OpenCV expected = (100, 150) # G-API g_in = cv.GMat() g_sz = cv.gapi.streaming.size(g_in) comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_sz)) pkg = cv.gapi_wip_kernels((custom_size, 'org.opencv.streaming.size')) actual = comp.apply(cv.gin(in_mat), args=cv.compile_args(pkg)) self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_custom_op_size(self): sz = (100, 150, 3) in_mat = np.full(sz, 45, dtype=np.uint8) # Open_cV expected = (100, 150) # G-API g_in = cv.GMat() g_sz = GSize.on(g_in) comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_sz)) pkg = cv.gapi.kernels(GSizeImpl) actual = comp.apply(cv.gin(in_mat), args=cv.compile_args(pkg)) self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_custom_sizeR(self): # x, y, h, w roi = (10, 15, 100, 150) expected = (100, 150) # G-API g_r = cv.GOpaqueT(cv.gapi.CV_RECT) g_sz = cv.gapi.streaming.size(g_r) comp = cv.GComputation(cv.GIn(g_r), cv.GOut(g_sz)) pkg = cv.gapi_wip_kernels((custom_sizeR, 'org.opencv.streaming.sizeR')) actual = comp.apply(cv.gin(roi), args=cv.compile_args(pkg)) # cv.norm works with tuples ? self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_custom_op_boundingRect(self): points = [(0, 0), (0, 1), (1, 0), (1, 1)] # OpenCV expected = cv.boundingRect(np.array(points)) # G-API g_pts = cv.GArray.Point() g_br = GBoundingRect.on(g_pts) comp = cv.GComputation(cv.GIn(g_pts), cv.GOut(g_br)) pkg = cv.gapi.kernels(GBoundingRectImpl) actual = comp.apply(cv.gin(points), args=cv.compile_args(pkg)) # cv.norm works with tuples ? self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_custom_op_sizeR(self): # x, y, h, w roi = (10, 15, 100, 150) expected = (100, 150) # G-API g_r = cv.GOpaque.Rect() g_sz = GSizeR.on(g_r) comp = cv.GComputation(cv.GIn(g_r), cv.GOut(g_sz)) pkg = cv.gapi.kernels(GSizeRImpl) actual = comp.apply(cv.gin(roi), args=cv.compile_args(pkg)) # cv.norm works with tuples ? self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_mean(self): sz = (1280, 720, 3) in_mat = np.random.randint(0, 100, sz).astype(np.uint8) # OpenCV expected = cv.mean(in_mat) # G-API g_in = cv.GMat() g_out = cv.gapi.mean(g_in) comp = cv.GComputation(g_in, g_out) for pkg in pkgs: actual = comp.apply(cv.gin(in_mat), args=cv.compile_args(pkg)) # Comparison self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_split3(self): sz = (1280, 720, 3) in_mat = np.random.randint(0, 100, sz).astype(np.uint8) # OpenCV expected = cv.split(in_mat) # G-API g_in = cv.GMat() b, g, r = cv.gapi.split3(g_in) comp = cv.GComputation(cv.GIn(g_in), cv.GOut(b, g, r)) for pkg in pkgs: actual = comp.apply(cv.gin(in_mat), args=cv.compile_args(pkg)) # Comparison for e, a in zip(expected, actual): self.assertEqual(0.0, cv.norm(e, a, cv.NORM_INF))
def test_mean(self): img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')]) in_mat = cv.imread(img_path) # OpenCV expected = cv.mean(in_mat) # G-API g_in = cv.GMat() g_out = cv.gapi.mean(g_in) comp = cv.GComputation(g_in, g_out) for pkg_name, pkg in pkgs: actual = comp.apply(cv.gin(in_mat), args=cv.compile_args(pkg)) # Comparison self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF), 'Failed on ' + pkg_name + ' backend')
def test_custom_goodFeaturesToTrack(self): # G-API img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')]) in_mat = cv.cvtColor(cv.imread(img_path), cv.COLOR_RGB2GRAY) # NB: goodFeaturesToTrack configuration max_corners = 50 quality_lvl = 0.01 min_distance = 10 block_sz = 3 use_harris_detector = True k = 0.04 mask = None # OpenCV expected = cv.goodFeaturesToTrack( in_mat, max_corners, quality_lvl, min_distance, mask=mask, blockSize=block_sz, useHarrisDetector=use_harris_detector, k=k) # G-API g_in = cv.GMat() g_out = cv.gapi.goodFeaturesToTrack(g_in, max_corners, quality_lvl, min_distance, mask, block_sz, use_harris_detector, k) comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_out)) pkg = cv.gapi_wip_kernels( (custom_goodFeaturesToTrack, 'org.opencv.imgproc.feature.goodFeaturesToTrack')) actual = comp.apply(cv.gin(in_mat), args=cv.compile_args(pkg)) # NB: OpenCV & G-API have different output types. # OpenCV - numpy array with shape (num_points, 1, 2) # G-API - list of tuples with size - num_points # Comparison self.assertEqual( 0.0, cv.norm(expected.flatten(), np.array(actual, dtype=np.float32).flatten(), cv.NORM_INF))
def test_custom_boundingRect(self): points = [(0, 0), (0, 1), (1, 0), (1, 1)] # OpenCV expected = cv.boundingRect(np.array(points)) # G-API g_pts = cv.GArrayT(cv.gapi.CV_POINT) g_br = cv.gapi.boundingRect(g_pts) comp = cv.GComputation(cv.GIn(g_pts), cv.GOut(g_br)) pkg = cv.gapi_wip_kernels( (custom_boundingRect, 'org.opencv.imgproc.shape.boundingRectVector32S')) actual = comp.apply(cv.gin(points), args=cv.compile_args(pkg)) # cv.norm works with tuples ? self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_mean_over_r(self): sz = (100, 100, 3) in_mat = np.random.randint(0, 100, sz).astype(np.uint8) # # OpenCV _, _, r_ch = cv.split(in_mat) expected = cv.mean(r_ch) # G-API g_in = cv.GMat() b, g, r = cv.gapi.split3(g_in) g_out = cv.gapi.mean(r) comp = cv.GComputation(g_in, g_out) for pkg in pkgs: actual = comp.apply(cv.gin(in_mat), args=cv.compile_args(pkg)) # Comparison self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_custom_op_addC(self): sz = (3, 3, 3) in_mat = np.full(sz, 45, dtype=np.uint8) sc = (50, 10, 20) # Numpy reference, make array from sc to keep uint8 dtype. expected = in_mat + np.array(sc, dtype=np.uint8) # G-API g_in = cv.GMat() g_sc = cv.GScalar() g_out = GAddC.on(g_in, g_sc, cv.CV_8UC1) comp = cv.GComputation(cv.GIn(g_in, g_sc), cv.GOut(g_out)) pkg = cv.gapi.kernels(GAddCImpl) actual = comp.apply(cv.gin(in_mat, sc), args=cv.compile_args(pkg)) self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_good_features_to_track(self): # TODO: Extend to use any type and size here img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')]) in1 = cv.cvtColor(cv.imread(img_path), cv.COLOR_RGB2GRAY) # NB: goodFeaturesToTrack configuration max_corners = 50 quality_lvl = 0.01 min_distance = 10 block_sz = 3 use_harris_detector = True k = 0.04 mask = None # OpenCV expected = cv.goodFeaturesToTrack( in1, max_corners, quality_lvl, min_distance, mask=mask, blockSize=block_sz, useHarrisDetector=use_harris_detector, k=k) # G-API g_in = cv.GMat() g_out = cv.gapi.goodFeaturesToTrack(g_in, max_corners, quality_lvl, min_distance, mask, block_sz, use_harris_detector, k) comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_out)) for pkg_name, pkg in pkgs: actual = comp.apply(cv.gin(in1), args=cv.compile_args(pkg)) # NB: OpenCV & G-API have different output shapes: # OpenCV - (num_points, 1, 2) # G-API - (num_points, 2) # Comparison self.assertEqual( 0.0, cv.norm(expected.flatten(), actual.flatten(), cv.NORM_INF), 'Failed on ' + pkg_name + ' backend')
def test_rgb2gray(self): # TODO: Extend to use any type and size here img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')]) in1 = cv.imread(img_path) # OpenCV expected = cv.cvtColor(in1, cv.COLOR_RGB2GRAY) # G-API g_in = cv.GMat() g_out = cv.gapi.RGB2Gray(g_in) comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_out)) for pkg_name, pkg in pkgs: actual = comp.apply(cv.gin(in1), args=cv.compile_args(pkg)) # Comparison self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF), 'Failed on ' + pkg_name + ' backend')
def test_split3(self): img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')]) in_mat = cv.imread(img_path) # OpenCV expected = cv.split(in_mat) # G-API g_in = cv.GMat() b, g, r = cv.gapi.split3(g_in) comp = cv.GComputation(cv.GIn(g_in), cv.GOut(b, g, r)) for pkg_name, pkg in pkgs: actual = comp.apply(cv.gin(in_mat), args=cv.compile_args(pkg)) # Comparison for e, a in zip(expected, actual): self.assertEqual(0.0, cv.norm(e, a, cv.NORM_INF), 'Failed on ' + pkg_name + ' backend') self.assertEqual(e.dtype, a.dtype, 'Failed on ' + pkg_name + ' backend')
def test_custom_add(self): sz = (3, 3) in_mat1 = np.full(sz, 45, dtype=np.uint8) in_mat2 = np.full(sz, 50, dtype=np.uint8) # OpenCV expected = cv.add(in_mat1, in_mat2) # G-API g_in1 = cv.GMat() g_in2 = cv.GMat() g_out = cv.gapi.add(g_in1, g_in2) comp = cv.GComputation(cv.GIn(g_in1, g_in2), cv.GOut(g_out)) pkg = cv.gapi_wip_kernels((custom_add, 'org.opencv.core.math.add')) actual = comp.apply(cv.gin(in_mat1, in_mat2), args=cv.compile_args(pkg)) self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_custom_mean(self): img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')]) in_mat = cv.imread(img_path) # OpenCV expected = cv.mean(in_mat) # G-API g_in = cv.GMat() g_out = cv.gapi.mean(g_in) comp = cv.GComputation(g_in, g_out) pkg = cv.gapi_wip_kernels((custom_mean, 'org.opencv.core.math.mean')) actual = comp.apply(cv.gin(in_mat), args=cv.compile_args(pkg)) # Comparison self.assertEqual(expected, actual)
def test_add(self): # TODO: Extend to use any type and size here sz = (1280, 720) in1 = np.random.randint(0, 100, sz).astype(np.uint8) in2 = np.random.randint(0, 100, sz).astype(np.uint8) # OpenCV expected = in1 + in2 # G-API g_in1 = cv.GMat() g_in2 = cv.GMat() g_out = cv.gapi.add(g_in1, g_in2) comp = cv.GComputation(cv.GIn(g_in1, g_in2), cv.GOut(g_out)) for pkg in pkgs: actual = comp.apply(cv.gin(in1, in2), args=cv.compile_args(pkg)) # Comparison self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
def test_add_uint8(self): sz = (720, 1280) in1 = np.full(sz, 100, dtype=np.uint8) in2 = np.full(sz, 50 , dtype=np.uint8) # OpenCV expected = cv.add(in1, in2) # G-API g_in1 = cv.GMat() g_in2 = cv.GMat() g_out = cv.gapi.add(g_in1, g_in2) comp = cv.GComputation(cv.GIn(g_in1, g_in2), cv.GOut(g_out)) for pkg_name, pkg in pkgs: actual = comp.apply(cv.gin(in1, in2), args=cv.compile_args(pkg)) # Comparison self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF), 'Failed on ' + pkg_name + ' backend') self.assertEqual(expected.dtype, actual.dtype, 'Failed on ' + pkg_name + ' backend')
def test_threshold(self): sz = (1280, 720) in_mat = np.random.randint(0, 100, sz).astype(np.uint8) rand_int = np.random.randint(0, 50) maxv = (rand_int, rand_int) # OpenCV expected_thresh, expected_mat = cv.threshold(in_mat, maxv[0], maxv[0], cv.THRESH_TRIANGLE) # G-API g_in = cv.GMat() g_sc = cv.GScalar() mat, threshold = cv.gapi.threshold(g_in, g_sc, cv.THRESH_TRIANGLE) comp = cv.GComputation(cv.GIn(g_in, g_sc), cv.GOut(mat, threshold)) for pkg in pkgs: actual_mat, actual_thresh = comp.apply(cv.gin(in_mat, maxv), args=cv.compile_args(pkg)) # Comparison self.assertEqual(0.0, cv.norm(expected_mat, actual_mat, cv.NORM_INF)) self.assertEqual(expected_thresh, actual_thresh[0])