Skip to content

Commit 7461899

Browse files
committed
Test push
1 parent a977aec commit 7461899

File tree

1 file changed

+347
-0
lines changed
  • vision/automl/src/main/java/com/google/cloud/vision/samples/automl

1 file changed

+347
-0
lines changed
Lines changed: 347 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,347 @@
1+
/*
2+
* Copyright 2018 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.google.cloud.vision.samples.automl;
18+
19+
// Imports the Google Cloud client library
20+
import com.google.cloud.automl.v1beta1.AutoMlClient;
21+
import com.google.cloud.automl.v1beta1.ClassificationProto.ClassificationType;
22+
import com.google.cloud.automl.v1beta1.Dataset;
23+
import com.google.cloud.automl.v1beta1.DatasetName;
24+
import com.google.cloud.automl.v1beta1.GcsDestination;
25+
import com.google.cloud.automl.v1beta1.GcsSource;
26+
import com.google.cloud.automl.v1beta1.ImageClassificationDatasetMetadata;
27+
import com.google.cloud.automl.v1beta1.InputConfig;
28+
import com.google.cloud.automl.v1beta1.ListDatasetsRequest;
29+
import com.google.cloud.automl.v1beta1.LocationName;
30+
31+
import com.google.cloud.automl.v1beta1.OutputConfig;
32+
import com.google.protobuf.Empty;
33+
import java.io.IOException;
34+
import java.io.PrintStream;
35+
36+
import net.sourceforge.argparse4j.ArgumentParsers;
37+
import net.sourceforge.argparse4j.inf.ArgumentParser;
38+
import net.sourceforge.argparse4j.inf.ArgumentParserException;
39+
import net.sourceforge.argparse4j.inf.Namespace;
40+
import net.sourceforge.argparse4j.inf.Subparser;
41+
import net.sourceforge.argparse4j.inf.Subparsers;
42+
43+
/**
44+
* Google Cloud AutoML Vision API sample application. Example usage: mvn package exec:java
45+
* -Dexec.mainClass ='com.google.cloud.vision.samples.automl.DatasetAPI' -Dexec.args='create_dataset
46+
* test_dataset'
47+
*/
48+
public class DatasetApi {
49+
50+
// [START automl_vision_create_dataset]
51+
52+
/**
53+
* Demonstrates using the AutoML client to create a dataset
54+
*
55+
* @param projectId the Google Cloud Project ID.
56+
* @param computeRegion the Region name. (e.g., "us-central1")
57+
* @param datasetName the name of the dataset to be created.
58+
* @param multiLabel the type of classification problem. Set to FALSE by default.
59+
* @throws IOException on Input/Output errors.
60+
*/
61+
public static void createDataset(
62+
String projectId, String computeRegion, String datasetName, Boolean multiLabel)
63+
throws IOException {
64+
// Instantiates a client
65+
AutoMlClient client = AutoMlClient.create();
66+
67+
// Resource representing the Google Cloud Platform location
68+
LocationName projectLocation = LocationName.of(projectId, computeRegion);
69+
70+
// Classification type assigned based on multiLabel value.
71+
ClassificationType classificationType =
72+
multiLabel ? ClassificationType.MULTILABEL : ClassificationType.MULTICLASS;
73+
74+
// Specify the image classification type for the dataset.
75+
ImageClassificationDatasetMetadata imageClassificationDatasetMetadata =
76+
ImageClassificationDatasetMetadata.newBuilder()
77+
.setClassificationType(classificationType)
78+
.build();
79+
80+
// Create a dataset with dataset name and set the dataset metadata.
81+
Dataset myDataset =
82+
Dataset.newBuilder()
83+
.setDisplayName(datasetName)
84+
.setImageClassificationDatasetMetadata(imageClassificationDatasetMetadata)
85+
.build();
86+
87+
// Create dataset with the dataset metadata in the region.
88+
Dataset dataset = client.createDataset(projectLocation, myDataset);
89+
90+
// Display the dataset information
91+
System.out.println(String.format("Dataset name: %s", dataset.getName()));
92+
System.out.println(
93+
String.format(
94+
"Dataset id: %s",
95+
dataset.getName().split("/")[dataset.getName().split("/").length - 1]));
96+
System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName()));
97+
System.out.println("Image classification dataset specification:");
98+
System.out.print(String.format("\t%s", dataset.getImageClassificationDatasetMetadata()));
99+
System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount()));
100+
System.out.println("Dataset create time:");
101+
System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds()));
102+
System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos()));
103+
}
104+
// [END automl_vision_create_dataset]
105+
106+
// [START automl_vision_listdatasets]
107+
/**
108+
* Demonstrates using the AutoML client to list all datasets.
109+
*
110+
* @param projectId - Id of the project.
111+
* @param computeRegion - Region name.
112+
* @param filter - Filter expression.
113+
* @throws IOException on Input/Output errors.
114+
*/
115+
public static void listDatasets(String projectId, String computeRegion, String filter)
116+
throws IOException {
117+
// Instantiates a client
118+
AutoMlClient client = AutoMlClient.create();
119+
120+
// A resource that represents Google Cloud Platform location.
121+
LocationName projectLocation = LocationName.of(projectId, computeRegion);
122+
123+
// Build the List datasets request
124+
ListDatasetsRequest request =
125+
ListDatasetsRequest.newBuilder()
126+
.setParent(projectLocation.toString())
127+
.setFilter(filter)
128+
.build();
129+
130+
// List all the datasets available in the region by applying the filter.
131+
System.out.println("List of datasets:");
132+
for (Dataset dataset : client.listDatasets(request).iterateAll()) {
133+
// Display the dataset information
134+
System.out.println(String.format("\nDataset name: %s", dataset.getName()));
135+
System.out.println(
136+
String.format(
137+
"Dataset id: %s",
138+
dataset.getName().split("/")[dataset.getName().split("/").length - 1]));
139+
System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName()));
140+
System.out.println("Image classification dataset specification:");
141+
System.out.print(String.format("\t%s", dataset.getImageClassificationDatasetMetadata()));
142+
System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount()));
143+
System.out.println("Dataset create time:");
144+
System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds()));
145+
System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos()));
146+
}
147+
}
148+
// [END automl_vision_listdatasets]
149+
150+
// [START automl_vision_getdataset]
151+
/**
152+
* Demonstrates using the AutoML client to get a dataset by ID.
153+
*
154+
* @param projectId - Id of the project.
155+
* @param computeRegion - Region name.
156+
* @param datasetId - Id of the dataset.
157+
* @throws IOException on Input/Output errors.
158+
*/
159+
public static void getDataset(String projectId, String computeRegion, String datasetId)
160+
throws IOException {
161+
// Instantiates a client
162+
AutoMlClient client = AutoMlClient.create();
163+
164+
// Get the complete path of the dataset.
165+
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
166+
167+
// Get all the information about a given dataset.
168+
Dataset dataset = client.getDataset(datasetFullId);
169+
170+
// Display the dataset information.
171+
System.out.println(String.format("Dataset name: %s", dataset.getName()));
172+
System.out.println(
173+
String.format(
174+
"Dataset id: %s",
175+
dataset.getName().split("/")[dataset.getName().split("/").length - 1]));
176+
System.out.println(String.format("Dataset display name: %s", dataset.getDisplayName()));
177+
System.out.println("Image classification dataset specification:");
178+
System.out.print(String.format("\t%s", dataset.getImageClassificationDatasetMetadata()));
179+
System.out.println(String.format("Dataset example count: %d", dataset.getExampleCount()));
180+
System.out.println("Dataset create time:");
181+
System.out.println(String.format("\tseconds: %s", dataset.getCreateTime().getSeconds()));
182+
System.out.println(String.format("\tnanos: %s", dataset.getCreateTime().getNanos()));
183+
}
184+
// [END automl_vision_getdataset]
185+
186+
// [START automl_vision_importdata]
187+
/**
188+
* Demonstrates using the AutoML client to import labeled images.
189+
*
190+
* @param projectId - Id of the project.
191+
* @param computeRegion - Region name.
192+
* @param datasetId - Id of the dataset to which the training data will be imported.
193+
* @param path - Google Cloud Storage URIs. Target files must be in AutoML vision CSV format.
194+
* @throws Exception on AutoML Client errors
195+
*/
196+
public static void importData(
197+
String projectId, String computeRegion, String datasetId, String path) throws Exception {
198+
// Instantiates a client
199+
AutoMlClient client = AutoMlClient.create();
200+
201+
// Get the complete path of the dataset.
202+
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
203+
204+
GcsSource.Builder gcsSource = GcsSource.newBuilder();
205+
206+
// Get multiple training data files to be imported
207+
String[] inputUris = path.split(",");
208+
for (String inputUri : inputUris) {
209+
gcsSource.addInputUris(inputUri);
210+
}
211+
212+
// Import data
213+
InputConfig inputConfig = InputConfig.newBuilder().setGcsSource(gcsSource).build();
214+
System.out.println("Processing import...");
215+
Empty response = client.importDataAsync(datasetFullId.toString(), inputConfig).get();
216+
System.out.println(String.format("Dataset imported. %s", response));
217+
}
218+
// [END automl_vision_importdata]
219+
220+
// [START automl_vision_exportdata]
221+
/**
222+
* Demonstrates using the AutoML client to export a dataset to a Google Cloud Storage bucket.
223+
*
224+
* @param projectId - Id of the project.
225+
* @param computeRegion - Region name.
226+
* @param datasetId - Id of the dataset.
227+
* @param gcsUri - Destination URI (Google Cloud Storage)
228+
* @throws Exception on AutoML Client errors
229+
*/
230+
public static void exportData(
231+
String projectId, String computeRegion, String datasetId, String gcsUri) throws Exception {
232+
// Instantiates a client
233+
AutoMlClient client = AutoMlClient.create();
234+
235+
// Get the complete path of the dataset.
236+
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
237+
238+
// Set the output URI
239+
GcsDestination gcsDestination = GcsDestination.newBuilder().setOutputUriPrefix(gcsUri).build();
240+
241+
// Export the dataset to the output URI.
242+
OutputConfig outputConfig = OutputConfig.newBuilder().setGcsDestination(gcsDestination).build();
243+
System.out.println("Processing export...");
244+
245+
Empty response = client.exportDataAsync(datasetFullId, outputConfig).get();
246+
System.out.println(String.format("Dataset exported. %s", response));
247+
}
248+
// [END automl_vision_exportdata]
249+
250+
// [START automl_vision_deletedataset]
251+
/**
252+
* Delete a dataset.
253+
*
254+
* @param projectId - Id of the project.
255+
* @param computeRegion - Region name.
256+
* @param datasetId - Id of the dataset.
257+
* @throws Exception on AutoML Client errors
258+
*/
259+
public static void deleteDataset(String projectId, String computeRegion, String datasetId)
260+
throws Exception {
261+
// Instantiates a client
262+
AutoMlClient client = AutoMlClient.create();
263+
264+
// Get the complete path of the dataset.
265+
DatasetName datasetFullId = DatasetName.of(projectId, computeRegion, datasetId);
266+
267+
// Delete a dataset.
268+
Empty response = client.deleteDatasetAsync(datasetFullId).get();
269+
270+
System.out.println(String.format("Dataset deleted. %s", response));
271+
}
272+
// [END automl_vision_deleteDataset]
273+
274+
public static void main(String[] args) throws Exception {
275+
DatasetApi datasetApi = new DatasetApi();
276+
datasetApi.argsHelper(args, System.out);
277+
}
278+
279+
public static void argsHelper(String[] args, PrintStream out) throws Exception {
280+
ArgumentParser parser =
281+
ArgumentParsers.newFor("DatasetAPI")
282+
.build()
283+
.defaultHelp(true)
284+
.description("Dataset API operations.");
285+
Subparsers subparsers = parser.addSubparsers().dest("command");
286+
287+
Subparser createDatasetParser = subparsers.addParser("create_dataset");
288+
createDatasetParser.addArgument("datasetName");
289+
createDatasetParser
290+
.addArgument("multiLabel")
291+
.nargs("?")
292+
.type(Boolean.class)
293+
.choices(Boolean.FALSE, Boolean.TRUE)
294+
.setDefault(Boolean.FALSE);
295+
296+
Subparser listDatasetsParser = subparsers.addParser("list_datasets");
297+
listDatasetsParser
298+
.addArgument("filter")
299+
.nargs("?")
300+
.setDefault("imageClassificationDatasetMetadata:*");
301+
302+
Subparser getDatasetParser = subparsers.addParser("get_dataset");
303+
getDatasetParser.addArgument("datasetId");
304+
305+
Subparser importDataParser = subparsers.addParser("import_data");
306+
importDataParser.addArgument("datasetId");
307+
importDataParser.addArgument("path");
308+
309+
Subparser exportDataParser = subparsers.addParser("export_data");
310+
exportDataParser.addArgument("datasetId");
311+
exportDataParser.addArgument("gcsUri");
312+
313+
Subparser deleteDatasetParser = subparsers.addParser("delete_dataset");
314+
deleteDatasetParser.addArgument("datasetId");
315+
316+
String projectId = System.getenv("PROJECT_ID");
317+
String computeRegion = System.getenv("REGION_NAME");
318+
319+
Namespace ns = null;
320+
try {
321+
ns = parser.parseArgs(args);
322+
323+
if (ns.get("command").equals("create_dataset")) {
324+
createDataset(
325+
projectId, computeRegion, ns.getString("datasetName"), ns.getBoolean("multiLabel"));
326+
}
327+
if (ns.get("command").equals("list_datasets")) {
328+
listDatasets(projectId, computeRegion, ns.getString("filter"));
329+
}
330+
if (ns.get("command").equals("get_dataset")) {
331+
getDataset(projectId, computeRegion, ns.getString("datasetId"));
332+
}
333+
if (ns.get("command").equals("import_data")) {
334+
importData(projectId, computeRegion, ns.getString("datasetId"), ns.getString("path"));
335+
}
336+
if (ns.get("command").equals("export_data")) {
337+
exportData(projectId, computeRegion, ns.getString("datasetId"), ns.getString("gcsUri"));
338+
}
339+
if (ns.get("command").equals("delete_dataset")) {
340+
deleteDataset(projectId, computeRegion, ns.getString("datasetId"));
341+
}
342+
343+
} catch (ArgumentParserException e) {
344+
parser.handleError(e);
345+
}
346+
}
347+
}

0 commit comments

Comments
 (0)