Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add inpainting and outpainting samples and tests #3874

Merged
merged 10 commits into from
Oct 7, 2024
120 changes: 120 additions & 0 deletions ai-platform/snippets/imagen/editImageInpaintingInsertMask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const main = async (inputFile, maskFile, prompt) => {
// [START generativeaionvertexai_imagen_edit_image_inpainting_insert_mask]
/**
* TODO(developer): Update these variables before running the sample.
*/
const projectId = process.env.CAIP_PROJECT_ID;
const location = 'us-central1';

const aiplatform = require('@google-cloud/aiplatform');

// Imports the Google Cloud Prediction Service Client library
const {PredictionServiceClient} = aiplatform.v1;

// Import the helper module for converting arbitrary protobuf.Value objects
const {helpers} = aiplatform;

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: `${location}-aiplatform.googleapis.com`,
};

// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);

const fs = require('fs');
irataxy marked this conversation as resolved.
Show resolved Hide resolved
const util = require('util');

async function editImageInpaintingInsertMask() {
// Configure the parent resource
const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagegeneration@006`;

const imageFile = fs.readFileSync(inputFile);
// Convert the image data to a Buffer and base64 encode it.
const encodedImage = Buffer.from(imageFile).toString('base64');

const maskImageFile = fs.readFileSync(maskFile);
// Convert the image mask data to a Buffer and base64 encode it.
const encodedMask = Buffer.from(maskImageFile).toString('base64');

const promptObj = {
prompt: prompt, // The text prompt describing what you want to see inserted
editMode: 'inpainting-insert',
image: {
bytesBase64Encoded: encodedImage,
},
mask: {
image: {
bytesBase64Encoded: encodedMask,
},
},
};
const instanceValue = helpers.toValue(promptObj);
const instances = [instanceValue];

const parameter = {
// Optional parameters
seed: 100,
// Controls the strength of the prompt
// 0-9 (low strength), 10-20 (medium strength), 21+ (high strength)
guidanceScale: 21,
sampleCount: 1,
};
const parameters = helpers.toValue(parameter);

const request = {
endpoint,
instances,
parameters,
};

// Predict request
const [response] = await predictionServiceClient.predict(request);
const predictions = response.predictions;
if (predictions.length === 0) {
console.log(
'No image was generated. Check the request parameters and prompt.'
);
} else {
let i = 1;
for (const prediction of predictions) {
const buff = Buffer.from(
prediction.structValue.fields.bytesBase64Encoded.stringValue,
'base64'
);
// Write image content to the output file
const writeFile = util.promisify(fs.writeFile);
const filename = `output${i}.png`;
await writeFile(filename, buff);
console.log(`Saved image ${filename}`);
i++;
}
}
}
await editImageInpaintingInsertMask().catch(err => {
console.error(err.message);
process.exitCode = 1;
});
// [END generativeaionvertexai_imagen_edit_image_inpainting_insert_mask]
};

// node editImageInpaintingInsertMask.js <inputFile> <maskFile> <prompt>
main(...process.argv.slice(2));
irataxy marked this conversation as resolved.
Show resolved Hide resolved
120 changes: 120 additions & 0 deletions ai-platform/snippets/imagen/editImageInpaintingRemoveMask.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const main = async (inputFile, maskFile, prompt) => {
// [START generativeaionvertexai_imagen_edit_image_inpainting_remove_mask]
/**
* TODO(developer): Update these variables before running the sample.
*/
const projectId = process.env.CAIP_PROJECT_ID;
const location = 'us-central1';

const aiplatform = require('@google-cloud/aiplatform');

// Imports the Google Cloud Prediction Service Client library
const {PredictionServiceClient} = aiplatform.v1;

// Import the helper module for converting arbitrary protobuf.Value objects
const {helpers} = aiplatform;

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: `${location}-aiplatform.googleapis.com`,
};

// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);

const fs = require('fs');
const util = require('util');

async function editImageInpaintingRemoveMask() {
// Configure the parent resource
const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagegeneration@006`;

const imageFile = fs.readFileSync(inputFile);
// Convert the image data to a Buffer and base64 encode it.
const encodedImage = Buffer.from(imageFile).toString('base64');

const maskImageFile = fs.readFileSync(maskFile);
// Convert the image mask data to a Buffer and base64 encode it.
const encodedMask = Buffer.from(maskImageFile).toString('base64');

const promptObj = {
prompt: prompt, // The text prompt describing the entire image
editMode: 'inpainting-remove',
image: {
bytesBase64Encoded: encodedImage,
},
mask: {
image: {
bytesBase64Encoded: encodedMask,
},
},
};
const instanceValue = helpers.toValue(promptObj);
const instances = [instanceValue];

const parameter = {
// Optional parameters
seed: 100,
// Controls the strength of the prompt
// 0-9 (low strength), 10-20 (medium strength), 21+ (high strength)
guidanceScale: 21,
sampleCount: 1,
};
const parameters = helpers.toValue(parameter);

const request = {
endpoint,
instances,
parameters,
};

// Predict request
const [response] = await predictionServiceClient.predict(request);
const predictions = response.predictions;
if (predictions.length === 0) {
console.log(
'No image was generated. Check the request parameters and prompt.'
);
} else {
let i = 1;
for (const prediction of predictions) {
const buff = Buffer.from(
prediction.structValue.fields.bytesBase64Encoded.stringValue,
'base64'
);
// Write image content to the output file
const writeFile = util.promisify(fs.writeFile);
const filename = `output${i}.png`;
await writeFile(filename, buff);
console.log(`Saved image ${filename}`);
i++;
}
}
}
await editImageInpaintingRemoveMask().catch(err => {
console.error(err.message);
process.exitCode = 1;
});
// [END generativeaionvertexai_imagen_edit_image_inpainting_remove_mask]
};

// node editImageInpaintingRemoveMask.js <inputFile> <maskFile> <prompt>
main(...process.argv.slice(2));
110 changes: 110 additions & 0 deletions ai-platform/snippets/imagen/editImageMaskFree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const main = async (inputFile, prompt) => {
// [START generativeaionvertexai_imagen_edit_image_mask_free]
/**
* TODO(developer): Update these variables before running the sample.
*/
const projectId = process.env.CAIP_PROJECT_ID;
const location = 'us-central1';

const aiplatform = require('@google-cloud/aiplatform');

// Imports the Google Cloud Prediction Service Client library
const {PredictionServiceClient} = aiplatform.v1;

// Import the helper module for converting arbitrary protobuf.Value objects
const {helpers} = aiplatform;

// Specifies the location of the api endpoint
const clientOptions = {
apiEndpoint: `${location}-aiplatform.googleapis.com`,
};

// Instantiates a client
const predictionServiceClient = new PredictionServiceClient(clientOptions);

const fs = require('fs');
const util = require('util');

async function editImageMaskFree() {
// Configure the parent resource
const endpoint = `projects/${projectId}/locations/${location}/publishers/google/models/imagegeneration@002`;

const imageFile = fs.readFileSync(inputFile);
// Convert the image data to a Buffer and base64 encode it.
const encodedImage = Buffer.from(imageFile).toString('base64');

const promptObj = {
prompt: prompt, // The text prompt describing what you want to see
image: {
bytesBase64Encoded: encodedImage,
},
};
const instanceValue = helpers.toValue(promptObj);
const instances = [instanceValue];

const parameter = {
// Optional parameters
seed: 100,
// Controls the strength of the prompt
// 0-9 (low strength), 10-20 (medium strength), 21+ (high strength)
guidanceScale: 21,
sampleCount: 1,
};
const parameters = helpers.toValue(parameter);

const request = {
endpoint,
instances,
parameters,
};

// Predict request
const [response] = await predictionServiceClient.predict(request);
const predictions = response.predictions;
if (predictions.length === 0) {
console.log(
'No image was generated. Check the request parameters and prompt.'
);
} else {
let i = 1;
for (const prediction of predictions) {
const buff = Buffer.from(
prediction.structValue.fields.bytesBase64Encoded.stringValue,
'base64'
);
// Write image content to the output file
const writeFile = util.promisify(fs.writeFile);
const filename = `output${i}.png`;
await writeFile(filename, buff);
console.log(`Saved image ${filename}`);
i++;
}
}
}
await editImageMaskFree().catch(err => {
console.error(err.message);
process.exitCode = 1;
});
// [END generativeaionvertexai_imagen_edit_image_mask_free]
};

// node editImageMaskFree.js <inputFile> <prompt>
main(...process.argv.slice(2));
Loading