AnnDB
Search…
Images
Methods on image datasets accept either image URLs for publically accessible images or image objects for images stored in the filesystem. The maximum image size for locally stored images is 200kB. The transformation from images to vectors is done internally and your application doesn't have to worry about it.
Recommended image size is 224x224px. Larger images can cause latency issues while smaller images can hurt the search performance.

Create a Dataset

Create a dataset with Image type which tells AnnDB to embed your images and queries to vector representations.
In order to manage images in your dataset, create a corresponding dataset instance using the client.
Python
Ruby
1
dataset = client.images('<DATASET_NAME>')
Copied!
Python client accepts either image URLs or PIL.Image.Image objects as images.
1
dataset = client.images("<DATASET_NAME>")
Copied!
Ruby client accepts either image URLs or image byte arrays as images. To obtain a byte array from MiniMagick::Image use .to_blob.

Search using images

Python
Ruby
1
result = dataset.search(img, 10)
2
3
for item in result:
4
print(item.id, item.metadata)
Copied!
1
result = dataset.search(img, 10)
2
3
result.each do |item|
4
puts item.id, item.metadata
5
end
Copied!

Search using natural language

Python
Ruby
1
result = dataset.search('cute puppy', 10)
2
3
for item in result:
4
print(item.id, item.metadata)
Copied!
1
result = dataset.search("cute puppy", 10)
2
3
result.each do |item|
4
puts item.id, item.metadata
5
end
Copied!

Insert

Python
Ruby
1
# Single image
2
id = dataset.insert(img, metadata={'key': 'value'})
Copied!
1
# Batch
2
result = dataset.insert_batch([
3
anndb_api.ImageItem(None, img, {'key': 'value'}),
4
...
5
])
6
7
for r in result:
8
print(r.id, r.error)
Copied!
1
id = dataset.insert(img, metadata={ "key": "value" })
Copied!
1
result = dataset.insert_batch([
2
{
3
image: img.to_blob,
4
metadata: { "key": "value" }
5
},
6
...
7
])
8
9
result.each { |r|
10
puts r[:id], r[:error]
11
}
Copied!

Update

Python
Ruby
1
# Single image
2
id = dataset.update(id, img, metadata={'key': 'value'})
Copied!
1
# Batch
2
result = dataset.update_batch([
3
anndb_api.ImageItem(id, img, {'key': 'value'}),
4
...
5
])
6
7
for r in result:
8
print(r.id, r.error)
Copied!
1
id = dataset.update(id, img, metadata={ "key": "value" })
Copied!
1
result = dataset.update_batch([
2
{
3
id: id,
4
image: "https://my.domain/img.jpg",
5
metadata: { "key": "value" }
6
},
7
...
8
])
9
10
result.each { |r|
11
puts r[:id], r[:error]
12
}
Copied!

Delete

Python
Ruby
1
# Single image
2
dataset.delete(id)
Copied!
1
# Batch
2
result = dataset.delete_batch([id, ...])
3
4
for r in result:
5
print(r.id, r.error)
Copied!
1
dataset.delete(id)
Copied!
1
result = dataset.delete_batch([id, ...])
2
3
result.each { |r|
4
puts r[:id], r[:error]
5
}
Copied!
Last modified 7mo ago