Deployment error while trying to perform OCR [Azure] after capturing image to extract text

Vamika Satrasala 1 Reputation point
2021-11-05T04:58:41.583+00:00

I am trying to perform OCR on image captured on mobile camera and photos from user gallery to extract text but I keep getting deployment errors such as "system host not known"

async void UploadAndDetectButton_Clicked(System.Object sender, System.EventArgs e)
{

        var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
        {
            Title = "Please pick a photo"
        });
        if (result != null)
        {
            var stream = await result.OpenReadAsync();

            MyImage.Source = ImageSource.FromStream(() => stream);
        }
    }


    async void CaptureButton_Clicked(System.Object sender, System.EventArgs e)
     {
      var result = await MediaPicker.CapturePhotoAsync();
     if (result != null)
    {
      var stream = await result.OpenReadAsync();

            var client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(subscriptionKey)) { Endpoint = endpoint };


            var ocrResult = await client.ReadInStreamAsync(stream);

            var operationLocation = ocrResult.OperationLocation;
            const int numberOfCharsInOperationId = 36;
            string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);
            // Extract the text
            ReadOperationResult results;
            do
            {
                results = await client.GetReadResultAsync(Guid.Parse(operationId));
            }
            while ((results.Status == OperationStatusCodes.Running ||
            results.Status == OperationStatusCodes.NotStarted));
            // Display the text.
            var textResults = results.AnalyzeResult.ReadResults;
            foreach (ReadResult page in textResults)
            {
                string labelValue = "";
                foreach (Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models.Line line in page.Lines)
                {
                    labelValue += page.Lines;
                }


                Result1.Text = labelValue;
                OCRText = labelValue;
            }

            await TextToSpeech.SpeakAsync(OCRText, new SpeechOptions
            {
                Pitch = 0.5f
            });

            MyImage.Source = ImageSource.FromStream(() => stream);
    }
    }

}

I would like to know if there is something wrong or something I need to add for the code to perform ocr and also text-to-speech later on

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

1 answer

Sort by: Most helpful
  1. romungi-MSFT 41,961 Reputation points Microsoft Employee
    2021-11-05T11:08:10.603+00:00

    @Vamika Satrasala The most likely reason for the error could be the client is not initiated correctly with the right endpoint and subscription keys. Could you try something like below?

                ComputerVisionClient computerVision = new ComputerVisionClient(    
                    new ApiKeyServiceClientCredentials(subscriptionKey),    
                    new System.Net.Http.DelegatingHandler[] { });    
        
                // You must use the same region as you used to get your subscription    
                // keys. For example, if you got your subscription keys from westus,    
                // replace "westcentralus" with "westus".    
                //    
                // Free trial subscription keys are generated in the "westus"    
                // region. If you use a free trial subscription key, you shouldn't    
                // need to change the region.    
        
                // Specify the Azure region    
                computerVision.Endpoint = "https://centralindia.api.cognitive.microsoft.com";    
        
    

    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.