Request body format for image tagging using computer vision api

Tarun Gupta 21 Reputation points Microsoft Employee
2022-03-28T07:13:20.337+00:00

We have a requirement to get image tags from image.

I am using this API - https://learn.microsoft.com/en-us/rest/api/computervision/3.1/tag-image-in-stream/tag-image-in-stream

I am unclear about the image format that needs to be provided in Request Body of the api, as most of the times, I use to get InvalidImageFormat error.
I am using this code.

HttpClient client = new HttpClient();  
client.BaseAddress = new Uri("https://{test}/vision/v3.1/tag?overload=stream");  
client.DefaultRequestHeaders.Add("Accept", "application/octet-stream");  
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{apiSubscriptionKey}");  
  
var url = "https://wallpapercave.com/wp/Iw471VJ.jpg";  
  
HttpClient client2 = new HttpClient();  
var getStream = client2.GetByteArrayAsync(url);  
getStream.Wait();  
var imgByteArr = getStream.Result;  
  
string binaryString = string.Join("", imgByteArr.Select(byt => Convert.ToString(byt, 2).PadLeft(8, '0')));  
var postTask = client.PostAsJsonAsync<string>(URL, binaryString);  
postTask.Wait();  
  
var result = postTask.Result;  

Please help me with this.

Thanks.

Azure Computer Vision
Azure Computer Vision
An Azure artificial intelligence service that analyzes content in images and video.
311 questions
{count} votes

Accepted answer
  1. romungi-MSFT 42,206 Reputation points Microsoft Employee
    2022-03-28T14:08:57.13+00:00

    @Tarun Gupta The sample to use the REST API with C# is available here. You just need to update the uriBase to point to /vision/3.1/tag and use a local image to get the result.

    If you already have the image as a URL then you can directly call the API by passing the image URI with the TagImage API.
    You can also use any supported SDK client to analyze the image.

    If an answer is helpful, please click on 130616-image.png or upvote 130671-image.png which might help other community members reading this thread.


1 additional answer

Sort by: Most helpful
  1. Tarun Gupta 21 Reputation points Microsoft Employee
    2022-04-25T07:49:29.497+00:00

    Hi,

    How to pass region specific information to cognitive service? To reduce latency in api response time.

    Please help.
    Thanks.

    0 comments No comments