|
| 1 | +/* |
| 2 | + * Copyright 2018 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 | +import com.google.api.gax.longrunning.OperationFuture; |
| 20 | +import com.google.cloud.videointelligence.v1p2beta1.AnnotateVideoProgress; |
| 21 | +import com.google.cloud.videointelligence.v1p2beta1.AnnotateVideoRequest; |
| 22 | +import com.google.cloud.videointelligence.v1p2beta1.AnnotateVideoResponse; |
| 23 | +import com.google.cloud.videointelligence.v1p2beta1.Entity; |
| 24 | +import com.google.cloud.videointelligence.v1p2beta1.Feature; |
| 25 | +import com.google.cloud.videointelligence.v1p2beta1.NormalizedBoundingBox; |
| 26 | +import com.google.cloud.videointelligence.v1p2beta1.ObjectTrackingAnnotation; |
| 27 | +import com.google.cloud.videointelligence.v1p2beta1.ObjectTrackingFrame; |
| 28 | +import com.google.cloud.videointelligence.v1p2beta1.VideoAnnotationResults; |
| 29 | +import com.google.cloud.videointelligence.v1p2beta1.VideoIntelligenceServiceClient; |
| 30 | +import com.google.cloud.videointelligence.v1p2beta1.VideoSegment; |
| 31 | +import com.google.protobuf.ByteString; |
| 32 | + |
| 33 | +import com.google.protobuf.Duration; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.nio.file.Paths; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | + |
| 39 | +public class TrackObjects { |
| 40 | + |
| 41 | + // [START video_object_tracking_beta] |
| 42 | + /** |
| 43 | + * Track objects in a video. |
| 44 | + * |
| 45 | + * @param filePath the path to the video file to analyze. |
| 46 | + */ |
| 47 | + public static VideoAnnotationResults trackObjects(String filePath) throws Exception { |
| 48 | + try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { |
| 49 | + // Read file |
| 50 | + Path path = Paths.get(filePath); |
| 51 | + byte[] data = Files.readAllBytes(path); |
| 52 | + |
| 53 | + // Create the request |
| 54 | + AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder() |
| 55 | + .setInputContent(ByteString.copyFrom(data)) |
| 56 | + .addFeatures(Feature.OBJECT_TRACKING) |
| 57 | + .setLocationId("us-east1") |
| 58 | + .build(); |
| 59 | + |
| 60 | + // asynchronously perform object tracking on videos |
| 61 | + OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> future = |
| 62 | + client.annotateVideoAsync(request); |
| 63 | + |
| 64 | + System.out.println("Waiting for operation to complete..."); |
| 65 | + // The first result is retrieved because a single video was processed. |
| 66 | + AnnotateVideoResponse response = future.get(300, TimeUnit.SECONDS); |
| 67 | + VideoAnnotationResults results = response.getAnnotationResults(0); |
| 68 | + |
| 69 | + // Get only the first annotation for demo purposes. |
| 70 | + ObjectTrackingAnnotation annotation = results.getObjectAnnotations(0); |
| 71 | + System.out.println("Confidence: " + annotation.getConfidence()); |
| 72 | + |
| 73 | + if (annotation.hasEntity()) { |
| 74 | + Entity entity = annotation.getEntity(); |
| 75 | + System.out.println("Entity description: " + entity.getDescription()); |
| 76 | + System.out.println("Entity id:: " + entity.getEntityId()); |
| 77 | + } |
| 78 | + |
| 79 | + if (annotation.hasSegment()) { |
| 80 | + VideoSegment videoSegment = annotation.getSegment(); |
| 81 | + Duration startTimeOffset = videoSegment.getStartTimeOffset(); |
| 82 | + Duration endTimeOffset = videoSegment.getEndTimeOffset(); |
| 83 | + // Display the segment time in seconds, 1e9 converts nanos to seconds |
| 84 | + System.out.println(String.format( |
| 85 | + "Segment: %.2fs to %.2fs", |
| 86 | + startTimeOffset.getSeconds() + startTimeOffset.getNanos() / 1e9, |
| 87 | + endTimeOffset.getSeconds() + endTimeOffset.getNanos() / 1e9)); |
| 88 | + } |
| 89 | + |
| 90 | + // Here we print only the bounding box of the first frame in this segment. |
| 91 | + ObjectTrackingFrame frame = annotation.getFrames(0); |
| 92 | + // Display the offset time in seconds, 1e9 converts nanos to seconds |
| 93 | + Duration timeOffset = frame.getTimeOffset(); |
| 94 | + System.out.println(String.format( |
| 95 | + "Time offset of the first frame: %.2fs", |
| 96 | + timeOffset.getSeconds() + timeOffset.getNanos() / 1e9)); |
| 97 | + |
| 98 | + // Display the bounding box of the detected object |
| 99 | + NormalizedBoundingBox normalizedBoundingBox = frame.getNormalizedBoundingBox(); |
| 100 | + System.out.println("Bounding box position:"); |
| 101 | + System.out.println("\tleft: " + normalizedBoundingBox.getLeft()); |
| 102 | + System.out.println("\ttop: " + normalizedBoundingBox.getTop()); |
| 103 | + System.out.println("\tright: " + normalizedBoundingBox.getRight()); |
| 104 | + System.out.println("\tbottom: " + normalizedBoundingBox.getBottom()); |
| 105 | + return results; |
| 106 | + } |
| 107 | + } |
| 108 | + // [END video_object_tracking_beta] |
| 109 | + |
| 110 | + // [START video_object_tracking_gcs_beta] |
| 111 | + /** |
| 112 | + * Track objects in a video. |
| 113 | + * |
| 114 | + * @param gcsUri the path to the video file to analyze. |
| 115 | + */ |
| 116 | + public static VideoAnnotationResults trackObjectsGcs(String gcsUri) throws Exception { |
| 117 | + try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { |
| 118 | + // Create the request |
| 119 | + AnnotateVideoRequest request = AnnotateVideoRequest.newBuilder() |
| 120 | + .setInputUri(gcsUri) |
| 121 | + .addFeatures(Feature.OBJECT_TRACKING) |
| 122 | + .setLocationId("us-east1") |
| 123 | + .build(); |
| 124 | + |
| 125 | + // asynchronously perform object tracking on videos |
| 126 | + OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> future = |
| 127 | + client.annotateVideoAsync(request); |
| 128 | + |
| 129 | + System.out.println("Waiting for operation to complete..."); |
| 130 | + // The first result is retrieved because a single video was processed. |
| 131 | + AnnotateVideoResponse response = future.get(300, TimeUnit.SECONDS); |
| 132 | + VideoAnnotationResults results = response.getAnnotationResults(0); |
| 133 | + |
| 134 | + // Get only the first annotation for demo purposes. |
| 135 | + ObjectTrackingAnnotation annotation = results.getObjectAnnotations(0); |
| 136 | + System.out.println("Confidence: " + annotation.getConfidence()); |
| 137 | + |
| 138 | + if (annotation.hasEntity()) { |
| 139 | + Entity entity = annotation.getEntity(); |
| 140 | + System.out.println("Entity description: " + entity.getDescription()); |
| 141 | + System.out.println("Entity id:: " + entity.getEntityId()); |
| 142 | + } |
| 143 | + |
| 144 | + if (annotation.hasSegment()) { |
| 145 | + VideoSegment videoSegment = annotation.getSegment(); |
| 146 | + Duration startTimeOffset = videoSegment.getStartTimeOffset(); |
| 147 | + Duration endTimeOffset = videoSegment.getEndTimeOffset(); |
| 148 | + // Display the segment time in seconds, 1e9 converts nanos to seconds |
| 149 | + System.out.println(String.format( |
| 150 | + "Segment: %.2fs to %.2fs", |
| 151 | + startTimeOffset.getSeconds() + startTimeOffset.getNanos() / 1e9, |
| 152 | + endTimeOffset.getSeconds() + endTimeOffset.getNanos() / 1e9)); |
| 153 | + } |
| 154 | + |
| 155 | + // Here we print only the bounding box of the first frame in this segment. |
| 156 | + ObjectTrackingFrame frame = annotation.getFrames(0); |
| 157 | + // Display the offset time in seconds, 1e9 converts nanos to seconds |
| 158 | + Duration timeOffset = frame.getTimeOffset(); |
| 159 | + System.out.println(String.format( |
| 160 | + "Time offset of the first frame: %.2fs", |
| 161 | + timeOffset.getSeconds() + timeOffset.getNanos() / 1e9)); |
| 162 | + |
| 163 | + // Display the bounding box of the detected object |
| 164 | + NormalizedBoundingBox normalizedBoundingBox = frame.getNormalizedBoundingBox(); |
| 165 | + System.out.println("Bounding box position:"); |
| 166 | + System.out.println("\tleft: " + normalizedBoundingBox.getLeft()); |
| 167 | + System.out.println("\ttop: " + normalizedBoundingBox.getTop()); |
| 168 | + System.out.println("\tright: " + normalizedBoundingBox.getRight()); |
| 169 | + System.out.println("\tbottom: " + normalizedBoundingBox.getBottom()); |
| 170 | + return results; |
| 171 | + } |
| 172 | + } |
| 173 | + // [END video_object_tracking_gcs_beta] |
| 174 | +} |
| 175 | + |
0 commit comments