/// <summary>
        /// ローカルの画像のストリームからテキストをOCRして抽出します。
        /// </summary>
        /// <returns>OCRした結果の各地域のテキストを返します。</returns>
        /// <param name="imageStream">Image stream.</param>
        public async Task <List <string> > ExtractLocalTextAsync(Stream imageStream)
        {
            // ComputerVisionClient の準備
            var computerVisionClient = new ComputerVisionClient(new ApiKeyServiceClientCredentials(Secrets.ComputerVisionApiKey),
                                                                new System.Net.Http.DelegatingHandler[] { })
            {
                Endpoint = Secrets.ComputerVisionEndpoint
            };

            try
            {
                // API 呼び出し、結果取得
                var ocrResult = await computerVisionClient.RecognizePrintedTextInStreamAsync(true, imageStream, OcrLanguages.Ja);

                return(GetRegionTextAsync(ocrResult));
            }
            catch (ComputerVisionErrorException e)
            {
                Debug.WriteLine($"imageStream: {e.Message}");
            }
            catch (Exception e)
            {
                Debug.WriteLine($"Unknown error: {e.Message}");
            }

            return(new List <string> {
                "Could not recognize text."
            });
        }
        public static void Run(
            [BlobTrigger("new/{name}", Connection = "CnxStorageAccountDemo")] Stream myBlob,                    // the blob that trigger the function executation
            string name,                                                                                        // name of the blob (like found in container in the storage account
            [Queue("readyfortranslation", Connection = "CnxStorageAccountDemo")] out string messageToTranslate, // OUTput paremeters used bye function runtime to push a new message in the queue named 'readyfortranslation', using the same storage account connexion as the blobtrigger
            ILogger log                                                                                         // logger interface
            )
        {
            log.LogInformation($"OCRing blob [{name}]   Size: {myBlob.Length} Bytes");

            var visionClient = new ComputerVisionClient(new ApiKeyServiceClientCredentials(System.Environment.GetEnvironmentVariable("csVisionKey")))
            {
                Endpoint = System.Environment.GetEnvironmentVariable("csVisionEndpoint") // AppSetting are automatically mapped on EnvironmentVariable
            };

            var imgOcr = visionClient.RecognizePrintedTextInStreamAsync(true, myBlob).GetAwaiter().GetResult();

            if (imgOcr != null)
            {
                string textToTranslate = GetTextFromOcrResult(imgOcr);
                messageToTranslate = $"{name}##{textToTranslate}";
                // HACK : the previous line use a simple formatting to send the blob name AND the ocr text to the next function.
                // In real life, we must save the original text+langguage in a DB, push only the name of the blob
                // eg : we can use a CosmosDB database with json document linkied to each scanned document. this json doc will be enriching by each function (OCr, then translation, then feature extraction, ...)

                log.LogInformation("MESSAGE pushed to queue : " + messageToTranslate);
            }
            else
            {
                messageToTranslate = string.Empty;
                log.LogError($"ERROR WHILE OCRING BLOB {name}!");
            }
        }
示例#3
0
        static async Task OcrImage(string imgPath)
        {
            Console.Write("OCRing " + imgPath + " ...");

            using (var stImg = new FileStream(imgPath, FileMode.Open))
            {
                var visionClient = new ComputerVisionClient(new ApiKeyServiceClientCredentials(DemoSettings.csVisionKey))
                {
                    Endpoint = DemoSettings.csVisionEndpoint
                };

                var imgOcr = await visionClient.RecognizePrintedTextInStreamAsync(true, stImg);

                if (imgOcr != null)
                {
                    Console.WriteLine(" Ok.");
                    PrintOcrResult(imgOcr);
                    string textToTranslate = GetTextFromOcrResult(imgOcr);
                    Console.WriteLine($"Original text : {textToTranslate}");
                    await TranslateToFrench(textToTranslate);
                }
                else
                {
                    Console.WriteLine(" ERROR !");
                }
            }
        }
示例#4
0
        public async Task <CognitiveStep> CognitivePipeline_OCR([ActivityTrigger] CognitiveStep input, ILogger log)
        {
            log.LogInformation($"******* Starting OCR");

            string key      = GlobalSettings.GetKeyValue("computerVisionKey");
            string endpoint = GlobalSettings.GetKeyValue("computerVisionEndpoint");

            ComputerVisionClient computerVision = new ComputerVisionClient(
                new Microsoft.Azure.CognitiveServices.Vision.ComputerVision.ApiKeyServiceClientCredentials(key),
                new System.Net.Http.DelegatingHandler[] { })
            {
                Endpoint = endpoint
            };

            var data = await filesStorageRepo.GetFileAsync(input.FileUrl);

            var ocrResult = await computerVision.RecognizePrintedTextInStreamAsync(true, new MemoryStream(data));

            input.IsSuccessful  = true;
            input.Confidence    = -1;
            input.LastUpdatedAt = DateTime.UtcNow;
            input.RawOutput     = JsonConvert.SerializeObject(ocrResult);

            return(input);
        }
        /// <summary>
        /// Uploads the image to Cognitive Services and performs OCR.
        /// </summary>
        /// <param name="imageFilePath">The image file path.</param>
        /// <param name="language">The language code to recognize.</param>
        /// <returns>Awaitable OCR result.</returns>
        private async Task <OcrResult> UploadAndRecognizeImageAsync(string imageFilePath, OcrLanguages language)
        {
            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE STARTS HERE
            // -----------------------------------------------------------------------

            //
            // Create Cognitive Services Vision API Service client.
            //
            using (var client = new ComputerVisionClient(Credentials)
            {
                Endpoint = Endpoint
            })
            {
                Log("ComputerVisionClient is created");

                using (Stream imageFileStream = File.OpenRead(imageFilePath))
                {
                    //
                    // Upload an image and perform OCR.
                    //
                    Log("Calling ComputerVisionClient.RecognizePrintedTextInStreamAsync()...");
                    OcrResult ocrResult = await client.RecognizePrintedTextInStreamAsync(!DetectOrientation, imageFileStream, language);

                    return(ocrResult);
                }
            }

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
示例#6
0
        public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req, TraceWriter log)
        {
            String apiKey  = Environment.GetEnvironmentVariable("APIKey");
            String apiRoot = Environment.GetEnvironmentVariable("APIRoot");

            try
            {
                var form = await req.ReadFormAsync();

                var pictureFile = form.Files.GetFile("receipt");
                var stream      = pictureFile.OpenReadStream();
                ComputerVisionClient computerVision = new ComputerVisionClient(
                    new ApiKeyServiceClientCredentials(apiKey),
                    new System.Net.Http.DelegatingHandler[] { });
                computerVision.Endpoint = apiRoot;

                OcrResult result = await computerVision.RecognizePrintedTextInStreamAsync(true, stream, OcrLanguages.En);

                log.Info("C# HTTP trigger function processed a request.");
                var list = ReceiptEngine.GroupIntoRows(result);
                return((ActionResult) new OkObjectResult(Newtonsoft.Json.JsonConvert.SerializeObject(new object[] { list, result.Regions })));
            }
            catch (Exception e)
            {
                log.Error("Could not Process", e);
                return(new BadRequestObjectResult($"Could not process result: {e.Message}"));
            }
        }
示例#7
0
        public async Task <string> ExtractTextAsync(Stream imageStream)
        {
            var requestResult = await mClient.RecognizePrintedTextInStreamAsync(false, imageStream).ConfigureAwait(false);

            return(string.Join('\n', requestResult.Regions.SelectMany(l => l.Lines)));
            //// Poll for result
            //return await GetTextAsync(mClient, requestResult.).ConfigureAwait(false);
        }
示例#8
0
        private async Task <OcrResult> UploadAndRecognizeImage(StorageFile imageFile, OcrLanguages language)
        {
            var stream = await imageFile.OpenStreamForReadAsync();

            Log("Calling VisionServiceClient.RecognizeTextAsync()...");
            var ocrResult = await VisionServiceClient.RecognizePrintedTextInStreamAsync(true, stream, language);

            return(ocrResult);
        }
示例#9
0
        private void ClipboardNotification_ClipboardUpdate(object sender, EventArgs e)
        {
            try
            {
                //check clipboard for image
                if (Clipboard.ContainsImage())
                {
                    var image = Clipboard.GetImage();
                    image = EnsureImageSize(image);
                    //send to ocr
                    var cli = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
                    {
                        Endpoint = endpoint
                    };
                    //var task = cli.RecognizeTextInStreamAsync(image.ToStream(ImageFormat.Png), Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models.TextRecognitionMode.Handwritten);
                    var imageStream = image.ToStream(ImageFormat.Png);
                    var task        = cli.RecognizePrintedTextInStreamAsync(false, imageStream);
                    task.Wait();

                    //copy image that was sent
                    imageStream = image.ToStream(ImageFormat.Png);
                    imageStream.Seek(0, SeekOrigin.Begin);
                    var bytes = new byte[imageStream.Length];
                    imageStream.Read(bytes, 0, (int)imageStream.Length);
                    File.WriteAllBytes(@"c:\temp\last.png", bytes);

                    //dump text to form
                    StringBuilder sb = new StringBuilder();

                    task.Result.Regions.ToList().ForEach(r => r.Lines.ToList().ForEach(l =>
                    {
                        l.Words.ToList().ForEach(w => sb.Append(w.Text + " "));
                        if (cbRetainNewlines.Checked)
                        {
                            _ = sb.AppendLine();
                        }
                    }));
                    var extractedText = sb.ToString().Replace(" \n", "\n");
                    ComplianceRegExes.Select(r => new Regex(r)).ToList().ForEach(r => {
                        extractedText = r.Replace(extractedText, "[PII/PCI REDACTED]");
                    });
                    tbResults.Text = extractedText;
                    if (cbAutoCopy.Checked)
                    {
                        if (tbResults.Text.Length > 0)
                        {
                            Clipboard.SetText(tbResults.Text);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                tbResults.Text = $"Error: {ex}";
            }
        }
        public static async Task <OcrResult> RecognizeTextFromImageLocal(ComputerVisionClient client, string localFile, bool detectOrientation, OcrLanguages language = OcrLanguages.En)
        {
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine($"Recognize Printed Text (OCR) From a local image FILE: {Path.GetFileName(localFile)} ");
            Console.WriteLine();

            // Read text from URL
            var ocrResults = await client.RecognizePrintedTextInStreamAsync(detectOrientation, File.OpenRead(localFile), language);

            return(ocrResults);
        }
示例#11
0
        static void Main(string[] args)
        {
            var client = new ComputerVisionClient(
                new ApiKeyServiceClientCredentials(cognitiveServicesKey));

            client.Endpoint = cognitiveServicesUrl;

            var location      = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var fileName      = Path.Combine(location, "mybill.jpg");
            var fileToProcess = File.OpenRead(fileName);
            var apiResult     = client.RecognizePrintedTextInStreamAsync(false, fileToProcess);

            apiResult.Wait();

            var ocrResult = apiResult.Result;

            var words = new Dictionary <System.Drawing.Point, string>();

            foreach (var r in ocrResult.Regions)
            {
                foreach (var l in r.Lines)
                {
                    foreach (var w in l.Words)
                    {
                        var coordinates = w.BoundingBox.Split(',');

                        //Console.WriteLine($"Found word {w.Text} at" +
                        //$" X {coordinates[0]} and Y {coordinates[1]}");

                        words.Add(new System.Drawing.Point
                        {
                            X = int.Parse(coordinates[0]),
                            Y = int.Parse(coordinates[1])
                        }, w.Text);
                    }
                }
            }

            var dateWord = words.FirstOrDefault(x => x.Value == "Date");
            int yOffset  = 6; // the Y value can vary slightly for words on the same line
            int lookupX  = dateWord.Key.X;
            int lookupY1 = dateWord.Key.Y - yOffset;
            int lookupY2 = dateWord.Key.Y + yOffset;

            var nearestWord = words.Where(w => w.Key.X > lookupX &&
                                          w.Key.Y >= lookupY1 && w.Key.Y <= lookupY2).FirstOrDefault();

            Console.WriteLine("Date found: " + nearestWord.Value);
        }
示例#12
0
        private async void AddPhotoButton_Clicked(object sender, EventArgs e)
        {
            try
            {
                // 1. Add camera logic.
                await CrossMedia.Current.Initialize();

                MediaFile photo;
                //if (CrossMedia.Current.IsCameraAvailable)
                //{
                //    photo = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
                //    {
                //        Directory = "OrCeIT",
                //        Name = "Orce.jpg"
                //    });
                //}
                //else
                //{
                photo = await CrossMedia.Current.PickPhotoAsync();

                //}
                // 2. Add  OCR logic.

                OcrResult text;


                using (var client = new ComputerVisionClient(Credentials)
                {
                    Endpoint = "https://westcentralus.api.cognitive.microsoft.com"
                })
                {
                    using (photoStream = photo.GetStream())
                    {
                        text = await client.RecognizePrintedTextInStreamAsync(!DetectOrientation, photoStream, OcrLanguages.En);
                    }
                }


                //await UploadAndRecognizeImageAsync(photoStream.ToString() , OcrLanguages.En);
                // 3. Add to data-bound collection.

                textToTranslateEntry.Text = OcrStringBuilder(text);                             // Add to data-bound collection.
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine($"ERROR: {ex.Message}");
                await DisplayAlert("Error", ex.Message, "OK");
            }
        }
示例#13
0
        // [HttpPost]
        // [Route("api/Ocr/GetLines")]
        // public async Task<ActionResult<string>> GetLines()
        // {
        //     return this.Ok("Test");
        // }

        private async Task<OcrResult> AzureOcr(Stream fileStream)
        {
            string subscriptionKey = "";
            string endpoint = "";

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

            var analysis = await computerVision.RecognizePrintedTextInStreamAsync(true, fileStream);

            //var result = DisplayResults(analysis);

            return analysis;
        }
示例#14
0
        private static async Task OCRFromStreamAsync(ComputerVisionClient computerVision, string imagePath)
        {
            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))
            {
                OcrResult analysis = await computerVision.RecognizePrintedTextInStreamAsync(true, imageStream);

                Console.WriteLine(imagePath);
                DisplayResults(analysis);
            }
        }
示例#15
0
        public static async Task <byte[]> ReplaceWordsInImage(
            ComputerVisionClient client,
            byte[] originalImage,
            string[] wordsToReplace,
            decimal scaleUp = 1)
        {
            IImageFormat imgFormat;

            using (var slxImage = Image.Load(originalImage, out imgFormat)) {
                var oldSize         = new Size(slxImage.Width, slxImage.Height);
                var maxWidthRatio   = Convert.ToDecimal(4200d / slxImage.Width);
                var maxHeightRation = Convert.ToDecimal(4200d / slxImage.Height);
                var maxRatio        = maxWidthRatio < maxHeightRation ? maxWidthRatio : maxHeightRation;
                maxRatio = (maxRatio < scaleUp) ? maxRatio : scaleUp;
                var newSize = new Size(Convert.ToInt32(slxImage.Width * maxRatio), Convert.ToInt32(slxImage.Height * maxRatio));
                using (var ms = new MemoryStream()) {
                    // There is a real possibility that the image requires scaling up to read text at small resolutions (I've had issues at 1920 x 1080).
                    // So, we need the option to resize the image upwards.
                    if (maxRatio > 1)
                    {
                        slxImage.Mutate(ctx => ctx.Resize(newSize));
                    }
                    // Azure Computer Vision requires a stream, hence the save to MemoryStream
                    slxImage.Save(ms, imgFormat);
                    ms.Seek(0, SeekOrigin.Begin);
                    var result = await client.RecognizePrintedTextInStreamAsync(false, ms);

                    var bounds = result.Coordinates(wordsToReplace);

                    foreach (var boundingBox in bounds)
                    {
                        var rect = new Rectangle(boundingBox[0], boundingBox[1], boundingBox[2], boundingBox[3]);
                        slxImage.Mutate(ctx => ctx.BoxBlur(rect.Width / 2, rect));
                    }

                    if (maxRatio > 1)
                    {
                        slxImage.Mutate(ctx => ctx.Resize(oldSize));
                    }

                    using (var resultMs = new MemoryStream()) {
                        slxImage.Save(resultMs, imgFormat);
                        return(resultMs.ToArray());
                    }
                }
            }
        }
        /// <summary>
        /// Uploads the image to Cognitive Services and performs OCR.
        /// </summary>
        /// <param name="inputImage">The image byte[] to be analized.</param>
        /// <param name="language">The language code to recognize. Use Unk for automatic detection</param>
        /// <returns>Awaitable OCR result.</returns>
        public async Task <OcrResult> RecognizeImageOCRAsync(byte[] inputImage, OcrLanguages language)
        {
            // Create Cognitive Services Vision API Service client.
            using (var client = new ComputerVisionClient(Credentials)
            {
                Endpoint = Endpoint
            })
            {
                Log.LogInformation("ComputerVisionClient is created");

                // Upload an image and perform OCR.
                Log.LogInformation("Calling ComputerVisionClient.RecognizePrintedTextInStreamAsync()...");
                OcrResult ocrResult = await client.RecognizePrintedTextInStreamAsync(!DetectOrientation, new MemoryStream(inputImage), language);

                return(ocrResult);
            }
        }
示例#17
0
        /*
         * END - RECOGNIZE PRINTED TEXT - URL IMAGE
         */


        /*
         * RECOGNIZE PRINTED TEXT - LOCAL IMAGE
         */
        public static async Task RecognizePrintedTextLocal(ComputerVisionClient client, string localImage)
        {
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("RECOGNIZE PRINTED TEXT - LOCAL IMAGE");
            Console.WriteLine();

            using (Stream stream = File.OpenRead(localImage))
            {
                Console.WriteLine($"Performing OCR on local image {Path.GetFileName(localImage)}...");
                Console.WriteLine();
                // Get the recognized text
                OcrResult localFileOcrResult = await client.RecognizePrintedTextInStreamAsync(true, stream);

                // Display text, language, angle, orientation, and regions of text from the results.
                Console.WriteLine("Text:");
                Console.WriteLine("Language: " + localFileOcrResult.Language);
                Console.WriteLine("Text Angle: " + localFileOcrResult.TextAngle);
                Console.WriteLine("Orientation: " + localFileOcrResult.Orientation);
                Console.WriteLine();
                Console.WriteLine("Text regions: ");

                // Getting only one line of text for testing purposes. To see full demonstration, remove the counter & conditional.
                int counter = 0;
                foreach (var localRegion in localFileOcrResult.Regions)
                {
                    Console.WriteLine("Region bounding box: " + localRegion.BoundingBox);
                    foreach (var line in localRegion.Lines)
                    {
                        Console.WriteLine("Line bounding box: " + line.BoundingBox);

                        if (counter == 1)
                        {
                            Console.WriteLine();
                            return;
                        }
                        counter++;

                        foreach (var word in line.Words)
                        {
                            Console.WriteLine("Word bounding box: " + word.BoundingBox);
                            Console.WriteLine("Text: " + word.Text);
                        }
                    }
                }
            }
        }
示例#18
0
        // Recognize the text in a Stream with MSOCR
        private static async Task <OcrResult> RecognizeTextInStreamAsync(Stream stream, TraceWriter log, string logHeader)
        {
            var subscriptionKey = ConfigurationManager.AppSettings["MSOCRSubscriptionKey"];

            using (var computerVision = new ComputerVisionClient(
                       new ApiKeyServiceClientCredentials(subscriptionKey),
                       new System.Net.Http.DelegatingHandler[] { })
            {
                // E.g. Service running in West US region: https://westus.api.cognitive.microsoft.com. No Docker container support.
                Endpoint = ConfigurationManager.AppSettings["MSOCREndpoint"]
            })
            {
                log.Info($"{logHeader} Recognizing text with MSOCR...");

                return(await computerVision.RecognizePrintedTextInStreamAsync(true, stream, OcrLanguages.En));
            }
        }
示例#19
0
        public static async Task GetTextFromImage(ComputerVisionClient client, string imageFileName)
        {
            using (Stream stream = File.OpenRead(imageFileName))
            {
                // Get the recognized text
                OcrResult localFileOcrResult = await client.RecognizePrintedTextInStreamAsync(true, stream);

                // Display text, language, angle, orientation, and regions of text from the results.
                Console.WriteLine("File Name: " + imageFileName);
                Console.WriteLine("Language: " + localFileOcrResult.Language);
                Console.WriteLine("Text Angle: " + localFileOcrResult.TextAngle);
                Console.WriteLine("Orientation: " + localFileOcrResult.Orientation);
                Console.WriteLine();
                Console.WriteLine("Text regions: ");

                // Getting only one line of text for testing purposes. To see a full demonstration, remove the counter & conditional.
                int counter = 0;
                foreach (var localRegion in localFileOcrResult.Regions)
                {
                    Console.WriteLine("Region bounding box: " + localRegion.BoundingBox);
                    foreach (var line in localRegion.Lines)
                    {
                        Console.WriteLine("Line bounding box: " + line.BoundingBox);
                        counter++;

                        foreach (var word in line.Words)
                        {
                            Console.WriteLine("Word bounding box: " + word.BoundingBox);
                            Console.WriteLine("Text: " + word.Text);

                            // Validate using RegEx
                            Regex           rx      = new Regex(@"^[2-9]\d{2}-\d{3}-\d{4}$", RegexOptions.IgnorePatternWhitespace);
                            MatchCollection matches = rx.Matches(word.Text);

                            if (matches.Count >= 1)
                            {
                                Console.WriteLine($"Phone Number Found: {matches.First()}");
                                await IsTwilioVerified(matches.First().ToString());
                            }
                        }
                    }
                }
            }
        }
示例#20
0
        internal static async Task <OcrResult> UploadAndRecognizeImageAsync(string imageFilePath, OcrLanguages language)
        {
            string key         = "0d8a60b23e9b4441b748d01c93c8a88f";
            string endPoint    = "https://pruebaiavision.cognitiveservices.azure.com/";
            var    credentials = new ApiKeyServiceClientCredentials(key);

            using (var client = new ComputerVisionClient(credentials)
            {
                Endpoint = endPoint
            })
            {
                using (Stream imageFileStream = File.OpenRead(imageFilePath))
                {
                    OcrResult ocrResult = await client.RecognizePrintedTextInStreamAsync(false, imageFileStream, language);

                    return(ocrResult);
                }
            }
        }
示例#21
0
        internal static async Task <OcrResult> UploadAndRecognizeImageAsync(string imageFilePath, OcrLanguages language)
        {
            string key         = ConfigurationManager.AppSettings["ComputerVisionApiKey"];
            string endPoint    = ConfigurationManager.AppSettings["ComputerVisionEndpoint"];
            var    credentials = new ApiKeyServiceClientCredentials(key);

            using (var client = new ComputerVisionClient(credentials)
            {
                Endpoint = endPoint
            })
            {
                using (Stream imageFileStream = File.OpenRead(imageFilePath))
                {
                    OcrResult ocrResult = await client.RecognizePrintedTextInStreamAsync(false, imageFileStream, language);

                    return(ocrResult);
                }
            }
        }
示例#22
0
        private async Task <OcrResult> UploadAndRecognizeImageAsync(string imageFilePath, OcrLanguages language)
        {
            //
            // Create Cognitive Services Vision API Service client.
            //
            using (var client = new ComputerVisionClient(Credentials)
            {
                Endpoint = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0"
            })
            {
                using (Stream imageFileStream = File.OpenRead(imageFilePath))
                {
                    //
                    // Upload an image and perform OCR.
                    //

                    OcrResult ocrResult = await client.RecognizePrintedTextInStreamAsync(!DetectOrientation, imageFileStream, language);

                    return(ocrResult);
                }
            }
        }
示例#23
0
        public static async Task TextExtractionCore(string fname)
        {
            List <string> strList = new List <string>();

            using (Stream stream = File.OpenRead(fname))
            {
                ComputerVisionClient client = Authenticate(API_key, API_location);
                OcrResult            ocrRes = await client.RecognizePrintedTextInStreamAsync(true, stream);

                foreach (var localRegion in ocrRes.Regions)
                {
                    foreach (var line in localRegion.Lines)
                    {
                        strList.Add(GetLineAsString(line));
                    }
                }

                Console.WriteLine("Date: " +
                                  GetDate(strList.ToArray()));
                Console.WriteLine("Highest amount: " +
                                  HighestAmount(strList.ToArray()));
            }
        }
示例#24
0
        // Recognize text from a local image
        private static async Task ExtractLocalTextAsync(
            ComputerVisionClient computerVision, string imagePath)
        {
            if (!File.Exists(imagePath))
            {
                Console.WriteLine(
                    "\nUnable to open or read localImagePath:\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);

                OcrResult result = await computerVision.RecognizePrintedTextInStreamAsync(true, imageStream);

                if (result.Regions != null)
                {
                    foreach (OcrRegion region in result.Regions)
                    {
                        foreach (OcrLine line in region.Lines)
                        {
                            foreach (OcrWord word in line.Words)
                            {
                                Console.WriteLine(word.Text);
                            }
                        }
                    }
                }


                //await GetTextAsync(computerVision, textHeaders.OperationLocation);
            }
        }
示例#25
0
        public static async Task <ResultOCR> Process(Image image, OcrLanguages ocrLanguages, FormatImageCognitiveService formatImage)
        {
            bool   result           = false;
            string error            = null;
            string text             = null;
            Bitmap destinationImage = null;

            try
            {
                IComputerVisionClient client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
                {
                    Endpoint = OCR.endPoint
                };

                if ((image.Height < dimensioneMin) || (image.Width < dimensioneMin))
                {
                    int newW = image.Width < dimensioneMin ? dimensioneMin + delta : image.Width;
                    int newH = image.Height < dimensioneMin ? dimensioneMin + delta : image.Height;

                    destinationImage = new Bitmap(newW, newH);

                    using (Graphics g = Graphics.FromImage(destinationImage))
                    {
                        g.DrawImage(image, new Rectangle((newW - image.Width) / 2, (newH - image.Height) / 2, image.Width, image.Height), new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
                    }

                    image = destinationImage;

                    //image.Save(@"c:\temp\prova.png",ImageFormat.Png);
                }

                if ((image.Height > dimensioneMax) || (image.Width > dimensioneMax))
                {
                    throw new ApplicationException("Capture a smaller area!");
                }

                ImageFormat format = ImageFormat.Png;
                if (formatImage == FormatImageCognitiveService.jpeg)
                {
                    format = ImageFormat.Jpeg;
                }
                else if (formatImage == FormatImageCognitiveService.bmp)
                {
                    format = ImageFormat.Bmp;
                }

                using (Stream imageFileStream = new MemoryStream())
                {
                    image.Save(imageFileStream, format);

                    if (Helper.ConvertBytesToMegabytes(imageFileStream.Length) > 4d)
                    {
                        throw new ApplicationException("Capture a smaller area!");
                    }

                    imageFileStream.Seek(0, SeekOrigin.Begin);

                    OcrResult t = await client.RecognizePrintedTextInStreamAsync(false, imageFileStream, ocrLanguages);

                    if (t.Regions.Count == 0)
                    {
                        throw new ApplicationException("Failed to convert the text!");
                    }


                    text = string.Join("\n",
                                       t.Regions.ToList().Select(region =>
                                                                 string.Join("\n", region.Lines.ToList().Select(line =>
                                                                                                                string.Join(" ", line.Words.ToList().Select(word =>
                                                                                                                                                            word.Text).ToArray())).ToArray())).ToArray());


                    if (!string.IsNullOrWhiteSpace(text))
                    {
                        Clipboard.SetText(text);
                    }

                    result = true;
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
            }
            finally
            {
                destinationImage?.Dispose();
            }

            return(new ResultOCR()
            {
                Success = result, Error = error, Text = text
            });
        }
        private async void RealOcr_Clicked(object sender, EventArgs e)
        {
            try
            {
                OcrResult text1;
                var       media = Plugin.Media.CrossMedia.Current;
                await media.Initialize();

                var file = await media.TakePhotoAsync(new StoreCameraMediaOptions
                {
                    SaveToAlbum = false
                });

                UserDialogs.Instance.ShowLoading("Wait Till We extract Text");
                Img.Source = ImageSource.FromStream(() => file.GetStream());



                //Normal Post method

                //HttpClient client = new HttpClient();
                //// Request headers.
                //client.DefaultRequestHeaders.Add(
                //    "Ocp-Apim-Subscription-Key", "<Your COMPUTER_VISION_SUBSCRIPTION_KEY>");
                //// Request parameters.
                //// The language parameter doesn't specify a language, so the
                //// method detects it automatically.
                //// The detectOrientation parameter is set to true, so the method detects and
                //// and corrects text orientation before detecting text.
                //string requestParameters = "language=unk&detectOrientation=true";
                //// Assemble the URI for the REST API method.
                //string uri = "https://westcentralus.api.cognitive.microsoft.com/vision/v2.1/ocr" + "?" + requestParameters;
                //HttpResponseMessage response;
                //// Read the contents of the specified local image
                //// into a byte array.
                //BinaryReader binaryReader = new BinaryReader(file.GetStream());
                //byte[] byteData = binaryReader.ReadBytes((int)file.GetStream().Length);
                //// Add the byte array as an octet stream to the request body.
                //using (ByteArrayContent content = new ByteArrayContent(byteData))
                //{
                //    // This example uses the "application/octet-stream" content type.
                //    // The other content types you can use are "application/json"
                //    // and "multipart/form-data".
                //    content.Headers.ContentType =
                //        new MediaTypeHeaderValue("application/octet-stream");

                //    // Asynchronously call the REST API method.
                //    response = await client.PostAsync(uri, content);
                //}
                //// Asynchronously get the JSON response.
                //string contentString = await response.Content.ReadAsStringAsync();
                //// Display the JSON response.
                //var data = JToken.Parse(contentString).ToString();
                //text1 = JsonConvert.DeserializeObject<OcrResult>(data);



                //calling api using Computer vision nuget
                var visionClient = new ComputerVisionClient(new ApiKeyServiceClientCredentials("<Your COMPUTER_VISION_SUBSCRIPTION_KEY>"))
                {
                    Endpoint = "https://westcentralus.api.cognitive.microsoft.com"
                };

                text1 = await visionClient.RecognizePrintedTextInStreamAsync(true, file.GetStream(), OcrLanguages.Unk);

                LblResult.Text = "";



                //Common code to display fetched text
                foreach (var region in text1.Regions)
                {
                    foreach (var line in region.Lines)
                    {
                        foreach (var word in line.Words)
                        {
                            LblResult.Text += Convert.ToString(word.Text) + " ";
                        }
                        LblResult.Text += "\n";
                    }
                }
                UserDialogs.Instance.HideLoading();
            }
            catch (Exception ex)
            {
                UserDialogs.Instance.AlertAsync("We are facing some techinical issue please try again", "Alert", "Ok");
            }
        }
 private async Task <OcrResult> ComputerVisionRecognizedPrintedTextByStreamAsync(Stream imageStream, OcrLanguages ocrLanguage)
 {
     return(await _computerVisionClient.RecognizePrintedTextInStreamAsync(true, imageStream, ocrLanguage));
 }
示例#28
0
        private async Task <OcrResult> DetectTextAsync(string imageUrl)
        {
            Stream stream = File.OpenRead(imageUrl);

            return(await visionClient.RecognizePrintedTextInStreamAsync(true, stream));
        }
        public override async Task <PerformOcrStringResponse> PerformOcr(CognitiveServicesRequest request)
        {
            var response = new PerformOcrStringResponse();

            try
            {
                var apiKeyServiceClientCredentials = new ApiKeyServiceClientCredentials(request.EndPointKey1);
                var delegatingHandlers             = new System.Net.Http.DelegatingHandler[] { };

                IComputerVisionClient computerVision = new ComputerVisionClient(
                    credentials: apiKeyServiceClientCredentials,
                    handlers: delegatingHandlers)
                {
                    Endpoint = request.EndPoint
                };

                var contentString = await computerVision.RecognizePrintedTextInStreamAsync(
                    image : request.ImageStream,
                    detectOrientation : false,
                    language : OcrLanguages.En,
                    cancellationToken : CancellationToken.None);

                var resultsLinesFound = new List <string>();
                var resultsWordsFound = new List <string>();

                foreach (OcrRegion region in contentString.Regions)
                {
                    foreach (OcrLine line in region.Lines)
                    {
                        var words = line.Words.Select(word => word.Text).ToList();

                        resultsWordsFound.AddRange(words);

                        if (words.Count > 0)
                        {
                            resultsLinesFound.Add(string.Join <string>(" ", words));
                        }
                    }
                }

                var wordGrouping =
                    from word in resultsWordsFound
                    group word by word
                    into g
                    select new { InstanceFound = g.Key, Word = g };

                var groupedWordsFound = wordGrouping
                                        .Select(item => new PerformOcrWordInstance
                {
                    Word      = item.Word.Key,
                    Instances = item.Word.Count()
                });

                response.LinesFound = resultsLinesFound;
                response.WordsFound = groupedWordsFound.ToList();
                response.Success    = true;
            }
            catch (Exception e)
            {
                response.FailureInformation = e.Message;
            }

            return(response);
        }
示例#30
0
        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {
            int count = 0;
            var title = default(string);

            while (running)
            {
                using (var transformedDepth = new Image(ImageFormat.Depth16, colorWidth, colorHeight, colorWidth * sizeof(UInt16)))
                    using (var capture = await Task.Run(() => { return(this.kinect.GetCapture()); }))
                    {
                        count++;

                        this.transform.DepthImageToColorCamera(capture, transformedDepth);

                        this.bitmap.Lock();

                        var color  = capture.Color;
                        var region = new Int32Rect(0, 0, color.WidthPixels, color.HeightPixels);

                        unsafe
                        {
                            using (var pin = color.Memory.Pin())
                            {
                                this.bitmap.WritePixels(region, (IntPtr)pin.Pointer, (int)color.Size, color.StrideBytes);
                            }

                            if (boundingBox != null)
                            {
                                int y = (boundingBox.Y + boundingBox.H / 2);
                                int x = (boundingBox.X + boundingBox.W / 2);

                                this.StatusText = $"The {title} is: {transformedDepth.GetPixel<ushort>(y, x)} mm away";

                                this.Outline.Width  = boundingBox.W;
                                this.Outline.Height = boundingBox.H;
                                this.Outline.SetValue(Canvas.LeftProperty, boundingBox.X / this.CompositeImage.ActualWidth * this.ImageView.ActualWidth);
                                this.Outline.SetValue(Canvas.TopProperty, boundingBox.Y / this.CompositeImage.ActualHeight * this.ImageView.ActualHeight);
                            }
                        }

                        this.bitmap.AddDirtyRect(region);
                        this.bitmap.Unlock();

                        if (count % 30 == 0)
                        {
                            running = false;

                            var stream = StreamFromBitmapSource(this.bitmap);
                            var text   = await computerVision.RecognizePrintedTextInStreamAsync(true, stream, OcrLanguages.En);

                            if (text != null)
                            {
                                foreach (var textRegion in text.Regions)
                                {
                                    if (textRegion.Lines != null && textRegion.Lines.Any())
                                    {
                                        foreach (var line in textRegion.Lines)
                                        {
                                            Console.WriteLine(string.Join(" ", line.Words.Select(w => w.Text)));
                                        }
                                    }
                                }
                            }
                            // title = await AnalyzeImage().ConfigureAwait(false);

                            running = true;
                        }
                    }
            }
        }