예제 #1
0
        public async void ChoosePictureClicked(object sender, EventArgs e)
        {
            ChoosePictureButton.Text      = "Working...";
            TakePictureButton.IsEnabled   = false;
            ChoosePictureButton.IsEnabled = false;

            if (!tesseractApi.Initialized)
            {
                await tesseractApi.Init("eng");
            }

            var photo = await SelectPicture();

            if (photo != null)
            {
                var imageBytes = new byte[photo.Source.Length];
                photo.Source.Position = 0;
                photo.Source.Read(imageBytes, 0, (int)photo.Source.Length);
                photo.Source.Position = 0;

                TakenImage.Source = ImageSource.FromStream(() => photo.Source);
                var tessResult = await tesseractApi.SetImage(imageBytes);

                if (tessResult)
                {
                    RecognizedTextLabel.Text = tesseractApi.Text;
                }
            }

            ChoosePictureButton.Text      = "...or choose from gallery";
            TakePictureButton.IsEnabled   = true;
            ChoosePictureButton.IsEnabled = true;
        }
        public async void Sample1Jpg()
        {
            await _api.Init("eng");

            using (var stream = LoadSample("sample1.jpg")) {
                var result = await _api.SetImage(stream);

                Assert.IsTrue(result);
                Assert.AreEqual("The quick brown fox\njumped over the 5\nlazy dogs!\n\n", _api.Text);
                var data = _api.Results(PageIteratorLevel.Block).ToList();
                Assert.AreEqual(1, data.Count);
                Assert.AreEqual("The quick brown fox\njumped over the 5\nlazy dogs!\n\n", data [0].Text);
                data = _api.Results(PageIteratorLevel.Paragraph).ToList();
                Assert.AreEqual(1, data.Count);
                Assert.AreEqual("The quick brown fox\njumped over the 5\nlazy dogs!\n\n", data [0].Text);
                data = _api.Results(PageIteratorLevel.Symbol).ToList();
                Assert.AreEqual(39, data.Count);
                data = _api.Results(PageIteratorLevel.Textline).ToList();
                Assert.AreEqual(3, data.Count);
                Assert.AreEqual("The quick brown fox\n", data [0].Text);
                Assert.AreEqual("jumped over the 5\n", data [1].Text);
                Assert.AreEqual("lazy dogs!\n\n", data [2].Text);
                Assert.AreEqual(new Rectangle(37, 233, 415, 89), data [2].Box);
                data = _api.Results(PageIteratorLevel.Word).ToList();
                Assert.AreEqual(10, data.Count);
            }
        }
        /// <summary>
        /// Takes an image, and runs it through Tesseract, returning
        /// the cleaned string it detected.
        /// </summary>
        /// <returns>The ocr.</returns>
        /// <param name="image">Image.</param>
        private async Task <string[]> ProcessOCR(UIImage image)
        {
            if (!_tesseractInitialised)
            {
                //tesseract.SetPageSegmentationMode(PageSegmentationMode.SingleWord);
                _tesseractInitialised = await tesseract.Init("eng");

                tesseract.SetVariable("tosp_min_sane_kn_sp", "10");

                if (!string.IsNullOrEmpty(OCRWhiteList))
                {
                    tesseract.SetWhitelist(OCRWhiteList);
                }
            }

            bool success = await tesseract.SetImage(image.AsPNG().AsStream());

            if (!isDisposing)
            {
                if (success)
                {
                    var results = tesseract.Results(PageIteratorLevel.Word);
                    return(results?.Select(u => u.Text).ToArray());
                }
            }

            return(null);
        }
예제 #4
0
        async Task Recognise(MediaFile result)
        {
            if (result.Source == null)
            {
                return;
            }
            try {
                activityIndicator.IsRunning = true;
                if (!_tesseract.Initialized)
                {
                    var initialised = await _tesseract.Init("eng");

                    if (!initialised)
                    {
                        return;
                    }
                }
                if (!await _tesseract.SetImage(result.Source))
                {
                    return;
                }
            } catch (Exception ex) {
                DisplayAlert("Error", ex.Message, "Cancel");
            } finally {
                activityIndicator.IsRunning = false;
            }
            TextLabel.Text = _tesseract.Text;
            var words      = _tesseract.Results(PageIteratorLevel.Word);
            var symbols    = _tesseract.Results(PageIteratorLevel.Symbol);
            var blocks     = _tesseract.Results(PageIteratorLevel.Block);
            var paragraphs = _tesseract.Results(PageIteratorLevel.Paragraph);
            var lines      = _tesseract.Results(PageIteratorLevel.Textline);
        }
예제 #5
0
        async void TakePictureButton_Clicked(object sender, EventArgs e)
        {
            if (!_tesseractApi.Initialized)
            {
                await _tesseractApi.Init("eng");
            }

            var photo = await TakePic();

            if (photo != null)
            {
                var imageBytes = new byte[photo.Source.Length];
                photo.Source.Position = 0;
                photo.Source.Read(imageBytes, 0, (int)photo.Source.Length);
                photo.Source.Position = 0;

                _takenImage.Source = ImageSource.FromStream(() => photo.Source);

                var tessResult = await _tesseractApi.SetImage(imageBytes);

                //var path = $"android.resource://{PackageName}/Resource";
                //var tessResult = await _tesseractApi.SetImage(App._file.Path);
                if (tessResult)
                {
                    _recognizedTextLabel.Text = _tesseractApi.Text;
                }
                else
                {
                    _recognizedTextLabel.Text = "No Data";
                }
            }
        }
        async void TakePictureButton_Clicked(object sender, EventArgs e)
        {
            _takePictureButton.Text      = "Working...";
            _takePictureButton.IsEnabled = false;

            if (!_tesseractApi.Initialized)
            {
                await _tesseractApi.Init("eng");
            }

            var photo = await TakePic();

            if (photo != null)
            {
                // When setting an ImageSource using a stream,
                // the stream gets closed, so to avoid that I backed up
                // the image into a byte array with the following code:
                var imageBytes = new byte[photo.Source.Length];
                photo.Source.Position = 0;
                photo.Source.Read(imageBytes, 0, (int)photo.Source.Length);
                photo.Source.Position = 0;

                var tessResult = await _tesseractApi.SetImage(imageBytes);

                if (tessResult)
                {
                    _takenImage.Source        = ImageSource.FromStream(() => photo.Source);
                    _recognizedTextLabel.Text = _tesseractApi.Text;
                }
            }

            _takePictureButton.Text      = "New scan";
            _takePictureButton.IsEnabled = true;
        }
예제 #7
0
        async Task Recognise(MediaFile result)
        {
            if (result.Source == null)
            {
                return;
            }
            activityIndicator.IsRunning = true;
            if (!_tesseract.Initialized)
            {
                var initialised = await _tesseract.Init("eng");

                if (!initialised)
                {
                    return;
                }
            }
            _tesseract.SetImage(result.Source)
            .ContinueWith((t) =>
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    activityIndicator.IsRunning = false;
                    TextLabel.Text = _tesseract.Text;
                    var words      = _tesseract.Results(PageIteratorLevel.Word);
                    var symbols    = _tesseract.Results(PageIteratorLevel.Symbol);
                    var blocks     = _tesseract.Results(PageIteratorLevel.Block);
                    var paragraphs = _tesseract.Results(PageIteratorLevel.Paragraph);
                    var lines      = _tesseract.Results(PageIteratorLevel.Textline);
                });
            });
        }
        public async Task <string> GetStringFromImage(Mat image)
        {
            await _tesseractEngine.SetImage(image.ToBytes());

            var result = Regex.Replace(_tesseractEngine.Text, @"\s+", "");

            _tesseractEngine.Clear();
            return(result);
        }
예제 #9
0
        async Task <string> Ocr()
        {
            ITesseractApi api = DependencyService.Get <ITesseractService>().Init();
            await api.Init("spa");

            string file = "/storage/emulated/0/Android/data/mydata/textSpaCrop.png";
            await api.SetImage(file);

            return(api.Text);
        }
예제 #10
0
        private async void ScanButton_OnClicked(object sender, EventArgs e)
        {
            ScanButton.Text      = "Working...";
            ScanButton.IsEnabled = false;

            if (!_tesseractApi.Initialized)
            {
                await _tesseractApi.Init("eng");
            }

            MediaFile photo = await TakePic();

            if (photo != null)
            {
                // When setting an ImageSource using a stream,
                // the stream gets closed, so to avoid that I backed up
                // the image into a byte array with the following code:
                var imageBytes = new byte[photo.Source.Length];
                photo.Source.Position = 0;
                photo.Source.Read(imageBytes, 0, (int)photo.Source.Length);
                photo.Source.Position = 0;

                Assembly assembly       = this.GetType().GetTypeInfo().Assembly;
                var      resourceStream = assembly.GetManifestResourceStream("Xocr.TestImage.jpg");
                // read stream to byte array
                byte[] imageBytes;
                using (var memoryStream = new MemoryStream())
                {
                    await resourceStream.CopyToAsync(memoryStream);

                    imageBytes = memoryStream.ToArray();
                }

                var tessResult = await _tesseractApi.SetImage(new byte[30]);

                if (tessResult)
                {
                    // _takenImage.Source = ImageSource.FromStream(() => photo.Source);
                    RecognizedText.Text = _tesseractApi.Text;
                }
            }

            ScanButton.Text      = "New scan";
            ScanButton.IsEnabled = true;
        }
예제 #11
0
        async void TakePictureButton_Clicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
            {
                throw new Exception("No camera available.");
            }


            var opts = new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                //PhotoSize = PhotoSize.Full,
                //SaveToAlbum = true,
                //CompressionQuality = 90,
                //SaveMetaData = false,
                AllowCropping = true,
            };

            var file = await CrossMedia.Current.TakePhotoAsync(opts);



            var photo = file?.GetStream();

            if (photo != null)
            {
                var tessResult = await _tesseractApi.SetImage(photo);

                if (tessResult)
                {
                    var textReconized = _tesseractApi.Text;
                    var resultado     = textReconized.Contains("Importe venta");
                    var test          = _tesseractApi.Results(PageIteratorLevel.Textline);
                    foreach (var item in test)
                    {
                        if (item.Text.Contains("IMPORTE VENTA"))
                        {
                            MyStringProperty = item.Text;
                        }
                    }
                }
            }
        }
예제 #12
0
        async Task v_recognize(Stream p_str)
        {
            try
            {
                await g_tss.SetImage(p_str);
            }
            catch (Exception ex)
            {
                await DisplayAlert("Error", ex.Message, "Cancel");
            }
            finally
            {
                //activityIndicator.IsRunning = false;
            }

            await DisplayAlert("Ok", g_tss.Text, "Cancel");

            //var words = g_tss.Results(PageIteratorLevel.Word);
            //var symbols = g_tss.Results(PageIteratorLevel.Symbol);
            //var blocks = g_tss.Results(PageIteratorLevel.Block);
            //var paragraphs = g_tss.Results(PageIteratorLevel.Paragraph);
            //var lines = g_tss.Results(PageIteratorLevel.Textline);
        }
예제 #13
0
        public async Task <string> DoOCR(byte[] bytes)
        {
            // Take the photo
            if (!_tesseractInitialised)
            {
                _tesseractInitialised = await tesseract.Init("eng");
            }

            bool success = await tesseract.SetImage(bytes);

            if (success)
            {
                string textResult = tesseract.Text;
                textResult.Trim();
                textResult.Replace(" ", "");
                Console.WriteLine(textResult);
                return(textResult);
                // Dispatch some kind of event
                //return textResult;
            }

            return(null);
        }
예제 #14
0
        public async Task Recognise(Stream stream)
        {
            if (!_tesseract.Initialized)
            {
                var initialised = await _tesseract.Init("eng");

                if (!initialised)
                {
                    return;
                }
            }

            if (!await _tesseract.SetImage(stream))
            {
                //TO DO Wrong image
            }

            lbl_text.Text = _tesseract.Text;
            var words      = _tesseract.Results(PageIteratorLevel.Word);
            var symbols    = _tesseract.Results(PageIteratorLevel.Symbol);
            var blocks     = _tesseract.Results(PageIteratorLevel.Block);
            var paragraphs = _tesseract.Results(PageIteratorLevel.Paragraph);
            var lines      = _tesseract.Results(PageIteratorLevel.Textline);
        }
 public async void Sample3AllModes ()
 {
     using (_api = new TesseractApi (Android.App.Application.Context, AssetsDeployment.OncePerInitialization)) {
         foreach (var engineMode in Enum.GetValues(typeof(OcrEngineMode))) {
             Console.WriteLine ("Engine mode: {0}", engineMode);
             await _api.Init ("eng", (OcrEngineMode)engineMode);
             foreach (var segmentationMode in Enum.GetValues(typeof(PageSegmentationMode))) {
                 _api.SetPageSegmentationMode ((PageSegmentationMode)segmentationMode);
                 using (var stream = LoadSample ("sample3.png")) {
                     var result = await _api.SetImage (stream);
                     Assert.IsTrue (result);
                     Console.WriteLine ("Segmentation mode: {0}, result: \"{1}\"", segmentationMode, _api.Text.Replace ("\n", " "));
                 }
             }
         }
     }
 }
예제 #16
0
        public CalKnightsViewModel()
        {
            _tesseractApi = Resolver.Resolve <ITesseractApi>();
            _device       = Resolver.Resolve <IDevice>();
            _dicts        = new Dictionary <string, string>
            {
                { "vanguard", "test" }
            };

            PickImageCommand = new Command(
                execute: async() =>
            {
                ImageWaiting = false;

                if (!_tesseractApi.Initialized)
                {
                    await _tesseractApi.Init("kor", OcrEngineMode.TesseractOnly);
                    _tesseractApi.SetPageSegmentationMode(PageSegmentationMode.SparseText);
                }

                var imageFile = await TakePic();
                if (imageFile != null)
                {
                    //var imgStream = imageFile.GetStream();
                    //int imgLen = (int)imgStream.Length;
                    //var bytes = new byte[imgLen];

                    //imgStream.Position = 0;
                    //imgStream.Read(bytes, 0, imgLen);
                    //for (int i = 0; i < imgLen; i++)
                    //    bytes[i] = (byte)(0xFF ^ bytes[i]);

                    //var memoryStream = new MemoryStream();
                    //await mediaFile.GetStream().CopyToAsync(memoryStream);
                    //byte[] imageAsByte = memoryStream.ToArray();
                    byte[] bytes;

                    using (Stream stream = imageFile.GetStream())
                    {
                        //await stream.CopyToAsync(memStream);
                        //memStream.Seek(0, SeekOrigin.Begin);
                        var bitmap = SKBitmap.Decode(stream);

                        var img    = SKImage.FromBitmap(bitmap);
                        int height = img.Height;
                        int width  = img.Width;

                        var subset = SKRectI.Create(
                            (int)(width * 0.2f), (int)(height * 0.4f),
                            (int)(width * 0.5f), (int)(height * 0.3f));
                        var placeholder  = SKRectI.Empty;
                        var placeholder2 = SKPointI.Empty;

                        var img2 = img.ApplyImageFilter(
                            SKImageFilter.CreateColorFilter(_greyFilter),
                            subset, subset, out placeholder, out placeholder2);
                        if (img2 == null)
                        {
                            Debug.WriteLine("ISNULL");
                        }
                        img = img.Subset(subset);

                        bytes          = img.Encode().ToArray();
                        OCRImageSource = ImageSource.FromStream(() =>
                        {
                            return(new MemoryStream(bytes));
                        });
                    }

                    // ImageSource.FromStream(() => { return imageFile.GetStream(); });
                    var tessResult = await _tesseractApi.SetImage(bytes);

                    if (tessResult)
                    {
                        Debug.WriteLine("read");
                        Debug.WriteLine(_tesseractApi.Text);
                    }
                    else
                    {
                        Debug.WriteLine("unavaliable");
                    }
                }
                ImageWaiting = true;
            });

            FoldSelectorCommand = new Command <string>(
                execute: (string selector) =>
            {
                int i = 0;
                switch (selector)
                {
                case "filter": i = 0; break;

                case "tag": i = 1; break;

                case "sort": i = 2; break;
                }
                _isFrameVisible[i] = !_isFrameVisible[i];
                OnPropertyChanged("IsFrameVisible");
            });
        }
        /// <summary>
        /// Method for recognizing the characters. Input comes from the ScanPage.xaml.cs, and is then processed into words
        /// </summary>
        /// <param name="result">Stream of bytes of the picture to process</param>
        /// <returns></returns>
        private async Task Recognise(Stream result)
        {
            if (IsBusy)
            {
                return;
            }
            IsBusy = true;

            if (result == null)
            {
                return;
            }

            if (!_tesseract.Initialized)
            {
                var initialised = await _tesseract.Init("deu");

                _tesseract.SetWhitelist("0123456789");
                _tesseract.SetPageSegmentationMode(PageSegmentationMode.SingleChar);
                if (!initialised)
                {
                    return;
                }
            }


            bool success = await _tesseract.SetImage(result);

            if (success)
            {
                var words      = _tesseract.Results(PageIteratorLevel.Word);
                var symbols    = _tesseract.Results(PageIteratorLevel.Symbol);
                var blocks     = _tesseract.Results(PageIteratorLevel.Block);
                var paragraphs = _tesseract.Results(PageIteratorLevel.Paragraph);
                var lines      = _tesseract.Results(PageIteratorLevel.Textline);

                var textResult = "";

                var enumerator = words.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    var item = enumerator.Current.Text;
                    textResult += item;
                }

                bool isDigit = textResult.All(char.IsDigit);

                if (!isDigit)
                {
                    textResult = "?";
                }

                Reading += textResult;
            }
            else
            {
                Reading = "didnt work";
            }
            IsBusy = false;
        }
예제 #18
0
        public Camera()
        {
            InitializeComponent();

            takePhoto.Clicked += async(sender, args) =>
            {
                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    DisplayAlert("No Camera", "No camera avaialble.", "OK");
                    return;
                }

                var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    Directory          = "Test",
                    SaveToAlbum        = true,
                    CompressionQuality = 75,
                    CustomPhotoSize    = 50,
                    PhotoSize          = PhotoSize.MaxWidthHeight,
                    MaxWidthHeight     = 2000,
                    DefaultCamera      = CameraDevice.Front
                });

                ITesseractApi api = DependencyService.Get <ITesseractService>().Init();
                await api.Init("spa");

                //string path = "/storage/emulated/0/Android/data/mydata/textSpaCrop.png";
                string path = file.Path;
                await api.SetImage(path);

                if (file == null)
                {
                    return;
                }

                DisplayAlert("File Location", file.Path, "OK");

                image.Source = ImageSource.FromStream(() =>
                {
                    var stream = file.GetStream();
                    file.Dispose();
                    return(stream);
                });
            };

            pickPhoto.Clicked += async(sender, args) =>
            {
                if (!CrossMedia.Current.IsPickPhotoSupported)
                {
                    DisplayAlert("Photos Not Supported", ":( Permission not granted to photos.", "OK");
                    return;
                }
                var file = await Plugin.Media.CrossMedia.Current.PickPhotoAsync(new Plugin.Media.Abstractions.PickMediaOptions
                {
                    PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium,
                });

                if (file == null)
                {
                    return;
                }

                image.Source = ImageSource.FromStream(() =>
                {
                    var stream = file.GetStream();
                    file.Dispose();
                    return(stream);
                });
            };

            takeVideo.Clicked += async(sender, args) =>
            {
                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakeVideoSupported)
                {
                    DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
                    return;
                }

                var file = await CrossMedia.Current.TakeVideoAsync(new Plugin.Media.Abstractions.StoreVideoOptions
                {
                    Name      = "video.mp4",
                    Directory = "DefaultVideos",
                });

                if (file == null)
                {
                    return;
                }

                DisplayAlert("Video Recorded", "Location: " + file.Path, "OK");

                file.Dispose();
            };

            pickVideo.Clicked += async(sender, args) =>
            {
                if (!CrossMedia.Current.IsPickVideoSupported)
                {
                    DisplayAlert("Videos Not Supported", ":( Permission not granted to videos.", "OK");
                    return;
                }
                var file = await CrossMedia.Current.PickVideoAsync();

                if (file == null)
                {
                    return;
                }

                DisplayAlert("Video Selected", "Location: " + file.Path, "OK");
                file.Dispose();
            };
        }
예제 #19
0
        private async void CameraButton_Clicked(object sender, EventArgs e)
        {
            await CrossMedia.Current.Initialize();

            var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
            {
                Directory = "MedSticker",
                Name      = "sticker.jpg"
            });

            if (file == null)
            {
                return;
            }
            System.IO.Stream photoStream = file.GetStream();


            //photo.Source = ImageSource.FromStream(() =>
            // {
            //     var stream = file.GetStream();
            //file.Dispose();
            //     return stream;
            // });

            //NameEntry.Text = "1";
            //Photo.Source = ImageSource.FromStream(file.GetStream); //debug code
            //Bitmap bitmap = new Bitmap(photoStream);
            //Stream photoStream2 = RaiseContrast(bitmap);
            //Photo.Source = ImageSource.FromStream(() => stream2);
            bool initialised = await api.Init("eng");

            //NameEntry.Text += "2";
            api.SetWhitelist("0123456789:-,#'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
            bool success = await api.SetImage(photoStream);

            //NameEntry.Text += "3";
            if (success)
            {
                //List<Result> lines = api.Results(PageIteratorLevel.Textline);
                //List<Result> words = api.Results(PageIteratorLevel.Word);
                List <Result> results = api.Results(PageIteratorLevel.Symbol).ToList();
                //List<Result> blocks = api.Results(PageIteratorLevel.Block);

                //List<Result> results = api.Res    ults(PageIteratorLevel.Paragraph).ToList();
                var res  = " ";
                var conf = " ";
                foreach (Result r in results)
                {
                    if (r.Confidence > 85f)
                    {
                        res  += r.Text.ToUpper();
                        conf += r.Confidence.ToString() + " ";
                    }
                }
                TestLabel.Text = res;
                res            = res.Replace(":", "");

                //SexEntry.Text = res;
                //MREntry.Text = conf;
                if (res.Contains("SEX"))
                {
                    var sindex = res.LastIndexOf("SEX") + 2;
                    SexEntry.Text = res.Substring(sindex + 1, 1);
                }
                if (res.Contains("PCP"))
                {
                    var nindex = res.LastIndexOf("PCP");
                    NameEntry.Text = res.Substring(0, nindex);
                }
                else if (res.Contains("'MR"))
                {
                    var nindex2 = res.LastIndexOf("'MR");
                    NameEntry.Text = res.Substring(0, nindex2 - 1);
                }
                if (res.Contains("ACCT") & res.Contains("#"))
                {
                    var mindex2 = res.LastIndexOf("ACCT");
                    var mindex  = res.IndexOf("#");
                    MREntry.Text = res.Substring(mindex + 1, mindex2 - mindex - 1);
                }
                if (!(res.Contains("PR") | res.Contains("PCP") | res.Contains("ACCT") | res.Contains("#")))
                {
                    await DisplayAlert("Could not detect text.", "Try again or enter info manually.", "OK");
                }
            }
            else
            {
                NameEntry.Text = "Image Recognition Failed";
            }
        }