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 57 58 59 |
|
__call__
__call__(query)
Direct call for search function.
Source code in ultralytics/solutions/similarity_search.py
130 131 132 |
|
extract_image_feature
extract_image_feature(path)
Extract CLIP image embedding.
Source code in ultralytics/solutions/similarity_search.py
61 62 63 64 65 66 |
|
extract_text_feature
extract_text_feature(text)
Extract CLIP text embedding.
Source code in ultralytics/solutions/similarity_search.py
68 69 70 71 72 |
|
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
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 109 110 111 |
|
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
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|
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
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
index
index()
Function to process the user query and display output.
Source code in ultralytics/solutions/similarity_search.py
162 163 164 165 166 167 168 |
|
run
run(debug=False)
Runs the Flask web app.
Source code in ultralytics/solutions/similarity_search.py
170 171 172 |
|