aboutsummaryrefslogtreecommitdiff
path: root/src/argaze.test/OpenCVCuda.py
blob: e5524362bdf438514b6eea5ef88d062f4af548d5 (plain)
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
""" 

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
"""

__author__ = "Théo de la Hogue"
__credits__ = []
__copyright__ = "Copyright 2023, Ecole Nationale de l'Aviation Civile (ENAC)"
__license__ = "GPLv3"

import unittest

import numpy as np
import cv2 as cv
import os

class cuda_test(unittest.TestCase):
	"""Test Cuda-accelerated OpenCV functions class."""

	@unittest.skipIf(cv.cuda.getCudaEnabledDeviceCount() == 0, "No cuda device found")
	def test_cuda_upload_download(self):

		npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
		cuMat = cv.cuda_GpuMat()
		cuMat.upload(npMat)

		self.assertTrue(np.allclose(cuMat.download(), npMat))

	@unittest.skipIf(cv.cuda.getCudaEnabledDeviceCount() == 0, "No cuda device found")	
	def test_cuda_upload_download_stream(self):

		stream = cv.cuda_Stream()
		npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
		cuMat = cv.cuda_GpuMat(128,128, cv.CV_8UC3)
		cuMat.upload(npMat, stream)
		npMat2 = cuMat.download(stream=stream)
		stream.waitForCompletion()

		self.assertTrue(np.allclose(npMat2, npMat))

	@unittest.skipIf(cv.cuda.getCudaEnabledDeviceCount() == 0, "No cuda device found")
	def test_cuda_interop(self):

		npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
		cuMat = cv.cuda_GpuMat()
		cuMat.upload(npMat)

		self.assertTrue(cuMat.cudaPtr() != 0)

		stream = cv.cuda_Stream()
		self.assertTrue(stream.cudaPtr() != 0)

		asyncstream = cv.cuda_Stream(1)  # cudaStreamNonBlocking
		self.assertTrue(asyncstream.cudaPtr() != 0)

	@unittest.skipIf(cv.cuda.getCudaEnabledDeviceCount() == 0, "No cuda device found")
	def test_cuda_buffer_pool(self):

		cv.cuda.setBufferPoolUsage(True)
		cv.cuda.setBufferPoolConfig(cv.cuda.getDevice(), 1024 * 1024 * 64, 2)
		stream_a = cv.cuda.Stream()
		pool_a = cv.cuda.BufferPool(stream_a)
		cuMat = pool_a.getBuffer(1024, 1024, cv.CV_8UC3)
		cv.cuda.setBufferPoolUsage(False)

		self.assertEqual(cuMat.size(), (1024, 1024))
		self.assertEqual(cuMat.type(), cv.CV_8UC3)

	@unittest.skipIf(cv.cuda.getCudaEnabledDeviceCount() == 0, "No cuda device found")
	def test_cuda_release(self):

		npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
		cuMat = cv.cuda_GpuMat()
		cuMat.upload(npMat)
		cuMat.release()

		self.assertTrue(cuMat.cudaPtr() == 0)
		self.assertTrue(cuMat.step == 0)
		self.assertTrue(cuMat.size() == (0, 0))

	@unittest.skipIf(cv.cuda.getCudaEnabledDeviceCount() == 0, "No cuda device found")
	def test_cuda_denoising(self):

		self.assertEqual(True, hasattr(cv.cuda, 'fastNlMeansDenoising'))
		self.assertEqual(True, hasattr(cv.cuda, 'fastNlMeansDenoisingColored'))
		self.assertEqual(True, hasattr(cv.cuda, 'nonLocalMeans'))

if __name__ == '__main__':

	unittest.main()