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