任何人都可以解释为什么我收到404回复的任何可能原因
var postResponse = await httpClient.PostAsync(BingApiUrl, content);
请在本文末尾的代码中致电。
此代码是从找到的here的GitHub代码中复制的。
我正在尝试搜索与我存储在项目资产中的三星Galaxy手机图像相匹配的其他产品图片。
我知道我的订阅密钥适用于使用文本搜索键进行图像搜索。现在我试图找到类似于存储在内存中的图像的图像,其中包含的字节作为我的POST请求的主体。
在要求提供类似图像之前,我是否需要首先按名称搜索Bing以获取三星图像,或者我是否可以纯粹基于内存中的图像进行搜索?
private static readonly string BingApiUrl = "https://api.cognitive.microsoft.com/bing/v7.0/images/search?modulesRequested=VisuallySimilarProducts&mkt=en-au&form=BCSPRD";
private async void btnImgSearchByDrawing_Click(object sender, RoutedEventArgs e)
{
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Wait, 1);
RandomAccessStreamReference streamRef = RandomAccessStreamReference.CreateFromUri(new Uri("ms-appx:///Assets/Samsung-Galaxy-S7-32GB-3-xl.jpg"));
IRandomAccessStreamWithContentType streamWithContent = await streamRef.OpenReadAsync();
Stream stream = streamWithContent.AsStreamForRead();
await GetSimilarProductImagesAsync(stream);
Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 1);
}
/// <summary>
/// Gets a list of visually similar products from an image stream.
/// </summary>
/// <param name="stream">The stream to an image.</param>
/// <returns>List of visually similar images.</returns>
public async Task<IList<ImageResult>> GetSimilarProductImagesAsync(Stream stream)
{
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
var strContent = new StreamContent(stream);
strContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { FileName = "Samsung-Galaxy-S7-32GB-3-xl.jpg" };
var content = new MultipartFormDataContent();
content.Add(strContent);
var postResponse = await httpClient.PostAsync(BingApiUrl, content);
var text = await postResponse.Content.ReadAsStringAsync();
var response = JsonConvert.DeserializeObject<BingImageResponse>(text);
return response?.VisuallySimilarProducts?.Select(i => new ImageResult
{
HostPageDisplayUrl = i.HostPageDisplayUrl,
HostPageUrl = i.HostPageUrl,
Name = i.Name,
ThumbnailUrl = i.ThumbnailUrl,
WebSearchUrl = i.WebSearchUrl
})
.ToList();
}
}
答案 0 :(得分:0)
您关联的代码使用Bing Search v5。你的是使用v7,它只支持发出GET请求。请参阅文档here。另请注意,调用必须具有强制q
参数。
答案 1 :(得分:0)
您还可以使用以下调用从图片网址中获取类似图片:
GET&#34; https://api.cognitive.microsoft.com/bing/v7.0/images/details?imgurl=Your_IMAGE_URL&mkt=en-us&modules=similarimages&#34; -H&#34; Ocp-Apim-Subscription-Key:YOUR_ACCESS_KEY&#34;
这也适用于v5。