Image Search with Text Prompt
Community Article
Published
October 16, 2024
by Tony Assi
Use text prompt to search for images in folders. It can search through folders within folders. Built with 🤗 Transformers and 🤗 Datasets.
We'll use my ImageSearch module on Github.
Download
Download code from Github
git clone https://github.com/TonyAssi/ImageSearch.git
cd ImageSearch
Installation
Install the required libraries:
pip install -r requirements.txt
Usage
Import the module
from ImageSearch import ImageSearch
Define folder path
- image_dir Parent folder
- hf_key HuggingFace write access token can be created here.
image_search = ImageSearch(image_dir='images',
hf_key='HF_KEY')
The first time this is called the images will be converted to a 🤗 Dataset and it'll get uploaded to the 🤗 Hub. It'll print out the dataset id and store it in a meta.text file. It should look like tonyassi/images-ds. It'll take a little longer the first time it's called. After the dataset is created it should be very quick.
Search for image with text prompt
- text Text prompt
- download_path Images most similar to text prompt will be downloaded to this path (if download_path='' then images will not be downloaded)
- num Number of images (optional) defaults to 5
image_search.search(text='red rose',
download_path = 'found_images',
num=5)
The most similar images will be printed and downloaded.
flowers/0092.png
Score: 142.48643
Index: 0
flowers/0105.png
Score: 146.4621
Index: 1
flowers/0095.png
Score: 148.10144
Index: 2