Skip to content

Commit 4159646

Browse files
authored
chore: splitting sample into single files (#3083)
1 parent fa43d94 commit 4159646

39 files changed

+2599
-1107
lines changed

vision/cloud-client/src/main/java/com/example/vision/Detect.java

Lines changed: 4 additions & 899 deletions
Large diffs are not rendered by default.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2017 Google Inc.
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.vision.snippets;
18+
19+
// [START vision_crop_hint_detection]
20+
21+
import com.google.cloud.vision.v1.AnnotateImageRequest;
22+
import com.google.cloud.vision.v1.AnnotateImageResponse;
23+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
24+
import com.google.cloud.vision.v1.CropHint;
25+
import com.google.cloud.vision.v1.CropHintsAnnotation;
26+
import com.google.cloud.vision.v1.Feature;
27+
import com.google.cloud.vision.v1.Image;
28+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
29+
import com.google.protobuf.ByteString;
30+
import java.io.FileInputStream;
31+
import java.io.IOException;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
35+
public class DetectCropHints {
36+
public static void detectCropHints() throws IOException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String filePath = "path/to/your/image/file.jpg";
39+
detectCropHints(filePath);
40+
}
41+
42+
// Suggests a region to crop to for a local file.
43+
public static void detectCropHints(String filePath) throws IOException {
44+
List<AnnotateImageRequest> requests = new ArrayList<>();
45+
46+
ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
47+
48+
Image img = Image.newBuilder().setContent(imgBytes).build();
49+
Feature feat = Feature.newBuilder().setType(Feature.Type.CROP_HINTS).build();
50+
AnnotateImageRequest request =
51+
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
52+
requests.add(request);
53+
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests. After completing all of your requests, call
56+
// the "close" method on the client to safely clean up any remaining background resources.
57+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
58+
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
59+
List<AnnotateImageResponse> responses = response.getResponsesList();
60+
61+
for (AnnotateImageResponse res : responses) {
62+
if (res.hasError()) {
63+
System.out.format("Error: %s%n", res.getError().getMessage());
64+
return;
65+
}
66+
67+
// For full list of available annotations, see http://g.co/cloud/vision/docs
68+
CropHintsAnnotation annotation = res.getCropHintsAnnotation();
69+
for (CropHint hint : annotation.getCropHintsList()) {
70+
System.out.println(hint.getBoundingPoly());
71+
}
72+
}
73+
}
74+
}
75+
}
76+
// [END vision_crop_hint_detection]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2017 Google Inc.
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.vision.snippets;
18+
19+
// [START vision_crop_hint_detection_gcs]
20+
21+
import com.google.cloud.vision.v1.AnnotateImageRequest;
22+
import com.google.cloud.vision.v1.AnnotateImageResponse;
23+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
24+
import com.google.cloud.vision.v1.CropHint;
25+
import com.google.cloud.vision.v1.CropHintsAnnotation;
26+
import com.google.cloud.vision.v1.Feature;
27+
import com.google.cloud.vision.v1.Image;
28+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
29+
import com.google.cloud.vision.v1.ImageSource;
30+
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
public class DetectCropHintsGcs {
35+
36+
public static void detectCropHintsGcs() throws IOException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg";
39+
detectCropHintsGcs(filePath);
40+
}
41+
42+
// Suggests a region to crop to for a remote file on Google Cloud Storage.
43+
public static void detectCropHintsGcs(String gcsPath) throws IOException {
44+
List<AnnotateImageRequest> requests = new ArrayList<>();
45+
46+
ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
47+
Image img = Image.newBuilder().setSource(imgSource).build();
48+
Feature feat = Feature.newBuilder().setType(Feature.Type.CROP_HINTS).build();
49+
AnnotateImageRequest request =
50+
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
51+
requests.add(request);
52+
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests. After completing all of your requests, call
55+
// the "close" method on the client to safely clean up any remaining background resources.
56+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
57+
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
58+
List<AnnotateImageResponse> responses = response.getResponsesList();
59+
60+
for (AnnotateImageResponse res : responses) {
61+
if (res.hasError()) {
62+
System.out.format("Error: %s%n", res.getError().getMessage());
63+
return;
64+
}
65+
66+
// For full list of available annotations, see http://g.co/cloud/vision/docs
67+
CropHintsAnnotation annotation = res.getCropHintsAnnotation();
68+
for (CropHint hint : annotation.getCropHintsList()) {
69+
System.out.println(hint.getBoundingPoly());
70+
}
71+
}
72+
}
73+
}
74+
}
75+
// [END vision_crop_hint_detection_gcs]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2017 Google Inc.
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.vision.snippets;
18+
19+
// [START vision_landmark_detection]
20+
21+
import com.google.cloud.vision.v1.AnnotateImageRequest;
22+
import com.google.cloud.vision.v1.AnnotateImageResponse;
23+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
24+
import com.google.cloud.vision.v1.EntityAnnotation;
25+
import com.google.cloud.vision.v1.Feature;
26+
import com.google.cloud.vision.v1.Image;
27+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
28+
import com.google.cloud.vision.v1.LocationInfo;
29+
import com.google.protobuf.ByteString;
30+
import java.io.FileInputStream;
31+
import java.io.IOException;
32+
import java.util.ArrayList;
33+
import java.util.List;
34+
35+
public class DetectLandmarks {
36+
public static void detectLandmarks() throws IOException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String filePath = "path/to/your/image/file.jpg";
39+
detectLandmarks(filePath);
40+
}
41+
42+
// Detects landmarks in the specified local image.
43+
public static void detectLandmarks(String filePath) throws IOException {
44+
List<AnnotateImageRequest> requests = new ArrayList<>();
45+
ByteString imgBytes = ByteString.readFrom(new FileInputStream(filePath));
46+
47+
Image img = Image.newBuilder().setContent(imgBytes).build();
48+
Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_DETECTION).build();
49+
AnnotateImageRequest request =
50+
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
51+
requests.add(request);
52+
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests. After completing all of your requests, call
55+
// the "close" method on the client to safely clean up any remaining background resources.
56+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
57+
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
58+
List<AnnotateImageResponse> responses = response.getResponsesList();
59+
60+
for (AnnotateImageResponse res : responses) {
61+
if (res.hasError()) {
62+
System.out.format("Error: %s%n", res.getError().getMessage());
63+
return;
64+
}
65+
66+
// For full list of available annotations, see http://g.co/cloud/vision/docs
67+
for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
68+
LocationInfo info = annotation.getLocationsList().listIterator().next();
69+
System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng());
70+
}
71+
}
72+
}
73+
}
74+
}
75+
// [END vision_landmark_detection]
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2017 Google Inc.
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.vision.snippets;
18+
19+
// [START vision_landmark_detection_gcs]
20+
21+
import com.google.cloud.vision.v1.AnnotateImageRequest;
22+
import com.google.cloud.vision.v1.AnnotateImageResponse;
23+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
24+
import com.google.cloud.vision.v1.EntityAnnotation;
25+
import com.google.cloud.vision.v1.Feature;
26+
import com.google.cloud.vision.v1.Image;
27+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
28+
import com.google.cloud.vision.v1.ImageSource;
29+
import com.google.cloud.vision.v1.LocationInfo;
30+
import java.io.IOException;
31+
import java.util.ArrayList;
32+
import java.util.List;
33+
34+
public class DetectLandmarksGcs {
35+
36+
public static void detectLandmarksGcs() throws IOException {
37+
// TODO(developer): Replace these variables before running the sample.
38+
String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg";
39+
detectLandmarksGcs(filePath);
40+
}
41+
42+
// Detects landmarks in the specified remote image on Google Cloud Storage.
43+
public static void detectLandmarksGcs(String gcsPath) throws IOException {
44+
List<AnnotateImageRequest> requests = new ArrayList<>();
45+
46+
ImageSource imgSource = ImageSource.newBuilder().setGcsImageUri(gcsPath).build();
47+
Image img = Image.newBuilder().setSource(imgSource).build();
48+
Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_DETECTION).build();
49+
AnnotateImageRequest request =
50+
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
51+
requests.add(request);
52+
53+
// Initialize client that will be used to send requests. This client only needs to be created
54+
// once, and can be reused for multiple requests. After completing all of your requests, call
55+
// the "close" method on the client to safely clean up any remaining background resources.
56+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
57+
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
58+
List<AnnotateImageResponse> responses = response.getResponsesList();
59+
60+
for (AnnotateImageResponse res : responses) {
61+
if (res.hasError()) {
62+
System.out.format("Error: %s%n", res.getError().getMessage());
63+
return;
64+
}
65+
66+
// For full list of available annotations, see http://g.co/cloud/vision/docs
67+
for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
68+
LocationInfo info = annotation.getLocationsList().listIterator().next();
69+
System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng());
70+
}
71+
}
72+
}
73+
}
74+
}
75+
// [END vision_landmark_detection_gcs]
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2017 Google Inc.
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.vision.snippets;
18+
19+
import com.google.cloud.vision.v1.AnnotateImageRequest;
20+
import com.google.cloud.vision.v1.AnnotateImageResponse;
21+
import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;
22+
import com.google.cloud.vision.v1.EntityAnnotation;
23+
import com.google.cloud.vision.v1.Feature;
24+
import com.google.cloud.vision.v1.Image;
25+
import com.google.cloud.vision.v1.ImageAnnotatorClient;
26+
import com.google.cloud.vision.v1.ImageSource;
27+
import com.google.cloud.vision.v1.LocationInfo;
28+
import java.io.IOException;
29+
import java.util.ArrayList;
30+
import java.util.List;
31+
32+
public class DetectLandmarksUrl {
33+
34+
public static void detectLandmarksUrl() throws IOException {
35+
// TODO(developer): Replace these variables before running the sample.
36+
String filePath = "gs://your-gcs-bucket/path/to/image/file.jpg";
37+
detectLandmarksUrl(filePath);
38+
}
39+
40+
// Detects landmarks in the specified URI.
41+
public static void detectLandmarksUrl(String uri) throws IOException {
42+
List<AnnotateImageRequest> requests = new ArrayList<>();
43+
44+
ImageSource imgSource = ImageSource.newBuilder().setImageUri(uri).build();
45+
Image img = Image.newBuilder().setSource(imgSource).build();
46+
Feature feat = Feature.newBuilder().setType(Feature.Type.LANDMARK_DETECTION).build();
47+
AnnotateImageRequest request =
48+
AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();
49+
requests.add(request);
50+
51+
// Initialize client that will be used to send requests. This client only needs to be created
52+
// once, and can be reused for multiple requests. After completing all of your requests, call
53+
// the "close" method on the client to safely clean up any remaining background resources.
54+
try (ImageAnnotatorClient client = ImageAnnotatorClient.create()) {
55+
BatchAnnotateImagesResponse response = client.batchAnnotateImages(requests);
56+
List<AnnotateImageResponse> responses = response.getResponsesList();
57+
58+
for (AnnotateImageResponse res : responses) {
59+
if (res.hasError()) {
60+
System.out.format("Error: %s%n", res.getError().getMessage());
61+
return;
62+
}
63+
64+
// For full list of available annotations, see http://g.co/cloud/vision/docs
65+
for (EntityAnnotation annotation : res.getLandmarkAnnotationsList()) {
66+
LocationInfo info = annotation.getLocationsList().listIterator().next();
67+
System.out.format("Landmark: %s%n %s%n", annotation.getDescription(), info.getLatLng());
68+
}
69+
}
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)