2017-10-31 15 views
0

var postResponse = await httpClient.PostAsync(BingApiUrl, content); この記事の最後のコードで404の応答が返されている理由を説明できる人がいますか?視覚的に類似した製品のコグニティブ画像検索で404エラーが発生する

enter image description here

このコードはGitHubのコードからコピーされたhereを発見しました。

私は自分のプロジェクト資産に保存されているSamsung Galaxy携帯電話のイメージと一致する追加の製品イメージを検索しようとしています。

私のサブスクリプションキーは、テキスト検索キーを使用した画像検索に適しています。今私は、私のPOSTリクエストのボディとしてバイトが含まれているメモリに格納されたイメージに似たイメージを見つけようとしています。

類似の画像を要求する前に、サムスンの画像を最初に名前で検索する必要がありますか、または純粋にメモリ内の画像に基づいて検索を開始できますか?

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

あなたがリンクしたコードはBing Search v5を使用しています。あなたのユーザーはv7を使用しており、GETリクエストの発行のみサポートしています。ドキュメントhereを参照してください。また、呼び出しには必須のqパラメータが必要です。

+0

ご返信ありがとうございます。あなたが参照しているv7のドキュメントでは、imgUrlの下でこれを示しているので混乱しています。「洞察を得たい画像のURL」insightsTokenパラメータを使用して画像を指定する代わりに、このパラメータを使用します。イメージのバイナリをPOSTリクエストの本体に置くことによってイメージを作成します。バイナリオプションを使用する場合は、Content-Typeヘッダーを参照してください。 – Robert

+0

ええ、文書が奇妙です。そのパラメーターは別のエンドポイント用です。あなたのものは検索なので適用されません。そのページの各クエリパラメータを読み、それが/ searchに適用されるかどうかを確認する必要があります。 –

+0

もう一度おねがいします...私のアプリに既に保存されているものに似た画像を検索するためにapiを使用する方法があり、バイナリ形式でapiに提供されていると教えてください。私はちょうど自分のPCだけの画像をアップロードして、Webから似た画像を返すようにするBing/Imagesウェブページを模倣したいと思う。 Robert – Robert

関連する問題