-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add beta samples for Video Streaming (#1353)
* Add beta samples for Video Streaming * Update pom.xml * Update pom.xml
- Loading branch information
1 parent
9cec18a
commit fc15397
Showing
11 changed files
with
837 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
video/beta/src/main/java/com/example/video/StreamingAnnotationToStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
100 changes: 100 additions & 0 deletions
100
video/beta/src/main/java/com/example/video/StreamingExplicitContentDetection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
103 changes: 103 additions & 0 deletions
103
video/beta/src/main/java/com/example/video/StreamingLabelDetection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Oops, something went wrong.