Home

5. 🖼️ Image-to-image search

Image to Image search: Given an image as an input, you can search for similar images to the provided input. 🔍🖼️

Example: Given an image of a "rainy day", let's search for similar images.

To accomplish this, let's first take advantage of text search:

  1. We'll query the dataset for the word "rainy day" using a text-based search. 🔤
  2. The first two results of the above query will be used as the input to retrieve similar images. 📄➡️🖼️

5.1 Using text search to find "rainy day" images

search_result_rainy_day = dataset.search_images(  
    sort_by="vector_text(rainy day)",  
)
display_images(search_result_rainy_day, n_images_to_show=4, n_cols=2)

5.2 Searching for similar "rainy days" images

For Image to Image search, we are expected to provide the image key of the inputs. Let's find them.

img_keys = \[]  
for img in search_result_rainy_day[0:4]\:  
  print(img.key)  
  img_keys.append(img.key)

""" output
n014-2018-06-04-15-20-47-0400__CAM_FRONT_RIGHT__1528140160620182_jpg.rf.8c7efc3d47dd3c1e5e3c0f99abdd66f1
n014-2018-06-04-15-20-47-0400__CAM_BACK__1528140481187420_jpg.rf.a90226b79300811ed78c6c33bf3406bf
n003-2018-01-02-14-36-45-0800__CAM_FRONT_LEFT__1514875528239124_jpg.rf.5a38f43dc6e9e03e4ffc697fb9c480de
n008-2018-06-05-15-07-28-0400__CAM_FRONT_LEFT__1528225931754917_jpg.rf.248e9ab2aa56dd7958c2c2690a9013dc

"""

From the four images displayed above, we use the bottom right image as our input

img_to_img_search_result = dataset.search_images(  
    sort_by=f"vector_image(n008-2018-06-05-15-07-28-0400**CAM_FRONT_LEFT**1528225931754917_jpg.rf.248e9ab2aa56dd7958c2c2690a9013dc)",  
)
display_images(img_to_img_search_result, n_images_to_show=9)

Voilà! 🎉✨