使用Java的Microsoft Cognitive-SpeakerRecognition BadRequst

时间:2016-10-09 00:37:42

标签: java wav voice-recognition microsoft-cognitive

我只是在玩Cognitive-SpeakerRecognition并试图注册一个新用户。为此,我按照Microsoft API Reference中的示例进行操作。可悲的是我收到了错误回复:

{
  "error": {
    "code": "BadRequest",
    "message": "Invalid Audio Format: Not a WAVE file - no RIFF header"
  }
}

我花了很多时间来搜索问题,并在githubStackOverflow上找到相关的内容。不幸的是,我无法调整github的答案(他们用更长的音频例子修复了问题,我尝试但仍然得到错误响应)。那么有人能弄清楚我做错了什么吗?感谢阅读:)这是我的代码:

 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());
        }
    }

编辑:您可以在此处下载文件test.wav

1 个答案:

答案 0 :(得分:2)

我认为这项服务的多部分表单数据处理是非标准的,这可能是一个错误。无论哪种方式,由于您只有一个有效负载类型,您应该执行以下操作:

request.setEntity(
    new FileEntity(new File("test.wav"), ContentType.APPLICATION_OCTET_STREAM));

添加完整的Java,反映更新的端点:

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());
        }
    }
}