2022-07-20 01:01:56 +02:00
|
|
|
# Standard Modules
|
|
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
|
2021-11-27 13:44:29 +01:00
|
|
|
# External Packages
|
|
|
|
import pytest
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# Internal Packages
|
2022-08-06 02:05:35 +02:00
|
|
|
from src.utils.state import model
|
|
|
|
from src.utils.constants import web_directory
|
2021-10-03 05:28:33 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-09-14 12:22:27 +02:00
|
|
|
def test_image_metadata(content_config: ContentConfig):
|
|
|
|
"Verify XMP Description and Subjects Extracted from Image"
|
|
|
|
# Arrange
|
|
|
|
expected_metadata_image_name_pairs = [
|
|
|
|
(["Billi Ka Bacha.", "Cat", "Grass"], "kitten_park.jpg"),
|
|
|
|
(["Pasture.", "Horse", "Dog"], "horse_dog.jpg"),
|
|
|
|
(["Guinea Pig Eating Celery.", "Rodent", "Whiskers"], "guineapig_grass.jpg")]
|
|
|
|
|
|
|
|
test_image_paths = [
|
|
|
|
Path(content_config.image.input_directories[0] / image_name[1])
|
|
|
|
for image_name in expected_metadata_image_name_pairs
|
|
|
|
]
|
|
|
|
|
|
|
|
for expected_metadata, test_image_path in zip(expected_metadata_image_name_pairs, test_image_paths):
|
|
|
|
# Act
|
|
|
|
actual_metadata = image_search.extract_metadata(test_image_path)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
for expected_snippet in expected_metadata[0]:
|
|
|
|
assert expected_snippet in actual_metadata
|
|
|
|
|
|
|
|
|
2021-10-03 05:28:33 +02:00
|
|
|
# ----------------------------------------------------------------------------------------------------
|
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-07-20 01:01:56 +02:00
|
|
|
output_directory = resolve_absolute_path(web_directory)
|
2022-01-15 02:13:14 +01:00
|
|
|
model.image_search = image_search.setup(content_config.image, search_config.image, regenerate=False)
|
2022-07-20 01:01:56 +02:00
|
|
|
query_expected_image_pairs = [("kitten", "kitten_park.jpg"),
|
2021-10-03 05:28:33 +02: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-20 01:01:56 +02:00
|
|
|
output_directory=output_directory,
|
2022-07-28 18:45:12 +02:00
|
|
|
image_files_url='/static/images',
|
2021-10-03 05:28:33 +02:00
|
|
|
count=1)
|
|
|
|
|
2022-09-15 22:34:43 +02:00
|
|
|
actual_image_path = output_directory.joinpath(Path(results[0].entry).name)
|
2022-09-07 02:07:30 +02:00
|
|
|
actual_image = Image.open(actual_image_path)
|
2022-07-20 01:01:56 +02:00
|
|
|
expected_image = Image.open(content_config.image.input_directories[0].joinpath(expected_image_name))
|
2021-10-03 05:28:33 +02:00
|
|
|
|
|
|
|
# Assert
|
|
|
|
assert expected_image == actual_image
|
2022-09-07 02:07:30 +02:00
|
|
|
|
|
|
|
# Cleanup
|
|
|
|
# Delete the image files copied to results directory
|
|
|
|
actual_image_path.unlink()
|