File: test_gapi_core.py

package info (click to toggle)
opencv 4.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 282,092 kB
  • sloc: cpp: 1,178,079; xml: 682,621; python: 49,092; lisp: 31,150; java: 25,469; ansic: 11,039; javascript: 6,085; sh: 1,214; cs: 601; perl: 494; objc: 210; makefile: 173
file content (239 lines) | stat: -rw-r--r-- 8,525 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env python

import numpy as np
import cv2 as cv
import os
import sys
import unittest

from tests_common import NewOpenCVTests


try:

    if sys.version_info[:2] < (3, 0):
        raise unittest.SkipTest('Python 2.x is not supported')

    # Plaidml is an optional backend
    pkgs = [
              ('ocl'    , cv.gapi.core.ocl.kernels()),
              ('cpu'    , cv.gapi.core.cpu.kernels()),
              ('fluid'  , cv.gapi.core.fluid.kernels())
              # ('plaidml', cv.gapi.core.plaidml.kernels())
           ]


    class gapi_core_test(NewOpenCVTests):

        def test_add(self):
            # TODO: Extend to use any type and size here
            sz = (720, 1280)
            in1 = np.full(sz, 100)
            in2 = np.full(sz, 50)

            # 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.gapi.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_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.gapi.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_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.gapi.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.gapi.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_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.gapi.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_kmeans(self):
            # K-means params
            count    = 100
            sz       = (count, 2)
            in_mat   = np.random.random(sz).astype(np.float32)
            K        = 5
            flags    = cv.KMEANS_RANDOM_CENTERS
            attempts = 1
            criteria = (cv.TERM_CRITERIA_MAX_ITER + cv.TERM_CRITERIA_EPS, 30, 0)

            # G-API
            g_in = cv.GMat()
            compactness, out_labels, centers = cv.gapi.kmeans(g_in, K, criteria, attempts, flags)
            comp = cv.GComputation(cv.GIn(g_in), cv.GOut(compactness, out_labels, centers))

            compact, labels, centers = comp.apply(cv.gin(in_mat))

            # Assert
            self.assertTrue(compact >= 0)
            self.assertEqual(sz[0], labels.shape[0])
            self.assertEqual(1, labels.shape[1])
            self.assertTrue(labels.size != 0)
            self.assertEqual(centers.shape[1], sz[1])
            self.assertEqual(centers.shape[0], K)
            self.assertTrue(centers.size != 0)


        def generate_random_points(self, sz):
            arr = np.random.random(sz).astype(np.float32).T
            return list(zip(*[arr[i] for i in range(sz[1])]))


        def test_kmeans_2d(self):
            # K-means 2D params
            count     = 100
            sz        = (count, 2)
            amount    = sz[0]
            K         = 5
            flags     = cv.KMEANS_RANDOM_CENTERS
            attempts  = 1
            criteria  = (cv.TERM_CRITERIA_MAX_ITER + cv.TERM_CRITERIA_EPS, 30, 0)
            in_vector = self.generate_random_points(sz)
            in_labels = []

            # G-API
            data        = cv.GArrayT(cv.gapi.CV_POINT2F)
            best_labels = cv.GArrayT(cv.gapi.CV_INT)

            compactness, out_labels, centers = cv.gapi.kmeans(data, K, best_labels, criteria, attempts, flags)
            comp = cv.GComputation(cv.GIn(data, best_labels), cv.GOut(compactness, out_labels, centers))

            compact, labels, centers = comp.apply(cv.gin(in_vector, in_labels))

            # Assert
            self.assertTrue(compact >= 0)
            self.assertEqual(amount, len(labels))
            self.assertEqual(K, len(centers))


        def test_kmeans_3d(self):
            # K-means 3D params
            count     = 100
            sz        = (count, 3)
            amount    = sz[0]
            K         = 5
            flags     = cv.KMEANS_RANDOM_CENTERS
            attempts  = 1
            criteria  = (cv.TERM_CRITERIA_MAX_ITER + cv.TERM_CRITERIA_EPS, 30, 0)
            in_vector = self.generate_random_points(sz)
            in_labels = []

            # G-API
            data        = cv.GArrayT(cv.gapi.CV_POINT3F)
            best_labels = cv.GArrayT(cv.gapi.CV_INT)

            compactness, out_labels, centers = cv.gapi.kmeans(data, K, best_labels, criteria, attempts, flags)
            comp = cv.GComputation(cv.GIn(data, best_labels), cv.GOut(compactness, out_labels, centers))

            compact, labels, centers = comp.apply(cv.gin(in_vector, in_labels))

            # Assert
            self.assertTrue(compact >= 0)
            self.assertEqual(amount, len(labels))
            self.assertEqual(K, len(centers))


except unittest.SkipTest as e:

    message = str(e)

    class TestSkip(unittest.TestCase):
        def setUp(self):
            self.skipTest('Skip tests: ' + message)

        def test_skip():
            pass

    pass


if __name__ == '__main__':
    NewOpenCVTests.bootstrap()