test_cuda.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. stream = cv.cuda_Stream()
  35. self.assertTrue(stream.cudaPtr() != 0)
  36. asyncstream = cv.cuda_Stream(1) # cudaStreamNonBlocking
  37. self.assertTrue(asyncstream.cudaPtr() != 0)
  38. if __name__ == '__main__':
  39. NewOpenCVTests.bootstrap()