import unittest import numpy as np from cvdatasets.dataset.part.base import BasePart class PartCropTest(unittest.TestCase): def setUp(self): self.im = np.random.randn(300, 300, 3) def tearDown(self): pass def _check_crop(self, cropped_im, _should): self.assertIsNotNone(cropped_im, "method crop should return something!") self.assertIsInstance(cropped_im, type(self.im), "result should have the same type as the input image") crop_h, crop_w, _ = cropped_im.shape h, w, _ = _should.shape self.assertEqual(crop_h, h, "incorrect crop height") self.assertEqual(crop_w, w, "incorrect crop width") self.assertTrue((cropped_im == _should).all(), "crop was incorret") def test_bbox_part_crop(self): _id, x, y, w, h = annotation = (0, 20, 20, 100, 100) bbox = BasePart.new(self.im, annotation) cropped_im = bbox.crop(self.im) _should = self.im[y:y+h, x:x+w] self._check_crop(cropped_im, _should) def test_location_part_crop(self): _id, center_x, center_y, _vis = annotation = (0, 50, 50, 1) bbox = BasePart.new(self.im, annotation) h, w, c = self.im.shape for ratio in np.linspace(0.1, 0.3, num=9): _h, _w = int(h * ratio), int(w * ratio) cropped_im = bbox.crop(self.im, ratio=ratio) x, y = center_x - _h // 2, center_y - _w // 2 _should = self.im[y : y + _h, x : x + _w] self._check_crop(cropped_im, _should)