Image search
Image
search uses an image as a search input to perform vector similarity search.
Additional information
Configure image search
To use images as search inputs, configure an image vectorizer integration for your collection. See the model provider integrations page for a list of available integrations.
By local image path
Use the Near Image
operator to execute image search.
If your query image is stored in a file, you can use the client library to search by its filename.
- Python
- JS/TS
- Go
- Java
from pathlib import Path
dogs = client.collections.use("Dog")
response = dogs.query.near_image(
near_image=Path("./images/search-image.jpg"), # Provide a `Path` object
return_properties=["breed"],
limit=1,
# targetVector: "vector_name" # required when using multiple named vectors
)
print(response.objects[0])
client.close()
const myCollection = client.collections.use('Dog');
// Query based on the image content
const result = await myCollection.query.nearImage('./images/search-image.jpg', {
returnProperties: ['breed'],
limit: 1,
// targetVector: 'vector_name' // required when using multiple named vectors
})
console.log(JSON.stringify(result.objects, null, 2));
response, err := client.GraphQL().Get().
WithClassName("Dog").
WithFields(graphql.Field{Name: "breed"}).
WithNearImage((&graphql.NearImageArgumentBuilder{}).WithImage("image.jpg")).
WithLimit(1).
Do(ctx)
NearImageArgument nearImage = NearImageArgument.builder()
.imageFile(new File("./images/search-image.jpg"))
.build();
Fields fields = Fields.builder()
.fields(new Field[]{
Field.builder().name("Breed").build()
})
.build();
String query = GetBuilder.builder()
.className(className)
.fields(fields)
.withNearImageFilter(nearImage)
.limit(1)
.build()
.buildQuery();
Result<GraphQLResponse> result = client.graphQL().raw().withQuery(query).run();
Example response
By the base64 representation
You can search by a base64 representation of an image:
- Python
- JS/TS
- Go
- Java
base64_string="SOME_BASE_64_REPRESENTATION"
# Get the collection containing images
dogs = client.collections.use("Dog")
# Perform query
response = dogs.query.near_image(
near_image=base64_string,
return_properties=["breed"],
limit=1,
# targetVector: "vector_name" # required when using multiple named vectors
)
print(response.objects[0])
client.close()
import { toBase64FromMedia } from 'weaviate-client';
const myCollection = client.collections.use('Dog');
const filePath = './images/search-image.jpg'
const base64String = await toBase64FromMedia(file.path)
// Perform query
const result = await myCollection.query.nearImage(base64String, {
returnProperties: ['breed'],
limit: 1,
// targetVector: 'vector_name' // required when using multiple named vectors
})
console.log(JSON.stringify(result.objects, null, 2));
response, err := client.GraphQL().Get().
WithClassName("Dog").
WithFields(graphql.Field{Name: "breed"}).
WithNearImage((&graphql.NearImageArgumentBuilder{}).WithImage(base64String)).
WithLimit(1).
Do(ctx)
String base64_string = "SOME_BASE_64_REPRESENTATION";
NearImageArgument nearImage = NearImageArgument.builder()
.image(base64_string)
.build();
Fields fields = Fields.builder()
.fields(new Field[]{
Field.builder().name("Breed").build()
})
.build();
String query = GetBuilder.builder()
.className(className)
.fields(fields)
.withNearImageFilter(nearImage)
.limit(1)
.build()
.buildQuery();
Result<GraphQLResponse> result = client.graphQL().raw().withQuery(query).run();
Example response
{
"data": {
"Get": {
"Dog": [
{
"breed": "Corgi"
}
]
}
}
}
client.close()
Create a base64 representation of an online image.
You can create a base64 representation of an online image, and use it as input for similarity search as shown above.
- Python
- JS/TS
- Go
- Java
import base64, requests
def url_to_base64(url):
image_response = requests.get(url)
content = image_response.content
return base64.b64encode(content).decode("utf-8")
base64_img = url_to_base64("https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Deutsches_Museum_Portrait_4.jpg/500px-Deutsches_Museum_Portrait_4.jpg")
client.close()
const imageURL = 'https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Deutsches_Museum_Portrait_4.jpg/500px-Deutsches_Museum_Portrait_4.jpg'
async function urlToBase64(imageUrl: string) {
const response = await fetch(imageUrl);
const content = await response.buffer();
return content.toString('base64');
}
const base64 = await urlToBase64(imageURL)
console.log(base64)
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
base64string := base64.StdEncoding.EncodeToString(content)
try {
// Open the input stream for the image URL
URL url = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/1/14/Deutsches_Museum_Portrait_4.jpg/500px-Deutsches_Museum_Portrait_4.jpg");
InputStream inputStream = url.openStream();
// Read the image bytes into a ByteArrayOutputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
// Convert the bytes to a base64 encoded string
byte[] imageBytes = outputStream.toByteArray();
String base64String = Base64.getEncoder().encodeToString(imageBytes);
System.out.println(base64String);
inputStream.close();
outputStream.close();
} catch (IOException e) {
System.err.println("Error occurred while converting image to base64: " + e.getMessage());
e.printStackTrace();
}
Combination with other operators
A Near Image
search can be combined with any other operators (like filter, limit, etc.), just as other similarity search operators.
See the similarity search
page for more details.
Related pages
Questions and feedback
If you have any questions or feedback, let us know in the user forum.