Index Images for Search
Use AI to index your images based on semantic similarity.
To get started with search, you must first add images to the search index. You can add one or more images to the index at a time. You can supply an image either with a publicly accessible URL or by directly sending image bytes. You can send up to 128 images in one API call.
Java
NodeJS
Python
cURL
Javascript (REST)
1
import com.clarifai.grpc.api.*;
2
import com.clarifai.grpc.api.status.*;
3
4
// Insert here the initialization code as outlined on this page:
5
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
6
7
MultiInputResponse postInputsResponse = stub.postInputs(
8
PostInputsRequest.newBuilder()
9
.addInputs(
10
Input.newBuilder()
11
.setData(
12
Data.newBuilder().setImage(
13
Image.newBuilder()
14
.setUrl("https://samples.clarifai.com/metro-north.jpg")
15
.setAllowDuplicateUrl(true)
16
)
17
)
18
)
19
.addInputs(
20
Input.newBuilder()
21
.setData(
22
Data.newBuilder().setImage(
23
Image.newBuilder()
24
.setUrl("https://samples.clarifai.com/wedding.jpg")
25
.setAllowDuplicateUrl(true)
26
)
27
)
28
)
29
.addInputs(
30
Input.newBuilder()
31
.setData(
32
Data.newBuilder().setImage(
33
Image.newBuilder()
34
.setBase64(ByteString.copyFrom(Files.readAllBytes(
35
new File("{YOUR_IMAGE_FILE_LOCATION}").toPath()
36
)))
37
)
38
)
39
)
40
.build()
41
);
42
43
if (postInputsResponse.getStatus().getCode() != StatusCode.SUCCESS) {
44
for (Input input : postInputsResponse.getInputsList()) {
45
System.out.println("Input " + input.getId() + " status: ");
46
System.out.println(input.getStatus() + "\n");
47
}
48
49
throw new RuntimeException("Post inputs failed, status: " + postInputsResponse.getStatus());
50
}
Copied!
1
// Insert here the initialization code as outlined on this page:
2
// https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
const fs = require("fs");
5
const imageBytes = fs.readFileSync("{YOUR_IMAGE_FILE_LOCATION}");
6
7
stub.PostInputs(
8
{
9
inputs: [
10
{
11
data: {image: {url: "https://samples.clarifai.com/metro-north.jpg", allow_duplicate_url: true}}
12
},
13
{
14
data: {image: {url: "https://samples.clarifai.com/puppy.jpeg", allow_duplicate_url: true}}
15
},
16
{
17
data: {image: {base64: imageBytes}}
18
}
19
]
20
},
21
metadata,
22
(err, response) => {
23
if (err) {
24
throw new Error(err);
25
}
26
27
if (response.status.code !== 10000) {
28
for (const input of response.inputs) {
29
console.log("Input " + input.id + " status: ");
30
console.log(JSON.stringify(input.status, null, 2) + "\n");
31
}
32
33
throw new Error("Post inputs failed, status: " + response.status.description);
34
}
35
}
36
);
Copied!
1
# Insert here the initialization code as outlined on this page:
2
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions
3
4
with open("{YOUR_IMAGE_FILE_LOCATION}", "rb") as f:
5
file_bytes = f.read()
6
7
post_inputs_response = stub.PostInputs(
8
service_pb2.PostInputsRequest(
9
inputs=[
10
resources_pb2.Input(
11
data=resources_pb2.Data(
12
image=resources_pb2.Image(
13
url="https://samples.clarifai.com/metro-north.jpg",
14
allow_duplicate_url=True
15
)
16
)
17
),
18
resources_pb2.Input(
19
data=resources_pb2.Data(
20
image=resources_pb2.Image(
21
url="https://samples.clarifai.com/wedding.jpg",
22
allow_duplicate_url=True
23
)
24
)
25
),
26
resources_pb2.Input(
27
data=resources_pb2.Data(
28
image=resources_pb2.Image(
29
base64=file_bytes
30
)
31
)
32
),
33
]
34
),
35
metadata=metadata
36
)
37
38
if post_inputs_response.status.code != status_code_pb2.SUCCESS:
39
print("There was an error with your request!")
40
for input_response in post_inputs_response.inputs:
41
print("Input " + input_response.id + " status:")
42
print(input_response.status)
43
44
print("\tCode: {}".format(post_inputs_response.outputs[0].status.code))
45
print("\tDescription: {}".format(post_inputs_response.outputs[0].status.description))
46
print("\tDetails: {}".format(post_inputs_response.outputs[0].status.details))
47
raise Exception("Post inputs failed, status: " + post_inputs_response.status.description)
Copied!
1
curl -X POST \
2
-H "Authorization: Key YOUR_API_KEY" \
3
-H "Content-Type: application/json" \
4
-d '
5
{
6
"inputs": [
7
{
8
"data": {
9
"image": {
10
"url": "https://samples.clarifai.com/metro-north.jpg",
11
"allow_duplicate_url": true
12
}
13
}
14
},
15
{
16
"data": {
17
"image": {
18
"url": "https://samples.clarifai.com/wedding.jpg",
19
"allow_duplicate_url": true
20
}
21
}
22
}
23
]
24
}'\
25
https://api.clarifai.com/v2/inputs
26
27
# Use image's "base64" field to upload image from your local machine.
Copied!
1
const raw = JSON.stringify({
2
"user_app_id": {
3
"user_id": "{YOUR_USER_ID}",
4
"app_id": "{YOUR_APP_ID}"
5
},
6
"inputs": [
7
{
8
"data": {
9
"image": {
10
"url": "https://samples.clarifai.com/metro-north.jpg",
11
"allow_duplicate_url": true
12
}
13
}
14
},
15
{
16
"data": {
17
"image": {
18
"url": "https://samples.clarifai.com/wedding.jpg",
19
"allow_duplicate_url": true
20
}
21
}
22
}
23
]
24
});
25
26
// # Use image's "base64" field to upload image from your local machine.
27
28
const requestOptions = {
29
method: 'POST',
30
headers: {
31
'Accept': 'application/json',
32
'Authorization': 'Key {YOUR_PERSONAL_TOKEN}'
33
},
34
body: raw
35
};
36
37
fetch(`https://api.clarifai.com/v2/inputs`, requestOptions)
38
.then(response => response.text())
39
.then(result => console.log(result))
40
.catch(error => console.log('error', error));
Copied!
Response JSON
1
{
2
"status": {
3
"code": 10000,
4
"description": "Ok"
5
},
6
"inputs": [
7
{
8
"id": "edc70c917475499abdc7151f41d6cf3e",
9
"created_at": "2016-11-22T17:06:02Z",
10
"data": {
11
"image": {
12
"url": "https://samples.clarifai.com/metro-north.jpg"
13
}
14
},
15
"status": {
16
"code": 30001,
17
"description": "Download pending"
18
}
19
},
20
{
21
"id": "f96ca3bbf02041c59addcc13e3468b7d",
22
"created_at": "2016-11-22T17:06:02Z",
23
"data": {
24
"image": {
25
"url": "https://samples.clarifai.com/wedding.jpg"
26
}
27
},
28
"status": {
29
"code": 30001,
30
"description": "Download pending"
31
}
32
}
33
]
34
}
Copied!
Last modified 1mo ago
Copy link
Edit on GitHub