Skip to content

Commit 53350c0

Browse files
authored
feat: add Imagen samples and tests for getting image captions, responses, and watermark (#3895)
1 parent a662c8b commit 53350c0

5 files changed

+298
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main() {
20+
// [START generativeaionvertexai_imagen_get_short_form_image_captions]
21+
/**
22+
* TODO(developer): Update these variables before running the sample.
23+
*/
24+
const projectId = process.env.CAIP_PROJECT_ID;
25+
const location = 'us-central1';
26+
const inputFile = 'resources/cat.png';
27+
28+
const aiplatform = require('@google-cloud/aiplatform');
29+
30+
// Imports the Google Cloud Prediction Service Client library
31+
const {PredictionServiceClient} = aiplatform.v1;
32+
33+
// Import the helper module for converting arbitrary protobuf.Value objects
34+
const {helpers} = aiplatform;
35+
36+
// Specifies the location of the api endpoint
37+
const clientOptions = {
38+
apiEndpoint: `${location}-aiplatform.googleapis.com`,
39+
};
40+
41+
// Instantiates a client
42+
const predictionServiceClient = new PredictionServiceClient(clientOptions);
43+
44+
async function getShortFormImageCaptions() {
45+
const fs = require('fs');
46+
// Configure the parent resource
47+
const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagetext@001`;
48+
49+
const imageFile = fs.readFileSync(inputFile);
50+
// Convert the image data to a Buffer and base64 encode it.
51+
const encodedImage = Buffer.from(imageFile).toString('base64');
52+
53+
const instance = {
54+
image: {
55+
bytesBase64Encoded: encodedImage,
56+
},
57+
};
58+
const instanceValue = helpers.toValue(instance);
59+
const instances = [instanceValue];
60+
61+
const parameter = {
62+
// Optional parameters
63+
language: 'en',
64+
sampleCount: 2,
65+
};
66+
const parameters = helpers.toValue(parameter);
67+
68+
const request = {
69+
endpoint,
70+
instances,
71+
parameters,
72+
};
73+
74+
// Predict request
75+
const [response] = await predictionServiceClient.predict(request);
76+
const predictions = response.predictions;
77+
if (predictions.length === 0) {
78+
console.log(
79+
'No captions were generated. Check the request parameters and image.'
80+
);
81+
} else {
82+
predictions.forEach(prediction => {
83+
console.log(prediction.stringValue);
84+
});
85+
}
86+
}
87+
await getShortFormImageCaptions();
88+
// [END generativeaionvertexai_imagen_get_short_form_image_captions]
89+
}
90+
91+
main().catch(err => {
92+
console.error(err);
93+
process.exitcode = 1;
94+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main() {
20+
// [START generativeaionvertexai_imagen_get_short_form_image_responses]
21+
/**
22+
* TODO(developer): Update these variables before running the sample.
23+
*/
24+
const projectId = process.env.CAIP_PROJECT_ID;
25+
const location = 'us-central1';
26+
const inputFile = 'resources/cat.png';
27+
// The question about the contents of the image.
28+
const prompt = 'What breed of cat is this a picture of?';
29+
30+
const aiplatform = require('@google-cloud/aiplatform');
31+
32+
// Imports the Google Cloud Prediction Service Client library
33+
const {PredictionServiceClient} = aiplatform.v1;
34+
35+
// Import the helper module for converting arbitrary protobuf.Value objects
36+
const {helpers} = aiplatform;
37+
38+
// Specifies the location of the api endpoint
39+
const clientOptions = {
40+
apiEndpoint: `${location}-aiplatform.googleapis.com`,
41+
};
42+
43+
// Instantiates a client
44+
const predictionServiceClient = new PredictionServiceClient(clientOptions);
45+
46+
async function getShortFormImageResponses() {
47+
const fs = require('fs');
48+
// Configure the parent resource
49+
const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagetext@001`;
50+
51+
const imageFile = fs.readFileSync(inputFile);
52+
// Convert the image data to a Buffer and base64 encode it.
53+
const encodedImage = Buffer.from(imageFile).toString('base64');
54+
55+
const instance = {
56+
prompt: prompt,
57+
image: {
58+
bytesBase64Encoded: encodedImage,
59+
},
60+
};
61+
const instanceValue = helpers.toValue(instance);
62+
const instances = [instanceValue];
63+
64+
const parameter = {
65+
// Optional parameters
66+
sampleCount: 2,
67+
};
68+
const parameters = helpers.toValue(parameter);
69+
70+
const request = {
71+
endpoint,
72+
instances,
73+
parameters,
74+
};
75+
76+
// Predict request
77+
const [response] = await predictionServiceClient.predict(request);
78+
const predictions = response.predictions;
79+
if (predictions.length === 0) {
80+
console.log(
81+
'No responses were generated. Check the request parameters and image.'
82+
);
83+
} else {
84+
predictions.forEach(prediction => {
85+
console.log(prediction.stringValue);
86+
});
87+
}
88+
}
89+
await getShortFormImageResponses();
90+
// [END generativeaionvertexai_imagen_get_short_form_image_responses]
91+
}
92+
93+
main().catch(err => {
94+
console.error(err);
95+
process.exitcode = 1;
96+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
'use strict';
18+
19+
async function main() {
20+
// [START generativeaionvertexai_imagen_verify_image_watermark]
21+
/**
22+
* TODO(developer): Update these variables before running the sample.
23+
*/
24+
const projectId = process.env.CAIP_PROJECT_ID;
25+
const location = 'us-central1';
26+
const inputFile = 'resources/dog_newspaper.png'; // has watermark
27+
28+
const aiplatform = require('@google-cloud/aiplatform');
29+
30+
// Imports the Google Cloud Prediction Service Client library
31+
const {PredictionServiceClient} = aiplatform.v1;
32+
33+
// Import the helper module for converting arbitrary protobuf.Value objects
34+
const {helpers} = aiplatform;
35+
36+
// Specifies the location of the api endpoint
37+
const clientOptions = {
38+
apiEndpoint: `${location}-aiplatform.googleapis.com`,
39+
};
40+
41+
// Instantiates a client
42+
const predictionServiceClient = new PredictionServiceClient(clientOptions);
43+
44+
async function verifyImageWatermark() {
45+
const fs = require('fs');
46+
// Configure the parent resource
47+
const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imageverification@001`;
48+
49+
const imageFile = fs.readFileSync(inputFile);
50+
// Convert the image data to a Buffer and base64 encode it.
51+
const encodedImage = Buffer.from(imageFile).toString('base64');
52+
53+
const instance = {
54+
image: {
55+
bytesBase64Encoded: encodedImage,
56+
},
57+
};
58+
const instanceValue = helpers.toValue(instance);
59+
const instances = [instanceValue];
60+
61+
const request = {
62+
endpoint,
63+
instances,
64+
};
65+
66+
// Predict request
67+
const [response] = await predictionServiceClient.predict(request);
68+
const predictions = response.predictions;
69+
if (predictions.length === 0) {
70+
console.log('No decision was generated. Check the request image.');
71+
} else {
72+
predictions.forEach(prediction => {
73+
// "ACCEPT" if the image contains a digital watermark
74+
// "REJECT" if the image does not contain a digital watermark
75+
console.log(prediction.structValue.fields.decision.stringValue);
76+
});
77+
}
78+
}
79+
await verifyImageWatermark();
80+
// [END generativeaionvertexai_imagen_verify_image_watermark]
81+
}
82+
83+
main().catch(err => {
84+
console.error(err);
85+
process.exitcode = 1;
86+
});
1.02 MB
Loading

ai-platform/snippets/test/imagen.test.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const cp = require('child_process');
2424
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
2525
const cwd = path.join(__dirname, '..');
2626

27-
describe('AI platform generate and edit an image using Imagen', () => {
27+
describe('AI platform generate and edit an image using Imagen and check for a watermark', () => {
2828
it('should generate an image', async () => {
2929
const stdout = execSync('node ./imagen-generate-image.js', {
3030
cwd,
@@ -37,6 +37,12 @@ describe('AI platform generate and edit an image using Imagen', () => {
3737
});
3838
assert.match(stdout, /Saved image output1.png/);
3939
});
40+
it('should verify that an image contains a watermark', async () => {
41+
const stdout = execSync('node ./imagen-verify-image-watermark.js', {
42+
cwd,
43+
});
44+
assert.match(stdout, /ACCEPT/);
45+
});
4046
});
4147

4248
describe('AI platform edit image using Imagen inpainting and outpainting', () => {
@@ -65,3 +71,18 @@ describe('AI platform edit image using Imagen inpainting and outpainting', () =>
6571
assert.match(stdout, /Saved image output1.png/);
6672
});
6773
});
74+
75+
describe('AI platform get image captions and responses using Imagen', () => {
76+
it('should get short form captions for an image', async () => {
77+
const stdout = execSync('node ./imagen-get-short-form-image-captions.js', {
78+
cwd,
79+
});
80+
assert.match(stdout, /cat/);
81+
});
82+
it('should get short form responses for an image', async () => {
83+
const stdout = execSync('node ./imagen-get-short-form-image-responses.js', {
84+
cwd,
85+
});
86+
assert.match(stdout, /tabby/);
87+
});
88+
});

0 commit comments

Comments
 (0)