test_nvidiaopticalflow.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import os
  2. import cv2 as cv
  3. import numpy as np
  4. from tests_common import NewOpenCVTests, unittest
  5. class nvidiaopticalflow_test(NewOpenCVTests):
  6. def setUp(self):
  7. super(nvidiaopticalflow_test, self).setUp()
  8. if not cv.cuda.getCudaEnabledDeviceCount():
  9. self.skipTest("No CUDA-capable device is detected")
  10. @unittest.skipIf('OPENCV_TEST_DATA_PATH' not in os.environ,
  11. "OPENCV_TEST_DATA_PATH is not defined")
  12. def test_calc(self):
  13. frame1 = os.environ['OPENCV_TEST_DATA_PATH'] + '/gpu/opticalflow/frame0.png'
  14. frame2 = os.environ['OPENCV_TEST_DATA_PATH'] + '/gpu/opticalflow/frame1.png'
  15. npMat1 = cv.cvtColor(cv.imread(frame1),cv.COLOR_BGR2GRAY)
  16. npMat2 = cv.cvtColor(cv.imread(frame2),cv.COLOR_BGR2GRAY)
  17. cuMat1 = cv.cuda_GpuMat(npMat1)
  18. cuMat2 = cv.cuda_GpuMat(npMat2)
  19. try:
  20. nvof = cv.cuda_NvidiaOpticalFlow_1_0.create(cuMat1.shape[1], cuMat1.shape[0], 5, False, False, False, 0)
  21. flow = nvof.calc(cuMat1, cuMat2, None)
  22. self.assertTrue(flow.shape[1] > 0 and flow.shape[0] > 0)
  23. flowUpSampled = nvof.upSampler(flow[0], cuMat1.shape[1], cuMat1.shape[0], nvof.getGridSize(), None)
  24. nvof.collectGarbage()
  25. except cv.error as e:
  26. if e.code == cv.Error.StsBadFunc or e.code == cv.Error.StsBadArg or e.code == cv.Error.StsNullPtr:
  27. self.skipTest("Algorithm is not supported in the current environment")
  28. self.assertTrue(flowUpSampled.shape[1] > 0 and flowUpSampled.shape[0] > 0)
  29. if __name__ == '__main__':
  30. NewOpenCVTests.bootstrap()