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