Reference for ultralytics/solutions/similarity_search.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/solutions/similarity_search.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.solutions.similarity_search.VisualAISearch
VisualAISearch(**kwargs)
Bases: BaseSolution
VisualAISearch leverages OpenCLIP to generate high-quality image and text embeddings, aligning them in a shared semantic space. It then uses FAISS to perform fast and scalable similarity-based retrieval, allowing users to search large collections of images using natural language queries with high accuracy and speed.
Attributes:
Name | Type | Description |
---|---|---|
data |
str
|
Directory containing images. |
device |
str
|
Computation device, e.g., 'cpu' or 'cuda'. |
Source code in ultralytics/solutions/similarity_search.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
|
__call__
__call__(query)
Direct call for search function.
Source code in ultralytics/solutions/similarity_search.py
127 128 129 |
|
extract_image_feature
extract_image_feature(path)
Extract CLIP image embedding.
Source code in ultralytics/solutions/similarity_search.py
58 59 60 61 62 63 |
|
extract_text_feature
extract_text_feature(text)
Extract CLIP text embedding.
Source code in ultralytics/solutions/similarity_search.py
65 66 67 68 69 |
|
load_or_build_index
load_or_build_index()
Loads FAISS index or builds a new one from image features.
Source code in ultralytics/solutions/similarity_search.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
search
search(query, k=30, similarity_thresh=0.1)
Returns top-k semantically similar images to the given query.
Source code in ultralytics/solutions/similarity_search.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
ultralytics.solutions.similarity_search.SearchApp
SearchApp(data='images', device=None)
A Flask-based web interface powers the semantic image search experience, enabling users to input natural language queries and instantly view the most relevant images retrieved from the indexed database—all through a clean, responsive, and easily customizable frontend.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data
|
str
|
Path to images to index and search. |
'images'
|
device
|
str
|
Device to run inference on (e.g. 'cpu', 'cuda'). |
None
|
Source code in ultralytics/solutions/similarity_search.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
index
index()
Function to process the user query and display output.
Source code in ultralytics/solutions/similarity_search.py
159 160 161 162 163 164 165 |
|
run
run(debug=False)
Runs the Flask web app.
Source code in ultralytics/solutions/similarity_search.py
167 168 169 |
|