Azure 自定义视觉 API 为 JPG 图像返回不支持的媒体类型 url
Azure Custom Vision API returning unsupported media type for a JPG image url
我构建了一个小型 windows 表单应用程序,只是为了玩转图像识别。我训练了一个模型,并且构建了一个 windows 表单应用程序,它获取我的网络摄像头图像流,将帧作为 JPG 上传到 AWS S3 存储桶,然后将公开可读的 URL 传递给 Vision API 提供标签评分。
如果我通过 POSTMAN 调用传递图像,它工作正常,但是在我的代码中,我收到以下错误:
{StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
apim-request-id: 8d5759e5-d32a-4ba2-8b54-16f3a3f1aa40
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
Date: Thu, 20 Sep 2018 00:49:39 GMT
Content-Length: 116
Content-Type: application/json; charset=utf-8
}}
根据文档:
Response 415 Unsupported media type error. "Content-Type" does not
match the post content.
For image URL, "Content-Type" should be application/json For binary
image data, "Content-Type" should be application/octet-stream
正如您将从我下面的代码中看到的那样,我正在设置正确的内容类型。我只是 post 处理 posting 的函数到 API 因为这就是问题所在。任何帮助,将不胜感激。
首先,通过 POSTMAN 调用有效的证据:
这是我失败的方法。您可以看到我设置了正确的内容类型,并且在我的代码和 POSTMAN 之间使用相同的图像进行测试。
/// <summary>
/// Take a provided image url and return the Vision API results
/// </summary>
/// <param name="url">a valid HTTP or HTTPS url containing a reference to a JPG image</param>
/// <returns></returns>
static async Task<string> GenerateVisionResult(string url)
{
string visionResult = null;
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/3dd9e6c3-aac4-4da1-8c74-cd2d581d4782/url?iterationId=5c1b1548-98d7-45b0-a0e7-f397f35ffcd9");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Prediction-Key", "<redacted for post on Whosebug>");
var UrlBody = new UrlBody
{
Url = url
};
var httpRequestMessage = new HttpRequestMessage
{
Content = new StringContent(JsonConvert.SerializeObject(UrlBody),Encoding.UTF8),
Method = HttpMethod.Post
};
HttpResponseMessage httpResponse = await client.PostAsync(client.BaseAddress, httpRequestMessage.Content);
if (httpResponse.IsSuccessStatusCode)
{
visionResult = await httpResponse.Content.ReadAsStringAsync();
}
else
{
visionResult = httpResponse.StatusCode.ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return visionResult;
}
编辑:我想我刚刚意识到我的错误,我处理将 url 添加到 POST 方法的方式。一旦我尝试修复它,我会报告回来。
EDIT2:不。仍然没有骰子。我想也许我没有正确序列化 URL 对象,但我已经测试并且内容符合预期,只是序列化以下对象:
public class UrlBody
{
public string Url { get; set; }
}
感谢 Jamie 在 Fiddler 方面的帮助,但我发现了问题所在。您不能像我在原始代码中那样将内容传递给 POST 方法。您必须将内容转换为可以作为 HttpContent 传递给 post 方法的内容,例如
var content = JsonConvert.SerializeObject(url);
var buffer = System.Text.Encoding.UTF8.GetBytes(content);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
然后你会得到一个有效的回复。
在URL的末尾使用/image/(例如:“https://....//detect/iterations/Iteration37/image/nostore ")
Example postman
Binary
我构建了一个小型 windows 表单应用程序,只是为了玩转图像识别。我训练了一个模型,并且构建了一个 windows 表单应用程序,它获取我的网络摄像头图像流,将帧作为 JPG 上传到 AWS S3 存储桶,然后将公开可读的 URL 传递给 Vision API 提供标签评分。
如果我通过 POSTMAN 调用传递图像,它工作正常,但是在我的代码中,我收到以下错误:
{StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
apim-request-id: 8d5759e5-d32a-4ba2-8b54-16f3a3f1aa40
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
x-content-type-options: nosniff
Date: Thu, 20 Sep 2018 00:49:39 GMT
Content-Length: 116
Content-Type: application/json; charset=utf-8
}}
根据文档:
Response 415 Unsupported media type error. "Content-Type" does not match the post content.
For image URL, "Content-Type" should be application/json For binary image data, "Content-Type" should be application/octet-stream
正如您将从我下面的代码中看到的那样,我正在设置正确的内容类型。我只是 post 处理 posting 的函数到 API 因为这就是问题所在。任何帮助,将不胜感激。
首先,通过 POSTMAN 调用有效的证据:
/// <summary>
/// Take a provided image url and return the Vision API results
/// </summary>
/// <param name="url">a valid HTTP or HTTPS url containing a reference to a JPG image</param>
/// <returns></returns>
static async Task<string> GenerateVisionResult(string url)
{
string visionResult = null;
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://southcentralus.api.cognitive.microsoft.com/customvision/v2.0/Prediction/3dd9e6c3-aac4-4da1-8c74-cd2d581d4782/url?iterationId=5c1b1548-98d7-45b0-a0e7-f397f35ffcd9");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("Prediction-Key", "<redacted for post on Whosebug>");
var UrlBody = new UrlBody
{
Url = url
};
var httpRequestMessage = new HttpRequestMessage
{
Content = new StringContent(JsonConvert.SerializeObject(UrlBody),Encoding.UTF8),
Method = HttpMethod.Post
};
HttpResponseMessage httpResponse = await client.PostAsync(client.BaseAddress, httpRequestMessage.Content);
if (httpResponse.IsSuccessStatusCode)
{
visionResult = await httpResponse.Content.ReadAsStringAsync();
}
else
{
visionResult = httpResponse.StatusCode.ToString();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return visionResult;
}
编辑:我想我刚刚意识到我的错误,我处理将 url 添加到 POST 方法的方式。一旦我尝试修复它,我会报告回来。
EDIT2:不。仍然没有骰子。我想也许我没有正确序列化 URL 对象,但我已经测试并且内容符合预期,只是序列化以下对象:
public class UrlBody
{
public string Url { get; set; }
}
感谢 Jamie 在 Fiddler 方面的帮助,但我发现了问题所在。您不能像我在原始代码中那样将内容传递给 POST 方法。您必须将内容转换为可以作为 HttpContent 传递给 post 方法的内容,例如
var content = JsonConvert.SerializeObject(url);
var buffer = System.Text.Encoding.UTF8.GetBytes(content);
var byteContent = new ByteArrayContent(buffer);
byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
然后你会得到一个有效的回复。
在URL的末尾使用/image/(例如:“https://....//detect/iterations/Iteration37/image/nostore ")
Example postman Binary