-
Notifications
You must be signed in to change notification settings - Fork 509
/
ImageAnalysisQuickstart.java
332 lines (288 loc) · 16.2 KB
/
ImageAnalysisQuickstart.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* This Quickstart for the Azure Cognitive Services Computer Vision API shows how to analyze
* an image and recognize text for both a local and remote (URL) image.
*
* Analyzing an image includes:
* - Displaying image captions and confidence values
* - Displaying image category names and confidence values
* - Displaying image tags and confidence values
* - Displaying any faces found in the image and their bounding boxes
* - Displaying whether any adult or racy content was detected and the confidence values
* - Displaying the image color scheme
* - Displaying any celebrities detected in the image and their bounding boxes
* - Displaying any landmarks detected in the image and their bounding boxes
* - Displaying whether an image is a clip art or line drawing type
*/
// <snippet_imports_and_vars>
// <snippet_imports>
import com.microsoft.azure.cognitiveservices.vision.computervision.*;
import com.microsoft.azure.cognitiveservices.vision.computervision.implementation.ComputerVisionImpl;
import com.microsoft.azure.cognitiveservices.vision.computervision.models.*;
import java.io.*;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
// </snippet_imports>
// <snippet_classdef_1>
public class ImageAnalysisQuickstart {
// </snippet_classdef_1>
// <snippet_creds>
static String key = "PASTE_YOUR_COMPUTER_VISION_KEY_HERE";
static String endpoint = "PASTE_YOUR_COMPUTER_VISION_ENDPOINT_HERE";
// </snippet_creds>
// </snippet_imports_and_vars>
// <snippet_main>
public static void main(String[] args) {
System.out.println("\nAzure Cognitive Services Computer Vision - Java Quickstart Sample");
// Create an authenticated Computer Vision client.
ComputerVisionClient compVisClient = Authenticate(key, endpoint);
// Analyze local and remote images
AnalyzeLocalImage(compVisClient);
}
// </snippet_main>
// <snippet_auth>
public static ComputerVisionClient Authenticate(String key, String endpoint){
return ComputerVisionManager.authenticate(key).withEndpoint(endpoint);
}
// </snippet_auth>
// <snippet_analyzelocal_refs>
public static void AnalyzeLocalImage(ComputerVisionClient compVisClient) {
/*
* Analyze a local image:
*
* Set a string variable equal to the path of a local image. The image path
* below is a relative path.
*/
String pathToLocalImage = "src\\main\\resources\\myImage.png";
// </snippet_analyzelocal_refs>
// <snippet_analyzelocal_features>
// This list defines the features to be extracted from the image.
List<VisualFeatureTypes> featuresToExtractFromLocalImage = new ArrayList<>();
featuresToExtractFromLocalImage.add(VisualFeatureTypes.DESCRIPTION);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.CATEGORIES);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.TAGS);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.FACES);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.OBJECTS);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.BRANDS);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.ADULT);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.COLOR);
featuresToExtractFromLocalImage.add(VisualFeatureTypes.IMAGE_TYPE);
// </snippet_analyzelocal_features>
System.out.println("\nAnalyzing local image ...");
// <snippet_analyzelocal_analyze>
try {
// Need a byte array for analyzing a local image.
File rawImage = new File(pathToLocalImage);
byte[] imageByteArray = Files.readAllBytes(rawImage.toPath());
// Call the Computer Vision service and tell it to analyze the loaded image.
ImageAnalysis analysis = compVisClient.computerVision().analyzeImageInStream().withImage(imageByteArray)
.withVisualFeatures(featuresToExtractFromLocalImage).execute();
// </snippet_analyzelocal_analyze>
// <snippet_analyzelocal_captions>
// Display image captions and confidence values.
System.out.println("\nCaptions: ");
for (ImageCaption caption : analysis.description().captions()) {
System.out.printf("\'%s\' with confidence %f\n", caption.text(), caption.confidence());
}
// </snippet_analyzelocal_captions>
// <snippet_analyzelocal_category>
// Display image category names and confidence values.
System.out.println("\nCategories: ");
for (Category category : analysis.categories()) {
System.out.printf("\'%s\' with confidence %f\n", category.name(), category.score());
}
// </snippet_analyzelocal_category>
// <snippet_analyzelocal_tags>
// Display image tags and confidence values.
System.out.println("\nTags: ");
for (ImageTag tag : analysis.tags()) {
System.out.printf("\'%s\' with confidence %f\n", tag.name(), tag.confidence());
}
// </snippet_analyzelocal_tags>
// <snippet_analyzelocal_faces>
// Display any faces found in the image and their location.
System.out.println("\nFaces: ");
for (FaceDescription face : analysis.faces()) {
System.out.printf("\'%s\' of age %d at location (%d, %d), (%d, %d)\n", face.gender(), face.age(),
face.faceRectangle().left(), face.faceRectangle().top(),
face.faceRectangle().left() + face.faceRectangle().width(),
face.faceRectangle().top() + face.faceRectangle().height());
}
// </snippet_analyzelocal_faces>
// <snippet_analyzelocal_objects>
// Display any objects found in the image.
System.out.println("\nObjects: ");
for ( DetectedObject object : analysis.objects()) {
System.out.printf("Object \'%s\' detected at location (%d, %d)\n", object.objectProperty(),
object.rectangle().x(), object.rectangle().y());
}
// </snippet_analyzelocal_objects>
// <snippet_analyzelocal_brands>
// Display any brands found in the image.
System.out.println("\nBrands: ");
for ( DetectedBrand brand : analysis.brands()) {
System.out.printf("Brand \'%s\' detected at location (%d, %d)\n", brand.name(),
brand.rectangle().x(), brand.rectangle().y());
}
// </snippet_analyzelocal_brands>
// <snippet_analyzelocal_adult>
// Display whether any adult/racy/gory content was detected and the confidence
// values.
System.out.println("\nAdult: ");
System.out.printf("Is adult content: %b with confidence %f\n", analysis.adult().isAdultContent(),
analysis.adult().adultScore());
System.out.printf("Has racy content: %b with confidence %f\n", analysis.adult().isRacyContent(),
analysis.adult().racyScore());
System.out.printf("Has gory content: %b with confidence %f\n", analysis.adult().isGoryContent(),
analysis.adult().goreScore());
// </snippet_analyzelocal_adult>
// <snippet_analyzelocal_colors>
// Display the image color scheme.
System.out.println("\nColor scheme: ");
System.out.println("Is black and white: " + analysis.color().isBWImg());
System.out.println("Accent color: " + analysis.color().accentColor());
System.out.println("Dominant background color: " + analysis.color().dominantColorBackground());
System.out.println("Dominant foreground color: " + analysis.color().dominantColorForeground());
System.out.println("Dominant colors: " + String.join(", ", analysis.color().dominantColors()));
// </snippet_analyzelocal_colors>
// <snippet_analyzelocal_celebrities>
// Display any celebrities detected in the image and their locations.
System.out.println("\nCelebrities: ");
for (Category category : analysis.categories()) {
if (category.detail() != null && category.detail().celebrities() != null) {
for (CelebritiesModel celeb : category.detail().celebrities()) {
System.out.printf("\'%s\' with confidence %f at location (%d, %d), (%d, %d)\n", celeb.name(),
celeb.confidence(), celeb.faceRectangle().left(), celeb.faceRectangle().top(),
celeb.faceRectangle().left() + celeb.faceRectangle().width(),
celeb.faceRectangle().top() + celeb.faceRectangle().height());
}
}
}
// </snippet_analyzelocal_celebrities>
// <snippet_analyzelocal_landmarks>
// Display any landmarks detected in the image and their locations.
System.out.println("\nLandmarks: ");
for (Category category : analysis.categories()) {
if (category.detail() != null && category.detail().landmarks() != null) {
for (LandmarksModel landmark : category.detail().landmarks()) {
System.out.printf("\'%s\' with confidence %f\n", landmark.name(), landmark.confidence());
}
}
}
// </snippet_analyzelocal_landmarks>
// <snippet_imagetype>
// Display what type of clip art or line drawing the image is.
System.out.println("\nImage type:");
System.out.println("Clip art type: " + analysis.imageType().clipArtType());
System.out.println("Line drawing type: " + analysis.imageType().lineDrawingType());
// </snippet_imagetype>
// <snippet_analyze_catch>
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
// </snippet_analyze_catch>
// END - Analyze a local image.
// <snippet_analyzeurl>
public static void AnalyzeRemoteImage(ComputerVisionClient compVisClient) {
/*
* Analyze an image from a URL:
*
* Set a string variable equal to the path of a remote image.
*/
// <snippet_urlimage>
String pathToRemoteImage = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/ComputerVision/Images/faces.jpg";
// </snippet_urlimage>
// <snippet_features_remote>
// This list defines the features to be extracted from the image.
List<VisualFeatureTypes> featuresToExtractFromRemoteImage = new ArrayList<>();
featuresToExtractFromRemoteImage.add(VisualFeatureTypes.DESCRIPTION);
featuresToExtractFromRemoteImage.add(VisualFeatureTypes.CATEGORIES);
featuresToExtractFromRemoteImage.add(VisualFeatureTypes.TAGS);
featuresToExtractFromRemoteImage.add(VisualFeatureTypes.FACES);
featuresToExtractFromRemoteImage.add(VisualFeatureTypes.ADULT);
featuresToExtractFromRemoteImage.add(VisualFeatureTypes.COLOR);
featuresToExtractFromRemoteImage.add(VisualFeatureTypes.IMAGE_TYPE);
// </snippet_features_remote>
System.out.println("\n\nAnalyzing an image from a URL ...");
try {
// <snippet_analyze>
// Call the Computer Vision service and tell it to analyze the loaded image.
ImageAnalysis analysis = compVisClient.computerVision().analyzeImage().withUrl(pathToRemoteImage)
.withVisualFeatures(featuresToExtractFromRemoteImage).execute();
// Display image captions and confidence values.
System.out.println("\nCaptions: ");
for (ImageCaption caption : analysis.description().captions()) {
System.out.printf("\'%s\' with confidence %f\n", caption.text(), caption.confidence());
}
// Display image category names and confidence values.
System.out.println("\nCategories: ");
for (Category category : analysis.categories()) {
System.out.printf("\'%s\' with confidence %f\n", category.name(), category.score());
}
// Display image tags and confidence values.
System.out.println("\nTags: ");
for (ImageTag tag : analysis.tags()) {
System.out.printf("\'%s\' with confidence %f\n", tag.name(), tag.confidence());
}
// Display any faces found in the image and their location.
System.out.println("\nFaces: ");
for (FaceDescription face : analysis.faces()) {
System.out.printf("\'%s\' of age %d at location (%d, %d), (%d, %d)\n", face.gender(), face.age(),
face.faceRectangle().left(), face.faceRectangle().top(),
face.faceRectangle().left() + face.faceRectangle().width(),
face.faceRectangle().top() + face.faceRectangle().height());
}
// Display whether any adult or racy content was detected and the confidence
// values.
System.out.println("\nAdult: ");
System.out.printf("Is adult content: %b with confidence %f\n", analysis.adult().isAdultContent(),
analysis.adult().adultScore());
System.out.printf("Has racy content: %b with confidence %f\n", analysis.adult().isRacyContent(),
analysis.adult().racyScore());
// Display the image color scheme.
System.out.println("\nColor scheme: ");
System.out.println("Is black and white: " + analysis.color().isBWImg());
System.out.println("Accent color: " + analysis.color().accentColor());
System.out.println("Dominant background color: " + analysis.color().dominantColorBackground());
System.out.println("Dominant foreground color: " + analysis.color().dominantColorForeground());
System.out.println("Dominant colors: " + String.join(", ", analysis.color().dominantColors()));
// Display any celebrities detected in the image and their locations.
System.out.println("\nCelebrities: ");
for (Category category : analysis.categories()) {
if (category.detail() != null && category.detail().celebrities() != null) {
for (CelebritiesModel celeb : category.detail().celebrities()) {
System.out.printf("\'%s\' with confidence %f at location (%d, %d), (%d, %d)\n", celeb.name(),
celeb.confidence(), celeb.faceRectangle().left(), celeb.faceRectangle().top(),
celeb.faceRectangle().left() + celeb.faceRectangle().width(),
celeb.faceRectangle().top() + celeb.faceRectangle().height());
}
}
}
// Display any landmarks detected in the image and their locations.
System.out.println("\nLandmarks: ");
for (Category category : analysis.categories()) {
if (category.detail() != null && category.detail().landmarks() != null) {
for (LandmarksModel landmark : category.detail().landmarks()) {
System.out.printf("\'%s\' with confidence %f\n", landmark.name(), landmark.confidence());
}
}
}
// Display what type of clip art or line drawing the image is.
System.out.println("\nImage type:");
System.out.println("Clip art type: " + analysis.imageType().clipArtType());
System.out.println("Line drawing type: " + analysis.imageType().lineDrawingType());
// </snippet_analyze>
}
catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
// END - Analyze an image from a URL.
// </snippet_analyzeurl>
// <snippet_classdef_2>
}
// </snippet_classdef_2>