2022-07-19 23:01:56 +00:00
|
|
|
# Standard Modules
|
|
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
|
2021-11-27 12:44:29 +00:00
|
|
|
# External Packages
|
|
|
|
import pytest
|
|
|
|
|
2021-10-03 03:28:33 +00:00
|
|
|
# Internal Packages
|
2022-08-06 00:05:35 +00:00
|
|
|
from src.utils.state import model
|
|
|
|
from src.utils.constants import web_directory
|
2021-10-03 03:28:33 +00:00
|
|
|
from src.search_type import image_search
|
|
|
|
from src.utils.helpers import resolve_absolute_path
|
2022-01-15 01:54:38 +00:00
|
|
|
from src.utils.rawconfig import ContentConfig, SearchConfig
|
2021-10-03 03:28:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
# Test
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-08-05 23:47:52 +00:00
|
|
|
@pytest.mark.skip(reason="upstream issues in loading image search model. disabled for now")
|
2022-01-15 01:54:38 +00:00
|
|
|
def test_image_search_setup(content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 03:28:33 +00:00
|
|
|
# Act
|
|
|
|
# Regenerate image search embeddings during image setup
|
2022-01-15 01:13:14 +00:00
|
|
|
image_search_model = image_search.setup(content_config.image, search_config.image, regenerate=True)
|
2021-10-03 03:28:33 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(image_search_model.image_names) == 3
|
|
|
|
assert len(image_search_model.image_embeddings) == 3
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-11-27 12:44:29 +00:00
|
|
|
@pytest.mark.skip(reason="results inconsistent currently")
|
2022-01-15 01:54:38 +00:00
|
|
|
def test_image_search(content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 03:28:33 +00:00
|
|
|
# Arrange
|
2022-07-19 23:01:56 +00:00
|
|
|
output_directory = resolve_absolute_path(web_directory)
|
2022-01-15 01:13:14 +00:00
|
|
|
model.image_search = image_search.setup(content_config.image, search_config.image, regenerate=False)
|
2022-07-19 23:01:56 +00:00
|
|
|
query_expected_image_pairs = [("kitten", "kitten_park.jpg"),
|
2021-10-03 03:28:33 +00:00
|
|
|
("horse and dog in a farm", "horse_dog.jpg"),
|
|
|
|
("A guinea pig eating grass", "guineapig_grass.jpg")]
|
|
|
|
|
|
|
|
# Act
|
|
|
|
for query, expected_image_name in query_expected_image_pairs:
|
|
|
|
hits = image_search.query(
|
|
|
|
query,
|
|
|
|
count = 1,
|
|
|
|
model = model.image_search)
|
|
|
|
|
|
|
|
results = image_search.collate_results(
|
|
|
|
hits,
|
|
|
|
model.image_search.image_names,
|
2022-07-19 23:01:56 +00:00
|
|
|
output_directory=output_directory,
|
2022-07-28 16:45:12 +00:00
|
|
|
image_files_url='/static/images',
|
2021-10-03 03:28:33 +00:00
|
|
|
count=1)
|
|
|
|
|
2022-07-19 23:01:56 +00:00
|
|
|
actual_image = Image.open(output_directory.joinpath(Path(results[0]["entry"]).name))
|
|
|
|
expected_image = Image.open(content_config.image.input_directories[0].joinpath(expected_image_name))
|
2021-10-03 03:28:33 +00:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert expected_image == actual_image
|