【发布时间】:2017-01-05 02:46:20
【问题描述】:
我正在尝试使用here 描述的 Bing 图像的 Microsoft API
我只想通过在帖子请求的正文中发送图像来使用图像洞察力来查找相似图像,因为文档说我可以提供 url 或图像。
图像由手机摄像头捕获并发送到 api,目的是最终获得相似的图像结果。
起初我收到一个错误,说需要“q”参数,但我不想只使用图像搜索查询。
所以我将 ContentType 更改为“multipart/form-data”并使用“/search?modulesRequested=similarimages”
这似乎做了一些事情,因为现在我没有收到任何错误,api 响应只是一个空字符串,所以我真的在这里迷路了......
这是我发送请求的代码。
public async Task<string> GetImageInsights(byte[] image)
{
var uri = "https://api.cognitive.microsoft.com/bing/v5.0/images/search?modulesRequested=similarimages";
var response = await RequestHelper.MakePostRequest(uri, new string(Encoding.UTF8.GetChars(image)), key, "multipart/form-data");
var respString = await response.Content.ReadAsStringAsync();
return respString;
}
public static async Task<HttpResponseMessage> MakePostRequest(string uri, string body, string key, string contentType)
{
var client = new HttpClient();
// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key);
// Request body
byte[] byteData = Encoding.UTF8.GetBytes(body);
using (var content = new ByteArrayContent(byteData))
{
content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
return await client.PostAsync(uri, content);
}
}
我在 Android 上使用 C# 和 Xamarin
【问题讨论】:
标签: c# android image microsoft-cognitive