|
| 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 | + * https://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 | +// DO NOT EDIT! This is a generated sample ("LongRunningRequestAsync", "video_detect_logo_beta") |
| 17 | +// sample-metadata: |
| 18 | +// title: |
| 19 | +// description: Performs asynchronous video annotation for logo recognition on a local file. |
| 20 | +// usage: gradle run -PmainClass=com.google.cloud.examples.videointelligence.v1p3beta1.VideoDetectLogoBeta [--args='[--local_file_path "resources/googlework_short.mp4"]'] |
| 21 | + |
| 22 | +package com.example.video; |
| 23 | + |
| 24 | +import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest; |
| 25 | +import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse; |
| 26 | +import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute; |
| 27 | +import com.google.cloud.videointelligence.v1p3beta1.Entity; |
| 28 | +import com.google.cloud.videointelligence.v1p3beta1.Feature; |
| 29 | +import com.google.cloud.videointelligence.v1p3beta1.LogoRecognitionAnnotation; |
| 30 | +import com.google.cloud.videointelligence.v1p3beta1.NormalizedBoundingBox; |
| 31 | +import com.google.cloud.videointelligence.v1p3beta1.TimestampedObject; |
| 32 | +import com.google.cloud.videointelligence.v1p3beta1.Track; |
| 33 | +import com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults; |
| 34 | +import com.google.cloud.videointelligence.v1p3beta1.VideoIntelligenceServiceClient; |
| 35 | +import com.google.cloud.videointelligence.v1p3beta1.VideoSegment; |
| 36 | +import com.google.protobuf.ByteString; |
| 37 | +import com.google.protobuf.Duration; |
| 38 | +import java.nio.file.Files; |
| 39 | +import java.nio.file.Path; |
| 40 | +import java.nio.file.Paths; |
| 41 | +import java.util.Arrays; |
| 42 | +import java.util.List; |
| 43 | +import org.apache.commons.cli.CommandLine; |
| 44 | +import org.apache.commons.cli.DefaultParser; |
| 45 | +import org.apache.commons.cli.Option; |
| 46 | +import org.apache.commons.cli.Options; |
| 47 | + |
| 48 | +public class VideoDetectLogoBeta { |
| 49 | + // [START video_detect_logo_beta] |
| 50 | + /* |
| 51 | + * Please include the following imports to run this sample. |
| 52 | + * |
| 53 | + * import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest; |
| 54 | + * import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse; |
| 55 | + * import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute; |
| 56 | + * import com.google.cloud.videointelligence.v1p3beta1.Entity; |
| 57 | + * import com.google.cloud.videointelligence.v1p3beta1.Feature; |
| 58 | + * import com.google.cloud.videointelligence.v1p3beta1.LogoRecognitionAnnotation; |
| 59 | + * import com.google.cloud.videointelligence.v1p3beta1.NormalizedBoundingBox; |
| 60 | + * import com.google.cloud.videointelligence.v1p3beta1.TimestampedObject; |
| 61 | + * import com.google.cloud.videointelligence.v1p3beta1.Track; |
| 62 | + * import com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults; |
| 63 | + * import com.google.cloud.videointelligence.v1p3beta1.VideoIntelligenceServiceClient; |
| 64 | + * import com.google.cloud.videointelligence.v1p3beta1.VideoSegment; |
| 65 | + * import com.google.protobuf.ByteString; |
| 66 | + * import com.google.protobuf.Duration; |
| 67 | + * import java.nio.file.Files; |
| 68 | + * import java.nio.file.Path; |
| 69 | + * import java.nio.file.Paths; |
| 70 | + * import java.util.Arrays; |
| 71 | + * import java.util.List; |
| 72 | + */ |
| 73 | + |
| 74 | + /** |
| 75 | + * Performs asynchronous video annotation for logo recognition on a local file. |
| 76 | + * |
| 77 | + * @param localFilePath Path to local video file, e.g. /path/video.mp4 |
| 78 | + */ |
| 79 | + public static void sampleAnnotateVideo(String localFilePath) { |
| 80 | + try (VideoIntelligenceServiceClient videoIntelligenceServiceClient = |
| 81 | + VideoIntelligenceServiceClient.create()) { |
| 82 | + // localFilePath = "resources/googlework_short.mp4"; |
| 83 | + Path path = Paths.get(localFilePath); |
| 84 | + byte[] data = Files.readAllBytes(path); |
| 85 | + ByteString inputContent = ByteString.copyFrom(data); |
| 86 | + Feature featuresElement = Feature.LOGO_RECOGNITION; |
| 87 | + List<Feature> features = Arrays.asList(featuresElement); |
| 88 | + AnnotateVideoRequest request = |
| 89 | + AnnotateVideoRequest.newBuilder() |
| 90 | + .setInputContent(inputContent) |
| 91 | + .addAllFeatures(features) |
| 92 | + .build(); |
| 93 | + AnnotateVideoResponse response = |
| 94 | + videoIntelligenceServiceClient.annotateVideoAsync(request).get(); |
| 95 | + // Get the first response, since we sent only one video. |
| 96 | + VideoAnnotationResults annotationResult = response.getAnnotationResultsList().get(0); |
| 97 | + // Annotations for list of logos detected, tracked and recognized in video. |
| 98 | + for (LogoRecognitionAnnotation logoRecognitionAnnotation : |
| 99 | + annotationResult.getLogoRecognitionAnnotationsList()) { |
| 100 | + Entity entity = logoRecognitionAnnotation.getEntity(); |
| 101 | + // Opaque entity ID. Some IDs may be available in [Google Knowledge Graph Search API](https://developers.google.com/knowledge-graph/). |
| 102 | + System.out.printf("Entity Id : %s\n", entity.getEntityId()); |
| 103 | + // Textual description, e.g. `Google`. |
| 104 | + System.out.printf("Description : %s\n", entity.getDescription()); |
| 105 | + // All logo tracks where the recognized logo appears. Each track corresponds to one logo instance appearing in consecutive frames. |
| 106 | + for (Track track : logoRecognitionAnnotation.getTracksList()) { |
| 107 | + // Video segment of a track. |
| 108 | + VideoSegment segment = track.getSegment(); |
| 109 | + Duration segmentStartTimeOffset = segment.getStartTimeOffset(); |
| 110 | + System.out.printf( |
| 111 | + "\n\tStart Time Offset : %s.%s\n", |
| 112 | + segmentStartTimeOffset.getSeconds(), segmentStartTimeOffset.getNanos()); |
| 113 | + Duration segmentEndTimeOffset = segment.getEndTimeOffset(); |
| 114 | + System.out.printf( |
| 115 | + "\tEnd Time Offset : %s.%s\n", |
| 116 | + segmentEndTimeOffset.getSeconds(), segmentEndTimeOffset.getNanos()); |
| 117 | + System.out.printf("\tConfidence : %s\n", track.getConfidence()); |
| 118 | + // The object with timestamp and attributes per frame in the track. |
| 119 | + for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) { |
| 120 | + // Normalized Bounding box in a frame, where the object is located. |
| 121 | + NormalizedBoundingBox normalizedBoundingBox = |
| 122 | + timestampedObject.getNormalizedBoundingBox(); |
| 123 | + System.out.printf("\n\t\tLeft : %s\n", normalizedBoundingBox.getLeft()); |
| 124 | + System.out.printf("\t\tTop : %s\n", normalizedBoundingBox.getTop()); |
| 125 | + System.out.printf("\t\tRight : %s\n", normalizedBoundingBox.getRight()); |
| 126 | + System.out.printf("\t\tBottom : %s\n", normalizedBoundingBox.getBottom()); |
| 127 | + // Optional. The attributes of the object in the bounding box. |
| 128 | + for (DetectedAttribute attribute : timestampedObject.getAttributesList()) { |
| 129 | + System.out.printf("\n\t\t\tName : %s\n", attribute.getName()); |
| 130 | + System.out.printf("\t\t\tConfidence : %s\n", attribute.getConfidence()); |
| 131 | + System.out.printf("\t\t\tValue : %s\n", attribute.getValue()); |
| 132 | + } |
| 133 | + } |
| 134 | + // Optional. Attributes in the track level. |
| 135 | + for (DetectedAttribute trackAttribute : track.getAttributesList()) { |
| 136 | + System.out.printf("\n\t\tName : %s\n", trackAttribute.getName()); |
| 137 | + System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence()); |
| 138 | + System.out.printf("\t\tValue : %s\n", trackAttribute.getValue()); |
| 139 | + } |
| 140 | + } |
| 141 | + // All video segments where the recognized logo appears. There might be multiple instances of the same logo class appearing in one VideoSegment. |
| 142 | + for (VideoSegment logoRecognitionAnnotationSegment : |
| 143 | + logoRecognitionAnnotation.getSegmentsList()) { |
| 144 | + Duration logoRecognitionAnnotationSegmentStartTimeOffset = |
| 145 | + logoRecognitionAnnotationSegment.getStartTimeOffset(); |
| 146 | + System.out.printf( |
| 147 | + "\n\tStart Time Offset : %s.%s\n", |
| 148 | + logoRecognitionAnnotationSegmentStartTimeOffset.getSeconds(), |
| 149 | + logoRecognitionAnnotationSegmentStartTimeOffset.getNanos()); |
| 150 | + Duration logoRecognitionAnnotationSegmentEndTimeOffset = |
| 151 | + logoRecognitionAnnotationSegment.getEndTimeOffset(); |
| 152 | + System.out.printf( |
| 153 | + "\tEnd Time Offset : %s.%s\n", |
| 154 | + logoRecognitionAnnotationSegmentEndTimeOffset.getSeconds(), |
| 155 | + logoRecognitionAnnotationSegmentEndTimeOffset.getNanos()); |
| 156 | + } |
| 157 | + } |
| 158 | + } catch (Exception exception) { |
| 159 | + System.err.println("Failed to create the client due to: " + exception); |
| 160 | + } |
| 161 | + } |
| 162 | + // [END video_detect_logo_beta] |
| 163 | + |
| 164 | + public static void main(String[] args) throws Exception { |
| 165 | + Options options = new Options(); |
| 166 | + options.addOption( |
| 167 | + Option.builder("").required(false).hasArg(true).longOpt("local_file_path").build()); |
| 168 | + |
| 169 | + CommandLine cl = (new DefaultParser()).parse(options, args); |
| 170 | + String localFilePath = cl.getOptionValue("local_file_path", "resources/googlework_short.mp4"); |
| 171 | + |
| 172 | + sampleAnnotateVideo(localFilePath); |
| 173 | + } |
| 174 | +} |
0 commit comments