Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add beta samples for Video Streaming #1353

Merged
merged 5 commits into from
Mar 22, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion video/beta/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@
</properties>

<dependencies>
<!-- [START video_java_dependencies_beta] -->
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-video-intelligence</artifactId>
<version>0.80.0-beta</version>
<version>0.84.0-beta</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>1.65.0</version>
</dependency>
<!-- [END video_java_dependencies_beta] -->

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> 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]
Original file line number Diff line number Diff line change
@@ -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<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> 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]
Original file line number Diff line number Diff line change
@@ -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<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> 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]
Loading