You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Requires model to be in raw resources at e.g res/raw/image_encoder_quant_int8.onnx
80
+
val imageEmbedder =ClipImageEmbedder(context, ResourceId(R.raw.image_encoder_quant_int8))
81
+
82
+
val embedding = imageEmbedder.embed(bitmap)
83
+
84
+
```
85
+
86
+
87
+
**Batch Example:**
88
+
89
+
```kotlin
90
+
val images:List<Bitmap> =...
91
+
val embeddings = imageEmbedder.embedBatch(images)
92
+
```
93
+
94
+
### Indexing
95
+
96
+
To get started with indexing media quickly, you can use the provided `ImageIndex` and `VideoIndexer` classes as shown below. You can optionally create your own indexers (including for text related data) by implementing the `BatchProcessor` interface. See docs for more details.
97
+
98
+
#### Image Indexing
99
+
100
+
Index images to enable similarity search. The index is saved as a binary file and managed with a FileEmbeddingStore.
101
+
> **Important**: During indexing the MediaStore Id is used to as the id in the `Embedding` which is stored. This can later be used for retrieval.
102
+
103
+
104
+
```kotlin
105
+
val imageEmbedder =ClipImageEmbedder(context, ResourceId(R.raw.image_encoder_quant_int8))
106
+
val imageStore =FileEmbeddingStore(File(context.filesDir, "image_index.bin"), imageEmbedder.embeddingDim, useCache =false) // cache not needed for indexing
107
+
val imageIndexer =ImageIndexer(imageEmbedder, context=context, listener =null, store = imageStore) //optionally pass a listener to handle events
108
+
val ids = getImageIds() // placeholder function to get MediaStore image ids
109
+
imageIndexer.run(ids)
110
+
```
111
+
112
+
#### Video Indexing
113
+
114
+
Index videos to enable similarity search. The index is saved as a binary file and managed with a FileEmbeddingStore.
115
+
116
+
```kotlin
117
+
val imageEmbedder =ClipImageEmbedder(context, ResourceId(R.raw.image_encoder_quant_int8))
118
+
val videoStore =FileEmbeddingStore(File(context.filesDir, "video_index.bin"), imageEmbedder.embeddingDim, useCache =false )
119
+
val videoIndexer =VideoIndexer(imageEmbedder, context=context, listener =null, store = videoStore, width =ClipConfig.IMAGE_SIZE_X, height =ClipConfig.IMAGE_SIZE_Y)
120
+
val ids = getVideoIds() // placeholder function to get MediaStore video ids
121
+
videoIndexer.run(ids)
122
+
```
123
+
124
+
### Searching
125
+
126
+
Below shows how to search using both text queries and an image. The returns results are List<Embedding>. You can use the id from each one, which corresponds to the MediaStore id, to retrieve the result images.
127
+
128
+
#### Text-to-Image Search
129
+
130
+
```kotlin
131
+
val imageStore =FileEmbeddingStore(File(context.filesDir, "image_index.bin"), imageEmbedder.embeddingDim, useCache =false) // cache not needed for indexing
132
+
val imageRetriever =FileEmbeddingRetriever(imageStore)
133
+
val textEmbedder =ClipTextEmbedder(context, ResourceId(R.raw.text_encoder_quant_int8))
134
+
val query ="my search query"
135
+
val embedding = textEmbedder.embed(query)
136
+
val topK =20
137
+
val similarityThreshold =0.2f
138
+
val results = retriever.query(embedding, topK, similarityThreshold)
139
+
140
+
```
141
+
142
+
#### Reverse Image Search
143
+
144
+
```kotlin
145
+
val imageStore =FileEmbeddingStore(File(context.filesDir, "image_index.bin"), imageEmbedder.embeddingDim, useCache =false) // cache not needed for indexing
146
+
val imageRetriever =FileEmbeddingRetriever(imageStore)
147
+
val imageEmbedder =ClipImageEmbedder(context, ResourceId(R.raw.image_encoder_quant_int8))
148
+
val embedding = imageEmbedder.embed(bitmap)
149
+
val topK =20
150
+
val similarityThreshold =0.2f
151
+
val results = retriever.query(embedding, topK, similarityThreshold)
0 commit comments