|
| 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; |
| 18 | + |
| 19 | +import static com.google.common.truth.Truth.assertThat; |
| 20 | + |
| 21 | +import java.io.ByteArrayOutputStream; |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.PrintStream; |
| 24 | + |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.runner.RunWith; |
| 29 | +import org.junit.runners.JUnit4; |
| 30 | + |
| 31 | +/** Tests for vision "Detect" sample. */ |
| 32 | +@RunWith(JUnit4.class) |
| 33 | +@SuppressWarnings("checkstyle:abbreviationaswordinname") |
| 34 | +public class DetectIT { |
| 35 | + private ByteArrayOutputStream bout; |
| 36 | + private PrintStream out; |
| 37 | + private Detect app; |
| 38 | + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); |
| 39 | + private static final String BUCKET = PROJECT_ID; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() throws IOException { |
| 43 | + bout = new ByteArrayOutputStream(); |
| 44 | + out = new PrintStream(bout); |
| 45 | + System.setOut(out); |
| 46 | + app = new Detect(); |
| 47 | + } |
| 48 | + |
| 49 | + @After |
| 50 | + public void tearDown() { |
| 51 | + System.setOut(null); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testFaces() throws Exception { |
| 56 | + // Act |
| 57 | + String[] args = {"faces", "./resources/face_no_surprise.jpg"}; |
| 58 | + Detect.argsHelper(args, out); |
| 59 | + |
| 60 | + // Assert |
| 61 | + String got = bout.toString(); |
| 62 | + assertThat(got).contains("anger: POSSIBLE"); |
| 63 | + assertThat(got).contains("joy: POSSIBLE"); |
| 64 | + assertThat(got).contains("surprise: LIKELY"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testFacesGcs() throws Exception { |
| 69 | + // Act |
| 70 | + String[] args = {"faces", "gs://" + BUCKET + "/vision/face_no_surprise.jpg"}; |
| 71 | + Detect.argsHelper(args, out); |
| 72 | + |
| 73 | + // Assert |
| 74 | + String got = bout.toString(); |
| 75 | + assertThat(got).contains("anger: POSSIBLE"); |
| 76 | + assertThat(got).contains("joy: POSSIBLE"); |
| 77 | + assertThat(got).contains("surprise: LIKELY"); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testLabels() throws Exception { |
| 82 | + // Act |
| 83 | + String[] args = {"labels", "./resources/wakeupcat.jpg"}; |
| 84 | + Detect.argsHelper(args, out); |
| 85 | + |
| 86 | + // Assert |
| 87 | + String got = bout.toString(); |
| 88 | + assertThat(got).contains("whiskers"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testLabelsGcs() throws Exception { |
| 93 | + // Act |
| 94 | + String[] args = {"labels", "gs://" + BUCKET + "/vision/wakeupcat.jpg"}; |
| 95 | + Detect.argsHelper(args, out); |
| 96 | + |
| 97 | + // Assert |
| 98 | + String got = bout.toString(); |
| 99 | + assertThat(got).contains("whiskers"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testLandmarks() throws Exception { |
| 104 | + // Act |
| 105 | + String[] args = {"landmarks", "./resources/landmark.jpg"}; |
| 106 | + Detect.argsHelper(args, out); |
| 107 | + |
| 108 | + // Assert |
| 109 | + String got = bout.toString(); |
| 110 | + assertThat(got).contains("Palace of Fine Arts"); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void testLandmarksGcs() throws Exception { |
| 115 | + // Act |
| 116 | + String[] args = {"landmarks", "gs://" + BUCKET + "/vision/landmark.jpg"}; |
| 117 | + Detect.argsHelper(args, out); |
| 118 | + |
| 119 | + // Assert |
| 120 | + String got = bout.toString(); |
| 121 | + assertThat(got).contains("Palace of Fine Arts"); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void testLandmarksUrl() throws Exception { |
| 126 | + // Act |
| 127 | + String uri = "https://storage-download.googleapis.com/" |
| 128 | + + BUCKET + "/vision/landmark.jpg"; |
| 129 | + String[] args = {"landmarks", uri}; |
| 130 | + Detect.argsHelper(args, out); |
| 131 | + |
| 132 | + // Assert |
| 133 | + String got = bout.toString(); |
| 134 | + assertThat(got).contains("Palace of Fine Arts"); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testLogos() throws Exception { |
| 139 | + // Act |
| 140 | + String[] args = {"logos", "./resources/logos.png"}; |
| 141 | + Detect.argsHelper(args, out); |
| 142 | + |
| 143 | + // Assert |
| 144 | + String got = bout.toString(); |
| 145 | + assertThat(got).contains("Google"); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testLogosGcs() throws Exception { |
| 150 | + // Act |
| 151 | + String[] args = {"logos", "gs://" + BUCKET + "/vision/logos.png"}; |
| 152 | + Detect.argsHelper(args, out); |
| 153 | + |
| 154 | + // Assert |
| 155 | + String got = bout.toString(); |
| 156 | + assertThat(got).contains("Google"); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void testText() throws Exception { |
| 161 | + // Act |
| 162 | + String[] args = {"text", "./resources/text.jpg"}; |
| 163 | + Detect.argsHelper(args, out); |
| 164 | + |
| 165 | + // Assert |
| 166 | + String got = bout.toString(); |
| 167 | + assertThat(got).contains("37%"); |
| 168 | + } |
| 169 | + |
| 170 | + @Test |
| 171 | + public void testTextGcs() throws Exception { |
| 172 | + // Act |
| 173 | + String[] args = {"text", "gs://" + BUCKET + "/vision/text.jpg"}; |
| 174 | + Detect.argsHelper(args, out); |
| 175 | + |
| 176 | + // Assert |
| 177 | + String got = bout.toString(); |
| 178 | + assertThat(got).contains("37%"); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + public void testSafeSearch() throws Exception { |
| 183 | + // Act |
| 184 | + String[] args = {"safe-search", "./resources/wakeupcat.jpg"}; |
| 185 | + Detect.argsHelper(args, out); |
| 186 | + |
| 187 | + // Assert |
| 188 | + String got = bout.toString(); |
| 189 | + assertThat(got).contains("adult: VERY_UNLIKELY"); |
| 190 | + assertThat(got).contains("racy: UNLIKELY"); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + public void testSafeSearchGcs() throws Exception { |
| 195 | + // Act |
| 196 | + String[] args = {"safe-search", "gs://" + BUCKET + "/vision/wakeupcat.jpg"}; |
| 197 | + Detect.argsHelper(args, out); |
| 198 | + |
| 199 | + // Assert |
| 200 | + String got = bout.toString(); |
| 201 | + assertThat(got).contains("adult: VERY_UNLIKELY"); |
| 202 | + assertThat(got).contains("racy: UNLIKELY"); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void testProperties() throws Exception { |
| 207 | + // Act |
| 208 | + String[] args = {"properties", "./resources/landmark.jpg"}; |
| 209 | + Detect.argsHelper(args, out); |
| 210 | + |
| 211 | + // Assert |
| 212 | + String got = bout.toString(); |
| 213 | + assertThat(got).contains("fraction:"); |
| 214 | + assertThat(got).contains("r:"); |
| 215 | + assertThat(got).contains("g:"); |
| 216 | + assertThat(got).contains("b:"); |
| 217 | + } |
| 218 | + |
| 219 | + @Test |
| 220 | + public void testPropertiesGcs() throws Exception { |
| 221 | + // Act |
| 222 | + String[] args = {"properties", "gs://" + BUCKET + "/vision/landmark.jpg"}; |
| 223 | + Detect.argsHelper(args, out); |
| 224 | + |
| 225 | + // Assert |
| 226 | + String got = bout.toString(); |
| 227 | + assertThat(got).contains("fraction:"); |
| 228 | + assertThat(got).contains("r:"); |
| 229 | + assertThat(got).contains("g:"); |
| 230 | + assertThat(got).contains("b:"); |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + public void detectWebAnnotations() throws Exception { |
| 235 | + // Act |
| 236 | + String[] args = {"web", "./resources/landmark.jpg"}; |
| 237 | + Detect.argsHelper(args, out); |
| 238 | + |
| 239 | + // Assert |
| 240 | + String got = bout.toString(); |
| 241 | + assertThat(got).contains("Palace of Fine Arts Theatre"); |
| 242 | + assertThat(got).contains("Best guess label: palace of fine arts"); |
| 243 | + } |
| 244 | + |
| 245 | + @Test |
| 246 | + public void detectWebAnnotationsGcs() throws Exception { |
| 247 | + // Act |
| 248 | + String[] args = {"web", "gs://" + BUCKET + "/vision/landmark.jpg"}; |
| 249 | + Detect.argsHelper(args, out); |
| 250 | + |
| 251 | + // Assert |
| 252 | + String got = bout.toString(); |
| 253 | + assertThat(got).contains("Palace of Fine Arts Theatre"); |
| 254 | + assertThat(got).contains("Best guess label: palace of fine arts"); |
| 255 | + } |
| 256 | + |
| 257 | + @Test |
| 258 | + public void testDetectWebEntities() throws Exception { |
| 259 | + // Act |
| 260 | + String[] args = {"web-entities", "./resources/city.jpg"}; |
| 261 | + Detect.argsHelper(args, out); |
| 262 | + |
| 263 | + // Assert |
| 264 | + String got = bout.toString(); |
| 265 | + assertThat(got).doesNotContain("Zepra"); |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + public void testDetectWebEntitiesGcs() throws Exception { |
| 270 | + // Act |
| 271 | + String[] args = {"web-entities", "gs://" + BUCKET + "/vision/landmark.jpg"}; |
| 272 | + Detect.argsHelper(args, out); |
| 273 | + |
| 274 | + String got = bout.toString(); |
| 275 | + assertThat(got).contains("Description: Palace of Fine Arts Theatre"); |
| 276 | + } |
| 277 | + |
| 278 | + @Test |
| 279 | + public void testDetectWebEntitiesIncludeGeoResults() throws Exception { |
| 280 | + // Act |
| 281 | + String[] args = {"web-entities-include-geo", "./resources/city.jpg"}; |
| 282 | + Detect.argsHelper(args, out); |
| 283 | + |
| 284 | + // Assert |
| 285 | + String got = bout.toString(); |
| 286 | + assertThat(got).contains("Zepra"); |
| 287 | + } |
| 288 | + |
| 289 | + @Test |
| 290 | + public void testDetectWebEntitiesIncludeGeoResultsGcs() throws Exception { |
| 291 | + // Act |
| 292 | + String[] args = {"web-entities-include-geo", "gs://" + BUCKET + "/vision/landmark.jpg"}; |
| 293 | + Detect.argsHelper(args, out); |
| 294 | + |
| 295 | + String got = bout.toString(); |
| 296 | + assertThat(got).contains("Description: Palace of Fine Arts Theatre"); |
| 297 | + } |
| 298 | + |
| 299 | + @Test |
| 300 | + public void testCropHints() throws Exception { |
| 301 | + // Act |
| 302 | + String[] args = {"crop", "./resources/wakeupcat.jpg"}; |
| 303 | + Detect.argsHelper(args, out); |
| 304 | + |
| 305 | + // Assert |
| 306 | + String got = bout.toString(); |
| 307 | + assertThat(got).contains("vertices {"); |
| 308 | + assertThat(got).contains("x: 599"); |
| 309 | + assertThat(got).contains("y: 475"); |
| 310 | + } |
| 311 | + |
| 312 | + @Test |
| 313 | + public void testCropHintsGcs() throws Exception { |
| 314 | + // Act |
| 315 | + String[] args = {"crop", "gs://" + BUCKET + "/vision/wakeupcat.jpg"}; |
| 316 | + Detect.argsHelper(args, out); |
| 317 | + |
| 318 | + // Assert |
| 319 | + String got = bout.toString(); |
| 320 | + assertThat(got).contains("vertices {"); |
| 321 | + assertThat(got).contains("x: 599"); |
| 322 | + assertThat(got).contains("y: 475"); |
| 323 | + } |
| 324 | + |
| 325 | + @Test |
| 326 | + public void testDocumentText() throws Exception { |
| 327 | + // Act |
| 328 | + String[] args = {"fulltext", "./resources/text.jpg"}; |
| 329 | + Detect.argsHelper(args, out); |
| 330 | + |
| 331 | + // Assert |
| 332 | + String got = bout.toString(); |
| 333 | + assertThat(got).contains("After preparation is complete, the "); |
| 334 | + assertThat(got).contains("37%"); |
| 335 | + assertThat(got).contains("Word text: class (confidence:"); |
| 336 | + } |
| 337 | + |
| 338 | + @Test |
| 339 | + public void testDocumentTextGcs() throws Exception { |
| 340 | + // Act |
| 341 | + String[] args = {"fulltext", "gs://" + BUCKET + "/vision/text.jpg"}; |
| 342 | + Detect.argsHelper(args, out); |
| 343 | + |
| 344 | + // Assert |
| 345 | + String got = bout.toString(); |
| 346 | + assertThat(got).contains("After preparation is complete, the "); |
| 347 | + assertThat(got).contains("37%"); |
| 348 | + assertThat(got).contains("Word text: class (confidence:"); |
| 349 | + } |
| 350 | +} |
0 commit comments