Reputation: 43
I was just playing around with the Cognitive-SpeakerRecognition and tried to enroll a new user. For that i followed the examples from the Microsoft API Reference. Sadly I get an error response:
{
"error": {
"code": "BadRequest",
"message": "Invalid Audio Format: Not a WAVE file - no RIFF header"
}
}
I spent a lot of time googleing the problem and found something related on github and StackOverflow. Unfortunately, I was not able to adapt the answer from github (They fixed the problem with a longer audio example, I tried but still getting the error response). So can someone figure out what I am doing wrong? Thanks for reading :) Here is my code:
public static void main(String[] args) {
HttpClient httpclient = HttpClients.createDefault();
try {
URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/spid/v1.0/identificationProfiles/{PROFIL}/enroll");
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
request.setHeader("Content-Type", "multipart/form-data");
request.setHeader("Ocp-Apim-Subscription-Key", "API_KEY");
FileInputStream someStream = new FileInputStream(new File("test.wav"));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int bytesRead;
byte[] bytes = new byte[1024];
while ((bytesRead = someStream.read(bytes)) > 0) {
byteArrayOutputStream.write(bytes, 0, bytesRead);
}
byte[] data = byteArrayOutputStream.toByteArray();
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entityBuilder.setStrictMode();
entityBuilder.addBinaryBody("enrollmentData", data, ContentType.MULTIPART_FORM_DATA, "test.wav");
request.setEntity(entityBuilder.build());
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
EDIT: Here you can download the file test.wav
Upvotes: 4
Views: 1061
Reputation: 2973
I think the multipart form data handling by this service is non-standard, which arguably is a bug. Either way, since you only have a single payload type, you should just do the following:
request.setEntity(
new FileEntity(new File("test.wav"), ContentType.APPLICATION_OCTET_STREAM));
Adding the full Java, reflecting the updated endpoint:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.HttpClients;
import java.io.File;
import java.net.URI;
public class EnrollSpeaker {
static String API_KEY = "YOUR-KEY";
static String PROFILE_ID = "YOUR-PROFILE-ID";
static String LOCATION = "westus"; // Check, might be different in the future
public static void main(String[] args) {
HttpClient httpclient = HttpClients.createDefault();
try {
URIBuilder builder = new URIBuilder(
String.format("https://%s.api.cognitive.microsoft.com/spid/v1.0/identificationProfiles/%s/enroll", LOCATION, PROFILE_ID));
URI uri = builder.build();
HttpPost request = new HttpPost(uri);
request.setHeader("Ocp-Apim-Subscription-Key", API_KEY);
request.setEntity(new FileEntity(new File("test.wav"), ContentType.APPLICATION_OCTET_STREAM));
HttpResponse response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
// Response is empty on success; the following will contain the URI where you can check the status
System.out.println(response.getHeaders("Operation-Location")[0].getValue());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Upvotes: 2