使用自定义视觉预测 API

Using Custom Vision Predict API

我尝试使用自定义视觉服务。

响应此错误消息:

{"Code":"BadRequestImageFormat","Message":""}

附上我的代码和测试图像文件

请检查并帮助我

<form id="imageform" method="POST" enctype="multipart/form-data">
    <input type="file"/>
</form>


var data = new FormData(document.getElementById('imageform'));

    var url = "https://southcentralus.api.cognitive.microsoft.com/customvision/v1.0/Prediction/my-key/image";

    $.ajax({
           url : url,
           data : data,
           processData : false,
           contentType : "multipart/form-data",
           headers : {
                   'Prediction-key' : 'key'
           },
           type : 'POST',
           success : function(response) {
                   var result = response["Predictions"];

                   buildResult(result);
           },
                   error : function(request, status, error) {
           }
    });

我参考了这篇文档。

https://southcentralus.dev.cognitive.microsoft.com/docs/services/57982f59b5964e36841e22dfbfe78fc1/operations/5a3044f608fa5e06b890f164

这对我有用:

function readImage(element) {
  var file = element.files[0];
  var reader = new FileReader();
  reader.onloadend = function() {
    $.ajax({
      url: "https://southcentralus.api.cognitive.microsoft.com/customvision/v1.1/Prediction/KEY/image?iterationId=ITERATIONID",
      data: reader.result,
      processData: false,
      contentType: "application/octet-streama",
      headers: {
        'Prediction-key': 'YOUR-KEY'
      },
      type: 'POST',
      success: function(response) {
        var result = response["Predictions"];

        alert(result);
      },
      error: function(error) {
        alert('error: ' + error);
      }
    });
  }
  reader.readAsArrayBuffer(file);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <input type="file" onchange="readImage(this)" />
</form>