Search in sources :

Example 16 with DataLabelingServiceClient

use of com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient in project java-datalabeling by googleapis.

the class LabelTextIT method setUp.

@Before
public void setUp() {
    System.setOut(new PrintStream(new ByteArrayOutputStream()));
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) {
        // Create the dataset
        CreateDataset.createDataset(PROJECT_ID, datasetName);
        ProjectName projectName = ProjectName.of(PROJECT_ID);
        // Get the Dataset
        ListDatasetsRequest datasetsRequest = ListDatasetsRequest.newBuilder().setParent(projectName.toString()).build();
        ListDatasetsPagedResponse datasetsResponse = dataLabelingServiceClient.listDatasets(datasetsRequest);
        for (Dataset returnedDataset : datasetsResponse.getPage().iterateAll()) {
            if (returnedDataset.getDisplayName().equals("LABEL_TEXT_DATASET_NAME")) {
                dataset = returnedDataset;
            }
        }
        // Import the texts
        GcsSource gcsSource = GcsSource.newBuilder().setInputUri(DATASET_GCS_SOURCE_URI).setMimeType("text/csv").build();
        InputConfig inputConfig = InputConfig.newBuilder().setDataType(// DataTypes: AUDIO, IMAGE, VIDEO, TEXT
        DataType.TEXT).setGcsSource(gcsSource).build();
        ImportDataRequest importDataRequest = ImportDataRequest.newBuilder().setName(dataset.getName()).setInputConfig(inputConfig).build();
        ImportDataOperationResponse response = dataLabelingServiceClient.importDataAsync(importDataRequest).get();
        System.out.format("Imported items: %d\n", response.getImportCount());
        // Create the instruction
        CreateInstruction.createInstruction(PROJECT_ID, INSTRUCTION_GCS_SOURCE_URI);
        // Create the annotation spec set
        CreateAnnotationSpecSet.createAnnotationSpecSet(PROJECT_ID);
        // Get the instruction
        ListInstructionsRequest instructionsRequest = ListInstructionsRequest.newBuilder().setParent(projectName.toString()).build();
        ListInstructionsPagedResponse instructionsResponse = dataLabelingServiceClient.listInstructions(instructionsRequest);
        for (Instruction returnedInstruction : instructionsResponse.getPage().iterateAll()) {
            if (returnedInstruction.getDisplayName().equals("YOUR_INSTRUCTION_DISPLAY_NAME")) {
                instruction = returnedInstruction;
            }
        }
        // Get the annotation spec set
        ListAnnotationSpecSetsRequest annotationRequest = ListAnnotationSpecSetsRequest.newBuilder().setParent(projectName.toString()).build();
        ListAnnotationSpecSetsPagedResponse annotationsResponse = dataLabelingServiceClient.listAnnotationSpecSets(annotationRequest);
        for (AnnotationSpecSet returnedAnnotation : annotationsResponse.getPage().iterateAll()) {
            if (returnedAnnotation.getDisplayName().equals("YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME")) {
                annotationSpecSet = returnedAnnotation;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ListAnnotationSpecSetsPagedResponse(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListAnnotationSpecSetsPagedResponse) PrintStream(java.io.PrintStream) DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) ListDatasetsPagedResponse(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListDatasetsPagedResponse) GcsSource(com.google.cloud.datalabeling.v1beta1.GcsSource) ProjectName(com.google.cloud.datalabeling.v1beta1.ProjectName) ListAnnotationSpecSetsRequest(com.google.cloud.datalabeling.v1beta1.ListAnnotationSpecSetsRequest) Dataset(com.google.cloud.datalabeling.v1beta1.Dataset) AnnotationSpecSet(com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Instruction(com.google.cloud.datalabeling.v1beta1.Instruction) IOException(java.io.IOException) ListDatasetsRequest(com.google.cloud.datalabeling.v1beta1.ListDatasetsRequest) ListInstructionsRequest(com.google.cloud.datalabeling.v1beta1.ListInstructionsRequest) ListInstructionsPagedResponse(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListInstructionsPagedResponse) InputConfig(com.google.cloud.datalabeling.v1beta1.InputConfig) ImportDataRequest(com.google.cloud.datalabeling.v1beta1.ImportDataRequest) ImportDataOperationResponse(com.google.cloud.datalabeling.v1beta1.ImportDataOperationResponse) Before(org.junit.Before)

Example 17 with DataLabelingServiceClient

use of com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient in project java-datalabeling by googleapis.

the class LabelVideoIT method tearDown.

@After
public void tearDown() {
    System.setOut(null);
    // Delete the created dataset.
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create()) {
        dataLabelingServiceClient.deleteDataset(dataset.getName());
        dataLabelingServiceClient.deleteInstruction(instruction.getName());
        dataLabelingServiceClient.deleteAnnotationSpecSet(annotationSpecSet.getName());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) IOException(java.io.IOException) After(org.junit.After)

Example 18 with DataLabelingServiceClient

use of com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient in project java-datalabeling by googleapis.

the class CreateAnnotationSpecSet method createAnnotationSpecSet.

// Create an annotation spec set.
static void createAnnotationSpecSet(String projectId) throws IOException {
    // String projectId = "YOUR_PROJECT_ID";
    Map<String, String> annotationLabels = new HashMap<>();
    annotationLabels.put("label_1", "label_1_description");
    annotationLabels.put("label_2", "label_2_description");
    // [END datalabeling_create_annotation_spec_set_beta]
    String endpoint = System.getenv("DATALABELING_ENDPOINT");
    if (endpoint == null) {
        endpoint = DataLabelingServiceSettings.getDefaultEndpoint();
    }
    // [START datalabeling_create_annotation_spec_set_beta]
    DataLabelingServiceSettings settings = DataLabelingServiceSettings.newBuilder().setEndpoint(endpoint).build();
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create(settings)) {
        ProjectName projectName = ProjectName.of(projectId);
        List<AnnotationSpec> annotationSpecs = new ArrayList<>();
        for (Entry<String, String> entry : annotationLabels.entrySet()) {
            AnnotationSpec annotationSpec = AnnotationSpec.newBuilder().setDisplayName(entry.getKey()).setDescription(entry.getValue()).build();
            annotationSpecs.add(annotationSpec);
        }
        AnnotationSpecSet annotationSpecSet = AnnotationSpecSet.newBuilder().setDisplayName("YOUR_ANNOTATION_SPEC_SET_DISPLAY_NAME").setDescription("YOUR_DESCRIPTION").addAllAnnotationSpecs(annotationSpecs).build();
        CreateAnnotationSpecSetRequest request = CreateAnnotationSpecSetRequest.newBuilder().setAnnotationSpecSet(annotationSpecSet).setParent(projectName.toString()).build();
        AnnotationSpecSet result = dataLabelingServiceClient.createAnnotationSpecSet(request);
        System.out.format("Name: %s\n", result.getName());
        System.out.format("DisplayName: %s\n", result.getDisplayName());
        System.out.format("Description: %s\n", result.getDescription());
        System.out.format("Annotation Count: %d\n", result.getAnnotationSpecsCount());
        for (AnnotationSpec annotationSpec : result.getAnnotationSpecsList()) {
            System.out.format("\tDisplayName: %s\n", annotationSpec.getDisplayName());
            System.out.format("\tDescription: %s\n\n", annotationSpec.getDescription());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) HashMap(java.util.HashMap) ProjectName(com.google.cloud.datalabeling.v1beta1.ProjectName) AnnotationSpecSet(com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AnnotationSpec(com.google.cloud.datalabeling.v1beta1.AnnotationSpec) DataLabelingServiceSettings(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings) CreateAnnotationSpecSetRequest(com.google.cloud.datalabeling.v1beta1.CreateAnnotationSpecSetRequest)

Example 19 with DataLabelingServiceClient

use of com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient in project java-datalabeling by googleapis.

the class CreateInstruction method createInstruction.

// Create a instruction for a dataset.
static void createInstruction(String projectId, String pdfUri) throws IOException {
    // String projectId = "YOUR_PROJECT_ID";
    // String pdfUri = "gs://YOUR_BUCKET_ID/path_to_pdf_or_csv";
    // [END datalabeling_create_instruction_beta]
    String endpoint = System.getenv("DATALABELING_ENDPOINT");
    if (endpoint == null) {
        endpoint = DataLabelingServiceSettings.getDefaultEndpoint();
    }
    // [START datalabeling_create_instruction_beta]
    DataLabelingServiceSettings settings = DataLabelingServiceSettings.newBuilder().setEndpoint(endpoint).build();
    try (DataLabelingServiceClient dataLabelingServiceClient = DataLabelingServiceClient.create(settings)) {
        ProjectName projectName = ProjectName.of(projectId);
        // There are two types of instructions: CSV (CsvInstruction) or PDF (PdfInstruction)
        PdfInstruction pdfInstruction = PdfInstruction.newBuilder().setGcsFileUri(pdfUri).build();
        Instruction instruction = Instruction.newBuilder().setDisplayName("YOUR_INSTRUCTION_DISPLAY_NAME").setDescription("YOUR_DESCRIPTION").setDataType(// DataTypes: AUDIO, IMAGE, VIDEO, TEXT
        DataType.IMAGE).setPdfInstruction(// .setCsvInstruction() or .setPdfInstruction()
        pdfInstruction).build();
        CreateInstructionRequest createInstructionRequest = CreateInstructionRequest.newBuilder().setInstruction(instruction).setParent(projectName.toString()).build();
        OperationFuture<Instruction, CreateInstructionMetadata> operation = dataLabelingServiceClient.createInstructionAsync(createInstructionRequest);
        Instruction result = operation.get();
        System.out.format("Name: %s\n", result.getName());
        System.out.format("DisplayName: %s\n", result.getDisplayName());
        System.out.format("Description: %s\n", result.getDescription());
        System.out.format("GCS SOURCE URI: %s\n", result.getPdfInstruction().getGcsFileUri());
    } catch (IOException | InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
}
Also used : DataLabelingServiceClient(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient) ProjectName(com.google.cloud.datalabeling.v1beta1.ProjectName) CreateInstructionRequest(com.google.cloud.datalabeling.v1beta1.CreateInstructionRequest) IOException(java.io.IOException) Instruction(com.google.cloud.datalabeling.v1beta1.Instruction) PdfInstruction(com.google.cloud.datalabeling.v1beta1.PdfInstruction) PdfInstruction(com.google.cloud.datalabeling.v1beta1.PdfInstruction) CreateInstructionMetadata(com.google.cloud.datalabeling.v1beta1.CreateInstructionMetadata) DataLabelingServiceSettings(com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

DataLabelingServiceClient (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient)19 IOException (java.io.IOException)19 ProjectName (com.google.cloud.datalabeling.v1beta1.ProjectName)11 DataLabelingServiceSettings (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceSettings)8 Dataset (com.google.cloud.datalabeling.v1beta1.Dataset)7 After (org.junit.After)7 ListDatasetsPagedResponse (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListDatasetsPagedResponse)6 ListDatasetsRequest (com.google.cloud.datalabeling.v1beta1.ListDatasetsRequest)6 ExecutionException (java.util.concurrent.ExecutionException)6 AnnotationSpecSet (com.google.cloud.datalabeling.v1beta1.AnnotationSpecSet)5 Instruction (com.google.cloud.datalabeling.v1beta1.Instruction)5 ListAnnotationSpecSetsPagedResponse (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListAnnotationSpecSetsPagedResponse)4 ListInstructionsPagedResponse (com.google.cloud.datalabeling.v1beta1.DataLabelingServiceClient.ListInstructionsPagedResponse)4 ListAnnotationSpecSetsRequest (com.google.cloud.datalabeling.v1beta1.ListAnnotationSpecSetsRequest)4 ListInstructionsRequest (com.google.cloud.datalabeling.v1beta1.ListInstructionsRequest)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 PrintStream (java.io.PrintStream)4 Before (org.junit.Before)4 AnnotatedDataset (com.google.cloud.datalabeling.v1beta1.AnnotatedDataset)3 GcsSource (com.google.cloud.datalabeling.v1beta1.GcsSource)3