|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# test for Image input/output |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +import os |
| 7 | +import numpy.testing as nt |
| 8 | +import unittest |
| 9 | +import contextlib |
| 10 | +import io |
| 11 | + |
| 12 | +# import machinevisiontoolbox as mvt |
| 13 | +from machinevisiontoolbox import Image, ImageCollection |
| 14 | +from machinevisiontoolbox.base import iread |
| 15 | +from pathlib import Path |
| 16 | +from collections.abc import Iterable |
| 17 | + |
| 18 | + |
| 19 | +class TestBaseImageIO(unittest.TestCase): |
| 20 | + |
| 21 | + def test_iread(self): |
| 22 | + # greyscale image |
| 23 | + im = iread("wally.png") |
| 24 | + self.assertIsInstance(im[0], np.ndarray) |
| 25 | + self.assertIsInstance(im[1], str) |
| 26 | + self.assertEqual(im[0].shape, (25, 21)) |
| 27 | + |
| 28 | + # color image |
| 29 | + im = iread("monalisa.png") |
| 30 | + self.assertIsInstance(im[0], np.ndarray) |
| 31 | + self.assertIsInstance(im[1], str) |
| 32 | + self.assertEqual(im[0].shape, (700, 677, 3)) |
| 33 | + |
| 34 | + # greyscale image sequence |
| 35 | + im = iread("seq/im*.png") |
| 36 | + self.assertIsInstance(im[0], list) |
| 37 | + self.assertEqual(len(im[0]), 9) |
| 38 | + self.assertIsInstance(im[0][0], np.ndarray) |
| 39 | + self.assertEqual(im[0][0].shape, (512, 512), self.assertIsInstance(im[1], list)) |
| 40 | + self.assertEqual(len(im[1]), 9) |
| 41 | + self.assertIsInstance(im[1][0], str) |
| 42 | + |
| 43 | + # color image sequence |
| 44 | + im = iread("campus/holdout/*.png") |
| 45 | + self.assertIsInstance(im[0], list) |
| 46 | + self.assertEqual(len(im[0]), 5) |
| 47 | + self.assertIsInstance(im[0][0], np.ndarray) |
| 48 | + self.assertEqual(im[0][0].shape, (426, 640, 3)) |
| 49 | + self.assertIsInstance(im[1], list) |
| 50 | + self.assertEqual(len(im[1]), 5) |
| 51 | + self.assertIsInstance(im[1][0], str) |
| 52 | + |
| 53 | + # URL image |
| 54 | + im = iread("https://petercorke.com/files/images/monalisa.png") |
| 55 | + self.assertIsInstance(im[0], np.ndarray) |
| 56 | + self.assertIsInstance(im[1], str) |
| 57 | + self.assertEqual(im[0].shape, (700, 677, 3)) |
| 58 | + |
| 59 | + # TODO unit tests: |
| 60 | + # test the various options to iread() |
| 61 | + # test writing |
| 62 | + # test_isimage - make sure Image rejects/fails with invalid input |
| 63 | + # test_imtypes - test Image works on different Image types? |
| 64 | + # test_getimage - make sure Image returns the same array but with valid |
| 65 | + # typing? |
| 66 | + # test_imwrite - test write/successfully save file? |
| 67 | + |
| 68 | + def tearDown(self): |
| 69 | + # Cleanup code if needed |
| 70 | + pass |
| 71 | + |
| 72 | + |
| 73 | +# ------------------------------------------------------------------------ # |
| 74 | +if __name__ == "__main__": |
| 75 | + |
| 76 | + unittest.main() |
0 commit comments