Merge pull request #18512 from TolyaTalamanov:at/fix-untyped-np-array-for-gapi-python
[platform/upstream/opencv.git] / modules / gapi / misc / python / test / test_gapi_core.py
1 #!/usr/bin/env python
2
3 import numpy as np
4 import cv2 as cv
5
6 from tests_common import NewOpenCVTests
7
8
9 # Plaidml is an optional backend
10 pkgs = [
11          cv.gapi.core.ocl.kernels(),
12          cv.gapi.core.cpu.kernels(),
13          cv.gapi.core.fluid.kernels()
14          # cv.gapi.core.plaidml.kernels()
15        ]
16
17
18 class gapi_core_test(NewOpenCVTests):
19
20     def test_add(self):
21         # TODO: Extend to use any type and size here
22         sz = (1280, 720)
23         in1 = np.random.randint(0, 100, sz)
24         in2 = np.random.randint(0, 100, sz)
25
26         # OpenCV
27         expected = cv.add(in1, in2)
28
29         # G-API
30         g_in1 = cv.GMat()
31         g_in2 = cv.GMat()
32         g_out = cv.gapi.add(g_in1, g_in2)
33         comp = cv.GComputation(cv.GIn(g_in1, g_in2), cv.GOut(g_out))
34
35         for pkg in pkgs:
36             actual = comp.apply(cv.gin(in1, in2), args=cv.compile_args(pkg))
37             # Comparison
38             self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
39             self.assertEqual(expected.dtype, actual.dtype)
40
41
42     def test_add_uint8(self):
43         sz = (1280, 720)
44         in1 = np.random.randint(0, 100, sz).astype(np.uint8)
45         in2 = np.random.randint(0, 100, sz).astype(np.uint8)
46
47         # OpenCV
48         expected = cv.add(in1, in2)
49
50         # G-API
51         g_in1 = cv.GMat()
52         g_in2 = cv.GMat()
53         g_out = cv.gapi.add(g_in1, g_in2)
54         comp = cv.GComputation(cv.GIn(g_in1, g_in2), cv.GOut(g_out))
55
56         for pkg in pkgs:
57             actual = comp.apply(cv.gin(in1, in2), args=cv.compile_args(pkg))
58             # Comparison
59             self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
60             self.assertEqual(expected.dtype, actual.dtype)
61
62
63     def test_mean(self):
64         sz = (1280, 720, 3)
65         in_mat = np.random.randint(0, 100, sz)
66
67         # OpenCV
68         expected = cv.mean(in_mat)
69
70         # G-API
71         g_in = cv.GMat()
72         g_out = cv.gapi.mean(g_in)
73         comp = cv.GComputation(g_in, g_out)
74
75         for pkg in pkgs:
76             actual = comp.apply(cv.gin(in_mat), args=cv.compile_args(pkg))
77             # Comparison
78             self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))
79
80
81     def test_split3(self):
82         sz = (1280, 720, 3)
83         in_mat = np.random.randint(0, 100, sz)
84
85         # OpenCV
86         expected = cv.split(in_mat)
87
88         # G-API
89         g_in = cv.GMat()
90         b, g, r = cv.gapi.split3(g_in)
91         comp = cv.GComputation(cv.GIn(g_in), cv.GOut(b, g, r))
92
93         for pkg in pkgs:
94             actual = comp.apply(cv.gin(in_mat), args=cv.compile_args(pkg))
95             # Comparison
96             for e, a in zip(expected, actual):
97                 self.assertEqual(0.0, cv.norm(e, a, cv.NORM_INF))
98                 self.assertEqual(e.dtype, a.dtype)
99
100
101     def test_threshold(self):
102         sz = (1280, 720)
103         in_mat = np.random.randint(0, 100, sz).astype(np.uint8)
104         rand_int = np.random.randint(0, 50)
105         maxv = (rand_int, rand_int)
106
107         # OpenCV
108         expected_thresh, expected_mat = cv.threshold(in_mat, maxv[0], maxv[0], cv.THRESH_TRIANGLE)
109
110         # G-API
111         g_in = cv.GMat()
112         g_sc = cv.GScalar()
113         mat, threshold = cv.gapi.threshold(g_in, g_sc, cv.THRESH_TRIANGLE)
114         comp = cv.GComputation(cv.GIn(g_in, g_sc), cv.GOut(mat, threshold))
115
116         for pkg in pkgs:
117             actual_mat, actual_thresh = comp.apply(cv.gin(in_mat, maxv), args=cv.compile_args(pkg))
118             # Comparison
119             self.assertEqual(0.0, cv.norm(expected_mat, actual_mat, cv.NORM_INF))
120             self.assertEqual(expected_mat.dtype, actual_mat.dtype)
121             self.assertEqual(expected_thresh, actual_thresh[0])
122
123
124 if __name__ == '__main__':
125     NewOpenCVTests.bootstrap()