I recently created an ASP.NET Core Web API project which communicates with a Computer Vision instance to generate image captions. To authenticate the API Server to Computer Vision, I use key credentials (I plan to switch to managed identity later). I call the Computer Vision instance using the following code:
var serviceOptions = new VisionServiceOptions(
"https://NAME.cognitiveservices.azure.com/",
new AzureKeyCredentials("KEY"));
using var imageSource = VisionSource.FromUrl(
new Uri("https://learn.microsoft.com/azure/cognitive-services/computer-vision/media/quickstarts/presentation.png"));
var analysisOptions = new ImageAnalysisOptions()
{
Features =
ImageAnalysisFeature.CropSuggestions
| ImageAnalysisFeature.Caption
| ImageAnalysisFeature.DenseCaptions
| ImageAnalysisFeature.Objects
| ImageAnalysisFeature.People
| ImageAnalysisFeature.Text
| ImageAnalysisFeature.Tags
};
var analyzer = new ImageAnalyzer(serviceOptions, imageSource, analysisOptions);
var result = analyzer.Analyze();
if (result.Reason == ImageAnalysisResultReason.Analyzed)
{
return Ok(result);
}
return NotFound(ImageAnalysisErrorDetails.FromResult(result));
Deploying this code to Azure App Service works flawlessly. Running the same code locally however, results in this error:
{
"message": "Unknown error in sending http request",
"errorCode": -1,
"reason": 5
}
Requesting the captions from the Computer Vision instance directly using curl like this also works locally without issue:
curl -H "Ocp-Apim-Subscription-Key: KEY" -H "Content-Type: application/json" "https://NAME.cognitiveservices.azure.com/computervision/imageanalysis:analyze?features=caption,read&model-version=latest&language=en&api-version=2023-02-01-preview" -d "{'url':'https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png'}"