|
| 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 | + * http://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 | +package aiplatform.imagen; |
| 18 | + |
| 19 | +// [START generativeaionvertexai_imagen_edit_image_mask_free] |
| 20 | + |
| 21 | +import com.google.api.gax.rpc.ApiException; |
| 22 | +import com.google.cloud.aiplatform.v1.EndpointName; |
| 23 | +import com.google.cloud.aiplatform.v1.PredictResponse; |
| 24 | +import com.google.cloud.aiplatform.v1.PredictionServiceClient; |
| 25 | +import com.google.cloud.aiplatform.v1.PredictionServiceSettings; |
| 26 | +import com.google.gson.Gson; |
| 27 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 28 | +import com.google.protobuf.Value; |
| 29 | +import com.google.protobuf.util.JsonFormat; |
| 30 | +import java.io.IOException; |
| 31 | +import java.nio.charset.StandardCharsets; |
| 32 | +import java.nio.file.Files; |
| 33 | +import java.nio.file.Path; |
| 34 | +import java.nio.file.Paths; |
| 35 | +import java.util.Base64; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +public class EditImageMaskFreeSample { |
| 41 | + |
| 42 | + public static void main(String[] args) throws IOException { |
| 43 | + // TODO(developer): Replace these variables before running the sample. |
| 44 | + String projectId = "my-project-id"; |
| 45 | + String location = "us-central1"; |
| 46 | + String inputPath = "/path/to/my-input.png"; |
| 47 | + String prompt = ""; // The text prompt describing what you want to see. |
| 48 | + |
| 49 | + editImageMaskFree(projectId, location, inputPath, prompt); |
| 50 | + } |
| 51 | + |
| 52 | + // Edit an image without using a mask. The edit is applied to the entire image and is saved to a |
| 53 | + // new file. |
| 54 | + public static PredictResponse editImageMaskFree( |
| 55 | + String projectId, String location, String inputPath, String prompt) |
| 56 | + throws ApiException, IOException { |
| 57 | + final String endpoint = String.format("%s-aiplatform.googleapis.com:443", location); |
| 58 | + PredictionServiceSettings predictionServiceSettings = |
| 59 | + PredictionServiceSettings.newBuilder().setEndpoint(endpoint).build(); |
| 60 | + |
| 61 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 62 | + // once, and can be reused for multiple requests. |
| 63 | + try (PredictionServiceClient predictionServiceClient = |
| 64 | + PredictionServiceClient.create(predictionServiceSettings)) { |
| 65 | + |
| 66 | + final EndpointName endpointName = |
| 67 | + EndpointName.ofProjectLocationPublisherModelName( |
| 68 | + projectId, location, "google", "imagegeneration@002"); |
| 69 | + |
| 70 | + // Convert the image to Base64. |
| 71 | + byte[] imageData = Base64.getEncoder().encode(Files.readAllBytes(Paths.get(inputPath))); |
| 72 | + String image = new String(imageData, StandardCharsets.UTF_8); |
| 73 | + Map<String, String> imageMap = new HashMap<>(); |
| 74 | + imageMap.put("bytesBase64Encoded", image); |
| 75 | + |
| 76 | + Map<String, Object> instancesMap = new HashMap<>(); |
| 77 | + instancesMap.put("prompt", prompt); |
| 78 | + instancesMap.put("image", imageMap); |
| 79 | + Value instances = mapToValue(instancesMap); |
| 80 | + |
| 81 | + Map<String, Object> paramsMap = new HashMap<>(); |
| 82 | + // Optional parameters |
| 83 | + paramsMap.put("seed", 1); |
| 84 | + // Controls the strength of the prompt. |
| 85 | + // 0-9 (low strength), 10-20 (medium strength), 21+ (high strength) |
| 86 | + paramsMap.put("guidanceScale", 21); |
| 87 | + paramsMap.put("sampleCount", 1); |
| 88 | + Value parameters = mapToValue(paramsMap); |
| 89 | + |
| 90 | + PredictResponse predictResponse = |
| 91 | + predictionServiceClient.predict( |
| 92 | + endpointName, Collections.singletonList(instances), parameters); |
| 93 | + |
| 94 | + for (Value prediction : predictResponse.getPredictionsList()) { |
| 95 | + Map<String, Value> fieldsMap = prediction.getStructValue().getFieldsMap(); |
| 96 | + if (fieldsMap.containsKey("bytesBase64Encoded")) { |
| 97 | + String bytesBase64Encoded = fieldsMap.get("bytesBase64Encoded").getStringValue(); |
| 98 | + Path tmpPath = Files.createTempFile("imagen-", ".png"); |
| 99 | + Files.write(tmpPath, Base64.getDecoder().decode(bytesBase64Encoded)); |
| 100 | + System.out.format("Image file written to: %s\n", tmpPath.toUri()); |
| 101 | + } |
| 102 | + } |
| 103 | + return predictResponse; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static Value mapToValue(Map<String, Object> map) throws InvalidProtocolBufferException { |
| 108 | + Gson gson = new Gson(); |
| 109 | + String json = gson.toJson(map); |
| 110 | + Value.Builder builder = Value.newBuilder(); |
| 111 | + JsonFormat.parser().merge(json, builder); |
| 112 | + return builder.build(); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +// [END generativeaionvertexai_imagen_edit_image_mask_free] |
0 commit comments