test_persistence.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. """"Core serialization tests."""
  3. import tempfile
  4. import os
  5. import cv2 as cv
  6. import numpy as np
  7. from tests_common import NewOpenCVTests
  8. class persistence_test(NewOpenCVTests):
  9. def test_yml_rw(self):
  10. fd, fname = tempfile.mkstemp(prefix="opencv_python_persistence_", suffix=".yml")
  11. os.close(fd)
  12. # Writing ...
  13. expected = np.array([[[0, 1, 2, 3, 4]]])
  14. expected_str = ("Hello", "World", "!")
  15. fs = cv.FileStorage(fname, cv.FILE_STORAGE_WRITE)
  16. fs.write("test", expected)
  17. fs.write("strings", expected_str)
  18. fs.release()
  19. # Reading ...
  20. fs = cv.FileStorage(fname, cv.FILE_STORAGE_READ)
  21. root = fs.getFirstTopLevelNode()
  22. self.assertEqual(root.name(), "test")
  23. test = fs.getNode("test")
  24. self.assertEqual(test.empty(), False)
  25. self.assertEqual(test.name(), "test")
  26. self.assertEqual(test.type(), cv.FILE_NODE_MAP)
  27. self.assertEqual(test.isMap(), True)
  28. actual = test.mat()
  29. self.assertEqual(actual.shape, expected.shape)
  30. self.assertEqual(np.array_equal(expected, actual), True)
  31. strings = fs.getNode("strings")
  32. self.assertEqual(strings.isSeq(), True)
  33. self.assertEqual(strings.size(), len(expected_str))
  34. self.assertEqual(all(strings.at(i).isString() for i in range(strings.size())), True)
  35. self.assertSequenceEqual([strings.at(i).string() for i in range(strings.size())], expected_str)
  36. fs.release()
  37. os.remove(fname)