test_cuda.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/env python
  2. '''
  3. CUDA-accelerated Computer Vision functions
  4. '''
  5. # Python 2/3 compatibility
  6. from __future__ import print_function
  7. import numpy as np
  8. import cv2 as cv
  9. import os
  10. from tests_common import NewOpenCVTests, unittest
  11. class cuda_test(NewOpenCVTests):
  12. def setUp(self):
  13. super(cuda_test, self).setUp()
  14. if not cv.cuda.getCudaEnabledDeviceCount():
  15. self.skipTest("No CUDA-capable device is detected")
  16. def test_cuda_upload_download(self):
  17. npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
  18. cuMat = cv.cuda_GpuMat()
  19. cuMat.upload(npMat)
  20. self.assertTrue(np.allclose(cuMat.download(), npMat))
  21. def test_cuda_upload_download_stream(self):
  22. stream = cv.cuda_Stream()
  23. npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
  24. cuMat = cv.cuda_GpuMat(128,128, cv.CV_8UC3)
  25. cuMat.upload(npMat, stream)
  26. npMat2 = cuMat.download(stream=stream)
  27. stream.waitForCompletion()
  28. self.assertTrue(np.allclose(npMat2, npMat))
  29. def test_cuda_interop(self):
  30. npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
  31. cuMat = cv.cuda_GpuMat()
  32. cuMat.upload(npMat)
  33. self.assertTrue(cuMat.cudaPtr() != 0)
  34. cuMatFromPtrSz = cv.cuda.createGpuMatFromCudaMemory(cuMat.size(),cuMat.type(),cuMat.cudaPtr(), cuMat.step)
  35. self.assertTrue(cuMat.cudaPtr() == cuMatFromPtrSz.cudaPtr())
  36. cuMatFromPtrRc = cv.cuda.createGpuMatFromCudaMemory(cuMat.size()[1],cuMat.size()[0],cuMat.type(),cuMat.cudaPtr(), cuMat.step)
  37. self.assertTrue(cuMat.cudaPtr() == cuMatFromPtrRc.cudaPtr())
  38. stream = cv.cuda_Stream()
  39. self.assertTrue(stream.cudaPtr() != 0)
  40. streamFromPtr = cv.cuda.wrapStream(stream.cudaPtr())
  41. self.assertTrue(stream.cudaPtr() == streamFromPtr.cudaPtr())
  42. asyncstream = cv.cuda_Stream(1) # cudaStreamNonBlocking
  43. self.assertTrue(asyncstream.cudaPtr() != 0)
  44. def test_cuda_buffer_pool(self):
  45. cv.cuda.setBufferPoolUsage(True)
  46. cv.cuda.setBufferPoolConfig(cv.cuda.getDevice(), 1024 * 1024 * 64, 2)
  47. stream_a = cv.cuda.Stream()
  48. pool_a = cv.cuda.BufferPool(stream_a)
  49. cuMat = pool_a.getBuffer(1024, 1024, cv.CV_8UC3)
  50. cv.cuda.setBufferPoolUsage(False)
  51. self.assertEqual(cuMat.size(), (1024, 1024))
  52. self.assertEqual(cuMat.type(), cv.CV_8UC3)
  53. def test_cuda_release(self):
  54. npMat = (np.random.random((128, 128, 3)) * 255).astype(np.uint8)
  55. cuMat = cv.cuda_GpuMat()
  56. cuMat.upload(npMat)
  57. cuMat.release()
  58. self.assertTrue(cuMat.cudaPtr() == 0)
  59. self.assertTrue(cuMat.step == 0)
  60. self.assertTrue(cuMat.size() == (0, 0))
  61. def test_cuda_denoising(self):
  62. self.assertEqual(True, hasattr(cv.cuda, 'fastNlMeansDenoising'))
  63. self.assertEqual(True, hasattr(cv.cuda, 'fastNlMeansDenoisingColored'))
  64. self.assertEqual(True, hasattr(cv.cuda, 'nonLocalMeans'))
  65. if __name__ == '__main__':
  66. NewOpenCVTests.bootstrap()