Skip to content

Commit d5ba43a

Browse files
irataxysubfuzion
andauthored
feat: add inpainting and outpainting samples and tests (#3874)
* feat: add inpainting and outpainting samples and tests * move samples to main snippets directory; move parameters to inside the main function * fix lint --------- Co-authored-by: Tony Pujals <[email protected]>
1 parent 5d35c16 commit d5ba43a

15 files changed

+550
-47
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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_edit_image_inpainting_insert_mask]
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/woman.png';
27+
const maskFile = 'resources/woman_inpainting_insert_mask.png';
28+
const prompt = 'hat';
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 editImageInpaintingInsertMask() {
47+
const fs = require('fs');
48+
const util = require('util');
49+
// Configure the parent resource
50+
const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagegeneration@006`;
51+
52+
const imageFile = fs.readFileSync(inputFile);
53+
// Convert the image data to a Buffer and base64 encode it.
54+
const encodedImage = Buffer.from(imageFile).toString('base64');
55+
56+
const maskImageFile = fs.readFileSync(maskFile);
57+
// Convert the image mask data to a Buffer and base64 encode it.
58+
const encodedMask = Buffer.from(maskImageFile).toString('base64');
59+
60+
const promptObj = {
61+
prompt: prompt, // The text prompt describing what you want to see inserted
62+
editMode: 'inpainting-insert',
63+
image: {
64+
bytesBase64Encoded: encodedImage,
65+
},
66+
mask: {
67+
image: {
68+
bytesBase64Encoded: encodedMask,
69+
},
70+
},
71+
};
72+
const instanceValue = helpers.toValue(promptObj);
73+
const instances = [instanceValue];
74+
75+
const parameter = {
76+
// Optional parameters
77+
seed: 100,
78+
// Controls the strength of the prompt
79+
// 0-9 (low strength), 10-20 (medium strength), 21+ (high strength)
80+
guidanceScale: 21,
81+
sampleCount: 1,
82+
};
83+
const parameters = helpers.toValue(parameter);
84+
85+
const request = {
86+
endpoint,
87+
instances,
88+
parameters,
89+
};
90+
91+
// Predict request
92+
const [response] = await predictionServiceClient.predict(request);
93+
const predictions = response.predictions;
94+
if (predictions.length === 0) {
95+
console.log(
96+
'No image was generated. Check the request parameters and prompt.'
97+
);
98+
} else {
99+
let i = 1;
100+
for (const prediction of predictions) {
101+
const buff = Buffer.from(
102+
prediction.structValue.fields.bytesBase64Encoded.stringValue,
103+
'base64'
104+
);
105+
// Write image content to the output file
106+
const writeFile = util.promisify(fs.writeFile);
107+
const filename = `output${i}.png`;
108+
await writeFile(filename, buff);
109+
console.log(`Saved image ${filename}`);
110+
i++;
111+
}
112+
}
113+
}
114+
await editImageInpaintingInsertMask();
115+
// [END generativeaionvertexai_imagen_edit_image_inpainting_insert_mask]
116+
}
117+
118+
main().catch(err => {
119+
console.error(err);
120+
process.exitcode = 1;
121+
});
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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_edit_image_inpainting_remove_mask]
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/volleyball_game.png';
27+
const maskFile = 'resources/volleyball_game_inpainting_remove_mask.png';
28+
const prompt = 'volleyball game';
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 editImageInpaintingRemoveMask() {
47+
const fs = require('fs');
48+
const util = require('util');
49+
// Configure the parent resource
50+
const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagegeneration@006`;
51+
52+
const imageFile = fs.readFileSync(inputFile);
53+
// Convert the image data to a Buffer and base64 encode it.
54+
const encodedImage = Buffer.from(imageFile).toString('base64');
55+
56+
const maskImageFile = fs.readFileSync(maskFile);
57+
// Convert the image mask data to a Buffer and base64 encode it.
58+
const encodedMask = Buffer.from(maskImageFile).toString('base64');
59+
60+
const promptObj = {
61+
prompt: prompt, // The text prompt describing the entire image
62+
editMode: 'inpainting-remove',
63+
image: {
64+
bytesBase64Encoded: encodedImage,
65+
},
66+
mask: {
67+
image: {
68+
bytesBase64Encoded: encodedMask,
69+
},
70+
},
71+
};
72+
const instanceValue = helpers.toValue(promptObj);
73+
const instances = [instanceValue];
74+
75+
const parameter = {
76+
// Optional parameters
77+
seed: 100,
78+
// Controls the strength of the prompt
79+
// 0-9 (low strength), 10-20 (medium strength), 21+ (high strength)
80+
guidanceScale: 21,
81+
sampleCount: 1,
82+
};
83+
const parameters = helpers.toValue(parameter);
84+
85+
const request = {
86+
endpoint,
87+
instances,
88+
parameters,
89+
};
90+
91+
// Predict request
92+
const [response] = await predictionServiceClient.predict(request);
93+
const predictions = response.predictions;
94+
if (predictions.length === 0) {
95+
console.log(
96+
'No image was generated. Check the request parameters and prompt.'
97+
);
98+
} else {
99+
let i = 1;
100+
for (const prediction of predictions) {
101+
const buff = Buffer.from(
102+
prediction.structValue.fields.bytesBase64Encoded.stringValue,
103+
'base64'
104+
);
105+
// Write image content to the output file
106+
const writeFile = util.promisify(fs.writeFile);
107+
const filename = `output${i}.png`;
108+
await writeFile(filename, buff);
109+
console.log(`Saved image ${filename}`);
110+
i++;
111+
}
112+
}
113+
}
114+
await editImageInpaintingRemoveMask();
115+
// [END generativeaionvertexai_imagen_edit_image_inpainting_remove_mask]
116+
}
117+
118+
main().catch(err => {
119+
console.error(err);
120+
process.exitcode = 1;
121+
});
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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_edit_image_mask_free]
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+
const prompt = 'dog';
28+
29+
const aiplatform = require('@google-cloud/aiplatform');
30+
31+
// Imports the Google Cloud Prediction Service Client library
32+
const {PredictionServiceClient} = aiplatform.v1;
33+
34+
// Import the helper module for converting arbitrary protobuf.Value objects
35+
const {helpers} = aiplatform;
36+
37+
// Specifies the location of the api endpoint
38+
const clientOptions = {
39+
apiEndpoint: `${location}-aiplatform.googleapis.com`,
40+
};
41+
42+
// Instantiates a client
43+
const predictionServiceClient = new PredictionServiceClient(clientOptions);
44+
45+
async function editImageMaskFree() {
46+
const fs = require('fs');
47+
const util = require('util');
48+
// Configure the parent resource
49+
const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagegeneration@002`;
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 promptObj = {
56+
prompt: prompt, // The text prompt describing what you want to see
57+
image: {
58+
bytesBase64Encoded: encodedImage,
59+
},
60+
};
61+
const instanceValue = helpers.toValue(promptObj);
62+
const instances = [instanceValue];
63+
64+
const parameter = {
65+
// Optional parameters
66+
seed: 100,
67+
// Controls the strength of the prompt
68+
// 0-9 (low strength), 10-20 (medium strength), 21+ (high strength)
69+
guidanceScale: 21,
70+
sampleCount: 1,
71+
};
72+
const parameters = helpers.toValue(parameter);
73+
74+
const request = {
75+
endpoint,
76+
instances,
77+
parameters,
78+
};
79+
80+
// Predict request
81+
const [response] = await predictionServiceClient.predict(request);
82+
const predictions = response.predictions;
83+
if (predictions.length === 0) {
84+
console.log(
85+
'No image was generated. Check the request parameters and prompt.'
86+
);
87+
} else {
88+
let i = 1;
89+
for (const prediction of predictions) {
90+
const buff = Buffer.from(
91+
prediction.structValue.fields.bytesBase64Encoded.stringValue,
92+
'base64'
93+
);
94+
// Write image content to the output file
95+
const writeFile = util.promisify(fs.writeFile);
96+
const filename = `output${i}.png`;
97+
await writeFile(filename, buff);
98+
console.log(`Saved image ${filename}`);
99+
i++;
100+
}
101+
}
102+
}
103+
await editImageMaskFree();
104+
// [END generativeaionvertexai_imagen_edit_image_mask_free]
105+
}
106+
107+
main().catch(err => {
108+
console.error(err);
109+
process.exitcode = 1;
110+
});

0 commit comments

Comments
 (0)