Microsoft Cognitive-SpeakerRecognition BadRequst 使用 Java

Microsoft Cognitive-SpeakerRecognition BadRequst using Java

我只是在玩弄 Cognitive-SpeakerRecognition 并尝试注册一个新用户。为此,我遵循了 Microsoft API Reference 的示例。遗憾的是我收到错误响应:

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

我花了很多时间在 google 上搜索这个问题,并在 github and Whosebug 上找到了一些相关的东西。不幸的是,我无法改编 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

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

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