diff --git a/mask_imposer/controller.py b/mask_imposer/controller.py index eb9e38e..3e93270 100644 --- a/mask_imposer/controller.py +++ b/mask_imposer/controller.py @@ -43,7 +43,9 @@ def __init__(self, bundled_mask_set_idx: int = 1) -> None: logger=self._logger ) - def impose_mask(self, image: Union[str, List[str]], show: bool = False) -> List[NDArray[Any]]: + def impose_mask( + self, image: Union[str, List[str]], show: bool = False + ) -> Union[NDArray[Any], List[NDArray[Any]]]: """Imposes mask on image. :param image: List of paths to images or single image path @@ -74,7 +76,6 @@ def save(cls, img: NDArray[Any], filepath: str) -> None: """Saves image in given path using opencv.""" cv2.imwrite(filepath, img) - # if __name__ == '__main__': # np_arr = cv2.imread( # "/home/popis/Documents/mask-imposer/tests/integration/data/input/sample.jpeg" diff --git a/mask_imposer/detector/image.py b/mask_imposer/detector/image.py index 1664a6e..2bbd1ef 100644 --- a/mask_imposer/detector/image.py +++ b/mask_imposer/detector/image.py @@ -20,7 +20,6 @@ def __init__(self, file: Union[str, Tuple[str, NDArray[Any]]]) -> None: self.img, self.__name = set_img(file) self._gray_img: Optional[cvtColor] = None self._rect: Optional[rectangle] = None - print(file) if self.img.shape[-1] == 3: self.img = self.converted_rgba() diff --git a/mask_imposer/test.png b/mask_imposer/test.png new file mode 100644 index 0000000..c718ec2 Binary files /dev/null and b/mask_imposer/test.png differ diff --git a/tests/integration/__init__.py b/tests/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/data/expected/sample.png b/tests/integration/data/expected/sample.png new file mode 100644 index 0000000..c718ec2 Binary files /dev/null and b/tests/integration/data/expected/sample.png differ diff --git a/tests/integration/data/expected/sample2.png b/tests/integration/data/expected/sample2.png new file mode 100644 index 0000000..6e87e0c Binary files /dev/null and b/tests/integration/data/expected/sample2.png differ diff --git a/tests/integration/data/expected/sample3.png b/tests/integration/data/expected/sample3.png new file mode 100644 index 0000000..859c4c8 Binary files /dev/null and b/tests/integration/data/expected/sample3.png differ diff --git a/tests/integration/data/input/sample.jpeg b/tests/integration/data/input/sample.jpeg new file mode 100644 index 0000000..36f077f Binary files /dev/null and b/tests/integration/data/input/sample.jpeg differ diff --git a/tests/integration/data/input/sample2.jpeg b/tests/integration/data/input/sample2.jpeg new file mode 100644 index 0000000..fffa337 Binary files /dev/null and b/tests/integration/data/input/sample2.jpeg differ diff --git a/tests/integration/data/input/sample3.jpeg b/tests/integration/data/input/sample3.jpeg new file mode 100644 index 0000000..9d19044 Binary files /dev/null and b/tests/integration/data/input/sample3.jpeg differ diff --git a/tests/integration/test_command_line/__init__.py b/tests/integration/test_command_line/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/test_command_line/test_command_line_usage.py b/tests/integration/test_command_line/test_command_line_usage.py new file mode 100644 index 0000000..674876e --- /dev/null +++ b/tests/integration/test_command_line/test_command_line_usage.py @@ -0,0 +1,54 @@ +import os +import shutil +import sys +from pathlib import Path +from typing import List +from unittest import TestCase +from unittest.mock import patch + +import cv2 + +import run + + +def sorted_by_fn(directory) -> List[str]: + return sorted( + [os.path.join(directory, f) for f in os.listdir(directory)], + key=lambda x: Path(x).name + ) + + +class CommandLineUsageTestCase(TestCase): + """Requires installed mask-imposer as package and shape predictor model.""" + + def setUp(self) -> None: + self.test_dir = Path(os.path.abspath(__file__)).parent + self.expected_dir = os.path.join(f"{self.test_dir}", "..", "data", "expected") + + def test_should_impose_masks_on_all_images_from_directory_by_cmd_call(self) -> None: + results_dir = os.path.join(f"{self.test_dir}", "actual") + + testargs = [ + "", + os.path.join(f"{self.test_dir}", "..", "data", "input"), + "--output-dir", + results_dir + ] + + with patch.object(sys, 'argv', testargs): + run.main() + + actual_paths = sorted_by_fn(results_dir) + expected_paths = sorted_by_fn(self.expected_dir) + + for ap, ep in zip(actual_paths, expected_paths): + cv2.imshow("Actual", cv2.imread(ap)) + cv2.imshow("Expected", cv2.imread(ep)) + cv2.waitKey(0) + + # assert all( + # compareHist(cv2.imread(h1), cv2.imread(h2), cv2.HISTCMP_CORREL) for h1, h2 in + # zip(actual_paths, expected_paths) + # ) + + shutil.rmtree(results_dir) diff --git a/tests/integration/test_package/__init__.py b/tests/integration/test_package/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/integration/test_package/test_python_package.py b/tests/integration/test_package/test_python_package.py new file mode 100644 index 0000000..bfaacc9 --- /dev/null +++ b/tests/integration/test_package/test_python_package.py @@ -0,0 +1,58 @@ +import os +from pathlib import Path +from typing import List +from unittest import TestCase + +import cv2 + +import mask_imposer + + +def sorted_by_fn(directory) -> List[str]: + return sorted( + [os.path.join(directory, f) for f in os.listdir(directory)], + key=lambda x: Path(x).name + ) + + +class PythonPackageTestCase(TestCase): + """Requires installed mask-imposer as package and shape predictor model.""" + + def setUp(self) -> None: + self.test_dir = Path(os.path.abspath(__file__)).parent + self.input_dir = os.path.join(f"{self.test_dir}", "..", "data", "input") + self.test_img_path = os.path.join(self.input_dir, "sample2.jpeg") + self.mask_path = os.path.join( + self.test_dir.parent.parent.parent, + "mask_imposer", + "bundled", + "set_01", + "mask_image.png" + ) + self.imp = mask_imposer.MaskImposer() + + def test_should_impose_mask_on_image_from_path(self) -> None: + # mask_img = cv2.cvtColor(cv2.imread(self.mask_path), cv2.COLOR_BGR2GRAY) + # masked_img = cv2.cvtColor( + # self.imp.impose_mask(self.test_img_path, show=False), cv2.COLOR_BGR2GRAY + # ) + self.imp.impose_mask(self.test_img_path, show=True) + + # heat_map = cv2.matchTemplate(masked_img, mask_img, cv2.TM_CCOEFF_NORMED) + # heat_map = cv2.matchTemplate(masked_img, mask_img, cv2.TM_CCOEFF_NORMED) + + # h, w = masked_img.shape + # print(masked_img.shape) + # print(heat_map.shape) + # y, x = np.unravel_index(np.argmax(heat_map), heat_map.shape) + # cv2.rectangle(masked_img, (x, y), (x + w, y + h), (0, 0, 255), 5) + # cv2.imshow("Match", masked_img) + # cv2.waitKey(0) + + def test_should_impose_mask_on_image_from_numpy_array(self) -> None: + self.imp.impose_mask((cv2.imread(self.test_img_path), "fake_mask"), show=True) + + def test_should_imposing_results_be_same_for_path_and_numpy_array_way(self): + m1 = self.imp.impose_mask((cv2.imread(self.test_img_path), "fake_mask"), show=False) + m2 = self.imp.impose_mask(self.test_img_path, show=False) + assert (m1 == m2).all() diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_mask_image_read.py b/tests/unit/test_mask_image_read.py similarity index 100% rename from tests/test_mask_image_read.py rename to tests/unit/test_mask_image_read.py diff --git a/tests/test_predictor_download.py b/tests/unit/test_predictor_download.py similarity index 90% rename from tests/test_predictor_download.py rename to tests/unit/test_predictor_download.py index b442582..7dcb44a 100644 --- a/tests/test_predictor_download.py +++ b/tests/unit/test_predictor_download.py @@ -17,6 +17,6 @@ class PredictorDownloadTestCase(TestCase): @patch("mask_imposer.detector.download.input", lambda _: "y") def test_should_download_predictor(self) -> None: download_predictor(getLogger("test"), predictor_name="SPZ.bz2") - self.assertTrue("SPZ.dat" in get_directory_files(".")) + self.assertTrue("SPZ.dat" in get_directory_files("..")) os.remove("SPZ.dat") os.remove("SPZ.bz2")