Image Search Demo
Getting Started
- Clone the examples repository.
-
Run Marqo:
For mode detailed instructions, check the getting started guide.docker rm -f marqo docker pull marqoai/marqo:latest docker run --name marqo -it --privileged -p 8882:8882 --add-host host.docker.internal:host-gateway marqoai/marqo:latest
-
Follow the instructions below or see the original code and article here.
Code
Example
import marqo
mq = marqo.Client("http://localhost:8882")
mq.get_marqo()
# {'message': 'Welcome to Marqo', 'version': 'X.X.X'}
####################################################
### STEP 1: Download Data
####################################################
# Download the data from [here](https://github.com/marqo-ai/marqo/tree/mainline/examples/ImageSearchGuide/data)
# store it in a data/ directory
#####################################################
### STEP 2. Start Marqo
#####################################################
# Follow the instructions here https://github.com/marqo-ai/marqo or see below
"""
docker rm -f marqo
docker pull marqoai/marqo:latest
docker run --name marqo -it --privileged -p 8882:8882 --add-host host.docker.internal:host-gateway marqoai/marqo:latest
"""
####################################################
### STEP 3: Index Data
####################################################
index_name = 'image-search-guide'
settings = {
"model": "ViT-L/14",
"treat_urls_and_pointers_as_images": True,
}
mq.create_index(index_name, **settings)
####################################################
### STEP 4: Access Local Images
####################################################
import subprocess
local_dir = "./data"
pid = subprocess.Popen(['python3', '-m', 'http.server', '8222', '--directory', local_dir], stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT)
import glob
import os
# Find all the local images
locators = glob.glob(local_dir + '*.jpg')
# Generate docker path for local images
docker_path = "http://host.docker.internal:8222/"
image_docker = [docker_path + os.path.basename(f) for f in locators]
print(image_docker)
"""
output:
['http://host.docker.internal:8222/image4.jpg',
'http://host.docker.internal:8222/image1.jpg',
'http://host.docker.internal:8222/image3.jpg',
'http://host.docker.internal:8222/image0.jpg',
'http://host.docker.internal:8222/image2.jpg']
"""
####################################################
### STEP 5: Add Images to the Index
####################################################
documents = [{"image_docker" : image, "_id" : idx} for idx, image in enumerate(image_docker)]
print(documents)
mq.index(index_name).add_documents(
documents, device="cpu", client_batch_size= 1,
tensor_fields=["image_docker"]
)
####################################################
### STEP 6: Search using Marqo
####################################################
search_results = mq.index(index_name).search("A rider on a horse jumping over the barrier",
searchable_attributes=['image_docker'], limit = 1,
device='cpu')
print(search_results)
####################################################
### STEP 7: Visualize the Output
####################################################
import requests
from PIL import Image
fig_path = search_results["hits"][0]["image_docker"].replace(docker_path, local_dir)
display(Image.open(fig_path))