-
Notifications
You must be signed in to change notification settings - Fork 2.9k
migrate code from googleapis/java-video-intelligence #7425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 198 commits
64500bc
efa4c49
897f84f
55b2fc3
d46667a
97ccbf0
791053e
801a8cc
bb03f21
665ca98
5f454cd
b85e6c1
1bd89d6
53b0058
346991a
a53782b
c4025e9
f5cb21f
ac19128
f9ff6e8
254882f
e073e9b
eef6e52
c8ee654
774053f
93ee3c7
32a08ee
b3debb4
7c861ce
79738ef
40b41c3
d442bfd
4e4b0ab
81eae9d
c7c22d7
3b260a0
268a3e0
3375798
89a61d2
6f59a0a
76279de
1833b31
94fae5e
4e8d3b9
6e9d03f
7dc8566
dbe2bdc
edc5313
58cafe0
e694b5c
9b2044f
06c8deb
888166d
9ae9a96
9d9c5d7
1a73332
a4eb81e
655ffb7
c6ab32a
eb80dab
88e60b0
e72259f
80bae8b
63ac027
149cb7c
4ffb905
f3c73d9
7a71c6d
de14f66
552cd56
8aa0ef3
f4e357d
4ecbe57
75e79b2
08ac2fb
9a5d861
ab7d443
fd74bbe
43eb235
a806f80
1c0951b
af85752
ecf36e2
36587af
90bf56a
644d436
88e3996
5975b29
d779f64
bd8b41d
cadbf52
35e4ee5
ed73474
491e5e7
490a3c6
bb52e8a
bc1427e
524a742
7f98e64
156c071
83112bd
a38833b
0c35b91
0b367f9
9d0055f
1ce6201
715bdbb
abe91b9
14ef9c0
1119af8
07035aa
caa2363
6f93c00
9d9a89c
3844188
0c8de4e
472751d
a627c05
d0aa78f
e85c705
229be24
c197b28
d778c08
191993d
cf47b9c
d64834e
5cdb87e
e4c49f5
ad5c69c
32848ea
67d3c52
5218932
ccd8f08
b9cb340
6785f23
1d89e33
db2929e
1333add
bb9f505
a008760
61cfe26
de46f20
3881962
b638c8c
d3ed19f
4e4a2d2
32b1798
b9a7136
a7871e3
512b7cb
f47b3d5
2019943
d25002c
744106d
253ffb0
2037956
e17a806
ad88b3b
7569c98
137a755
cf8ca49
c47cfde
5b77573
de06617
540bf03
c9ba84d
912d332
cf60a58
b39552e
ebc5e0a
2f8e80b
c7a244e
f0809d3
34cfc71
a396cba
fd1d4f5
bdca8aa
3c252ae
8774747
d7c357b
760ba1f
d88f80e
5cbfc7c
db1fbbd
75b2472
ee24c3d
76c73ff
7d2da70
c2d8003
3c692bd
221b2b6
3770888
c1ad1b1
d0f1c74
7f8a62a
ed78a85
f7e27ea
eb31a8b
dacfa50
a68313a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,36 @@ | ||
| # Video Intelligence Samples have been moved | ||
| # [Cloud Video Intelligence: Java Samples](https://github.com/googleapis/java-video-intelligence) | ||
|
|
||
| [https://github.com/googleapis/java-video-intelligence](https://github.com/googleapis/java-video-intelligence/tree/main/samples). | ||
| [![Open in Cloud Shell][shell_img]][shell_link] | ||
|
|
||
|
|
||
|
|
||
| ## Table of Contents | ||
|
|
||
| * [Build the sample](#build-the-sample) | ||
| * [Samples](#samples) | ||
|
|
||
|
|
||
| ## Build the sample | ||
|
|
||
| Install [Maven](http://maven.apache.org/). | ||
|
|
||
| Build your project with: | ||
|
|
||
| ``` | ||
| mvn clean package -DskipTests=True | ||
| ``` | ||
|
|
||
| ## Samples | ||
|
|
||
| Please follow [Before you begin](https://cloud.google.com/video-intelligence/docs/annotate-video-client-libraries#before-you-begin) for project and auth setup before you run the samples. | ||
|
|
||
|
|
||
| ## Run | ||
| Run all tests: | ||
| ``` | ||
| mvn clean verify | ||
| ``` | ||
|
|
||
| [shell_img]: https://gstatic.com/cloudssh/images/open-btn.png | ||
| [shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=video/README.md | ||
| [product-docs]: https://cloud.google.com/video-intelligence/docs/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?xml version='1.0' encoding='UTF-8'?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <groupId>com.google.cloud</groupId> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we update this to
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Shabirmean this looks fine to me as it makes it clear that it's a google cloud sample, any reason for this change?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was something we discussed in the migration team chat and agreed to use context: link-1 and link-2 |
||
| <artifactId>videointelligence-snippets</artifactId> | ||
| <packaging>jar</packaging> | ||
| <name>Google Cloud Video Intelligence Snippets</name> | ||
| <url>https://github.com/googleapis/java-video-intelligence</url> | ||
|
anguillanneuf marked this conversation as resolved.
Outdated
|
||
|
|
||
| <!-- | ||
| The parent pom defines common style checks and testing strategies for our samples. | ||
| Removing or replacing it should not affect the execution of the samples in anyway. | ||
| --> | ||
| <parent> | ||
| <groupId>com.google.cloud.samples</groupId> | ||
| <artifactId>shared-configuration</artifactId> | ||
| <version>1.2.0</version> | ||
| </parent> | ||
|
|
||
| <properties> | ||
| <maven.compiler.target>1.8</maven.compiler.target> | ||
| <maven.compiler.source>1.8</maven.compiler.source> | ||
| <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
| </properties> | ||
|
|
||
|
|
||
| <!-- [START video_install_with_bom] --> | ||
| <dependencyManagement> | ||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>libraries-bom</artifactId> | ||
| <version>26.1.3</version> | ||
| <type>pom</type> | ||
| <scope>import</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </dependencyManagement> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-video-intelligence</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-storage</artifactId> | ||
| </dependency> | ||
| <!-- [END video_install_with_bom] --> | ||
|
|
||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <version>4.13.2</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.truth</groupId> | ||
| <artifactId>truth</artifactId> | ||
| <version>1.1.3</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-core</artifactId> | ||
| <version>2.8.22</version> | ||
| <scope>test</scope> | ||
| <classifier>tests</classifier> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * Copyright 2018 Google Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package beta.video; | ||
|
|
||
| import com.google.api.gax.longrunning.OperationFuture; | ||
| import com.google.cloud.videointelligence.v1p1beta1.AnnotateVideoProgress; | ||
| import com.google.cloud.videointelligence.v1p1beta1.AnnotateVideoRequest; | ||
| import com.google.cloud.videointelligence.v1p1beta1.AnnotateVideoResponse; | ||
| import com.google.cloud.videointelligence.v1p1beta1.Feature; | ||
| import com.google.cloud.videointelligence.v1p1beta1.SpeechRecognitionAlternative; | ||
| import com.google.cloud.videointelligence.v1p1beta1.SpeechTranscription; | ||
| import com.google.cloud.videointelligence.v1p1beta1.SpeechTranscriptionConfig; | ||
| import com.google.cloud.videointelligence.v1p1beta1.VideoAnnotationResults; | ||
| import com.google.cloud.videointelligence.v1p1beta1.VideoContext; | ||
| import com.google.cloud.videointelligence.v1p1beta1.VideoIntelligenceServiceClient; | ||
| import com.google.cloud.videointelligence.v1p1beta1.WordInfo; | ||
| import java.io.IOException; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class Detect { | ||
| /** | ||
| * Detects video transcription using the Video Intelligence API | ||
| * | ||
| * @param args specifies features to detect and the path to the video on Google Cloud Storage. | ||
| */ | ||
| public static void main(String[] args) { | ||
| try { | ||
| argsHelper(args); | ||
| } catch (Exception e) { | ||
| System.out.println("Exception while running:\n" + e.getMessage() + "\n"); | ||
| e.printStackTrace(System.out); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Helper that handles the input passed to the program. | ||
| * | ||
| * @param args specifies features to detect and the path to the video on Google Cloud Storage. | ||
| * @throws IOException on Input/Output errors. | ||
| */ | ||
| public static void argsHelper(String[] args) throws Exception { | ||
| if (args.length < 1) { | ||
| System.out.println("Usage:"); | ||
| System.out.printf( | ||
| "\tjava %s \"<command>\" \"<path-to-video>\"\n" | ||
| + "Commands:\n" | ||
| + "\tspeech-transcription\n" | ||
| + "Path:\n\tA URI for a Cloud Storage resource (gs://...)\n" | ||
| + "Examples: ", | ||
| Detect.class.getCanonicalName()); | ||
| return; | ||
| } | ||
| String command = args[0]; | ||
| String path = args.length > 1 ? args[1] : ""; | ||
|
|
||
| if (command.equals("speech-transcription")) { | ||
| speechTranscription(path); | ||
| } | ||
| } | ||
|
|
||
| // [START video_speech_transcription_gcs_beta] | ||
| /** | ||
| * Transcribe speech from a video stored on GCS. | ||
| * | ||
| * @param gcsUri the path to the video file to analyze. | ||
| */ | ||
| public static void speechTranscription(String gcsUri) throws Exception { | ||
| // Instantiate a com.google.cloud.videointelligence.v1p1beta1.VideoIntelligenceServiceClient | ||
| try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { | ||
| // Set the language code | ||
| SpeechTranscriptionConfig config = | ||
| SpeechTranscriptionConfig.newBuilder() | ||
| .setLanguageCode("en-US") | ||
| .setEnableAutomaticPunctuation(true) | ||
| .build(); | ||
|
|
||
| // Set the video context with the above configuration | ||
| VideoContext context = VideoContext.newBuilder().setSpeechTranscriptionConfig(config).build(); | ||
|
|
||
| // Create the request | ||
| AnnotateVideoRequest request = | ||
| AnnotateVideoRequest.newBuilder() | ||
| .setInputUri(gcsUri) | ||
| .addFeatures(Feature.SPEECH_TRANSCRIPTION) | ||
| .setVideoContext(context) | ||
| .build(); | ||
|
|
||
| // asynchronously perform speech transcription on videos | ||
| OperationFuture<AnnotateVideoResponse, AnnotateVideoProgress> response = | ||
| client.annotateVideoAsync(request); | ||
|
|
||
| System.out.println("Waiting for operation to complete..."); | ||
| // Display the results | ||
| for (VideoAnnotationResults results : | ||
| response.get(300, TimeUnit.SECONDS).getAnnotationResultsList()) { | ||
| for (SpeechTranscription speechTranscription : results.getSpeechTranscriptionsList()) { | ||
| try { | ||
| // Print the transcription | ||
| if (speechTranscription.getAlternativesCount() > 0) { | ||
| SpeechRecognitionAlternative alternative = speechTranscription.getAlternatives(0); | ||
|
|
||
| System.out.printf("Transcript: %s\n", alternative.getTranscript()); | ||
| System.out.printf("Confidence: %.2f\n", alternative.getConfidence()); | ||
|
|
||
| System.out.println("Word level information:"); | ||
| for (WordInfo wordInfo : alternative.getWordsList()) { | ||
| double startTime = | ||
| wordInfo.getStartTime().getSeconds() + wordInfo.getStartTime().getNanos() / 1e9; | ||
| double endTime = | ||
| wordInfo.getEndTime().getSeconds() + wordInfo.getEndTime().getNanos() / 1e9; | ||
| System.out.printf( | ||
| "\t%4.2fs - %4.2fs: %s\n", startTime, endTime, wordInfo.getWord()); | ||
| } | ||
| } else { | ||
| System.out.println("No transcription found"); | ||
| } | ||
| } catch (IndexOutOfBoundsException ioe) { | ||
| System.out.println("Could not retrieve frame: " + ioe.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // [END video_speech_transcription_gcs_beta] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| /* | ||
| * Copyright 2020 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package beta.video; | ||
|
|
||
| // [START video_detect_logo_beta] | ||
|
|
||
| import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoRequest; | ||
| import com.google.cloud.videointelligence.v1p3beta1.AnnotateVideoResponse; | ||
| import com.google.cloud.videointelligence.v1p3beta1.DetectedAttribute; | ||
| import com.google.cloud.videointelligence.v1p3beta1.Entity; | ||
| import com.google.cloud.videointelligence.v1p3beta1.Feature; | ||
| import com.google.cloud.videointelligence.v1p3beta1.LogoRecognitionAnnotation; | ||
| import com.google.cloud.videointelligence.v1p3beta1.NormalizedBoundingBox; | ||
| import com.google.cloud.videointelligence.v1p3beta1.TimestampedObject; | ||
| import com.google.cloud.videointelligence.v1p3beta1.Track; | ||
| import com.google.cloud.videointelligence.v1p3beta1.VideoAnnotationResults; | ||
| import com.google.cloud.videointelligence.v1p3beta1.VideoIntelligenceServiceClient; | ||
| import com.google.cloud.videointelligence.v1p3beta1.VideoSegment; | ||
| import com.google.protobuf.ByteString; | ||
| import com.google.protobuf.Duration; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.concurrent.ExecutionException; | ||
|
|
||
| public class DetectLogo { | ||
|
|
||
| public void detectLogo() throws IOException, ExecutionException, InterruptedException { | ||
| String filePath = "path/to/your/video.mp4"; | ||
| detectLogo(filePath); | ||
| } | ||
|
|
||
| public static void detectLogo(String localFilePath) | ||
| throws IOException, ExecutionException, InterruptedException { | ||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. After completing all of your requests, call | ||
| // the "close" method on the client to safely clean up any remaining background resources. | ||
| try (VideoIntelligenceServiceClient client = VideoIntelligenceServiceClient.create()) { | ||
| // Read the files contents | ||
| Path path = Paths.get(localFilePath); | ||
| byte[] data = Files.readAllBytes(path); | ||
| ByteString inputContent = ByteString.copyFrom(data); | ||
|
|
||
| // Build the request with the inputContent and set the Feature | ||
| AnnotateVideoRequest request = | ||
| AnnotateVideoRequest.newBuilder() | ||
| .setInputContent(inputContent) | ||
| .addFeatures(Feature.LOGO_RECOGNITION) | ||
| .build(); | ||
|
|
||
| // Make the asynchronous request | ||
| AnnotateVideoResponse response = client.annotateVideoAsync(request).get(); | ||
|
|
||
| // Get the first response, since we sent only one video. | ||
| VideoAnnotationResults annotationResult = response.getAnnotationResultsList().get(0); | ||
|
|
||
| // Annotations for list of logos detected, tracked and recognized in the video. | ||
| for (LogoRecognitionAnnotation logoRecognitionAnnotation : | ||
| annotationResult.getLogoRecognitionAnnotationsList()) { | ||
|
|
||
| Entity entity = logoRecognitionAnnotation.getEntity(); | ||
| // Opaque entity ID. Some IDs may be available in [Google Knowledge Graph Search | ||
| // API](https://developers.google.com/knowledge-graph/). | ||
| System.out.printf("Entity Id: %s\n", entity.getEntityId()); | ||
| System.out.printf("Description: %s\n", entity.getDescription()); | ||
|
|
||
| // All logo tracks where the recognized logo appears. Each track corresponds to one logo | ||
| // instance appearing in consecutive frames. | ||
| for (Track track : logoRecognitionAnnotation.getTracksList()) { | ||
|
|
||
| // Video segment of a track. | ||
| VideoSegment segment = track.getSegment(); | ||
| Duration segmentStartTimeOffset = segment.getStartTimeOffset(); | ||
| System.out.printf( | ||
| "\n\tStart Time Offset: %s.%s\n", | ||
| segmentStartTimeOffset.getSeconds(), segmentStartTimeOffset.getNanos()); | ||
| Duration segmentEndTimeOffset = segment.getEndTimeOffset(); | ||
| System.out.printf( | ||
| "\tEnd Time Offset: %s.%s\n", | ||
| segmentEndTimeOffset.getSeconds(), segmentEndTimeOffset.getNanos()); | ||
| System.out.printf("\tConfidence: %s\n", track.getConfidence()); | ||
|
|
||
| // The object with timestamp and attributes per frame in the track. | ||
| for (TimestampedObject timestampedObject : track.getTimestampedObjectsList()) { | ||
|
|
||
| // Normalized Bounding box in a frame, where the object is located. | ||
| NormalizedBoundingBox normalizedBoundingBox = | ||
| timestampedObject.getNormalizedBoundingBox(); | ||
| System.out.printf("\n\t\tLeft: %s\n", normalizedBoundingBox.getLeft()); | ||
| System.out.printf("\t\tTop: %s\n", normalizedBoundingBox.getTop()); | ||
| System.out.printf("\t\tRight: %s\n", normalizedBoundingBox.getRight()); | ||
| System.out.printf("\t\tBottom: %s\n", normalizedBoundingBox.getBottom()); | ||
|
|
||
| // Optional. The attributes of the object in the bounding box. | ||
| for (DetectedAttribute attribute : timestampedObject.getAttributesList()) { | ||
| System.out.printf("\n\t\t\tName: %s\n", attribute.getName()); | ||
| System.out.printf("\t\t\tConfidence: %s\n", attribute.getConfidence()); | ||
| System.out.printf("\t\t\tValue: %s\n", attribute.getValue()); | ||
| } | ||
| } | ||
|
|
||
| // Optional. Attributes in the track level. | ||
| for (DetectedAttribute trackAttribute : track.getAttributesList()) { | ||
| System.out.printf("\n\t\tName : %s\n", trackAttribute.getName()); | ||
| System.out.printf("\t\tConfidence : %s\n", trackAttribute.getConfidence()); | ||
| System.out.printf("\t\tValue : %s\n", trackAttribute.getValue()); | ||
| } | ||
| } | ||
|
|
||
| // All video segments where the recognized logo appears. There might be multiple instances | ||
| // of the same logo class appearing in one VideoSegment. | ||
| for (VideoSegment logoRecognitionAnnotationSegment : | ||
| logoRecognitionAnnotation.getSegmentsList()) { | ||
| Duration logoRecognitionAnnotationSegmentStartTimeOffset = | ||
| logoRecognitionAnnotationSegment.getStartTimeOffset(); | ||
| System.out.printf( | ||
| "\n\tStart Time Offset : %s.%s\n", | ||
| logoRecognitionAnnotationSegmentStartTimeOffset.getSeconds(), | ||
| logoRecognitionAnnotationSegmentStartTimeOffset.getNanos()); | ||
| Duration logoRecognitionAnnotationSegmentEndTimeOffset = | ||
| logoRecognitionAnnotationSegment.getEndTimeOffset(); | ||
| System.out.printf( | ||
| "\tEnd Time Offset : %s.%s\n", | ||
| logoRecognitionAnnotationSegmentEndTimeOffset.getSeconds(), | ||
| logoRecognitionAnnotationSegmentEndTimeOffset.getNanos()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| // [END video_detect_logo_beta] |
Uh oh!
There was an error while loading. Please reload this page.