编程语言
首页 > 编程语言> > 使用Java的Microsoft Cognitive-SpeakerRecognition BadRequst

使用Java的Microsoft Cognitive-SpeakerRecognition BadRequst

作者:互联网

我只是在玩Cognitive-SpeakerRecognition并试图注册一个新用户.为此,我按照Microsoft API Reference中的示例.可悲的是我得到一个错误响应:

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

我花了很多时间googleing这个问题并在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

解决方法:

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

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

标签:java,wav,voice-recognition,microsoft-cognitive
来源: https://codeday.me/bug/20190702/1352537.html