|
| 1 | +/* |
| 2 | + * Copyright 2020 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 | + |
| 18 | +package com.example.video; |
| 19 | + |
| 20 | +import static com.google.common.truth.Truth.assertThat; |
| 21 | + |
| 22 | +import com.google.cloud.videointelligence.v1.TextAnnotation; |
| 23 | +import com.google.cloud.videointelligence.v1.VideoAnnotationResults; |
| 24 | +import java.io.ByteArrayOutputStream; |
| 25 | +import java.io.PrintStream; |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.List; |
| 28 | +import org.junit.After; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Rule; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +public class DetectTextTest { |
| 34 | + static final String SPEECH_GCS_LOCATION = |
| 35 | + "gs://java-docs-samples-testing/video/googlework_short.mp4"; |
| 36 | + private static final List<String> POSSIBLE_TEXTS = |
| 37 | + Arrays.asList( |
| 38 | + "Google", |
| 39 | + "SUR", |
| 40 | + "SUR", |
| 41 | + "ROTO", |
| 42 | + "Vice President", |
| 43 | + "58oo9", |
| 44 | + "LONDRES", |
| 45 | + "OMAR", |
| 46 | + "PARIS", |
| 47 | + "METRO", |
| 48 | + "RUE", |
| 49 | + "CARLO"); |
| 50 | + @Rule public Retry retry = new Retry(3); |
| 51 | + private ByteArrayOutputStream bout; |
| 52 | + private PrintStream out; |
| 53 | + private PrintStream originalPrintStream; |
| 54 | + |
| 55 | + @Before |
| 56 | + public void setUp() { |
| 57 | + bout = new ByteArrayOutputStream(); |
| 58 | + out = new PrintStream(bout); |
| 59 | + originalPrintStream = System.out; |
| 60 | + System.setOut(out); |
| 61 | + } |
| 62 | + |
| 63 | + @After |
| 64 | + public void tearDown() { |
| 65 | + System.out.flush(); |
| 66 | + System.setOut(originalPrintStream); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testTextDetection() throws Exception { |
| 71 | + VideoAnnotationResults result = TextDetection.detectText("resources/googlework_short.mp4"); |
| 72 | + |
| 73 | + boolean textExists = false; |
| 74 | + for (TextAnnotation textAnnotation : result.getTextAnnotationsList()) { |
| 75 | + for (String possibleText : POSSIBLE_TEXTS) { |
| 76 | + if (textAnnotation.getText().toUpperCase().contains(possibleText.toUpperCase())) { |
| 77 | + textExists = true; |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + assertThat(textExists).isTrue(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testTextDetectionGcs() throws Exception { |
| 88 | + VideoAnnotationResults result = TextDetection.detectTextGcs(SPEECH_GCS_LOCATION); |
| 89 | + |
| 90 | + boolean textExists = false; |
| 91 | + for (TextAnnotation textAnnotation : result.getTextAnnotationsList()) { |
| 92 | + for (String possibleText : POSSIBLE_TEXTS) { |
| 93 | + if (textAnnotation.getText().toUpperCase().contains(possibleText.toUpperCase())) { |
| 94 | + textExists = true; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + assertThat(textExists).isTrue(); |
| 101 | + } |
| 102 | +} |
0 commit comments