File: test_gapi_streaming.py

package info (click to toggle)
opencv 4.5.1%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 268,248 kB
  • sloc: cpp: 969,170; xml: 682,525; python: 36,732; lisp: 30,170; java: 25,155; ansic: 7,927; javascript: 5,643; objc: 2,041; sh: 935; cs: 601; perl: 494; makefile: 145
file content (202 lines) | stat: -rw-r--r-- 5,932 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
#!/usr/bin/env python

import numpy as np
import cv2 as cv
import os

from tests_common import NewOpenCVTests

class test_gapi_streaming(NewOpenCVTests):

    def test_image_input(self):
        sz = (1280, 720)
        in_mat = np.random.randint(0, 100, sz).astype(np.uint8)

        # OpenCV
        expected = cv.medianBlur(in_mat, 3)

        # G-API
        g_in = cv.GMat()
        g_out = cv.gapi.medianBlur(g_in, 3)
        c = cv.GComputation(g_in, g_out)
        ccomp = c.compileStreaming(cv.descr_of(cv.gin(in_mat)))
        ccomp.setSource(cv.gin(in_mat))
        ccomp.start()

        _, actual = ccomp.pull()

        # Assert
        self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))


    def test_video_input(self):
        ksize = 3
        path = self.find_file('cv/video/768x576.avi', [os.environ['OPENCV_TEST_DATA_PATH']])

        # OpenCV
        cap = cv.VideoCapture(path)

        # G-API
        g_in = cv.GMat()
        g_out = cv.gapi.medianBlur(g_in, ksize)
        c = cv.GComputation(g_in, g_out)

        ccomp = c.compileStreaming()
        source = cv.gapi.wip.make_capture_src(path)
        ccomp.setSource(source)
        ccomp.start()

        # Assert
        max_num_frames  = 10
        proc_num_frames = 0
        while cap.isOpened():
            has_expected, expected = cap.read()
            has_actual,   actual   = ccomp.pull()

            self.assertEqual(has_expected, has_actual)

            if not has_actual:
                break

            self.assertEqual(0.0, cv.norm(cv.medianBlur(expected, ksize), actual, cv.NORM_INF))

            proc_num_frames += 1
            if proc_num_frames == max_num_frames:
                break;


    def test_video_split3(self):
        path = self.find_file('cv/video/768x576.avi', [os.environ['OPENCV_TEST_DATA_PATH']])

        # OpenCV
        cap = cv.VideoCapture(path)

        # G-API
        g_in = cv.GMat()
        b, g, r = cv.gapi.split3(g_in)
        c = cv.GComputation(cv.GIn(g_in), cv.GOut(b, g, r))

        ccomp = c.compileStreaming()
        source = cv.gapi.wip.make_capture_src(path)
        ccomp.setSource(source)
        ccomp.start()

        # Assert
        max_num_frames  = 10
        proc_num_frames = 0
        while cap.isOpened():
            has_expected, frame = cap.read()
            has_actual,   actual   = ccomp.pull()

            self.assertEqual(has_expected, has_actual)

            if not has_actual:
                break

            expected = cv.split(frame)
            for e, a in zip(expected, actual):
                self.assertEqual(0.0, cv.norm(e, a, cv.NORM_INF))

            proc_num_frames += 1
            if proc_num_frames == max_num_frames:
                break;


    def test_video_add(self):
        sz = (576, 768, 3)
        in_mat = np.random.randint(0, 100, sz).astype(np.uint8)

        path = self.find_file('cv/video/768x576.avi', [os.environ['OPENCV_TEST_DATA_PATH']])

        # OpenCV
        cap = cv.VideoCapture(path)

        # G-API
        g_in1 = cv.GMat()
        g_in2 = cv.GMat()
        out = cv.gapi.add(g_in1, g_in2)
        c = cv.GComputation(cv.GIn(g_in1, g_in2), cv.GOut(out))

        ccomp = c.compileStreaming()
        source = cv.gapi.wip.make_capture_src(path)
        ccomp.setSource(cv.gin(source, in_mat))
        ccomp.start()

        # Assert
        max_num_frames  = 10
        proc_num_frames = 0
        while cap.isOpened():
            has_expected, frame  = cap.read()
            has_actual,   actual = ccomp.pull()

            self.assertEqual(has_expected, has_actual)

            if not has_actual:
                break

            expected = cv.add(frame, in_mat)
            self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF))

            proc_num_frames += 1
            if proc_num_frames == max_num_frames:
                break;


    def test_video_good_features_to_track(self):
        path = self.find_file('cv/video/768x576.avi', [os.environ['OPENCV_TEST_DATA_PATH']])

        # 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
        cap = cv.VideoCapture(path)

        # G-API
        g_in = cv.GMat()
        g_gray = cv.gapi.RGB2Gray(g_in)
        g_out = cv.gapi.goodFeaturesToTrack(g_gray, max_corners, quality_lvl,
                                            min_distance, mask, block_sz, use_harris_detector, k)

        c = cv.GComputation(cv.GIn(g_in), cv.GOut(g_out))

        ccomp = c.compileStreaming()
        source = cv.gapi.wip.make_capture_src(path)
        ccomp.setSource(source)
        ccomp.start()

        # Assert
        max_num_frames  = 10
        proc_num_frames = 0
        while cap.isOpened():
            has_expected, frame  = cap.read()
            has_actual,   actual = ccomp.pull()

            self.assertEqual(has_expected, has_actual)

            if not has_actual:
                break

            # OpenCV
            frame = cv.cvtColor(frame, cv.COLOR_RGB2GRAY)
            expected = cv.goodFeaturesToTrack(frame, max_corners, quality_lvl,
                                              min_distance, mask=mask,
                                              blockSize=block_sz, useHarrisDetector=use_harris_detector, k=k)
            for e, a in zip(expected, actual):
                # NB: OpenCV & G-API have different output shapes:
                # OpenCV - (num_points, 1, 2)
                # G-API  - (num_points, 2)
                self.assertEqual(0.0, cv.norm(e.flatten(), a.flatten(), cv.NORM_INF))

            proc_num_frames += 1
            if proc_num_frames == max_num_frames:
                break;


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