-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Add new beta samples for Video Intelligence Streaming with an AutoML … #1568
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1503117
Add new beta samples for Video Intelligence Streaming with an AutoML …
nnegrey b0c0d78
Update StreamingAutoMlClassification.java
nnegrey 8604e26
Merge branch 'master' into video-automl-streaming
nnegrey 165d4ce
Update Track Objects timeout for Java8
nnegrey 101ffc7
Same timeout increase for Java8
nnegrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
117 changes: 117 additions & 0 deletions
117
video/beta/src/main/java/com/example/video/StreamingAutoMlClassification.java
This file contains hidden or 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,117 @@ | ||
/* | ||
* 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_automl_classification_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.StreamingAutomlClassificationConfig; | ||
import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature; | ||
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.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
|
||
class StreamingAutoMlClassification { | ||
|
||
// Perform streaming video classification with an AutoML Model | ||
static void streamingAutoMlClassification(String filePath, String projectId, String modelId) { | ||
// String filePath = "path_to_your_video_file"; | ||
// String projectId = "YOUR_GCP_PROJECT_ID"; | ||
// String modelId = "YOUR_AUTO_ML_CLASSIFICATION_MODEL_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); | ||
|
||
String modelPath = String.format("projects/%s/locations/us-central1/models/%s", | ||
projectId, | ||
modelId); | ||
|
||
System.out.println(modelPath); | ||
|
||
StreamingAutomlClassificationConfig streamingAutomlClassificationConfig = | ||
StreamingAutomlClassificationConfig.newBuilder() | ||
.setModelName(modelPath) | ||
.build(); | ||
|
||
StreamingVideoConfig streamingVideoConfig = StreamingVideoConfig.newBuilder() | ||
.setFeature(StreamingFeature.STREAMING_AUTOML_CLASSIFICATION) | ||
.setAutomlClassificationConfig(streamingAutomlClassificationConfig) | ||
.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) { | ||
if (response.hasError()) { | ||
System.out.println(response.getError().getMessage()); | ||
break; | ||
} | ||
|
||
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 (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
// [END video_streaming_automl_classification_beta] |
This file contains hidden or 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
This file contains hidden or 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
67 changes: 67 additions & 0 deletions
67
video/beta/src/test/java/com/example/video/StreamingAutoMlClassificationIT.java
This file contains hidden or 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,67 @@ | ||
/* | ||
* 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 StreamingAutoMlClassification}. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
@SuppressWarnings("checkstyle:abbreviationaswordinname") | ||
public class StreamingAutoMlClassificationIT { | ||
|
||
private static String PROJECT_ID = "779844219229"; // System.getenv().get("GOOGLE_CLOUD_PROJECT"); | ||
private static String MODEL_ID = "VCN6455760532254228480"; | ||
|
||
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 testStreamingAutoMlClassification() { | ||
StreamingAutoMlClassification.streamingAutoMlClassification( | ||
"resources/cat.mp4", | ||
PROJECT_ID, | ||
MODEL_ID); | ||
String got = bout.toString(); | ||
|
||
assertThat(got).contains("brush_hair"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you really want beta in the tag name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, we started adding it when we had overlap with sample tracker and product teams still wanted to keep beta pages live.