Yurij Mikhalevich
August 25, 2021

rclip: An AI-Powered Command-Line Photo Search Tool

I have 72,769 photos stored on my NAS and organized into subfolders named according to the pattern “YEAR.MM.DD - Event description.” This organizational approach is software-agnostic and creates a structure that is easy to navigate. I have followed it for more than a dozen years. However, there are situations when I want to find a particular photo. Looking for it even within a specific subfolder can take a lot of time, especially if that subfolder is packed with many pictures. Another challenging scenario is finding a picture that will fit a particular theme without having a specific one in mind. In both of these scenarios, having content-based search available is helpful. I looked around and didn’t find a solution that satisfied me. What to do when you find yourself in a situation like this? Build your own solution!

Techniques allowing you to search images by their content are not necessarily new, but they usually only allow you to search using a set of predefined keywords and, sometimes, their synonyms. OpenAI’s CLIP takes computer image understanding to a whole new level, allowing us to search for photos using any text query we can think of. This is why I used it to build my tool — rclip. In essence, rclip is a simple interface to the CLIP model. All I had to do was load the model, extract feature vectors from all of the images in the directory, store them somewhere, extract the feature vector from the query, compute similarities between the query feature vector and the image feature vectors, and output the most similar images. The vector extraction is done only once for each image, on the first run, and then the vectors are stored in the SQLite DB. SQLite is not the most performant way to manage that kind of data, but it’s a quite convenient one. And, given that the time needed to query the DB on my 73 thousand photos catalog contributes only up to 12% of the whole execution time, I have decided to keep it for the time being.

I use rgrep a lot. rgrep is a simple alias to the grep command-line tool that searches recursively for a substring match in all of the files in the current directory. In its minimal definition (without a bunch of unwanted directories excluded), it looks like this: alias rgrep="grep -rni". To use it, open your terminal, navigate to the directory containing the files that you want to grep, type “rgrep” “search query,” and hit “Enter.”

rclip implements exactly the same interface, but searches through the images’ content. The name stands for “recursive CLIP.” Its usage looks like this:

cd images && rclip "search query"

rclip usage demo

For the detailed demonstration, watch the video below:

That’s it! If you want to try rclip yourself, follow the installation instructions described here.

ABOUT YURIJ MIKHALEVICH
Builds the future at Lightning AI, creator of the Move Fast and Break Things community of software engineers, DeepLearning.AI mentor, creator of rclip, writes about tech, software engineering, books, what to watch, and beyond, practices creative writing and captures moments through photography
This post is tagged with software engineering. Grab the software engineering RSS feed or the main RSS feed to get future posts.