2021-11-27 13:44:29 +01:00
|
|
|
# External Packages
|
|
|
|
import pytest
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# Internal Packages
|
|
|
|
from src.main import model
|
|
|
|
from src.search_type import image_search
|
|
|
|
from src.utils.helpers import resolve_absolute_path
|
2022-01-15 02:54:38 +01:00
|
|
|
from src.utils.rawconfig import ContentConfig, SearchConfig
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Test
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_image_search_setup(content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 05:28:33 +02:00
|
|
|
# Act
|
|
|
|
# Regenerate image search embeddings during image setup
|
2022-01-15 02:13:14 +01:00
|
|
|
image_search_model = image_search.setup(content_config.image, search_config.image, regenerate=True)
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert len(image_search_model.image_names) == 3
|
|
|
|
assert len(image_search_model.image_embeddings) == 3
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------------------------------------------------------------------------------------
|
2021-11-27 13:44:29 +01:00
|
|
|
@pytest.mark.skip(reason="results inconsistent currently")
|
2022-01-15 02:54:38 +01:00
|
|
|
def test_image_search(content_config: ContentConfig, search_config: SearchConfig):
|
2021-10-03 05:28:33 +02:00
|
|
|
# Arrange
|
2022-01-15 02:13:14 +01:00
|
|
|
model.image_search = image_search.setup(content_config.image, search_config.image, regenerate=False)
|
2021-10-03 05:28:33 +02:00
|
|
|
query_expected_image_pairs = [("brown kitten next to plant", "kitten_park.jpg"),
|
|
|
|
("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-01-15 02:13:14 +01:00
|
|
|
content_config.image.input_directory,
|
2021-10-03 05:28:33 +02:00
|
|
|
count=1)
|
|
|
|
|
|
|
|
actual_image = results[0]["Entry"]
|
2022-01-15 02:13:14 +01:00
|
|
|
expected_image = resolve_absolute_path(content_config.image.input_directory.joinpath(expected_image_name))
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert expected_image == actual_image
|