├── demo.png ├── images ├── images.jpeg ├── thumb.jpeg ├── download.jpeg ├── download (1).jpeg ├── download (2).jpeg ├── download (3).jpeg ├── download (4).jpeg ├── download (5).jpeg ├── download (6).jpeg ├── ia_100001914.jpg ├── maxresdefault.jpg ├── ia_100000000322.jpg ├── 1200px-Cordage_en_chanvre.jpg ├── climbing-4343935_960_720.jpg ├── 27281_c0-41-990-701_r1050x700.jpg ├── GettyImages-92032688-2-628x305.jpg ├── Face_Masks_Street_1296x728-header-1.jpg ├── Climbing-Western-Ghats-on-Brompton-33-1024x922.jpg ├── silhouettes-two-climbers-reaching-summit-260nw-1438889969.jpg ├── 2019_0520-plastic-pollution-aspect-ratio-2000-1200-1024x614.jpg ├── web3-happy-people-outside-smile-sun-nature-eduardo-dutra-620857-unsplash.jpg ├── young-people-are-climbing-to-the-highest-hills-dressed-in-bright-clothes-HXGK6E.jpg └── https___specials-images.forbesimg.com_imageserve_1214079235_0x0.jpg_cropX1=0&cropX2=5140&cropY1=261&cropY2=3152 ├── script ├── load_model.py └── install.sh ├── LICENSE ├── README.md └── search.py /demo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/demo.png -------------------------------------------------------------------------------- /images/images.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/images.jpeg -------------------------------------------------------------------------------- /images/thumb.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/thumb.jpeg -------------------------------------------------------------------------------- /images/download.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/download.jpeg -------------------------------------------------------------------------------- /images/download (1).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/download (1).jpeg -------------------------------------------------------------------------------- /images/download (2).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/download (2).jpeg -------------------------------------------------------------------------------- /images/download (3).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/download (3).jpeg -------------------------------------------------------------------------------- /images/download (4).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/download (4).jpeg -------------------------------------------------------------------------------- /images/download (5).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/download (5).jpeg -------------------------------------------------------------------------------- /images/download (6).jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/download (6).jpeg -------------------------------------------------------------------------------- /images/ia_100001914.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/ia_100001914.jpg -------------------------------------------------------------------------------- /images/maxresdefault.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/maxresdefault.jpg -------------------------------------------------------------------------------- /images/ia_100000000322.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/ia_100000000322.jpg -------------------------------------------------------------------------------- /images/1200px-Cordage_en_chanvre.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/1200px-Cordage_en_chanvre.jpg -------------------------------------------------------------------------------- /images/climbing-4343935_960_720.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/climbing-4343935_960_720.jpg -------------------------------------------------------------------------------- /images/27281_c0-41-990-701_r1050x700.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/27281_c0-41-990-701_r1050x700.jpg -------------------------------------------------------------------------------- /images/GettyImages-92032688-2-628x305.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/GettyImages-92032688-2-628x305.jpg -------------------------------------------------------------------------------- /images/Face_Masks_Street_1296x728-header-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/Face_Masks_Street_1296x728-header-1.jpg -------------------------------------------------------------------------------- /images/Climbing-Western-Ghats-on-Brompton-33-1024x922.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/Climbing-Western-Ghats-on-Brompton-33-1024x922.jpg -------------------------------------------------------------------------------- /images/silhouettes-two-climbers-reaching-summit-260nw-1438889969.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/silhouettes-two-climbers-reaching-summit-260nw-1438889969.jpg -------------------------------------------------------------------------------- /images/2019_0520-plastic-pollution-aspect-ratio-2000-1200-1024x614.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/2019_0520-plastic-pollution-aspect-ratio-2000-1200-1024x614.jpg -------------------------------------------------------------------------------- /script/load_model.py: -------------------------------------------------------------------------------- 1 | import clip 2 | import torch 3 | 4 | device = "cuda" if torch.cuda.is_available() else "cpu" 5 | model, preprocess = clip.load("ViT-B/32", device=device) 6 | 7 | print("model loaded") -------------------------------------------------------------------------------- /images/web3-happy-people-outside-smile-sun-nature-eduardo-dutra-620857-unsplash.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/web3-happy-people-outside-smile-sun-nature-eduardo-dutra-620857-unsplash.jpg -------------------------------------------------------------------------------- /images/young-people-are-climbing-to-the-highest-hills-dressed-in-bright-clothes-HXGK6E.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/young-people-are-climbing-to-the-highest-hills-dressed-in-bright-clothes-HXGK6E.jpg -------------------------------------------------------------------------------- /images/https___specials-images.forbesimg.com_imageserve_1214079235_0x0.jpg_cropX1=0&cropX2=5140&cropY1=261&cropY2=3152: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/samiptimalsena/Image-Search/master/images/https___specials-images.forbesimg.com_imageserve_1214079235_0x0.jpg_cropX1=0&cropX2=5140&cropY1=261&cropY2=3152 -------------------------------------------------------------------------------- /script/install.sh: -------------------------------------------------------------------------------- 1 | #creating and activating conda env 2 | 3 | conda create -n image_search 4 | conda activate image_search 5 | 6 | #installing pytorch and CLIP 7 | conda install --yes -c pytorch pytorch=1.7.1 torchvision cudatoolkit=11.0 8 | pip3 install ftfy regex tqdm 9 | pip3 install opencv-python 10 | pip3 install git+https://github.com/openai/CLIP.git 11 | 12 | #downloading one of the available clip model 13 | python3 load_model.py -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Samip Timalsena 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Image-Search 2 | 3 | A python script to search an image in a folder by describing it in natural language. It uses [CLIP](https://github.com/openai/CLIP) to find the similar images as described in the search query. 4 | 5 | ## Installation 6 | 7 | Clone the repository and change your working directory to project's root directory. 8 | 9 | **_Note_:** 10 | * _prior installation of [conda](https://www.anaconda.com/products/individual) is required_ 11 | * _Replace cudatoolkit=11.0 in the install.sh with the appropriate CUDA version on your machine or cpuonly when installing on a machine without a GPU._ 12 | ``` 13 | cd script 14 | ./install.sh 15 | python search.py 16 | ``` 17 | 18 | Since the default query is 'Green hills', we now see three images containing hills with image name printed in terminal. You can pass your desired query to search for images in your custom directory. 19 | 20 | ## Arguments 21 | 22 | |Arguments|Default|Description| 23 | |---------|-------|-----------| 24 | |-p, --path|images/|Path to Image folder| 25 | |-q, --query|'Green hills'|Search Query| 26 | |-c, --count|3|Number of images to return| 27 | 28 | Hence, ```python3 search.py -p ~/py_works/notebooks/images/ -q 'Rainbow' -c 1``` returned a single image containing rainbow. 29 | 30 | demo image 31 | -------------------------------------------------------------------------------- /search.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import clip 3 | from PIL import Image 4 | import cv2 5 | import os 6 | import argparse 7 | 8 | device = "cuda" if torch.cuda.is_available() else "cpu" 9 | 10 | if device == "cuda": 11 | torch.cuda.empty_cache() 12 | 13 | model, preprocess = clip.load("ViT-B/32", device=device) 14 | 15 | parser = argparse.ArgumentParser(description='Image Search') 16 | parser.add_argument('-p', '--path', type=str, default='images/', help='Path to Image folder') 17 | parser.add_argument('-q', '--query', type=str, default='Green hills', help='Search Query') 18 | parser.add_argument('-c', '--count', type=int, default=3, help='Number of images to return') 19 | 20 | args = parser.parse_args() 21 | 22 | images_path = None 23 | 24 | def read_images(path): 25 | global images_path 26 | images_path = os.listdir(path) 27 | images = torch.empty((len(images_path),3,224,224)) 28 | 29 | for i, img_path in enumerate(images_path): 30 | img = Image.open(path+img_path) 31 | images[i] = preprocess(img) 32 | 33 | images = images.to(device) 34 | return images 35 | 36 | def tokenize_text(search_query): 37 | search_query = clip.tokenize(search_query).to(device) 38 | return search_query 39 | 40 | def find_images(images, search_query, result_count): 41 | with torch.no_grad(): 42 | img_probs, query_probs = model(images, search_query) 43 | idxs = (-query_probs).argsort().cpu().numpy()[0][:result_count] 44 | 45 | for idx in idxs: 46 | print(images_path[idx]) 47 | img = cv2.imread(args.path+images_path[idx]) 48 | img_resize = cv2.resize(img, (500,500)) 49 | cv2.imshow(images_path[idx], img_resize) 50 | 51 | cv2.waitKey(0) 52 | cv2.destroyAllWindows() 53 | 54 | 55 | images = read_images(args.path) 56 | search_query = tokenize_text(args.query) 57 | 58 | find_images(images, search_query, args.count) --------------------------------------------------------------------------------