test_gapi_imgproc.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python
  2. import numpy as np
  3. import cv2 as cv
  4. import os
  5. import sys
  6. import unittest
  7. from tests_common import NewOpenCVTests
  8. try:
  9. if sys.version_info[:2] < (3, 0):
  10. raise unittest.SkipTest('Python 2.x is not supported')
  11. # Plaidml is an optional backend
  12. pkgs = [
  13. ('ocl' , cv.gapi.core.ocl.kernels()),
  14. ('cpu' , cv.gapi.core.cpu.kernels()),
  15. ('fluid' , cv.gapi.core.fluid.kernels())
  16. # ('plaidml', cv.gapi.core.plaidml.kernels())
  17. ]
  18. class gapi_imgproc_test(NewOpenCVTests):
  19. def test_good_features_to_track(self):
  20. # TODO: Extend to use any type and size here
  21. img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')])
  22. in1 = cv.cvtColor(cv.imread(img_path), cv.COLOR_RGB2GRAY)
  23. # NB: goodFeaturesToTrack configuration
  24. max_corners = 50
  25. quality_lvl = 0.01
  26. min_distance = 10
  27. block_sz = 3
  28. use_harris_detector = True
  29. k = 0.04
  30. mask = None
  31. # OpenCV
  32. expected = cv.goodFeaturesToTrack(in1, max_corners, quality_lvl,
  33. min_distance, mask=mask,
  34. blockSize=block_sz, useHarrisDetector=use_harris_detector, k=k)
  35. # G-API
  36. g_in = cv.GMat()
  37. g_out = cv.gapi.goodFeaturesToTrack(g_in, max_corners, quality_lvl,
  38. min_distance, mask, block_sz, use_harris_detector, k)
  39. comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_out))
  40. for pkg_name, pkg in pkgs:
  41. actual = comp.apply(cv.gin(in1), args=cv.gapi.compile_args(pkg))
  42. # NB: OpenCV & G-API have different output shapes:
  43. # OpenCV - (num_points, 1, 2)
  44. # G-API - (num_points, 2)
  45. # Comparison
  46. self.assertEqual(0.0, cv.norm(expected.flatten(),
  47. np.array(actual, dtype=np.float32).flatten(),
  48. cv.NORM_INF),
  49. 'Failed on ' + pkg_name + ' backend')
  50. def test_rgb2gray(self):
  51. # TODO: Extend to use any type and size here
  52. img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')])
  53. in1 = cv.imread(img_path)
  54. # OpenCV
  55. expected = cv.cvtColor(in1, cv.COLOR_RGB2GRAY)
  56. # G-API
  57. g_in = cv.GMat()
  58. g_out = cv.gapi.RGB2Gray(g_in)
  59. comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_out))
  60. for pkg_name, pkg in pkgs:
  61. actual = comp.apply(cv.gin(in1), args=cv.gapi.compile_args(pkg))
  62. # Comparison
  63. self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF),
  64. 'Failed on ' + pkg_name + ' backend')
  65. def test_bounding_rect(self):
  66. sz = 1280
  67. fscale = 256
  68. def sample_value(fscale):
  69. return np.random.uniform(0, 255 * fscale) / fscale
  70. points = np.array([(sample_value(fscale), sample_value(fscale)) for _ in range(1280)], np.float32)
  71. # OpenCV
  72. expected = cv.boundingRect(points)
  73. # G-API
  74. g_in = cv.GMat()
  75. g_out = cv.gapi.boundingRect(g_in)
  76. comp = cv.GComputation(cv.GIn(g_in), cv.GOut(g_out))
  77. for pkg_name, pkg in pkgs:
  78. actual = comp.apply(cv.gin(points), args=cv.gapi.compile_args(pkg))
  79. # Comparison
  80. self.assertEqual(0.0, cv.norm(expected, actual, cv.NORM_INF),
  81. 'Failed on ' + pkg_name + ' backend')
  82. except unittest.SkipTest as e:
  83. message = str(e)
  84. class TestSkip(unittest.TestCase):
  85. def setUp(self):
  86. self.skipTest('Skip tests: ' + message)
  87. def test_skip():
  88. pass
  89. pass
  90. if __name__ == '__main__':
  91. NewOpenCVTests.bootstrap()