|
| 1 | +/* |
| 2 | + * Copyright 2019 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 com.example.datalabeling; |
| 18 | + |
| 19 | +// [START datalabeling_create_annotation_spec_set_beta] |
| 20 | +import com.google.cloud.datalabeling.v1beta1.AnnotationSpec; |
| 21 | +import com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet; |
| 22 | +import com.google.cloud.datalabeling.v1beta1.CreateAnnotationSpecSetRequest; |
| 23 | +import com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient; |
| 24 | +import com.google.cloud.datalabeling.v1beta1.ProjectName; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Map.Entry; |
| 31 | + |
| 32 | +class CreateAnnotationSpecSet { |
| 33 | + |
| 34 | + // Create an annotation spec set. |
| 35 | + static void createAnnotationSpecSet(String projectId) { |
| 36 | + // String projectId = "YOUR_PROJECT_ID"; |
| 37 | + |
| 38 | + Map<String, String> annotationLabels = new HashMap<>(); |
| 39 | + annotationLabels.put("label_1", "label_1_description"); |
| 40 | + annotationLabels.put("label_2", "label_2_description"); |
| 41 | + |
| 42 | + try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) { |
| 43 | + |
| 44 | + ProjectName projectName = ProjectName.of(projectId); |
| 45 | + |
| 46 | + List<AnnotationSpec> annotationSpecs = new ArrayList<>(); |
| 47 | + for (Entry<String, String> entry : annotationLabels.entrySet()) { |
| 48 | + AnnotationSpec annotationSpec = AnnotationSpec.newBuilder() |
| 49 | + .setDisplayName(entry.getKey()) |
| 50 | + .setDescription(entry.getValue()) |
| 51 | + .build(); |
| 52 | + annotationSpecs.add(annotationSpec); |
| 53 | + } |
| 54 | + |
| 55 | + AnnotationSpecSet annotationSpecSet = AnnotationSpecSet.newBuilder() |
| 56 | + .setDisplayName("YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME") |
| 57 | + .setDescription("YOUR_DESCRIPTION") |
| 58 | + .addAllAnnotationSpecs(annotationSpecs) |
| 59 | + .build(); |
| 60 | + |
| 61 | + CreateAnnotationSpecSetRequest request = CreateAnnotationSpecSetRequest.newBuilder() |
| 62 | + .setAnnotationSpecSet(annotationSpecSet) |
| 63 | + .setParent(projectName.toString()) |
| 64 | + .build(); |
| 65 | + |
| 66 | + AnnotationSpecSet result = dataLabelingServiceClient.createAnnotationSpecSet(request); |
| 67 | + |
| 68 | + System.out.format("Name: %s\n", result.getName()); |
| 69 | + System.out.format("DisplayName: %s\n", result.getDisplayName()); |
| 70 | + System.out.format("Description: %s\n", result.getDescription()); |
| 71 | + System.out.format("Annotation Count: %d\n", result.getAnnotationSpecsCount()); |
| 72 | + |
| 73 | + for (AnnotationSpec annotationSpec : result.getAnnotationSpecsList()) { |
| 74 | + System.out.format("\tDisplayName: %s\n", annotationSpec.getDisplayName()); |
| 75 | + System.out.format("\tDescription: %s\n\n", annotationSpec.getDescription()); |
| 76 | + } |
| 77 | + } catch (IOException e) { |
| 78 | + e.printStackTrace(); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | +// [END datalabeling_create_annotation_spec_set_beta] |
0 commit comments