|
| 1 | +/* |
| 2 | + * Copyright 2020 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_detect_logo] |
| 20 | + |
| 21 | +import com.google.api.gax.longrunning.OperationFuture; |
| 22 | +import com.google.cloud.videointelligence.v1.AnnotateVideoProgress; |
| 23 | +import com.google.cloud.videointelligence.v1.AnnotateVideoRequest; |
| 24 | +import com.google.cloud.videointelligence.v1.AnnotateVideoResponse; |
| 25 | +import com.google.cloud.videointelligence.v1.DetectedAttribute; |
| 26 | +import com.google.cloud.videointelligence.v1.Entity; |
| 27 | +import com.google.cloud.videointelligence.v1.Feature; |
| 28 | +import com.google.cloud.videointelligence.v1.LogoRecognitionAnnotation; |
| 29 | +import com.google.cloud.videointelligence.v1.NormalizedBoundingBox; |
| 30 | +import com.google.cloud.videointelligence.v1.TimestampedObject; |
| 31 | +import com.google.cloud.videointelligence.v1.Track; |
| 32 | +import com.google.cloud.videointelligence.v1.VideoAnnotationResults; |
| 33 | +import com.google.cloud.videointelligence.v1.VideoIntelligenceServiceClient; |
| 34 | +import com.google.cloud.videointelligence.v1.VideoSegment; |
| 35 | +import com.google.protobuf.ByteString; |
| 36 | +import com.google.protobuf.Duration; |
| 37 | +import java.io.IOException; |
| 38 | +import java.nio.file.Files; |
| 39 | +import java.nio.file.Path; |
| 40 | +import java.nio.file.Paths; |
| 41 | +import java.util.concurrent.ExecutionException; |
| 42 | +import java.util.concurrent.TimeUnit; |
| 43 | +import java.util.concurrent.TimeoutException; |
| 44 | + |
| 45 | +public class LogoDetection { |
| 46 | + |
| 47 | + public static void detectLogo() throws Exception { |
| 48 | + // TODO(developer): Replace these variables before running the sample. |
| 49 | + String localFilePath = "path/to/your/video.mp4"; |
| 50 | + detectLogo(localFilePath); |
| 51 | + } |
| 52 | + |
| 53 | + public static void detectLogo(String filePath) |
| 54 | + throws IOException, ExecutionException, InterruptedException, TimeoutException { |
| 55 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 56 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 57 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 58 | + try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { |
| 59 | + // Read file |
| 60 | + Path path = Paths.get(filePath); |
| 61 | + byte[] data = Files.readAllBytes(path); |
| 62 | + // Create the request |
| 63 | + AnnotateVideoRequest request = |
| 64 | + AnnotateVideoRequest.newBuilder() |
| 65 | + .setInputContent(ByteString.copyFrom(data)) |
| 66 | + .addFeatures(Feature.LOGO_RECOGNITION) |
| 67 | + .build(); |
| 68 | + |
| 69 | + // asynchronously perform object tracking on videos |
| 70 | + OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> future = |
| 71 | + client.annotateVideoAsync(request); |
| 72 | + |
| 73 | + System.out.println("Waiting for operation to complete..."); |
| 74 | + // The first result is retrieved because a single video was processed. |
| 75 | + AnnotateVideoResponse response = future.get(300, TimeUnit.SECONDS); |
| 76 | + VideoAnnotationResults annotationResult = response.getAnnotationResults(0); |
| 77 | + |
| 78 | + // Annotations for list of logos detected, tracked and recognized in video. |
| 79 | + for (LogoRecognitionAnnotation logoRecognitionAnnotation : |
| 80 | + annotationResult.getLogoRecognitionAnnotationsList()) { |
| 81 | + Entity entity = logoRecognitionAnnotation.getEntity(); |
| 82 | + // Opaque entity ID. Some IDs may be available in |
| 83 | + // [Google Knowledge Graph Search API](https://developers.google.com/knowledge-graph/). |
| 84 | + System.out.printf("Entity Id : %s\n", entity.getEntityId()); |
| 85 | + System.out.printf("Description : %s\n", entity.getDescription()); |
| 86 | + // All logo tracks where the recognized logo appears. Each track corresponds to one logo |
| 87 | + // instance appearing in consecutive frames. |
| 88 | + for (Track track : logoRecognitionAnnotation.getTracksList()) { |
| 89 | + |
| 90 | + // Video segment of a track. |
| 91 | + Duration startTimeOffset = track.getSegment().getStartTimeOffset(); |
| 92 | + System.out.printf( |
| 93 | + "\n\tStart Time Offset: %s.%s\n", |
| 94 | + startTimeOffset.getSeconds(), startTimeOffset.getNanos()); |
| 95 | + Duration endTimeOffset = track.getSegment().getEndTimeOffset(); |
| 96 | + System.out.printf( |
| 97 | + "\tEnd Time Offset: %s.%s\n", endTimeOffset.getSeconds(), endTimeOffset.getNanos()); |
| 98 | + System.out.printf("\tConfidence: %s\n", track.getConfidence()); |
| 99 | + |
| 100 | + // The object with timestamp and attributes per frame in the track. |
| 101 | + for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) { |
| 102 | + |
| 103 | + // Normalized Bounding box in a frame, where the object is located. |
| 104 | + NormalizedBoundingBox normalizedBoundingBox = |
| 105 | + timestampedObject.getNormalizedBoundingBox(); |
| 106 | + System.out.printf("\n\t\tLeft: %s\n", normalizedBoundingBox.getLeft()); |
| 107 | + System.out.printf("\t\tTop: %s\n", normalizedBoundingBox.getTop()); |
| 108 | + System.out.printf("\t\tRight: %s\n", normalizedBoundingBox.getRight()); |
| 109 | + System.out.printf("\t\tBottom: %s\n", normalizedBoundingBox.getBottom()); |
| 110 | + |
| 111 | + // Optional. The attributes of the object in the bounding box. |
| 112 | + for (DetectedAttribute attribute : timestampedObject.getAttributesList()) { |
| 113 | + System.out.printf("\n\t\t\tName: %s\n", attribute.getName()); |
| 114 | + System.out.printf("\t\t\tConfidence: %s\n", attribute.getConfidence()); |
| 115 | + System.out.printf("\t\t\tValue: %s\n", attribute.getValue()); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Optional. Attributes in the track level. |
| 120 | + for (DetectedAttribute trackAttribute : track.getAttributesList()) { |
| 121 | + System.out.printf("\n\t\tName : %s\n", trackAttribute.getName()); |
| 122 | + System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence()); |
| 123 | + System.out.printf("\t\tValue : %s\n", trackAttribute.getValue()); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // All video segments where the recognized logo appears. There might be multiple instances |
| 128 | + // of the same logo class appearing in one VideoSegment. |
| 129 | + for (VideoSegment segment : logoRecognitionAnnotation.getSegmentsList()) { |
| 130 | + System.out.printf( |
| 131 | + "\n\tStart Time Offset : %s.%s\n", |
| 132 | + segment.getStartTimeOffset().getSeconds(), segment.getStartTimeOffset().getNanos()); |
| 133 | + System.out.printf( |
| 134 | + "\tEnd Time Offset : %s.%s\n", |
| 135 | + segment.getEndTimeOffset().getSeconds(), segment.getEndTimeOffset().getNanos()); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | +// [END video_detect_logo] |
0 commit comments