Search by image at path only if query of form "file:/path/to/image"

Previously no query syntax helpers, like the "file:" prefix, were used
before checking if query contains file path.

This made query to image search brittle to misinterpretation and
pointless checking

Add test to verify search by image at file works as expected
This commit is contained in:
Debanjum Singh Solanky
2023-01-21 14:06:56 -03:00
parent 655ef11653
commit 3d9ed91e42
2 changed files with 44 additions and 4 deletions

View File

@@ -136,12 +136,12 @@ def extract_metadata(image_name):
def query(raw_query, count, model: ImageSearchModel):
# Set query to image content if query is a filepath
if pathlib.Path(raw_query).is_file():
query_imagepath = resolve_absolute_path(pathlib.Path(raw_query), strict=True)
# Set query to image content if query is of form file:/path/to/file.png
if raw_query.startswith("file:") and pathlib.Path(raw_query[5:]).is_file():
query_imagepath = resolve_absolute_path(pathlib.Path(raw_query[5:]), strict=True)
query = copy.deepcopy(Image.open(query_imagepath))
query.thumbnail((640, query.height)) # scale down image for faster processing
logger.info(f"Find Images similar to Image at {query_imagepath}")
logger.info(f"Find Images by Image: {query_imagepath}")
else:
query = raw_query
logger.info(f"Find Images by Text: {query}")

View File

@@ -1,4 +1,5 @@
# Standard Modules
import logging
from pathlib import Path
from PIL import Image
@@ -77,3 +78,42 @@ def test_image_search(content_config: ContentConfig, search_config: SearchConfig
# Cleanup
# Delete the image files copied to results directory
actual_image_path.unlink()
# ----------------------------------------------------------------------------------------------------
def test_image_search_by_filepath(content_config: ContentConfig, search_config: SearchConfig, caplog):
# Arrange
output_directory = resolve_absolute_path(web_directory)
model.image_search = image_search.setup(content_config.image, search_config.image, regenerate=False)
image_directory = content_config.image.input_directories[0]
query = f"file:{image_directory.joinpath('kitten_park.jpg')}"
expected_image_path = f"{image_directory.joinpath('kitten_park.jpg')}"
# Act
with caplog.at_level(logging.INFO, logger="src.search_type.image_search"):
hits = image_search.query(
query,
count = 1,
model = model.image_search)
results = image_search.collate_results(
hits,
model.image_search.image_names,
output_directory=output_directory,
image_files_url='/static/images',
count=1)
actual_image_path = output_directory.joinpath(Path(results[0].entry).name)
actual_image = Image.open(actual_image_path)
expected_image = Image.open(expected_image_path)
# Assert
# Ensure file search triggered instead of query with file path as string
assert f"Find Images by Image: {resolve_absolute_path(expected_image_path)}" in caplog.text, "File search not triggered"
# Ensure the correct image is returned
assert expected_image == actual_image, "Incorrect image returned by file search"
# Cleanup
# Delete the image files copied to results directory
actual_image_path.unlink()