|
| 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 | +}); |
0 commit comments