コード例 #1
0
        private void UpdateTextRecognition(TextRecognitionMode textRecognitionMode)
        {
            imageWithFacesControl.TextRecognitionMode = textRecognitionMode;

            var currentImageDisplay = this.imageWithFacesControl;

            if (currentImageDisplay.DataContext != null)
            {
                var img = currentImageDisplay.DataContext;

                ImageAnalyzer analyzer = (ImageAnalyzer)img;

                if (analyzer.TextOperationResult?.RecognitionResult != null)
                {
                    UpdateOcrTextBoxContent(analyzer);
                }
                else
                {
                    analyzer.TextRecognitionCompleted += (s, args) =>
                    {
                        UpdateOcrTextBoxContent(analyzer);
                    };
                }

                currentImageDisplay.DataContext = null;
                currentImageDisplay.DataContext = img;
            }
        }
コード例 #2
0
        private void UpdateTextRecognition(TextRecognitionMode textRecognitionMode)
        {
            if (this.imageFromCameraWithFaces == null || this.imageWithFacesControl == null)
            {
                return;
            }

            imageFromCameraWithFaces.TextRecognitionMode = textRecognitionMode;
            imageWithFacesControl.TextRecognitionMode    = textRecognitionMode;

            var currentImageDisplay = this.imageWithFacesControl.Visibility == Visibility.Visible ? this.imageWithFacesControl : this.imageFromCameraWithFaces;

            if (currentImageDisplay.DataContext != null)
            {
                var           img      = currentImageDisplay.DataContext;
                ImageAnalyzer analyzer = (ImageAnalyzer)img;
                if (analyzer.TextOperationResult?.RecognitionResult != null)
                {
                    UpdateOcrTextBoxContent(analyzer);
                }
                else
                {
                    analyzer.TextRecognitionCompleted += (s, args) =>
                    {
                        UpdateOcrTextBoxContent(analyzer);
                    };
                }
                currentImageDisplay.DataContext = null;
                currentImageDisplay.DataContext = img;
            }
        }
コード例 #3
0
        public async Task RecognizeTextAsync(TextRecognitionMode textRecognitionMode)
        {
            try
            {
                this.TextRecognitionMode = textRecognitionMode;
                if (this.ImageUrl != null)
                {
                    this.TextOperationResult = await VisionServiceHelper.RecognizeTextAsync(this.ImageUrl, textRecognitionMode);
                }
                else if (this.GetImageStreamCallback != null)
                {
                    this.TextOperationResult = await VisionServiceHelper.RecognizeTextAsync(this.GetImageStreamCallback, textRecognitionMode);
                }
            }
            catch (Exception ex)
            {
                ErrorTrackingHelper.TrackException(ex, "Vision API RecognizeTextAsync error");

                this.TextOperationResult = new TextOperationResult();

                if (this.ShowDialogOnFaceApiErrors)
                {
                    await ErrorTrackingHelper.GenericApiCallExceptionHandler(ex, "Vision API failed.");
                }
            }
            finally
            {
                this.TextRecognitionCompleted?.Invoke(this, EventArgs.Empty);
            }
        }
コード例 #4
0
        public async Task <IList <Line> > ExtractTextAsync(Stream image, TextRecognitionMode recognitionMode)
        {
            RecognizeTextInStreamHeaders headers = await _client.RecognizeTextInStreamAsync(image, recognitionMode);

            IList <Line> detectedLines = await GetTextAsync(_client, headers.OperationLocation);

            return(detectedLines);
        }
コード例 #5
0
        public async Task <RecognitionResult> RecognizeText(ImageFile imageFile, TextRecognitionMode textRecognitionMode)
        {
            RecognitionResult recognitionResult = _computerVisionRepository.GetRecognitionResult(imageFile, textRecognitionMode);

            if (recognitionResult == null)
            {
                recognitionResult = await RecognizeTextAndAdd(imageFile, textRecognitionMode);
            }
            return(recognitionResult);
        }
コード例 #6
0
        public async Task <RecognitionResult> RecognizeText(ImageFile imageFile, TextRecognitionMode textRecognitionMode)
        {
            await AddImageFileIfMissing(imageFile);

            if (imageFile.Size > _fileSizeLimit)
            {
                return(null);
            }
            RecognitionResult recognitionResult = await _analysisService.RecognizeText(imageFile, textRecognitionMode);

            return(recognitionResult);
        }
コード例 #7
0
        internal static string ToSerializedValue(this TextRecognitionMode value)
        {
            switch (value)
            {
            case TextRecognitionMode.Handwritten:
                return("Handwritten");

            case TextRecognitionMode.Printed:
                return("Printed");
            }
            return(null);
        }
コード例 #8
0
        // Read text from a remote image
        private static async Task ExtractTextFromUrlAsync(ComputerVisionClient computerVision, string imageUrl, int numberOfCharsInOperationId)
        {
            if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
            {
                Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl);
                return;
            }

            // For handwritten text, change to TextRecognitionMode.Handwritten
            TextRecognitionMode textRecognitionMode = TextRecognitionMode.Printed;

            // Start the async process to read the text
            BatchReadFileHeaders textHeaders = await computerVision.BatchReadFileAsync(imageUrl, textRecognitionMode);

            await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
        }
コード例 #9
0
        // Recognize text from a local image
        private static async Task ExtractTextFromStreamAsync(ComputerVisionClient computerVision, string imagePath, int numberOfCharsInOperationId)
        {
            if (!File.Exists(imagePath))
            {
                Console.WriteLine("\nUnable to open or read local image path:\n{0} \n", imagePath);
                return;
            }

            // For printed text, change to TextRecognitionMode.Printed
            TextRecognitionMode textRecognitionMode = TextRecognitionMode.Handwritten;

            using (Stream imageStream = File.OpenRead(imagePath))
            {
                // Start the async process to recognize the text
                BatchReadFileInStreamHeaders textHeaders = await computerVision.BatchReadFileInStreamAsync(imageStream, textRecognitionMode);
                await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
            }
        }
コード例 #10
0
ファイル: Vision.cs プロジェクト: Thomas-CV/cs-vision-text
        public static async Task <TextOperationResult> RecognizeText(string filename, string subscriptionKey, string serviceEndpoint)
        {
            ComputerVisionClient vision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(subscriptionKey), new DelegatingHandler[] { })
            {
                Endpoint = serviceEndpoint
            };

            TextRecognitionMode mode = TextRecognitionMode.Printed;

            using (Stream stream = File.OpenRead(filename))
            {
                try
                {
                    RecognizeTextInStreamHeaders headers = await vision.RecognizeTextInStreamAsync(stream, mode);

                    string id = headers.OperationLocation.Substring(headers.OperationLocation.LastIndexOf('/') + 1);

                    int count = 0;
                    TextOperationResult result;
                    do
                    {
                        result = await vision.GetTextOperationResultAsync(id);

                        if (result.Status == TextOperationStatusCodes.Succeeded || result.Status == TextOperationStatusCodes.Failed)
                        {
                            return(result);
                        }
                        await Task.Delay(DelayBetweenRetriesInMilliseconds);
                    }while ((result.Status == TextOperationStatusCodes.Running || result.Status == TextOperationStatusCodes.NotStarted) && count++ < MaximumNumberOfRetries);
                }
                catch
                {
                    // TODO: handle exception here
                }
            }
            return(null);
        }
コード例 #11
0
        /// <summary>
        /// Recognizes the text asynchronous.
        /// </summary>
        /// <param name="imageStream">The image stream.</param>
        /// <param name="mode">The recognition mode.</param>
        /// <returns>TextRecognitionOperation created</returns>
        public async Task <TextRecognitionOperation> CreateTextRecognitionOperationAsync(Stream imageStream, TextRecognitionMode mode)
        {
            string requestUrl = string.Format("{0}/recognizeText?mode={1}&{2}={3}", ServiceHost, mode.ToString(), _subscriptionKeyName, _subscriptionKey);
            var    request    = WebRequest.Create(requestUrl);

            return(await this.SendAsync <Stream, TextRecognitionOperation>("POST", imageStream, request).ConfigureAwait(false));
        }
コード例 #12
0
        /// <summary>
        /// Recognizes the text asynchronous.
        /// </summary>
        /// <param name="imageUrl">The image URL.</param>
        /// <param name="mode">The recognition mode.</param>
        /// <returns>TextRecognitionOperation created</returns>
        public async Task <TextRecognitionOperation> CreateTextRecognitionOperationAsync(string imageUrl, TextRecognitionMode mode)
        {
            string requestUrl = string.Format("{0}/recognizeText?mode={1}&{2}={3}", ServiceHost, mode.ToString(), _subscriptionKeyName, _subscriptionKey);
            var    request    = WebRequest.Create(requestUrl);

            dynamic requestObject = new ExpandoObject();

            requestObject.url = imageUrl;

            return(await this.SendAsync <ExpandoObject, TextRecognitionOperation>("POST", requestObject, request).ConfigureAwait(false));
        }
コード例 #13
0
        // Recognize text from a local image
        private static async Task RecognizeTextFromStreamAsync(ComputerVisionClient computerVision, string imagePath, int numberOfCharsInOperationId, TextRecognitionMode textRecognitionMode)
        {
            if (!File.Exists(imagePath))
            {
                Console.WriteLine("\nUnable to open or read local image path:\n{0} \n", imagePath);
                return;
            }

            using (Stream imageStream = File.OpenRead(imagePath))
            {
                // Start the async process to recognize the text
                RecognizeTextInStreamHeaders textHeaders = await computerVision.RecognizeTextInStreamAsync(imageStream, textRecognitionMode);
                await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
            }
        }
コード例 #14
0
        // Read text from a remote image
        private static async Task RecognizeTextFromUrlAsync(ComputerVisionClient computerVision, string imageUrl, int numberOfCharsInOperationId, TextRecognitionMode textRecognitionMode)
        {
            if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
            {
                Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl);
                return;
            }

            // Start the async process to read the text
            RecognizeTextHeaders textHeaders = await computerVision.RecognizeTextAsync(imageUrl, textRecognitionMode);

            await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
        }
コード例 #15
0
 /// <summary>
 /// Use this interface to get the result of a Read Document operation,
 /// employing the state-of-the-art Optical Character Recognition (OCR)
 /// algorithms optimized for text-heavy documents. When you use the Read
 /// Document interface, the response contains a field called
 /// "Operation-Location". The "Operation-Location" field contains the URL that
 /// you must use for your "Get Read Result operation" to access OCR results.​
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='image'>
 /// An image stream.
 /// </param>
 /// <param name='mode'>
 /// Type of text to recognize. Possible values include: 'Handwritten',
 /// 'Printed'
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <BatchReadFileInStreamHeaders> BatchReadFileInStreamAsync(this IComputerVisionClient operations, Stream image, TextRecognitionMode mode, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.BatchReadFileInStreamWithHttpMessagesAsync(image, mode, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Headers);
     }
 }
コード例 #16
0
 /// <summary>
 /// Recognize Text operation. When you use the Recognize Text interface, the
 /// response contains a field called 'Operation-Location'. The
 /// 'Operation-Location' field contains the URL that you must use for your Get
 /// Recognize Text Operation Result operation.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='mode'>
 /// Type of text to recognize. Possible values include: 'Handwritten',
 /// 'Printed'
 /// </param>
 /// <param name='url'>
 /// Publicly reachable URL of an image.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <RecognizeTextHeaders> RecognizeTextAsync(this IComputerVisionClient operations, string url, TextRecognitionMode mode, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.RecognizeTextWithHttpMessagesAsync(url, mode, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Headers);
     }
 }
        public static async Task <TextOperationResult> RecognizeTextAsync(Func <Task <Stream> > imageStreamCallback, TextRecognitionMode textRecognitionMode, bool detectOrientation = true)
        {
            RecognizeTextInStreamHeaders textHeaders = await client.RecognizeTextInStreamAsync(await imageStreamCallback(), textRecognitionMode);

            return(await GetTextRecognitionResultAsync(client, textHeaders.OperationLocation));
        }
        public static async Task <TextOperationResult> RecognizeTextAsync(string imageUrl, TextRecognitionMode textRecognitionMode, bool detectOrientation = true)
        {
            RecognizeTextHeaders textHeaders = await client.RecognizeTextAsync(imageUrl, textRecognitionMode);

            return(await GetTextRecognitionResultAsync(client, textHeaders.OperationLocation));
        }
コード例 #19
0
        private async Task <RecognitionResult> RecognizeTextAndAdd(ImageFile imageFile, TextRecognitionMode textRecognitionMode)
        {
            RecognitionResult   recognitionResult   = null;
            TextOperationResult textOperationResult = await UploadAndRecognizeImageAsync(imageFile.Path, textRecognitionMode);

            if (textOperationResult.Status == TextOperationStatusCodes.Succeeded)
            {
                recognitionResult = textOperationResult.RecognitionResult;
                _computerVisionRepository.AddRecognitionResult(imageFile, recognitionResult, textRecognitionMode);
            }
            return(recognitionResult);
        }
コード例 #20
0
        private async Task <TextOperationResult> UploadAndRecognizeImageAsync(string imageFilePath, TextRecognitionMode textRecognitionMode)
        {
            // https://github.com/Microsoft/Cognitive-Vision-Windows
            StorageFile storageFile = await StorageFile.GetFileFromPathAsync(imageFilePath);

            for (int attempt = 1; attempt <= 3; attempt++)
            {
                try
                {
                    using (Stream imageStream = await storageFile.OpenStreamForReadAsync())
                    {
                        return(await RecognizeAsync(
                                   async (ComputerVisionClient client) => await client.RecognizeTextInStreamAsync(imageStream, textRecognitionMode),
                                   headers => headers.OperationLocation));
                    }
                }
                catch (ComputerVisionErrorException ex)
                {
                    await HandleComputerVisionErrorException(ex);
                }
                catch (Exception ex)
                {
                }
            }
            return(null);
        }