Skip to content

Commit

Permalink
Merge pull request #21 from sqoshi/tests
Browse files Browse the repository at this point in the history
Manual tests
  • Loading branch information
sqoshi authored and popis committed Oct 18, 2021
2 parents a226f9d + 8e86f44 commit 76e6fe7
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 4 deletions.
5 changes: 3 additions & 2 deletions mask_imposer/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion mask_imposer/detector/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Binary file added mask_imposer/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file added tests/integration/__init__.py
Empty file.
Binary file added tests/integration/data/expected/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/integration/data/expected/sample2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/integration/data/expected/sample3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/integration/data/input/sample.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/integration/data/input/sample2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/integration/data/input/sample3.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
54 changes: 54 additions & 0 deletions tests/integration/test_command_line/test_command_line_usage.py
Original file line number Diff line number Diff line change
@@ -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)
Empty file.
58 changes: 58 additions & 0 deletions tests/integration/test_package/test_python_package.py
Original file line number Diff line number Diff line change
@@ -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()
Empty file added tests/unit/__init__.py
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 76e6fe7

Please sign in to comment.