2018-08-06 19:46:46 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
'''
|
|
|
|
CUDA-accelerated Computer Vision functions
|
|
|
|
'''
|
|
|
|
|
|
|
|
# Python 2/3 compatibility
|
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
import cv2 as cv
|
2019-12-04 23:57:58 +08:00
|
|
|
import os
|
2018-08-06 19:46:46 +08:00
|
|
|
|
2019-12-04 23:57:58 +08:00
|
|
|
from tests_common import NewOpenCVTests, unittest
|
2018-08-06 19:46:46 +08:00
|
|
|
|
|
|
|
class cuda_test(NewOpenCVTests):
|
|
|
|
def setUp(self):
|
2018-08-25 04:57:24 +08:00
|
|
|
super(cuda_test, self).setUp()
|
2018-08-06 19:46:46 +08:00
|
|
|
if not cv.cuda.getCudaEnabledDeviceCount():
|
|
|
|
self.skipTest("No CUDA-capable device is detected")
|
|
|
|
|
|
|
|
def test_cuda_upload_download(self):
|
2018-08-25 04:57:24 +08:00
|
|
|
npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
|
|
|
|
cuMat = cv.cuda_GpuMat()
|
|
|
|
cuMat.upload(npMat)
|
2018-08-06 19:46:46 +08:00
|
|
|
|
2018-08-25 04:57:24 +08:00
|
|
|
self.assertTrue(np.allclose(cuMat.download(), npMat))
|
2018-08-06 19:46:46 +08:00
|
|
|
|
2021-05-01 09:18:14 +08:00
|
|
|
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))
|
|
|
|
|
2020-02-05 01:25:48 +08:00
|
|
|
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)
|
2021-01-28 23:14:01 +08:00
|
|
|
asyncstream = cv.cuda_Stream(1) # cudaStreamNonBlocking
|
|
|
|
self.assertTrue(asyncstream.cudaPtr() != 0)
|
2020-02-05 01:25:48 +08:00
|
|
|
|
2018-08-06 19:46:46 +08:00
|
|
|
if __name__ == '__main__':
|
|
|
|
NewOpenCVTests.bootstrap()
|