|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 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 | +package beta.video; |
| 18 | + |
| 19 | +// [START video_streaming_automl_action_recognition_beta] |
| 20 | + |
| 21 | +import com.google.api.gax.rpc.BidiStream; |
| 22 | +import com.google.cloud.videointelligence.v1p3beta1.LabelAnnotation; |
| 23 | +import com.google.cloud.videointelligence.v1p3beta1.LabelFrame; |
| 24 | +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest; |
| 25 | +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse; |
| 26 | +import com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlActionRecognitionConfig; |
| 27 | +import com.google.cloud.videointelligence.v1p3beta1.StreamingFeature; |
| 28 | +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoAnnotationResults; |
| 29 | +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoConfig; |
| 30 | +import com.google.cloud.videointelligence.v1p3beta1.StreamingVideoIntelligenceServiceClient; |
| 31 | +import com.google.protobuf.ByteString; |
| 32 | +import io.grpc.StatusRuntimeException; |
| 33 | +import java.io.IOException; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.nio.file.Paths; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.concurrent.TimeoutException; |
| 39 | + |
| 40 | +class StreamingAutoMlActionRecognition { |
| 41 | + |
| 42 | + // Perform streaming video action recognition |
| 43 | + static void streamingAutoMlActionRecognition(String filePath, String projectId, String modelId) |
| 44 | + throws IOException, TimeoutException, StatusRuntimeException { |
| 45 | + |
| 46 | + try (StreamingVideoIntelligenceServiceClient client = |
| 47 | + StreamingVideoIntelligenceServiceClient.create()) { |
| 48 | + |
| 49 | + Path path = Paths.get(filePath); |
| 50 | + byte[] data = Files.readAllBytes(path); |
| 51 | + // Set the chunk size to 5MB (recommended less than 10MB). |
| 52 | + int chunkSize = 5 * 1024 * 1024; |
| 53 | + int numChunks = (int) Math.ceil((double) data.length / chunkSize); |
| 54 | + |
| 55 | + String modelPath = |
| 56 | + String.format("projects/%s/locations/us-central1/models/%s", projectId, modelId); |
| 57 | + |
| 58 | + System.out.println(modelPath); |
| 59 | + |
| 60 | + StreamingAutomlActionRecognitionConfig streamingAutomlActionRecognitionConfig = |
| 61 | + StreamingAutomlActionRecognitionConfig.newBuilder().setModelName(modelPath).build(); |
| 62 | + |
| 63 | + StreamingVideoConfig streamingVideoConfig = |
| 64 | + StreamingVideoConfig.newBuilder() |
| 65 | + .setFeature(StreamingFeature.STREAMING_AUTOML_ACTION_RECOGNITION) |
| 66 | + .setAutomlActionRecognitionConfig(streamingAutomlActionRecognitionConfig) |
| 67 | + .build(); |
| 68 | + |
| 69 | + BidiStream<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> call = |
| 70 | + client.streamingAnnotateVideoCallable().call(); |
| 71 | + |
| 72 | + // The first request must **only** contain the video configuration: |
| 73 | + call.send( |
| 74 | + StreamingAnnotateVideoRequest.newBuilder().setVideoConfig(streamingVideoConfig).build()); |
| 75 | + |
| 76 | + // Subsequent requests must **only** contain the video data. |
| 77 | + // Send the requests in chunks |
| 78 | + for (int i = 0; i < numChunks; i++) { |
| 79 | + call.send( |
| 80 | + StreamingAnnotateVideoRequest.newBuilder() |
| 81 | + .setInputContent( |
| 82 | + ByteString.copyFrom( |
| 83 | + Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize))) |
| 84 | + .build()); |
| 85 | + } |
| 86 | + |
| 87 | + // Tell the service you are done sending data |
| 88 | + call.closeSend(); |
| 89 | + |
| 90 | + for (StreamingAnnotateVideoResponse response : call) { |
| 91 | + if (response.hasError()) { |
| 92 | + System.out.println(response.getError().getMessage()); |
| 93 | + break; |
| 94 | + } |
| 95 | + |
| 96 | + StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults(); |
| 97 | + |
| 98 | + for (LabelAnnotation annotation : annotationResults.getLabelAnnotationsList()) { |
| 99 | + String entity = annotation.getEntity().getDescription(); |
| 100 | + |
| 101 | + // There is only one frame per annotation |
| 102 | + LabelFrame labelFrame = annotation.getFrames(0); |
| 103 | + double offset = |
| 104 | + labelFrame.getTimeOffset().getSeconds() + labelFrame.getTimeOffset().getNanos() / 1e9; |
| 105 | + float confidence = labelFrame.getConfidence(); |
| 106 | + |
| 107 | + System.out.format("At %fs segment: %s (%f)\n", offset, entity, confidence); |
| 108 | + } |
| 109 | + } |
| 110 | + System.out.println("Video streamed successfully."); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +// [END video_streaming_automl_action_recognition_beta] |
0 commit comments