Assistance Required: Error in image analysis - No response content on Azure Cognitive Services Vision API

vievillage 60 Reputation points
2023-11-02T18:34:31.9633333+00:00

I have encountered an issue while trying to use Azure Cognitive Services Vision API in my ASP.NET application. Despite setting up the required permissions in Azure and configuring the app settings correctly, I keep receiving an error: "Error in image analysis: No response content" when I try to submit an image for analysis.

Here is a snippet from the code where the error occurs:

var client = new HttpClient();

client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _configuration.GetValue

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
2,784 questions
{count} votes

Accepted answer
  1. navba-MSFT 23,625 Reputation points Microsoft Employee
    2023-11-05T06:57:14.5866667+00:00

    @vievillage Thanks for getting back and sharing updates about my action plan. Since all the action plans shared in my above comment works fine, we have isolated that the issue is specific to your application.

    I have created a simple application which works fine. I am sharing the code here if that helps:

    using Microsoft.AspNetCore.Mvc;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net.Http;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Configuration;
    using Newtonsoft.Json;
    using System.Net.Http.Headers;
    
    
    namespace OpenAIWebAPi.Controllers
    {
        [Route("api/Home")]
        [ApiController]
        public class HomeController : ControllerBase
        {
            private readonly string _subscriptionKey = "771f958cXXXXXXXXXXXXXXXXX6da83";
            private readonly string _visionEndpoint = "https://VISIONNAME.cognitiveservices.azure.com/";
    
    
           
    
            [HttpPost]
            public async Task<IActionResult> Post(IFormFile file)
            {
                try
                {
                    var client = new HttpClient();
                    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _subscriptionKey);
    
                    byte[] data;
    
                    using (var br = new BinaryReader(file.OpenReadStream()))
                        data = br.ReadBytes((int)file.OpenReadStream().Length);
    
                    ByteArrayContent bytes = new ByteArrayContent(data);
                    bytes.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    
                    var response = await client.PostAsync(new Uri($"{_visionEndpoint}/computervision/imageanalysis:analyze?api-version=2023-02-01-preview&features=caption"), bytes);
                    response.EnsureSuccessStatusCode();
    
                    if (!response.IsSuccessStatusCode)
                    {
                        return BadRequest("Failed to analyze image. Please try again later.");
                    }
    
                    var responseJson = await response.Content.ReadAsStringAsync();
                    var captionResult = JsonConvert.DeserializeObject<dynamic>(responseJson).captionResult;
                    string captionText = null;
                    if (captionResult != null)
                    {
                        captionText = captionResult.text.ToString();
                    }
    
                    return Ok(captionText);
    
    
                }
                catch (Exception ex)
                {
                    return StatusCode(StatusCodes.Status500InternalServerError, $"Error in image analysis: {ex.Message}");
                }
            }
        }
        
    
    }
    
    

    You can then leverage the postman tool with request header Content-Type : multipart/form-data to send the request to this application as shown below and you should see 200 success status code. :
    User's image

    More info about the vision API is here.

    Hope this helps.

    **
    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.