-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
added gcs read for audio file #249
Changes from 5 commits
aaf5e91
e81f538
ed37b39
e84a183
aef1f1e
ef82a90
ca7cb9e
c1f92ac
00f367a
8051728
9ccfdf1
7b92531
8e01d28
edbc815
cecea6c
4605479
3ba3ea0
6a9f60e
c73f642
babbf11
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* 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 com.google.cloud.speech.grpc.demos; | ||
|
||
import com.google.cloud.speech.v1.AudioRequest; | ||
import com.google.cloud.storage.Blob; | ||
import com.google.cloud.storage.BlobId; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import com.google.protobuf.ByteString; | ||
|
||
import java.io.IOException; | ||
|
||
import java.net.URI; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
/* | ||
* AudioRequestFactory takes a URI as an input and creates an AudioRequest. The URI can point to a | ||
* local file or a file on google cloud storage. | ||
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. nit. Google Cloud Storage. |
||
*/ | ||
public class AudioRequestFactory { | ||
|
||
private static final String FILE = "file"; | ||
private static final String GS = "gs"; | ||
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. It's kind of terrible to name a string variable GS when it's contents are "gs". It doesn't really describe the intention. (File is okay, since that's what it is.) I think CLOUD_STORAGE would be more appropriate. 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. @tswast Its a uri scheme. CLOUD_STORAGE is not appropriate. Hmmm May be GS_SCHEME? 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. Actually see the thing is this final is longer than this "gs". Unnecessary. |
||
|
||
/** | ||
* Takes an input URI of form $scheme:// and converts to audio request. | ||
* | ||
* @param uri input uri | ||
* @return AudioRequest audio request | ||
*/ | ||
public static AudioRequest createRequest(URI uri) | ||
throws IOException { | ||
if (uri.getScheme() == null || uri.getScheme().equals(FILE)) { | ||
Path path = Paths.get(uri); | ||
return audioFromBytes(Files.readAllBytes(path)); | ||
} else if (uri.getScheme().equals(GS)) { | ||
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. @jerjou Can the speech API read directly from Cloud Storage in the way that the Vision API does? Or do we have to download and re-upload like this does? 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. I believe it can. https://cloud.google.com/speech/reference/rest/v1/speech/recognize#audiorequest Do not download and re-upload the image, just pass it in to the URI parameter. |
||
Storage storage = StorageOptions.defaultInstance().service(); | ||
String path = uri.getPath(); | ||
BlobId blobId = BlobId.of(uri.getHost(), path.substring(1,path.length())); | ||
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. nit. Missing space after comma. |
||
Blob blob = storage.get(blobId); | ||
return audioFromBytes(blob.content()); | ||
} | ||
throw new RuntimeException("scheme not supported " + uri.getScheme()); | ||
} | ||
|
||
/** | ||
* Convert bytes to AudioRequest. | ||
* | ||
* @param bytes input bytes | ||
* @return AudioRequest audio request | ||
*/ | ||
private static AudioRequest audioFromBytes(byte[] bytes) { | ||
return AudioRequest.newBuilder() | ||
.setContent(ByteString.copyFrom(bytes)) | ||
.build(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this
gcloud-java
dependency necessary? I don't see where you use anything from this package.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, nevermind, I see the storage imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually after switching to use the URI parameter, https://cloud.google.com/speech/reference/rest/v1/speech/recognize#audiorequest you should not need this dependency.