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); }
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); }); }); }
/// <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); }
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!", _api.Text); var data = _api.Results(Tesseract.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(Tesseract.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(Tesseract.PageIteratorLevel.Symbol).ToList(); Assert.AreEqual(39, data.Count); data = _api.Results(Tesseract.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(Tesseract.PageIteratorLevel.Word).ToList(); Assert.AreEqual(10, data.Count); } }
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); }
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; } } } } }
/// <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; }
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"; } }