|
| 1 | +/* |
| 2 | + * Copyright 2021 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_object_tracking_beta] |
| 20 | + |
| 21 | +import com.google.api.gax.rpc.BidiStream; |
| 22 | +import com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingAnnotation; |
| 23 | +import com.google.cloud.videointelligence.v1p3beta1.ObjectTrackingFrame; |
| 24 | +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoRequest; |
| 25 | +import com.google.cloud.videointelligence.v1p3beta1.StreamingAnnotateVideoResponse; |
| 26 | +import com.google.cloud.videointelligence.v1p3beta1.StreamingAutomlObjectTrackingConfig; |
| 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 | + |
| 39 | +class StreamingAutoMlObjectTracking { |
| 40 | + |
| 41 | + public static void main(String[] args) |
| 42 | + throws IOException { |
| 43 | + // TODO(developer): Replace these variables before running the sample. |
| 44 | + String filePath = "YOUR_VIDEO_FILE"; |
| 45 | + String projectId = "YOUR_PROJECT_ID"; |
| 46 | + String modelId = "YOUR_AUTOML_OBJECT_TRACKING_MODEL_ID"; |
| 47 | + streamingAutoMlObjectTracking(filePath, projectId, modelId); |
| 48 | + } |
| 49 | + |
| 50 | + // Perform streaming video object tracking with an AutoML Model |
| 51 | + static void streamingAutoMlObjectTracking(String filePath, String projectId, String modelId) |
| 52 | + throws StatusRuntimeException, IOException { |
| 53 | + |
| 54 | + try (StreamingVideoIntelligenceServiceClient client = |
| 55 | + StreamingVideoIntelligenceServiceClient.create()) { |
| 56 | + |
| 57 | + Path path = Paths.get(filePath); |
| 58 | + byte[] data = Files.readAllBytes(path); |
| 59 | + // Set the chunk size to 5MB (recommended less than 10MB). |
| 60 | + int chunkSize = 5 * 1024 * 1024; |
| 61 | + int numChunks = (int) Math.ceil((double) data.length / chunkSize); |
| 62 | + |
| 63 | + String modelPath = |
| 64 | + String.format("projects/%s/locations/us-central1/models/%s", projectId, modelId); |
| 65 | + |
| 66 | + StreamingAutomlObjectTrackingConfig streamingAutomlObjectTrackingConfig = |
| 67 | + StreamingAutomlObjectTrackingConfig.newBuilder() |
| 68 | + .setModelName(modelPath) |
| 69 | + .build(); |
| 70 | + |
| 71 | + StreamingVideoConfig streamingVideoConfig = |
| 72 | + StreamingVideoConfig.newBuilder() |
| 73 | + .setFeature(StreamingFeature.STREAMING_AUTOML_OBJECT_TRACKING) |
| 74 | + .setAutomlObjectTrackingConfig(streamingAutomlObjectTrackingConfig) |
| 75 | + .build(); |
| 76 | + |
| 77 | + BidiStream<StreamingAnnotateVideoRequest, StreamingAnnotateVideoResponse> call = |
| 78 | + client.streamingAnnotateVideoCallable().call(); |
| 79 | + |
| 80 | + // The first request must **only** contain the audio configuration: |
| 81 | + call.send( |
| 82 | + StreamingAnnotateVideoRequest.newBuilder() |
| 83 | + .setVideoConfig(streamingVideoConfig) |
| 84 | + .build()); |
| 85 | + |
| 86 | + // Subsequent requests must **only** contain the audio data. |
| 87 | + // Send the requests in chunks |
| 88 | + for (int i = 0; i < numChunks; i++) { |
| 89 | + call.send( |
| 90 | + StreamingAnnotateVideoRequest.newBuilder() |
| 91 | + .setInputContent( |
| 92 | + ByteString.copyFrom( |
| 93 | + Arrays.copyOfRange(data, i * chunkSize, i * chunkSize + chunkSize))) |
| 94 | + .build()); |
| 95 | + } |
| 96 | + |
| 97 | + // Tell the service you are done sending data |
| 98 | + call.closeSend(); |
| 99 | + |
| 100 | + for (StreamingAnnotateVideoResponse response : call) { |
| 101 | + StreamingVideoAnnotationResults annotationResults = response.getAnnotationResults(); |
| 102 | + |
| 103 | + for (ObjectTrackingAnnotation objectAnnotations : |
| 104 | + annotationResults.getObjectAnnotationsList()) { |
| 105 | + |
| 106 | + String entity = objectAnnotations.getEntity().getDescription(); |
| 107 | + float confidence = objectAnnotations.getConfidence(); |
| 108 | + long trackId = objectAnnotations.getTrackId(); |
| 109 | + System.out.format("%s: %f (ID: %d)\n", entity, confidence, trackId); |
| 110 | + |
| 111 | + // In streaming, there is always one frame. |
| 112 | + ObjectTrackingFrame frame = objectAnnotations.getFrames(0); |
| 113 | + double offset = |
| 114 | + frame.getTimeOffset().getSeconds() + frame.getTimeOffset().getNanos() / 1e9; |
| 115 | + System.out.format("Offset: %f\n", offset); |
| 116 | + |
| 117 | + System.out.println("Bounding Box:"); |
| 118 | + System.out.format("\tLeft: %f\n", frame.getNormalizedBoundingBox().getLeft()); |
| 119 | + System.out.format("\tTop: %f\n", frame.getNormalizedBoundingBox().getTop()); |
| 120 | + System.out.format("\tRight: %f\n", frame.getNormalizedBoundingBox().getRight()); |
| 121 | + System.out.format("\tBottom: %f\n", frame.getNormalizedBoundingBox().getBottom()); |
| 122 | + } |
| 123 | + } |
| 124 | + System.out.println("Video streamed successfully."); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +// [END video_streaming_automl_object_tracking_beta] |
0 commit comments