コード例 #1
0
    public async Task AnalyzeImageAsync(RequestType requestType, Picture screenshot)
    {
        // get correct ocr api instance depending on request type
        switch (requestType)
        {
        case RequestType.REMOTE:
            Api = ApiMicrosoftAzureOcr.Instance;
            break;

        case RequestType.LOCAL:
        default:
            Api = ApiMicrosoftMediaOcr.Instance;
            break;
        }

        Screenshot = screenshot;

        try
        {
            result = await Api.HttpPostImage(Screenshot);
        }
        catch (HttpRequestException e)
        {
            Api    = ApiMicrosoftMediaOcr.Instance;
            result = await Api.HttpPostImage(Screenshot);
        }

        OnImageAnalysed(new AnalyseImageEventArgs(result));
    }
コード例 #2
0
ファイル: Program.cs プロジェクト: Jono120/ComputerVision
        private static void DisplayResults(OcrResult analysis)
        {
            //text
            Console.WriteLine("Text:");
            Console.WriteLine("Language: " + analysis.Language);
            Console.WriteLine("Text Angle: " + analysis.TextAngle);
            Console.WriteLine("Orientation: " + analysis.Orientation);
            Console.WriteLine("Text regions: ");
            foreach (var region in analysis.Regions)
            {
                Console.WriteLine("Region bounding box: " + region.BoundingBox);
                foreach (var line in region.Lines)
                {
                    Console.WriteLine("Line bounding box: " + line.BoundingBox);

                    foreach (var word in line.Words)
                    {
                        Console.WriteLine("Word bounding box: " + word.BoundingBox);
                        Console.WriteLine("Text: " + word.Text);
                    }
                    Console.WriteLine("\n");
                }
                Console.WriteLine("\n \n");
            }
        }
コード例 #3
0
        private async Task <bool> ContainsAnyAsync(IEnumerable <string> words)
        {
            using (Bitmap capture = captureStrategy.Capture(Handle))
            {
                using (SoftwareBitmap softwareBitmap = await GetSoftwareBitmapAsync(capture).ConfigureAwait(false))
                {
                    OcrResult result = await ocrEngine.RecognizeAsync(softwareBitmap);

                    foreach (var word in words)
                    {
                        if (result.Text.Contains(word))
                        {
                            logger.LogDebug($"{word} has been found.");
                            return(true);
                        }
                        else
                        {
                            logger.LogDebug($"{word} not found.");
                        }
                    }

                    if (settings.Capture.SaveCaptureFailureCondition)
                    {
                        Directory.CreateDirectory(settings.CapturesPath);
                        capture.Save(Path.Combine(settings.CapturesPath, Guid.NewGuid().ToString() + ".bmp"));
                    }
                }
            }

            return(false);
        }
コード例 #4
0
        static void PrintOcrResult(OcrResult imgOcr)
        {
            Console.WriteLine("*** OCR result : ");

            Console.WriteLine($"Language      : {imgOcr.Language}");
            Console.WriteLine($"Orientation   : {imgOcr.Orientation}");
            Console.WriteLine($"Text angle    : {imgOcr.TextAngle}");
            Console.WriteLine($"Regions found : {imgOcr.Regions.Count}");
            int regionNum = 0;

            foreach (var r in imgOcr.Regions)
            {
                regionNum++;
                Console.WriteLine($"**--> REGION #{regionNum} : ({r.BoundingBox})");
                foreach (var l in r.Lines)
                {
                    Console.Write($"LINE ({l.BoundingBox}) : ");
                    foreach (var w in l.Words)
                    {
                        //Console.WriteLine($"WORD: ({w.BoundingBox})");
                        Console.Write($"{w.Text} ");
                        //Console.WriteLine();
                    }
                    Console.WriteLine();
                }
            }
        }
コード例 #5
0
        private static async Task <string> RunOcr(MemoryStream memoryStream)
        {
            if (memoryStream == null || memoryStream.Length == 0)
            {
                return("");
            }

            using (var memoryRandomAccessStream = new InMemoryRandomAccessStream())
            {
                await memoryRandomAccessStream.WriteAsync(memoryStream.ToArray().AsBuffer());

                OcrEngine engine = null;

                if (!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["LanguageCode"]))
                {
                    engine = OcrEngine.TryCreateFromLanguage(new Language(ConfigurationManager.AppSettings["LanguageCode"]));
                }
                if (engine == null)
                {
                    engine = OcrEngine.TryCreateFromUserProfileLanguages();
                }
                var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(memoryRandomAccessStream);

                OcrResult result = await engine.RecognizeAsync(await decoder.GetSoftwareBitmapAsync());

                return(result.Text);
            }
        }
コード例 #6
0
        public async void DoAsyncOCR()
        {
            var image = Screenshot(Screen.AllScreens[(int)NUDDisplay.Value - 1]);

            IronTesseract IronOCR = new IronTesseract();
            OcrResult     Result  = await Task.Run(() => IronOCR.Read(image));

            var s = trimmer.Replace(Result.Text, " ");

            s = s.Replace(".", "");
            string[] words = s.Split(' ');

            Console.WriteLine("Comparing Pokemon");
            List <string> FoundPKMN = new List <string>();

            foreach (var WordFromScreen in words.Where(x => !string.IsNullOrWhiteSpace(x)))
            {
                foreach (var PokemonName in PokemonData.getPokemonNames())
                {
                    double Simularity = CalculateSimilarity(WordFromScreen, PokemonName);
                    if (Simularity >= (double)Tolerance && !FoundPKMN.Contains(PokemonName))
                    {
                        FoundPKMN.Add(PokemonName);
                    }
                }
            }
            FoundPKMN.Sort();
            FoundPKMN.Reverse();
            PrintResults(FoundPKMN);
        }
コード例 #7
0
ファイル: AzureService.cs プロジェクト: Techie09/readr
        public async Task AddScan(string userSessionId, byte[] imageData)
        {
            //var taskResult = await BatchAnalyzeAsync(imageData);
            //taskResult.EnsureSuccessStatusCode();
            File.WriteAllBytes("screenshot.png", imageData);

            var ocrResult = new OcrResult();

            // Analyze an image to get features and other properties.
            using (var client = Authenticate(GetOcrEndpoint(null), _azureSettings.ApiKey))
                using (var memStream = new MemoryStream(imageData))
                {
                    var recognizePrintedTextResult = await client.RecognizePrintedTextInStreamAsync(true, memStream);

                    ObjectId sessionId         = ObjectId.Parse(userSessionId);
                    var      userSessionResult = await _userSessionRepository.GetUserSessionByUserSessionId(sessionId);

                    ObjectId modifiedById  = userSessionResult.ModifiedById;
                    var      sessionDetail = new SessionDetail()
                    {
                        Id           = ObjectId.GenerateNewId(),
                        SessionId    = sessionId,
                        ModifiedById = modifiedById,

                        PrintedTextResult = recognizePrintedTextResult
                    };

                    await _sessionDetailRepository.UpsertSessionDetail(sessionDetail);
                }

            return;
        }
コード例 #8
0
        public async Task <List <string> > RecognizeAsync(List <StorageFile> pickedFiles)
        {
            List <string> results = new List <string>();
            // OcrEngine engine = OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language("en-US"));
            OcrEngine engine = OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language("pl"));
            int       i      = 0;

            foreach (StorageFile file in pickedFiles)
            {
                Informator.Log("    OCR - " + i + " out of " + pickedFiles.Count);
                StringBuilder text   = new StringBuilder();
                var           stream = await file.OpenAsync(FileAccessMode.Read);

                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                OcrResult ocrResult = await engine.RecognizeAsync(softwareBitmap);

                foreach (OcrLine line in ocrResult.Lines)
                {
                    text.Append(line.Text + "\n");
                }
                results.Add(text.ToString());
                i++;
            }
            return(results);
        }
コード例 #9
0
        public IEnumerable <AnalyzedText> Analyze(OcrResult ocrResult)
        {
            var continuousText = ocrResult.AsContinuousText().ReplaceFaultyCharacters(Constants.NumericAnalysisOcrFixDictionary);

            var amounts = Constants.TextAnalysisConfiguration.AmountRegexes
                          .Select(x => Regex.Matches(continuousText, x))
                          .SelectMany(y => y.Cast <Match>()
                                      .Select(x => x.Value));

            amounts = amounts.Select(x => Regex.Replace(x, Constants.TextAnalysisConfiguration.AmountIgnoreRegex, string.Empty));

            amounts = amounts.Select(x =>
            {
                if ((x.Count(c => c == '.') > 0 && x.Count(c => c == ',') > 0) || x.Count(c => c == '.') > 1 || x.Count(c => c == ',') > 1)
                {
                    Regex regex = new Regex("(,|\\.)");
                    return(regex.Replace(x, string.Empty, 1));
                }

                return(x);
            });

            amounts = amounts.Replace(",", ".");

            return(amounts.Select(x => new AnalyzedText()
            {
                Text = x,
                TextType = TextType.Amount.ToString(),
                BoundingBox = ocrResult.BoundingBox
            })
                   .Distinct());
        }
コード例 #10
0
        private void btnBrowse_Click(object sender, EventArgs e)
        {
            DialogResult result = ofdSelectFile.ShowDialog();

            if (result == DialogResult.OK)
            {
                btnProcessImg.Visible = true;

                //pboxFileImage.ImageLocation = ofdSelectFile.FileName;
                var suffix = Path.GetExtension(ofdSelectFile.FileName);

                if (suffix == ".pdf")
                {
                    OcrResult resultOcr = MyOcr.AdvOcr.ReadPdf(ofdSelectFile.FileName, 1);
                    var       page      = resultOcr.Pages[0];
                    pboxFileImage.Image = page.Image;
                    imgOriginal         = page.Image;
                    //pboxFileImage.ImageLocation = resultOcr.Pages[0].;

                    this.isPdf = true;
                }
                else
                {
                    pboxFileImage.Image = Image.FromFile(ofdSelectFile.FileName);

                    this.isPdf = false;
                }

                txtImage.Text = ofdSelectFile.SafeFileName;
            }
        }
コード例 #11
0
        /// <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
            // -----------------------------------------------------------------------
        }
コード例 #12
0
        private void detectBtn_Click(object sender, EventArgs e)
        {
            if (ocrEngin == null)
            {
                MessageBox.Show("未初始化,无法执行!");
                return;
            }
            string targetImg = pathTextBox.Text;

            if (!File.Exists(targetImg))
            {
                MessageBox.Show("目标图片不存在,请用Open按钮打开");
                return;
            }
            int       padding        = (int)paddingNumeric.Value;
            int       imgResize      = (int)imgResizeNumeric.Value;
            float     boxScoreThresh = (float)boxScoreThreshNumeric.Value;
            float     boxThresh      = (float)boxThreshNumeric.Value;
            float     unClipRatio    = (float)unClipRatioNumeric.Value;
            bool      doAngle        = doAngleCheckBox.Checked;
            bool      mostAngle      = mostAngleCheckBox.Checked;
            OcrResult ocrResult      = ocrEngin.Detect(pathTextBox.Text, padding, imgResize, boxScoreThresh, boxThresh, unClipRatio, doAngle, mostAngle);

            ocrResultTextBox.Text = ocrResult.ToString();
            strRestTextBox.Text   = ocrResult.StrRes;
            pictureBox.Image      = ocrResult.BoxImg.ToBitmap();
        }
コード例 #13
0
        /// <summary>
        /// Sends a URL to Cognitive Services and performs OCR.
        /// </summary>
        /// <param name="imageUrl">The image URL for which to perform recognition.</param>
        /// <param name="language">The language code to recognize.</param>
        /// <returns>Awaitable OCR result.</returns>
        private async Task <OcrResult> RecognizeUrlAsync(string imageUrl, 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");

                //
                // Perform OCR on the given URL.
                //
                Log("Calling ComputerVisionClient.RecognizePrintedTextAsync()...");
                OcrResult ocrResult = await client.RecognizePrintedTextAsync(!DetectOrientation, imageUrl, language);

                return(ocrResult);
            }

            // -----------------------------------------------------------------------
            // KEY SAMPLE CODE ENDS HERE
            // -----------------------------------------------------------------------
        }
コード例 #14
0
        // Retuns array of words.
        public static async Task <string[]> RecognizePrintedTextUrl(ComputerVisionClient client, string imageUrl)
        {
            // Perform OCR on image
            OcrResult remoteOcrResult = null;

            try
            {
                remoteOcrResult = await client.RecognizePrintedTextAsync(true, imageUrl);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }

            List <string> result = new List <string>();

            foreach (var remoteRegion in remoteOcrResult.Regions)
            {
                foreach (var line in remoteRegion.Lines)
                {
                    result.Add(string.Join(' ', line.Words.Select(w => w.Text)));
                }
            }

            return(result.ToArray());
        }
コード例 #15
0
        public async Task <string> Run()
        {
            foreach (string argument in arguments)
            {
                if (!System.IO.File.Exists(argument))
                {
                    continue;
                }

                System.IO.FileInfo info = new System.IO.FileInfo(argument);

                if (!info.Exists)
                {
                    continue;
                }

                StorageFile file = await StorageFile.GetFileFromPathAsync(info.FullName);

                IRandomAccessStreamWithContentType stream = await file.OpenReadAsync();

                var decoder = await BitmapDecoder.CreateAsync(stream);

                SoftwareBitmap image = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Rgba8, BitmapAlphaMode.Premultiplied);

                OcrEngine engine = OcrEngine.TryCreateFromUserProfileLanguages();

                OcrResult result = await engine.RecognizeAsync(image);

                Console.WriteLine(result.Text);

                return(result.Text);
            }

            return(null);
        }
コード例 #16
0
        public async Task <string> RecognizeFromImage(StorageFile file)
        {
            string recognizedText = string.Empty;

            if (file != null)
            {
                Byte[] imageBytes = await this.ImageFileToByteArray(file);

                ImageProperties imageProperties = await file.Properties.GetImagePropertiesAsync();

                OcrResult ocrResult = await ocrEngine.RecognizeAsync(imageProperties.Height, imageProperties.Width, imageBytes);

                foreach (var line in ocrResult.Lines)
                {
                    foreach (var word in line.Words)
                    {
                        recognizedText += word.Text + " ";
                    }

                    recognizedText += Environment.NewLine;
                }
            }

            return(recognizedText);
        }
コード例 #17
0
        public static async Task <string> ExtractTextAsync(Image img, string lang)
        {
            MemoryStream memoryStream             = new MemoryStream();
            InMemoryRandomAccessStream randStream = new InMemoryRandomAccessStream();
            string result = "";

            try
            {
                img.Save(memoryStream, ImageFormat.Bmp);
                await randStream.WriteAsync(memoryStream.ToArray().AsBuffer());

                if (!OcrEngine.IsLanguageSupported(new Language(lang)))
                {
                    Console.Write("This language is not supported!");
                }
                OcrEngine ocrEngine = OcrEngine.TryCreateFromLanguage(new Language(lang));
                if (ocrEngine == null)
                {
                    ocrEngine = OcrEngine.TryCreateFromUserProfileLanguages();
                }
                var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(randStream);

                OcrResult ocrResult = await ocrEngine.RecognizeAsync(await decoder.GetSoftwareBitmapAsync());

                result = ocrResult.Text;
                return(result);
            }
            finally
            {
                memoryStream.Dispose();
                randStream.Dispose();
                GC.Collect(0);
            }
        }
コード例 #18
0
ファイル: OCR.cs プロジェクト: RedRin/team-development
        public static string GetValidateCode(Bitmap img)
        {
            Log.log.Info("Getting validateCode from image");
            OcrResult Results = null;

            try
            {
                Threshold(ref img, 100);
                var Ocr = new AdvancedOcr()
                {
                    Language = IronOcr.Languages.English.OcrLanguagePack
                };
                Results = Ocr.Read(img);
            } catch (Exception e)
            {
                MessageBox.Show("图片为空");
            }
            if (Results != null)
            {
                return(Results.Text);
            }
            else
            {
                return("");
            }
        }
コード例 #19
0
        public static OcrResult PopulateMatrixFromImage(Image image)
        {
            Pix           pix    = LoadBitmapToPix(image as Bitmap);
            List <string> Cells  = GetCellsFromPix(pix);
            var           root   = (int)Math.Floor(Math.Sqrt(Cells.Count));
            var           result = new OcrResult(root);

            for (int cell = 0, row = 0; cell < Cells.Count; cell++)
            {
                if (cell % root == 0 && cell != 0)
                {
                    row++;
                }

                if (row == root)
                {
                    break;
                }

                result.SetCell(row, (int)Math.Floor((decimal)cell % root), Cells[cell]);
            }
            image.Dispose();
            pix.Dispose();

            return(result);
        }
コード例 #20
0
ファイル: InvoiceProcessor.cs プロジェクト: Sammo351/HubV2
        public static string FindLargestText(OcrResult result)
        {
            OcrResult.Word largestWord = result.Pages[0].Words[0];

            for (int i = 0; i < result.Pages[0].Words.Length; i++)
            {
                if (result.Pages[0].Words[i].Font.FontSize > largestWord.Font.FontSize)
                {
                    largestWord = result.Pages[0].Words[i];
                }
            }

            int lineNumber   = largestWord.Line.LineNumber;
            int numberInLine = largestWord.WordNumber;

            var    lineOfText = result.Pages[0].Lines[lineNumber - 1];
            int    f          = numberInLine;
            string textInLine = "";

            for (int i = 0; i < lineOfText.Words.Length; i++)
            {
                if (lineOfText.Words[i].Font.FontSize == largestWord.Font.FontSize)
                {
                    textInLine += " " + lineOfText.Words[i].Text;
                }
                else
                {
                    break;
                }
            }

            return(textInLine);
        }
コード例 #21
0
        static public List <OCRYokoText> GetYoko(OcrResult result)
        {
            var list = new List <OCRYokoText>();

            foreach (var line in result.Lines)
            {
                foreach (var word in line.Words)
                {
                    bool flag = false;
                    for (var i = 0; i < list.Count; i++)
                    {
                        var t  = list[i];
                        var y1 = Math.Abs(t.Rect.Y - word.BoundingRect.Y);
                        if (word.BoundingRect.Height - y1 > ((double)word.BoundingRect.Height) * 0.5)
                        {
                            var width = word.BoundingRect.Width / word.Text.Length;
                            var x1    = (t.Rect.X + t.Rect.Width) - (word.BoundingRect.X - width);
                            if (x1 >= 0 && x1 < width)
                            {
                                t.Text += word.Text;
                                t.Rect  = word.BoundingRect;
                                list[i] = t;
                                flag    = true;
                            }
                        }
                    }
                    if (!flag)
                    {
                        var t = new OCRYokoText(word);
                        list.Add(t);
                    }
                }
            }
            return(list);
        }
コード例 #22
0
        private async Task <bool> ContainsAllAsync(IEnumerable <string> words)
        {
            try
            {
                using (Bitmap capture = captureStrategy.Capture(Handle))
                {
                    using (SoftwareBitmap softwareBitmap = await GetSoftwareBitmapAsync(capture))
                    {
                        OcrResult result = await engine.RecognizeAsync(softwareBitmap);

                        var containsAll = words.All(word => result.Text.Contains(word));

                        if (containsAll)
                        {
                            return(true);
                        }

                        if (settings.Capture.SaveCaptureFailureCondition)
                        {
                            capture.Save(Path.Combine(settings.CapturesPath, DateTime.Now.ToString()));
                        }
                    }
                }
            }
            catch
            {
            }

            return(false);
        }
コード例 #23
0
        /*
         * END - BATCH READ FILE - LOCAL IMAGE
         */

        /*
         * RECOGNIZE PRINTED TEXT - URL IMAGE
         */
        public static async Task RecognizePrintedTextUrl(ComputerVisionClient client, string imageUrl)
        {
            Console.WriteLine("----------------------------------------------------------");
            Console.WriteLine("RECOGNIZE PRINTED TEXT - URL IMAGE");
            Console.WriteLine();
            Console.WriteLine($"Performing OCR on URL image {Path.GetFileName(imageUrl)}...");
            Console.WriteLine();

            // Perform OCR on image
            OcrResult remoteOcrResult = await client.RecognizePrintedTextAsync(true, imageUrl);

            // Print the recognized text
            Console.WriteLine("Text:");
            Console.WriteLine("Language: " + remoteOcrResult.Language);
            Console.WriteLine("Text Angle: " + remoteOcrResult.TextAngle);
            Console.WriteLine("Orientation: " + remoteOcrResult.Orientation);
            Console.WriteLine();
            Console.WriteLine("Text regions: ");
            foreach (var remoteRegion in remoteOcrResult.Regions)
            {
                Console.WriteLine("Region bounding box: " + remoteRegion.BoundingBox);
                foreach (var line in remoteRegion.Lines)
                {
                    Console.WriteLine("Line bounding box: " + line.BoundingBox);

                    foreach (var word in line.Words)
                    {
                        Console.WriteLine("Word bounding box: " + word.BoundingBox);
                        Console.WriteLine("Text: " + word.Text);
                    }
                    Console.WriteLine();
                }
            }
        }
コード例 #24
0
        private bool WordsPositionsSimilarity(OcrResult currentOcr, OcrResult previousOcr)
        {
            bool similarEnough = false;
            int  equalposition = 0;

            List <OcrWord> words         = currentOcr.Lines.SelectMany(x => x.Words).ToList();
            List <OcrWord> wordsPrevious = previousOcr.Lines.SelectMany(x => x.Words).ToList();

            for (int i = 0; i < words.Count; i++)
            {
                for (int p = 0; p < wordsPrevious.Count; p++)
                {
                    if (words[i].BoundingRect == wordsPrevious[p].BoundingRect)
                    {
                        equalposition = 3;
                        break;
                    }
                }
                if (equalposition > 2)
                {
                    similarEnough = true;
                    break;
                }
            }

            return(similarEnough);
        }
コード例 #25
0
ファイル: ReceiptEngine.cs プロジェクト: Herlitzd/Seez
        public static IDictionary <int, IList <String> > GroupIntoRows(OcrResult ocrResult)
        {
            Dictionary <int, IList <String> > lines = new Dictionary <int, IList <string> >();


            foreach (var region in ocrResult.Regions)
            {
                foreach (var line in region.Lines)
                {
                    // Bounding boxes are "x,y,w,h"
                    var y        = int.Parse(line.BoundingBox.Split(',')[1]);
                    var roundedY = roundToNearest(y, 5);
                    var lineText = line.Words.Aggregate(String.Empty, (acc, next) => acc += next.Text);
                    if (lines.ContainsKey(roundedY))
                    {
                        lines[roundedY].Add(lineText);
                    }
                    else
                    {
                        lines.Add(roundedY, new List <String>()
                        {
                            lineText
                        });
                    }
                }
            }

            return(lines);
        }
コード例 #26
0
ファイル: ScanManager.cs プロジェクト: 24/source_04
        public async Task ScanPdf(string pdfFile, string range, string scanFile)
        {
            OcrRequest request = CreateOcrRequest();

            request.DocumentFile = pdfFile;
            request.PageRange    = range;

            OcrResult <OcrProcessDocumentResponse> response = await _ocrWebService.ProcessDocument(request);

            if (response.Success)
            {
                //string directory = GetScanDirectory();
                //zdir.CreateDirectory(scanDirectory);
                zfile.CreateFileDirectory(scanFile);
                //string scanFile = zPath.Combine(scanDirectory, "scan");
                Trace.WriteLine($"save scan to \"{scanFile}\"");
                await _ocrWebService.DownloadResultDocuments(response.Data, scanFile);

                Trace.WriteLine($"scan ok {response.Data.ProcessedPages} pages - remainder {response.Data.AvailablePages} pages");
            }
            else
            {
                Trace.WriteLine($"scan error code {response.StatusCode}");
            }
        }
コード例 #27
0
        private async void ClickButtonMakePhoto(object sender, RoutedEventArgs e)
        {
            CameraCaptureUI captureUI = new CameraCaptureUI();

            captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;

            StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

            if (photo == null)
            {
                return;
            }

            IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);

            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

            SoftwareBitmap bitmap = await decoder.GetSoftwareBitmapAsync();

            Language  ocrLanguage = new Language("en");
            OcrEngine ocrEngine   = OcrEngine.TryCreateFromUserProfileLanguages();
            OcrResult ocrResult   = await ocrEngine.RecognizeAsync(bitmap);

            string result = ocrResult.Text;

            Frame.Navigate(typeof(SelectTextAppoinmentPage), result);
        }
コード例 #28
0
        private async void ClickBottonBrowse(object sender, RoutedEventArgs e)
        {
            FileOpenPicker fileOpenPicker = new FileOpenPicker();

            fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            fileOpenPicker.FileTypeFilter.Add(".jpg");
            fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;

            var inputFile = await fileOpenPicker.PickSingleFileAsync();

            if (inputFile == null)
            {
                return;
            }

            SoftwareBitmap bitmap;

            using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read))
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                bitmap = await decoder.GetSoftwareBitmapAsync();
            }

            Language  ocrLanguage = new Language("en");
            OcrEngine ocrEngine   = OcrEngine.TryCreateFromUserProfileLanguages();
            OcrResult ocrResult   = await ocrEngine.RecognizeAsync(bitmap);

            string result = ocrResult.Text;

            Frame.Navigate(typeof(SelectTextAppoinmentPage), result);
        }
コード例 #29
0
 public OcrResultIterator(OcrResultIterator i)
 {
     result    = i.result;
     lineIndex = i.lineIndex;
     wordIndex = i.wordIndex;
     charIndex = i.charIndex;
 }
コード例 #30
0
        /// <summary>
        /// Log text from the given OCR results object.
        /// </summary>
        /// <param name="results">The OCR results.</param>
        protected void LogOcrResults(OcrResult results)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (results != null && results.Regions != null)
            {
                stringBuilder.Append("Text: ");
                stringBuilder.AppendLine();
                foreach (var item in results.Regions)
                {
                    foreach (var line in item.Lines)
                    {
                        foreach (var word in line.Words)
                        {
                            stringBuilder.Append(word.Text);
                            stringBuilder.Append(" ");
                        }

                        stringBuilder.AppendLine();
                    }

                    stringBuilder.AppendLine();
                }
            }

            Log(stringBuilder.ToString());
        }
コード例 #31
0
ファイル: MainPage.xaml.cs プロジェクト: CHS982/Coding4Fun
 private void RepeatForOcrWords(OcrResult ocrResult, Action<OcrResult, OcrWord> repeater)
 {
     if (ocrResult.Lines != null)
     {
         foreach (OcrLine line in ocrResult.Lines)
         {
             foreach (OcrWord word in line.Words)
             {
                 //call the action method 
                 repeater(ocrResult, word);
             }
         }
     }
 }
コード例 #32
0
ファイル: OcrVoucher.cs プロジェクト: jhonner72/plat
 public OcrVoucher()
 {
     AmountResult = new OcrResult();
     CodelineResult = new OcrResult();
     DateResult = new OcrResult();
 }
コード例 #33
0
ファイル: MainPage.xaml.cs プロジェクト: CHS982/Coding4Fun
        private void ApplyPatternMatching(OcrResult ocrResult)
        {
            Contact contact = new Contact();
            //set the picture
            contact.SourceDisplayPicture = _photoFile;
            // this method uses an action that will run as a 'callback' for the method
            // more info here https://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx
            RepeatForOcrWords(ocrResult, (result, word) =>
            {
                bool isNumber = false;
                //check the recognized type and then add the type to the contact 
                switch (CardRecognizer.Recognize(word.Text))
                {
                    case RecognitionType.Other:
                        break;
                    case RecognitionType.Email:
                        contact.Emails.Add(new ContactEmail() { Address = word.Text });
                        break;
                    case RecognitionType.Name:
                        contact.FirstName = word.Text;
                        break;
                    case RecognitionType.Number:
                        isNumber = true;
                        //NOTE: Phone numbers are not as easy to validate because OCR results splits the numbers if they contain spaces.
                        _phoneNumber += word.Text;
                        RecognitionType type = CardRecognizer.Recognize(_phoneNumber);
                        if (type == RecognitionType.PhoneNumber)
                        {
                            contact.Phones.Add(new ContactPhone() { Number = _phoneNumber });
                        }
                        break;
                    case RecognitionType.WebPage:
                        try
                        {
                            contact.Websites.Add(new ContactWebsite() { Uri = new Uri(word.Text) });
                        }
                        catch (Exception)
                        {
                            Debug.WriteLine("OCR Result cannot be converted to a URI");
                        }
                        break;
                    default:
                        break;
                }

                //Encounted a word or a value other than a number. 
                //If we havent validated as a phone number at this stage it is clearly not a phone number so clear the string
                if (!isNumber)
                {
                    _phoneNumber = string.Empty;
                }
            });

            if (!contact.Phones.Any()) //contact must have either a phone or email when calling ContactManager.ShowContactCard.
            {
                if (!contact.Emails.Any())
                {
                    Debug.WriteLine("Contact must have phone or email info.");

                    return;
                }
            }

            Rect rect = GetElementRect(GetDetailsButton);
            ContactManager.ShowContactCard(contact, rect, Windows.UI.Popups.Placement.Default);
        }