diff --git a/video/src/main/java/com/example/video/StreamingAnnotationToStorage.java b/video/src/main/java/com/example/video/StreamingAnnotationToStorage.java new file mode 100644 index 00000000000..5598717e3af --- /dev/null +++ b/video/src/main/java/com/example/video/StreamingAnnotationToStorage.java @@ -0,0 +1,96 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.video; + +// [START video_streaming_annotation_to_storage_beta] +import com.google.api.gax.rpc.BidiStream; +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest; +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse; +import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature; +import com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig; +import com.google.cloud.videointelligence.v1p3beta1.StreamingStorageConfig; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +public class StreamingAnnotationToStorage { + + // Perform streaming video detection for explicit content + static void streamingAnnotationToStorage(String filePath, String gcsUri) { + // String filePath = "path_to_your_video_file"; + // String gcsUri = "gs://BUCKET_ID"; + + try (StreamingVideoIntelligenceServiceClient client = + StreamingVideoIntelligenceServiceClient.create()) { + + Path path = Paths.get(filePath); + byte[] data = Files.readAllBytes(path); + // Set the chunk size to 5MB (recommended less than 10MB). + int chunkSize = 5 * 1024 * 1024; + int numChunks = (int) Math.ceil((double) data.length / chunkSize); + + StreamingStorageConfig streamingStorageConfig = StreamingStorageConfig.newBuilder() + .setEnableStorageAnnotationResult(true) + .setAnnotationResultStorageDirectory(gcsUri) + .build(); + + StreamingLabelDetectionConfig labelConfig = StreamingLabelDetectionConfig.newBuilder() + .setStationaryCamera(false) + .build(); + + StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder() + .setFeature(StreamingFeature.STREAMING_LABEL_DETECTION) + .setLabelDetectionConfig(labelConfig) + .setStorageConfig(streamingStorageConfig) + .build(); + + BidiStream call = + client.streamingAnnotateVideoCallable().call(); + + // The first request must **only** contain the audio configuration: + call.send( + StreamingAnnotateVideoRequest.newBuilder() + .setVideoConfig(streamingVideoConfig) + .build()); + + // Subsequent requests must **only** contain the audio data. + // Send the requests in chunks + for (int i = 0; i < numChunks; i++) { + call.send( + StreamingAnnotateVideoRequest.newBuilder() + .setInputContent(ByteString.copyFrom( + Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize))) + .build()); + } + + // Tell the service you are done sending data + call.closeSend(); + + for (StreamingAnnotateVideoResponse response : call) { + System.out.format("Storage Uri: %s\n", response.getAnnotationResultsUri()); + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END video_streaming_annotation_to_storage_beta] \ No newline at end of file diff --git a/video/src/main/java/com/example/video/StreamingExplicitContentDetection.java b/video/src/main/java/com/example/video/StreamingExplicitContentDetection.java new file mode 100644 index 00000000000..5227bf1292b --- /dev/null +++ b/video/src/main/java/com/example/video/StreamingExplicitContentDetection.java @@ -0,0 +1,100 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.video; + +// [START video_streaming_explicit_content_detection_beta] +import com.google.api.gax.rpc.BidiStream; +import com.google.cloud.videointelligence.v1p3beta1.ExplicitContentFrame; +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest; +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse; +import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature; +import com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +class StreamingExplicitContentDetection { + + // Perform streaming video detection for explicit content + static void streamingExplicitContentDetection(String filePath) { + // String filePath = "path_to_your_video_file"; + + try (StreamingVideoIntelligenceServiceClient client = + StreamingVideoIntelligenceServiceClient.create()) { + + Path path = Paths.get(filePath); + byte[] data = Files.readAllBytes(path); + // Set the chunk size to 5MB (recommended less than 10MB). + int chunkSize = 5 * 1024 * 1024; + int numChunks = (int) Math.ceil((double) data.length / chunkSize); + + StreamingLabelDetectionConfig labelConfig = StreamingLabelDetectionConfig.newBuilder() + .setStationaryCamera(false) + .build(); + + StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder() + .setFeature(StreamingFeature.STREAMING_EXPLICIT_CONTENT_DETECTION) + .setLabelDetectionConfig(labelConfig) + .build(); + + BidiStream call = + client.streamingAnnotateVideoCallable().call(); + + // The first request must **only** contain the audio configuration: + call.send( + StreamingAnnotateVideoRequest.newBuilder() + .setVideoConfig(streamingVideoConfig) + .build()); + + // Subsequent requests must **only** contain the audio data. + // Send the requests in chunks + for (int i = 0; i < numChunks; i++) { + call.send( + StreamingAnnotateVideoRequest.newBuilder() + .setInputContent(ByteString.copyFrom( + Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize))) + .build()); + } + + // Tell the service you are done sending data + call.closeSend(); + + for (StreamingAnnotateVideoResponse response : call) { + StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults(); + + for (ExplicitContentFrame frame : + annotationResults.getExplicitAnnotation().getFramesList()) { + + double offset = frame.getTimeOffset().getSeconds() + + frame.getTimeOffset().getNanos() / 1e9; + + System.out.format("Offset: %f\n", offset); + System.out.format("\tPornography: %s", frame.getPornographyLikelihood()); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END video_streaming_explicit_content_detection_beta] diff --git a/video/src/main/java/com/example/video/StreamingLabelDetection.java b/video/src/main/java/com/example/video/StreamingLabelDetection.java new file mode 100644 index 00000000000..99afce5c5e9 --- /dev/null +++ b/video/src/main/java/com/example/video/StreamingLabelDetection.java @@ -0,0 +1,103 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.video; + +// [START video_streaming_label_detection_beta] +import com.google.api.gax.rpc.BidiStream; +import com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation; +import com.google.cloud.videointelligence.v1p3beta1.LabelFrame; +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest; +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse; +import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature; +import com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +class StreamingLabelDetection { + + // Perform streaming video label detection + static void streamingLabelDetection(String filePath) { + // String filePath = "path_to_your_video_file"; + + try (StreamingVideoIntelligenceServiceClient client = + StreamingVideoIntelligenceServiceClient.create()) { + + Path path = Paths.get(filePath); + byte[] data = Files.readAllBytes(path); + // Set the chunk size to 5MB (recommended less than 10MB). + int chunkSize = 5 * 1024 * 1024; + int numChunks = (int) Math.ceil((double) data.length / chunkSize); + + StreamingLabelDetectionConfig labelConfig = StreamingLabelDetectionConfig.newBuilder() + .setStationaryCamera(false) + .build(); + + StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder() + .setFeature(StreamingFeature.STREAMING_LABEL_DETECTION) + .setLabelDetectionConfig(labelConfig) + .build(); + + BidiStream call = + client.streamingAnnotateVideoCallable().call(); + + // The first request must **only** contain the audio configuration: + call.send( + StreamingAnnotateVideoRequest.newBuilder() + .setVideoConfig(streamingVideoConfig) + .build()); + + // Subsequent requests must **only** contain the audio data. + // Send the requests in chunks + for (int i = 0; i < numChunks; i++) { + call.send( + StreamingAnnotateVideoRequest.newBuilder() + .setInputContent(ByteString.copyFrom( + Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize))) + .build()); + } + + // Tell the service you are done sending data + call.closeSend(); + + for (StreamingAnnotateVideoResponse response : call) { + StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults(); + + for (LabelAnnotation annotation : annotationResults.getLabelAnnotationsList()) { + String entity = annotation.getEntity().getDescription(); + + // There is only one frame per annotation + LabelFrame labelFrame = annotation.getFrames(0); + double offset = labelFrame.getTimeOffset().getSeconds() + + labelFrame.getTimeOffset().getNanos() / 1e9; + float confidence = labelFrame.getConfidence(); + + System.out.format("%fs: %s (%f)\n", offset, entity, confidence); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END video_streaming_label_detection_beta] \ No newline at end of file diff --git a/video/src/main/java/com/example/video/StreamingObjectTracking.java b/video/src/main/java/com/example/video/StreamingObjectTracking.java new file mode 100644 index 00000000000..2c2e0403b92 --- /dev/null +++ b/video/src/main/java/com/example/video/StreamingObjectTracking.java @@ -0,0 +1,112 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.video; + +// [START video_streaming_object_tracking_beta] +import com.google.api.gax.rpc.BidiStream; +import com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingAnnotation; +import com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingFrame; +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest; +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse; +import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature; +import com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +class StreamingObjectTracking { + + // Perform streaming video object tracking + static void streamingObjectTracking(String filePath) { + // String filePath = "path_to_your_video_file"; + + try (StreamingVideoIntelligenceServiceClient client = + StreamingVideoIntelligenceServiceClient.create()) { + + Path path = Paths.get(filePath); + byte[] data = Files.readAllBytes(path); + // Set the chunk size to 5MB (recommended less than 10MB). + int chunkSize = 5 * 1024 * 1024; + int numChunks = (int) Math.ceil((double) data.length / chunkSize); + + StreamingLabelDetectionConfig labelConfig = StreamingLabelDetectionConfig.newBuilder() + .setStationaryCamera(false) + .build(); + + StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder() + .setFeature(StreamingFeature.STREAMING_OBJECT_TRACKING) + .setLabelDetectionConfig(labelConfig) + .build(); + + BidiStream call = + client.streamingAnnotateVideoCallable().call(); + + // The first request must **only** contain the audio configuration: + call.send( + StreamingAnnotateVideoRequest.newBuilder() + .setVideoConfig(streamingVideoConfig) + .build()); + + // Subsequent requests must **only** contain the audio data. + // Send the requests in chunks + for (int i = 0; i < numChunks; i++) { + call.send( + StreamingAnnotateVideoRequest.newBuilder() + .setInputContent(ByteString.copyFrom( + Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize))) + .build()); + } + + // Tell the service you are done sending data + call.closeSend(); + + for (StreamingAnnotateVideoResponse response : call) { + StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults(); + + for (ObjectTrackingAnnotation objectAnnotations : + annotationResults.getObjectAnnotationsList()) { + + String entity = objectAnnotations.getEntity().getDescription(); + float confidence = objectAnnotations.getConfidence(); + long trackId = objectAnnotations.getTrackId(); + System.out.format("%s: %f (ID: %d)\n", entity, confidence, trackId); + + // In streaming, there is always one frame. + ObjectTrackingFrame frame = objectAnnotations.getFrames(0); + double offset = frame.getTimeOffset().getSeconds() + + frame.getTimeOffset().getNanos() / 1e9; + System.out.format("Offset: %f\n", offset); + + System.out.println("Bounding Box:"); + System.out.format("\tLeft: %f\n", frame.getNormalizedBoundingBox().getLeft()); + System.out.format("\tTop: %f\n", frame.getNormalizedBoundingBox().getTop()); + System.out.format("\tRight: %f\n", frame.getNormalizedBoundingBox().getRight()); + System.out.format("\tBottom: %f\n", frame.getNormalizedBoundingBox().getBottom()); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END video_streaming_object_tracking_beta] diff --git a/video/src/main/java/com/example/video/StreamingShotChangeDetection.java b/video/src/main/java/com/example/video/StreamingShotChangeDetection.java new file mode 100644 index 00000000000..374964d0ca1 --- /dev/null +++ b/video/src/main/java/com/example/video/StreamingShotChangeDetection.java @@ -0,0 +1,100 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.video; + +// [START video_streaming_shot_change_detection_beta] +import com.google.api.gax.rpc.BidiStream; +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest; +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse; +import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature; +import com.google.cloud.videointelligence.v1p3beta1.StreamingLabelDetectionConfig; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; +import com.google.cloud.videointelligence.v1p3beta1.VideoSegment; +import com.google.protobuf.ByteString; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Arrays; + +class StreamingShotChangeDetection { + + // Perform streaming video detection for shot changes + static void streamingShotChangeDetection(String filePath) { + // String filePath = "path_to_your_video_file"; + + try (StreamingVideoIntelligenceServiceClient client = + StreamingVideoIntelligenceServiceClient.create()) { + + Path path = Paths.get(filePath); + byte[] data = Files.readAllBytes(path); + // Set the chunk size to 5MB (recommended less than 10MB). + int chunkSize = 5 * 1024 * 1024; + int numChunks = (int) Math.ceil((double) data.length / chunkSize); + + StreamingLabelDetectionConfig labelConfig = StreamingLabelDetectionConfig.newBuilder() + .setStationaryCamera(false) + .build(); + + StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder() + .setFeature(StreamingFeature.STREAMING_SHOT_CHANGE_DETECTION) + .setLabelDetectionConfig(labelConfig) + .build(); + + BidiStream call = + client.streamingAnnotateVideoCallable().call(); + + // The first request must **only** contain the audio configuration: + call.send( + StreamingAnnotateVideoRequest.newBuilder() + .setVideoConfig(streamingVideoConfig) + .build()); + + // Subsequent requests must **only** contain the audio data. + // Send the requests in chunks + for (int i = 0; i < numChunks; i++) { + call.send( + StreamingAnnotateVideoRequest.newBuilder() + .setInputContent(ByteString.copyFrom( + Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize))) + .build()); + } + + // Tell the service you are done sending data + call.closeSend(); + + for (StreamingAnnotateVideoResponse response : call) { + StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults(); + + for (VideoSegment segment : annotationResults.getShotAnnotationsList()) { + double startTimeOffset = segment.getStartTimeOffset().getSeconds() + + segment.getStartTimeOffset().getNanos() / 1e9; + double endTimeOffset = segment.getEndTimeOffset().getSeconds() + + segment.getEndTimeOffset().getNanos() / 1e9; + + System.out.format("Shot: %fs to %fs\n", startTimeOffset, endTimeOffset); + } + } + } catch (IOException e) { + e.printStackTrace(); + } + } +} +// [END video_streaming_shot_change_detection_beta] + diff --git a/video/src/test/java/com/example/video/StreamingAnnotationToStorageIT.java b/video/src/test/java/com/example/video/StreamingAnnotationToStorageIT.java new file mode 100644 index 00000000000..05698811fd2 --- /dev/null +++ b/video/src/test/java/com/example/video/StreamingAnnotationToStorageIT.java @@ -0,0 +1,85 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.video; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.api.gax.paging.Page; +import com.google.cloud.storage.Blob; +import com.google.cloud.storage.Storage; +import com.google.cloud.storage.Storage.BlobListOption; +import com.google.cloud.storage.StorageOptions; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration (system) tests for {@link StreamingAnnotationToStorage}. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class StreamingAnnotationToStorageIT { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String OUTPUT_PREFIX = "VIDEO_STREAMING_TEST_OUTPUT"; + + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testStreamingAnnotationToStorage() { + String gcsUri = String.format("gs://%s/%s", PROJECT_ID, OUTPUT_PREFIX); + StreamingAnnotationToStorage.streamingAnnotationToStorage("resources/cat.mp4", gcsUri); + String got = bout.toString(); + + assertThat(got).contains(String.format("Storage Uri: %s", gcsUri)); + + Storage storage = StorageOptions.getDefaultInstance().getService(); + + Page blobs = storage.list(PROJECT_ID, BlobListOption.currentDirectory(), + BlobListOption.prefix(OUTPUT_PREFIX + "/")); + + deleteDirectory(storage, blobs); + } + + private void deleteDirectory(Storage storage, Page blobs) { + for (Blob blob : blobs.iterateAll()) { + System.out.println(blob.getName()); + if (!blob.delete()) { + Page subBlobs = storage.list(PROJECT_ID, BlobListOption.currentDirectory(), + BlobListOption.prefix(blob.getName())); + + deleteDirectory(storage, subBlobs); + } + } + } +} diff --git a/video/src/test/java/com/example/video/StreamingExplicitContentDetectionIT.java b/video/src/test/java/com/example/video/StreamingExplicitContentDetectionIT.java new file mode 100644 index 00000000000..324414db3f7 --- /dev/null +++ b/video/src/test/java/com/example/video/StreamingExplicitContentDetectionIT.java @@ -0,0 +1,57 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.video; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration (system) tests for {@link StreamingExplicitContentDetection}. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class StreamingExplicitContentDetectionIT { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testStreamingExplicitContent() { + StreamingExplicitContentDetection.streamingExplicitContentDetection("resources/cat.mp4"); + String got = bout.toString(); + + assertThat(got).contains("UNLIKELY"); + } +} diff --git a/video/src/test/java/com/example/video/StreamingLabelDetectionIT.java b/video/src/test/java/com/example/video/StreamingLabelDetectionIT.java new file mode 100644 index 00000000000..70f7affd126 --- /dev/null +++ b/video/src/test/java/com/example/video/StreamingLabelDetectionIT.java @@ -0,0 +1,57 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.video; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration (system) tests for {@link StreamingLabelDetection}. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class StreamingLabelDetectionIT { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testStreamingLabelDetection() { + StreamingLabelDetection.streamingLabelDetection("resources/cat.mp4"); + String got = bout.toString(); + + assertThat(got).contains("cat"); + } +} diff --git a/video/src/test/java/com/example/video/StreamingObjectTrackingIT.java b/video/src/test/java/com/example/video/StreamingObjectTrackingIT.java new file mode 100644 index 00000000000..c1097e1403d --- /dev/null +++ b/video/src/test/java/com/example/video/StreamingObjectTrackingIT.java @@ -0,0 +1,61 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.video; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration (system) tests for {@link StreamingObjectTracking}. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class StreamingObjectTrackingIT { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testStreamingObjectTracking() { + StreamingObjectTracking.streamingObjectTracking("resources/cat.mp4"); + String got = bout.toString(); + + assertThat(got).contains("cat"); + assertThat(got).contains("Left: 0.1"); + assertThat(got).contains("Top: 0.2"); + assertThat(got).contains("Right: 0.7"); + assertThat(got).contains("Bottom: 0.8"); + } +} diff --git a/video/src/test/java/com/example/video/StreamingShotChangeDetectionIT.java b/video/src/test/java/com/example/video/StreamingShotChangeDetectionIT.java new file mode 100644 index 00000000000..00b0787418c --- /dev/null +++ b/video/src/test/java/com/example/video/StreamingShotChangeDetectionIT.java @@ -0,0 +1,58 @@ +/* + * Copyright 2019 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.example.video; + +import static com.google.common.truth.Truth.assertThat; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** + * Integration (system) tests for {@link StreamingShotChangeDetection}. + */ +@RunWith(JUnit4.class) +@SuppressWarnings("checkstyle:abbreviationaswordinname") +public class StreamingShotChangeDetectionIT { + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() { + System.setOut(null); + } + + @Test + public void testStreamingShotChangeDetection() { + StreamingShotChangeDetection.streamingShotChangeDetection("resources/cat.mp4"); + String got = bout.toString(); + + assertThat(got).contains("Shot: 0.0"); + assertThat(got).contains("to 14.8"); + } +}