test_parts.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import unittest
  2. import numpy as np
  3. from cvdatasets.dataset.part.base import BasePart
  4. class PartCropTest(unittest.TestCase):
  5. def setUp(self):
  6. self.im = np.random.randn(300, 300, 3)
  7. def tearDown(self):
  8. pass
  9. def _check_crop(self, cropped_im, _should):
  10. self.assertIsNotNone(cropped_im,
  11. "method crop should return something!")
  12. self.assertIsInstance(cropped_im, type(self.im),
  13. "result should have the same type as the input image")
  14. crop_h, crop_w, _ = cropped_im.shape
  15. h, w, _ = _should.shape
  16. self.assertEqual(crop_h, h, "incorrect crop height")
  17. self.assertEqual(crop_w, w, "incorrect crop width")
  18. self.assertTrue((cropped_im == _should).all(),
  19. "crop was incorret")
  20. def test_bbox_part_crop(self):
  21. _id, x, y, w, h = annotation = (0, 20, 20, 100, 100)
  22. bbox = BasePart.new(self.im, annotation)
  23. cropped_im = bbox.crop(self.im)
  24. _should = self.im[y:y+h, x:x+w]
  25. self._check_crop(cropped_im, _should)
  26. def test_location_part_crop(self):
  27. _id, center_x, center_y, _vis = annotation = (0, 50, 50, 1)
  28. bbox = BasePart.new(self.im, annotation)
  29. h, w, c = self.im.shape
  30. for ratio in np.linspace(0.1, 0.3, num=9):
  31. _h, _w = int(h * ratio), int(w * ratio)
  32. cropped_im = bbox.crop(self.im, ratio=ratio)
  33. x, y = center_x - _h // 2, center_y - _w // 2
  34. _should = self.im[y : y + _h, x : x + _w]
  35. self._check_crop(cropped_im, _should)