public Pix DisplayNumaHeatmap(Numa NumaH = null, Pix BgPix = null, bool Swap = false) { var NumaW = this; if (Swap == true) { var TMpH = NumaH; var TmpW = NumaW; NumaH = TmpW; NumaW = TMpH; } int NumaHCnt = 1; if (NumaH != null) { NumaHCnt = NumaH.n; } int NumaWCnt = 1; if (NumaW != null) { NumaWCnt = NumaW.n; } double NumaHMax = 1; if (NumaH != null) { NumaHMax = (int)NumaH.array.Max(); } double NumaWMax = 1; if (NumaW != null) { NumaWMax = (int)NumaW.array.Max(); } int VMaxH = (int)(Math.Max(NumaHMax, NumaWCnt)); if (VMaxH > 2000) { VMaxH = 2000; } int VMaxW = (int)(Math.Max(NumaWMax, NumaHCnt)); if (VMaxW > 2000) { VMaxW = 2000; } int xres = 96, yres = 96; if (BgPix != null) { VMaxH = (int)BgPix.h; VMaxW = (int)BgPix.w; xres = BgPix.xres; yres = BgPix.yres; } double VDiffH = VMaxH / NumaHMax; double VDiffW = VMaxW / NumaWMax; using (var n = new Bitmap(VMaxW, VMaxH, PixelFormat.Format32bppArgb)) { n.SetResolution(xres, yres); using (var g = Graphics.FromImage(n)) { if (BgPix == null) { g.FillRectangle(Brushes.WhiteSmoke, new Rectangle(0, 0, VMaxW, VMaxH)); } if (BgPix != null) { g.DrawImage(BgPix.ToBitmap(), new Point(0, 0)); } if (NumaW != null) { int BarHeight = System.Convert.ToInt32(VMaxH / NumaWCnt); for (int i = 0; i <= NumaW.n - 1; i++) { int BarPos = (i * BarHeight) + (BarHeight == 1 ? 0 : (BarHeight / 2)); using (var Pen = new Pen(Color.FromArgb(System.Convert.ToInt32((130 / NumaWMax) * NumaW.array[i]), 0, 255, 0), BarHeight)) { var P1 = new Point(0, BarPos); var P2 = new Point(VMaxW, BarPos); g.DrawLine(Pen, P1, P2); } } } if (NumaH != null) { int BarWidth = Math.Max(System.Convert.ToInt32(VMaxW / NumaHCnt), 1); for (int i = 0; i <= NumaH.n - 1; i++) { int BarPos = (i * BarWidth) + (BarWidth == 1 ? 0 : (BarWidth / 2)); using (var Pen = new Pen(Color.FromArgb(System.Convert.ToInt32((130 / NumaHMax) * NumaH.array[i]), 0, 0, 255), BarWidth)) { var P1 = new Point(BarPos, 0); var P2 = new Point(BarPos, VMaxH); g.DrawLine(Pen, P1, P2); } } } return(new Pix(n)); } } }
public void Display(Pix Pix = null) { var n = new ShowPix(GetBitmap(Pix)); }
private bool CaptureLoadsXBOX() { bool isLoad = false; if ((timerStart.Ticks - liveSplitState.Run.Offset.Ticks) <= DateTime.Now.Ticks) { framesSinceLastManualSplit++; //Console.WriteLine("TIME NOW: {0}", DateTime.Now - lastTime); //Console.WriteLine("TIME DIFF START: {0}", DateTime.Now - lastTime); //lastTime = DateTime.Now; wasBlackScreen = isBlackScreen; //Capture image using the settings defined for the component Bitmap capture = settings.CaptureImage(); isBlackScreen = FeatureDetector.IsBlackScreen(ref capture, settings.blacklevel); BitmapToPixConverter btp = new BitmapToPixConverter(); //if (!testSaved) //{ // Bitmap jpntestbmp = new Bitmap("cutout.bmp"); // FeatureDetector.ClearBackgroundPostLoad(ref jpntestbmp); // jpntestbmp.Save("jpntest.bmp"); // Pix jpntest = btp.Convert(jpntestbmp); // using (var page = engine.Process(jpntest, PageSegMode.SingleChar)) // { // Console.WriteLine(page.GetText()); // } // testSaved = true; //} if (wasBlackScreen && !isBlackScreen) { //This could be a pre-load transition, start timing it transitionStart = DateTime.Now; waitOnLoad = true; } if (!isBlackScreen && !waitOnFadeIn && waitOnLoad) { specialLoad = FeatureDetector.ClearBackground(ref capture, settings.blacklevel); Pix img = btp.Convert(capture); string ResultText = ""; using (var page = engine.Process(img, PageSegMode.SingleChar)) { ResultText = page.GetText(); } int counter = 0; foreach (char c in expectedResultEng) { if (ResultText.Contains(c)) { counter++; } } if (counter > 5 && ResultText.Length == 8) { isLoading = true; isLoad = true; } } timer.CurrentState.IsGameTimePaused = isLoading || isBlackScreen; if (waitOnFadeIn && !specialLoad) { capture = settings.CaptureImagePostLoad(); int lowestBitLast = lowestBit; lowestBit = FeatureDetector.ClearBackgroundPostLoad(ref capture, settings.blacklevel); if (lowestBit == lowestBitLast && lowestBit != 0) { Pix img = btp.Convert(capture); using (var page = engine.Process(img, PageSegMode.SingleChar)) { string result = page.GetText(); if (result != "\n") { Console.WriteLine(page.GetText()); waitOnFadeIn = false; isLoading = false; lowestBit = 0; //the lifecounter coming in from the top takes a quarter of a second to stop TimeSpan quarter = new TimeSpan(2500000); timer.CurrentState.SetGameTime(timer.CurrentState.GameTimePauseTime + quarter); } } } } if (waitOnFadeIn && isBlackScreen && !specialLoad) { waitOnFadeIn = false; isLoading = false; } if (waitOnFadeIn && specialLoad && FeatureDetector.IsEndOfSpecialLoad(ref capture, settings.blacklevel)) { specialLoad = false; waitOnFadeIn = false; isLoading = false; } if (isLoading && waitOnLoad) { // This was a pre-load transition, subtract the gametime TimeSpan delta = (DateTime.Now - transitionStart); timer.CurrentState.SetGameTime(timer.CurrentState.GameTimePauseTime - delta); waitOnLoad = false; waitOnFadeIn = true; } } return(isLoad); }
private void CalibrateBlacklevel() { Bitmap capture = settings.CaptureImage(); int tempBlacklevel = FeatureDetector.GetBlackLevel(ref capture); if (tempBlacklevel != -1 && tempBlacklevel < settings.cmpBlackLevel) { settings.cmpBlackLevel = tempBlacklevel; } FeatureDetector.ClearBackground(ref capture, settings.cmpBlackLevel); BitmapToPixConverter btp = new BitmapToPixConverter(); Pix img = btp.Convert(capture); string ResultText = ""; using (var page = engine.Process(img, PageSegMode.SingleChar)) { ResultText = page.GetText(); } int counter = 0; if (settings.platform == "ENG/PS2" || settings.platform == "ENG/XBOX&GC") { foreach (char c in expectedResultEng) { if (ResultText.Contains(c)) { counter++; } } if (counter > 5 && ResultText.Length == 8) { isCmpFinished = true; } } else if (settings.platform == "JPN/PS2") { foreach (char c in expectedResultJpn) { if (ResultText.Contains(c)) { counter++; } } if (counter > 3) { isCmpFinished = true; } } else { foreach (char c in expectedResultKrn) { if (ResultText.Contains(c)) { counter++; } } if (counter > 2) { isCmpFinished = true; } } if (isCmpFinished) { settings.blacklevel = settings.cmpBlackLevel; isCmpFinished = false; settings.isCalibratingBlacklevel = false; settings.cmpBlackLevel = 100; Console.WriteLine("BLACKLEVEL: {0}", settings.blacklevel); settings.UpdateBlacklevelLabel(); settings.Refresh(); } }
public Pix(int w, int h) { Pix OBJ = LeptonicaSharp._All.pixCreate(w, h, 32); Pointer = OBJ.Pointer; }
/// <summary> /// Converts the specified <paramref name="pix"/> to a Bitmap. /// </summary> /// <param name="pix">The source image to be converted.</param> /// <returns>The converted pix as a <see cref="Bitmap"/>.</returns> public static Bitmap ToBitmap(Pix pix) { return(pixConverter.Convert(pix)); }
public static List <Row> DoOcr2(string imageDir) { // string imageDir = @"C\test."; try { var rowlist = new List <Row>(); var tets = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); using (var engine = new TesseractEngine(Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "tessdata"), "eng", EngineMode.TesseractAndLstm)) { engine.SetVariable("tessedit_char_whitelist", "16.00ABCDEFGHIJKLMNOPQRSTUVWXYZ(quick) brown 1 2 3 4 5 6 7 8 9 0 fox jumps!over the $3,456.78 < lazy >: #90 dog & duck/goose, as 12.5% of Email from [email protected] is spam?"); // engine.DefaultPageSegMode = PageSegMode.AutoOsd; Pix pixImage = Pix.LoadFromFile(imageDir); pixImage = pixImage.Deskew(); using (var image = new System.Drawing.Bitmap(imageDir)) { using (var pix = PixConverter.ToPix(image)) { Scew scew; var pixDeskew = pix.Deskew(new ScewSweep(range: 90), Pix.DefaultBinarySearchReduction, Tesseract.Pix.DefaultBinaryThreshold, out scew); using (var page = engine.Process(pix)) { var i = 1; var j = 1; var ki = page.GetIterator(); using (var iter = page.GetIterator()) { iter.Begin(); do { do { // Console.WriteLine("in-looop"); do { j = 1; do { Rect symbolBounds; if (iter.TryGetBoundingBox(PageIteratorLevel.Word, out symbolBounds)) { } Identify strType = new Identify(); rowlist.Add(new Row { Line = i, Colomun = j, StringType = Convert.ToString( strType.StringType( iter.GetText(PageIteratorLevel.Word))), Type = Convert.ToInt16( strType.StringType( iter.GetText(PageIteratorLevel.Word))), Word = iter.GetText(PageIteratorLevel.Word), XCordinateStart = symbolBounds.X1, XCordinateEnd = symbolBounds.X2, YCordinateEnd = symbolBounds.Y1, YCordinateStart = symbolBounds.Y2 }); j = j + 1; } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word)); i++; } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine)); } while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para)); } while (iter.Next(PageIteratorLevel.Block)); } } } } } XmlDocument doc = new XmlDocument(); //create the serialiser to create the xml XmlSerializer serialiser = new XmlSerializer(typeof(List <Row>)); if (!Directory.Exists(@"C:\TEST\")) { Directory.CreateDirectory(@"C:\TEST\"); } // Create the TextWriter for the serialiser to use TextWriter filestream = new StreamWriter(@"C:\TEST\" + Path.GetFileNameWithoutExtension(imageDir) + ".xml"); //write to the file serialiser.Serialize(filestream, rowlist); // Close the file filestream.Close(); return(rowlist); } catch (Exception ex) { return(null); } }
private Pix LoadTestImage(string path) { var fullExampleImagePath = TestFilePath(path); return(Pix.LoadFromFile(fullExampleImagePath)); }
public static bool OcrPage(string file, out MyPage page) { page = new MyPage(); using (var ocrengine = new TesseractEngine(@".\tessdata", "eng", EngineMode.Default)) { var img = Pix.LoadFromFile(file); //img = img.ConvertRGBToGray(); var res = ocrengine.Process(img); var iter = res.GetIterator(); // parse header bool headerFound = false; do { string header = iter.GetText(PageIteratorLevel.TextLine).Trim(); if (header.Contains("Time Received")) { headerFound = true; } } while (!headerFound && iter.Next(PageIteratorLevel.TextLine)); if (headerFound) { int x = 0; while (iter.Next(PageIteratorLevel.TextLine)) { string s = iter.GetText(PageIteratorLevel.TextLine); s = s.Trim(); if (!string.IsNullOrWhiteSpace(s)) { string[] lines = s.Split(' ', StringSplitOptions.RemoveEmptyEntries); if (lines.Length > 3) { string typeLine = lines[0].ToLower(); bool isWeapon = typeLine.Contains("weapon"); bool isCharacter = typeLine.Contains("character"); if (!isWeapon && !isCharacter) { Console.WriteLine($"Faulty type found in file {file}, line {s}."); return(false); } else if (isWeapon && isCharacter) { throw new Exception("this should never happen"); } string dateStr = $"{lines[lines.Length - 2]} {lines[lines.Length - 1]}"; DateTime date; if (!DateTime.TryParse(dateStr, out date)) { Console.WriteLine($"Incorrect date format, file {file}, line {s}."); return(false); } StringBuilder sb = new StringBuilder(); bool first = true; for (int i = 1; i < lines.Length - 2; i++) { if (first) { first = false; } else { sb.Append(" "); } sb.Append(lines[i]); } string valueItem = sb.ToString(); valueItem = Massage(valueItem); Selection sel = new Selection(x, isWeapon, isCharacter, valueItem, date); x++; page.Selections.Add(sel); } } } } else { Console.WriteLine($"Error: no header found for {file}, Data: {res.GetText().Trim()}."); return(false); } //Console.WriteLine(res.GetText().Trim()); if (page.Selections.Count > 6) { Console.WriteLine($"Error: too many selections for {file}, Data: {res.GetText().Trim()}."); } } return(true); }
private void SetMainImageSize(Pix pix) { string picSize = pix.Height + "x" + pix.Width; MainImageSize.Text = picSize; }
private void openFile_Click(object sender, EventArgs e) { char[,] braille2D = new char[, ] { //{ " el ", " ⠮ " }, //{ " y ", " ⠯ " }, //{ " de ", " ⠷ " }, //{ " con ", " ⠾ " }, //{ " en ", " ⠔ " }, //{ " o ", " ⠖ " }, //{ " para ", " ⠿ " }, { ',', '⠠' }, { '/', '⠌' }, { '?', '⠹' }, { '.', '⠨' }, { 'á', '⠁' }, { 'é', '⠑' }, { 'í', '⠊' }, { 'ó', '⠕' }, { 'ú', '⠥' }, { 'a', '⠁' }, { 'b', '⠃' }, { 'c', '⠉' }, { 'd', '⠙' }, { 'e', '⠑' }, { 'f', '⠋' }, { 'g', '⠛' }, { 'h', '⠓' }, { 'i', '⠊' }, { 'j', '⠚' }, { 'k', '⠅' }, { 'l', '⠇' }, { 'm', '⠍' }, { 'ñ', '⠝' }, { 'n', '⠝' }, { 'o', '⠕' }, { 'p', '⠏' }, { 'q', '⠟' }, { 'r', '⠗' }, { 's', '⠎' }, { 't', '⠞' }, { 'u', '⠥' }, { 'v', '⠧' }, { 'w', '⠺' }, { 'x', '⠭' }, { 'y', '⠽' }, { 'z', '⠵' }, { 'A', '⠁' }, { 'Á', '⠁' }, { 'B', '⠃' }, { 'C', '⠉' }, { 'D', '⠙' }, { 'E', '⠑' }, { 'É', '⠑' }, { 'F', '⠋' }, { 'G', '⠛' }, { 'H', '⠓' }, { 'I', '⠊' }, { 'Í', '⠊' }, { 'J', '⠚' }, { 'K', '⠅' }, { 'L', '⠇' }, { 'M', '⠍' }, { 'N', '⠝' }, { 'O', '⠕' }, { 'Ó', '⠕' }, { 'P', '⠏' }, { 'Q', '⠟' }, { 'R', '⠗' }, { 'S', '⠎' }, { 'T', '⠞' }, { 'U', '⠥' }, { 'Ú', '⠥' }, { 'V', '⠧' }, { 'W', '⠺' }, { 'X', '⠭' }, { 'Y', '⠽' }, { 'Z', '⠵' }, { '#', '⠼' }, { '1', '⠂' }, { '2', '⠆' }, { '3', '⠲' }, { '4', '⠲' }, { '5', '⠢' }, { '6', '⠖' }, { '7', '⠶' }, { '8', '⠦' }, { '9', '⠔' }, { '0', '⠴' }, { ' ', ' ' } }; //var fileContent = string.Empty; var filePath = string.Empty; using (OpenFileDialog openFileDialog1 = new OpenFileDialog()) { sb.Append("se crea openfiledialog\n"); openFileDialog1.InitialDirectory = "c:\\"; openFileDialog1.Filter = "Image Files(*.png; *.jpg; *.bmp; *.gif)|*.png; *.jpg; *.bmp; *.gif"; openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() == DialogResult.OK) { sb.Append("se abre openfile dialog\n"); //Get the path of specified file filePath = openFileDialog1.FileName; //Read the contents of the file into a stream var fileStream = openFileDialog1.OpenFile(); //using (StreamReader reader = new StreamReader(fileStream)) //{ // fileContent = reader.ReadToEnd(); //} } } if (filePath != null && filePath != "") { //sb.Append("se valida el filepath\n"); MessageBox.Show("Imagen Cargada Correctamente", "Exito!", MessageBoxButtons.OK); pictureBox1.Image = Image.FromFile(filePath); var testImagePath = filePath; try { //sb.Append("entra en try de tesseract\n"); using (var engine = new TesseractEngine(@"./tessdata", "spa", EngineMode.Default)) { //sb.Append("entra en tesseract engine\n"); using (var img = Pix.LoadFromFile(testImagePath)) { //sb.Append("usa pix para loadfromimage\n"); using (var page = engine.Process(img)) { //sb.Append("hace engine process para path de imagen\n"); var text = page.GetText().TrimEnd(); //sb.Append("justo antes del for para replace\n"); for (int i = 0; i < 79; i++) { //Console.WriteLine("indice de foreach: " + i); //Console.WriteLine("valor: " + braille2D[i, 0]); //Console.WriteLine("remplazo: " + braille2D[i, 1]); text = text.Replace(braille2D[i, 0], braille2D[i, 1]); } textBox1.Text = text; var txtInput = Path.GetTempPath() + "log.txt"; File.WriteAllText(txtInput, text); //sb.Append("se termina de escribir el output en un path temporal\n"); //File.AppendAllText("logger.txt", //sb.ToString()); //sb.Clear(); } } } } catch (Exception err) { //sb.Append("Se encontro algun error durante el try tesseract\n"+ err.ToString()+"\n"); Trace.TraceError(err.ToString()); Console.WriteLine("Unexpected Error: " + err.Message); Console.WriteLine("Details: "); Console.WriteLine(err.ToString()); //File.AppendAllText("logger.txt", //sb.ToString()); //sb.Clear(); } } }
private void ConvertImages() { for (int i = 0; i < files.Count; i++) { OcrFile file = files[i]; Bitmap bitmap = new Bitmap(file.FileInfo.FullName); if (bitmap == null) { file.Status = "Not an image..."; //Refresh the UI lvFiles.Dispatcher.BeginInvoke(DispatcherPriority.Normal, null, null); continue; } string invertedImagePath = Path.Combine(file.FileInfo.DirectoryName, GetInvertedImageName(file.FileInfo)); //First remove transparency by drawing a black background and drawing the text on it //Then invert the colors of the whole thing. It's better for the OCR //Finally scale it, also better for the OCR using (var b = new Bitmap(bitmap.Width, bitmap.Height)) { b.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution); using (var g = Graphics.FromImage(b)) { g.Clear(Color.Black); g.DrawImageUnscaled(bitmap, 0, 0); } MagickImage magickImage = new MagickImage(b); magickImage.Negate(Channels.RGB); //magickImage.FilterType = FilterType.Lanczos2; magickImage.Resize(1024, 1024); Bitmap inverted = magickImage.ToBitmap(); inverted.Save(invertedImagePath); inverted.Dispose(); magickImage.Dispose(); } try { using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) { using (var img = Pix.LoadFromFile(invertedImagePath)) { using (var page = engine.Process(img)) { File.WriteAllText(string.Concat(file.FileInfo.FullName, ".txt"), page.GetText()); file.Confidence = page.GetMeanConfidence(); file.Status = string.Concat("Done! (", file.Confidence.ToString("0.00"), ")"); } } } } catch (Exception error) { file.Status = "Tesseract Error: " + error.Message; } File.Delete(invertedImagePath); //Refresh the UI lvFiles.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateFileDelegate(UpdateFile), file, i); } //I have no idea what I'm doing... //lvFiles.ItemsSource = null; //lvFiles.ItemsSource = files; Dispatcher.BeginInvoke(DispatcherPriority.Normal, new ConversionDoneDelegate(ConversionDone)); }
public void Init() { Engine = CreateEngine(); EmptyPix = LoadTestPix("Ocr\\blank.tif"); }
internal static Bitmap Injection(byte[] original, byte[] content) { int index = 0; byte[] length = BitConverter.GetBytes(content.Length); byte[] data = new byte[content.Length + 4]; Array.Copy(length, data, 4); Array.Copy(content, 0, data, 4, content.Length); var bmp = (Bitmap)Image.FromStream(new MemoryStream(original)); { unsafe { BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); int size = bmd.Stride / bmp.Width; bool hasAlpha = size == 4; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { int offset = (y * bmd.Stride) + (x * size); Pix pix = new Pix { blue = ((byte*)bmd.Scan0)[offset], green = ((byte*)bmd.Scan0)[offset + 1], red = ((byte*)bmd.Scan0)[offset + 2], alpha = ((hasAlpha) ? ((byte*)bmd.Scan0)[offset + 3] : (byte)255) }; if (index >= data.Length) { pix = MixByteAndPixel(pix, ByteDecomposition((byte)rnd.Next(0, 255), hasAlpha)); ((byte*)bmd.Scan0)[offset] = pix.blue; ((byte*)bmd.Scan0)[offset + 1] = pix.green; ((byte*)bmd.Scan0)[offset + 2] = pix.red; if(hasAlpha) ((byte*)bmd.Scan0)[offset + 3] = pix.alpha; } else { pix = MixByteAndPixel(pix, ByteDecomposition(data[index], hasAlpha)); ((byte*)bmd.Scan0)[offset] = pix.blue; ((byte*)bmd.Scan0)[offset + 1] = pix.green; ((byte*)bmd.Scan0)[offset + 2] = pix.red; if (hasAlpha) ((byte*)bmd.Scan0)[offset + 3] = pix.alpha; } index++; } } bmp.UnlockBits(bmd); } } return bmp; }
private void SaveResult(Pix result, string filename) { var runFilename = TestResultRunFile(Path.Combine(ResultsDirectory, filename)); result.Save(runFilename); }
public static void Main(string[] args) { var testImagePath = "./phototest.tif"; if (args.Length > 0) { testImagePath = args[0]; } try { var logger = new FormattedConsoleLogger(); var resultPrinter = new ResultPrinter(logger); using (var engine = new TesseractEngine(@"./tessdata", "fra", EngineMode.Default)) { using (var img = Pix.LoadFromFile(testImagePath)) { using (logger.Begin("Process image")) { var i = 1; using (var page = engine.Process(img)) { var text = page.GetText(); logger.Log("Text: {0}", text); logger.Log("Mean confidence: {0}", page.GetMeanConfidence()); using (var iter = page.GetIterator()) { iter.Begin(); do { if (i % 2 == 0) { using (logger.Begin("Line {0}", i)) { do { using (logger.Begin("Word Iteration")) { if (iter.IsAtBeginningOf(PageIteratorLevel.Block)) { logger.Log("New block"); } if (iter.IsAtBeginningOf(PageIteratorLevel.Para)) { logger.Log("New paragraph"); } if (iter.IsAtBeginningOf(PageIteratorLevel.TextLine)) { logger.Log("New line"); } logger.Log("word: " + iter.GetText(PageIteratorLevel.Word)); } } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word)); } } i++; } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine)); } } } } } } catch (Exception e) { Trace.TraceError(e.ToString()); Console.WriteLine("Unexpected Error: " + e.Message); Console.WriteLine("Details: "); Console.WriteLine(e.ToString()); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
public unsafe static OcrNode OCR( TesseractEngine engine, Mat input, int resolution) { using var pix = Pix.Create(input.Width, input.Height, 8); pix.XRes = resolution; pix.YRes = resolution; var pixData = pix.GetData(); var data = (uint *)pixData.Data; var wordsPerLine = pixData.WordsPerLine; input.ForEachAsByte((value, position) => { var y = position[0]; var x = position[1]; var color = *value; PixData.SetDataByte(data + y * wordsPerLine, x, color); }); using var page = engine.Process(pix); var ocrPage = page.GetTsvText(1). Split("\n", StringSplitOptions.RemoveEmptyEntries). Select(line => line.Split("\t")). Select(columns => new OcrNode { Level = int.Parse(columns[0]), PageNum = int.Parse(columns[1]), BlockNum = int.Parse(columns[2]), ParNum = int.Parse(columns[3]), LineNum = int.Parse(columns[4]), WordNum = int.Parse(columns[5]), Left = int.Parse(columns[6]), Top = int.Parse(columns[7]), Width = int.Parse(columns[8]), Height = int.Parse(columns[9]), Confidence = int.Parse(columns[10]), Text = columns.Length > 11 ? columns[11] : null }). Aggregate((prev, next) => { if (prev == null) { if (next.Level != 1) { throw new InvalidOperationException("nodes"); } } else { if ((next.Level <= 0) || (next.Level > prev.Level + 1)) { throw new InvalidOperationException("nodes"); } while (next.Level != prev.Level + 1) { prev = prev.Parent; } next.Parent = prev; if (prev.Children == null) { prev.Children = new List <OcrNode>(); } next.Parent.Children.Add(next); } return(next); }); while (ocrPage.Parent != null) { ocrPage = ocrPage.Parent; } ocrPage.Text = page.GetText(); return(ocrPage); }
private void button1_Click(object sender, EventArgs e) { if (textBox1.Text != "" && textBox3.Text != "") { var dialogTypeName = "System.Windows.Forms.PropertyGridInternal.GridErrorDlg"; var dialogType = typeof(Form).Assembly.GetType(dialogTypeName); string ex1 = textBox1.Text; var dictionary = new Dictionary <string, Person>(); string[] files = Directory.GetFiles(textBox1.Text); List <string> outputs = new List <string>(); List <string> resolvedoutputs = new List <string>(); for (int i = 0; i < files.Length; i++) { try { using (var engine = new TesseractEngine(@"tessdata", "eng", EngineMode.Default)) { using (var img = Pix.LoadFromFile(files[i])) { using (var page = engine.Process(img)) { var text = page.GetText(); outputs.Add(text); } } } } catch (Exception ex) { MessageBox.Show(String.Format("Error: {0}", ex.Message)); } resolvedoutputs.Add(outputs[i]); int indexOfFirstPhrase = outputs[i].IndexOf("32920"); if (indexOfFirstPhrase >= 0) { indexOfFirstPhrase += textBox2.Text.Length; int indexOfSecondPhrase = outputs[i].IndexOf("khfjlkahfgkjeahfjhaekhfkjahjslf", indexOfFirstPhrase); if (indexOfSecondPhrase >= 0) { resolvedoutputs[i] = outputs[i].Substring(indexOfFirstPhrase, indexOfSecondPhrase - indexOfFirstPhrase); } else { resolvedoutputs[i] = outputs[i].Substring(indexOfFirstPhrase); } } string tempOut = DeleteLines(resolvedoutputs[i], 3, false); int index = resolvedoutputs[i].IndexOf(tempOut, StringComparison.Ordinal); string cleanPath = (index < 0) ? resolvedoutputs[i] : resolvedoutputs[i].Remove(index, tempOut.Length); string[] resolvedOutputsName = resolvedoutputs[i].Split(new Char[] { '\n' }); int resolvingIndex = 0; resolvedOutputsName = resolvedOutputsName.Where(x => !string.IsNullOrEmpty(x)).ToArray(); string personName = "name not set"; string personStreet = "street not set"; string personCity = "person not set"; string personState = "state not set"; string personZip = "zip not set"; foreach (string s in resolvedOutputsName) { if (true) { switch (resolvingIndex) { case 0: //MessageBox.Show("Name: " + s); personName = s.Trim(); break; case 1: //MessageBox.Show("Street Address: " + s); personStreet = s.Trim(); break; case 2: //MessageBox.Show("City: " + s); personCity = s.Trim(); break; } } resolvingIndex++; } try { personZip = personCity.Substring(personCity.Length - 5); } catch (Exception ex) { var dialog = (Form)Activator.CreateInstance(dialogType, new PropertyGrid()); dialog.Text = "Error reading document!"; dialogType.GetProperty("Details").SetValue(dialog, ex.ToString(), null); dialogType.GetProperty("Message").SetValue(dialog, "Significant OCR errors detected, manual analysis very likely needed!\nError: Could not detect ZIP code!", null); // Display error var result = dialog.ShowDialog(); } int cityIndex = personCity.IndexOf(","); if (personCity.Length > cityIndex + 2) { if (cityIndex > 0) { personState = personCity.Substring(cityIndex + 2, 2); } } if (cityIndex > 0) { personCity = personCity.Substring(0, cityIndex); } dictionary.Add("NewPerson" + i, new Person()); dictionary["NewPerson" + i].Name = personName; dictionary["NewPerson" + i].Street = personStreet; dictionary["NewPerson" + i].City = personCity; dictionary["NewPerson" + i].State = personState; dictionary["NewPerson" + i].Zip = personZip; Form2 form2 = new Form2(); form2.Text = String.Format("{0} / {1} - {2}", i + 1, files.Length, files[i]); form2.textBox1.Text = dictionary["NewPerson" + i].Name; form2.textBox2.Text = dictionary["NewPerson" + i].Street; form2.textBox3.Text = dictionary["NewPerson" + i].City; form2.textBox4.Text = dictionary["NewPerson" + i].State; form2.textBox5.Text = dictionary["NewPerson" + i].Zip.ToString(); form2.textBox6.Text = textBox3.Text; form2.richTextBox1.Text = resolvedoutputs[i]; if (form2.richTextBox1.Text.Contains("UFF") || form2.richTextBox1.Text.Contains("INS")) { form2.radioButton8.Checked = true; } else if (form2.richTextBox1.Text.Contains("DEL")) { form2.radioButton1.Checked = true; } else if (form2.richTextBox1.Text.Contains("EPT") || form2.richTextBox1.Text.Contains("AI")) { form2.radioButton5.Checked = true; } else if (form2.richTextBox1.Text.Contains("STR")) { form2.radioButton4.Checked = true; } else if (form2.richTextBox1.Text.Contains("NUM")) { form2.radioButton2.Checked = true; } else if (form2.richTextBox1.Text.Contains("VAC")) { form2.radioButton3.Checked = true; } else if (form2.richTextBox1.Text.Contains("KNO") || form2.richTextBox1.Text.Contains("TT")) { form2.radioButton7.Checked = true; } if (form2.richTextBox1.Text.Contains("Attn")) { form2.textBox1.Text = "Attn: Property Owner"; } else if (form2.richTextBox1.Text.Contains("Asset") || form2.richTextBox1.Text.Contains("Mana") || form2.richTextBox1.Text.Contains("Comm")) { form2.textBox1.Text = "Asset Manager for Massachusetts Commercial & Industrial Property"; } form2.pictureBox1.Image = Image.FromFile(files[i]); form2.ShowDialog(); string reason = ""; //Determining Reason if (form2.radioButton1.Checked) { reason = form2.radioButton1.Text; } else if (form2.radioButton2.Checked) { reason = form2.radioButton2.Text; } else if (form2.radioButton3.Checked) { reason = form2.radioButton3.Text; } else if (form2.radioButton4.Checked) { reason = form2.radioButton4.Text; } else if (form2.radioButton5.Checked) { reason = form2.radioButton5.Text; } else if (form2.radioButton6.Checked) { reason = form2.textBox7.Text; } else if (form2.radioButton7.Checked) { reason = form2.radioButton7.Text; } else if (form2.radioButton8.Checked) { reason = form2.radioButton8.Text; } using (StreamWriter sw = File.AppendText(textBox1.Text + "\\" + form2.textBox6.Text + ".sql")) { sw.WriteLine(String.Format("INSERT INTO people (name, street, city, state, zip, reason) VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}');", form2.textBox1.Text, form2.textBox2.Text, form2.textBox3.Text, form2.textBox4.Text, form2.textBox5.Text, reason)); } if (i == files.Length - 1) { //Confirmation MessageBox.Show("Saved to: " + textBox1.Text + "\\" + form2.textBox6.Text + ".sql"); } } Application.Exit(); } else { MessageBox.Show("Please fill in all required forms!"); } }
/// <summary>Performs default image preprocessing.</summary> /// <remarks> /// Performs default image preprocessing. /// It includes the following actions: /// removing alpha channel, /// converting to grayscale, /// thresholding. /// </remarks> /// <param name="pix"> /// /// <see cref="Tesseract.Pix"/> /// object to be processed /// </param> /// <returns> /// preprocessed /// <see cref="Tesseract.Pix"/> /// object /// </returns> internal static Pix PreprocessPix(Pix pix) { pix = ConvertToGrayscale(pix); pix = OtsuImageThresholding(pix); return(pix); }
public async Task <IActionResult> ImageHandle(ApplicationUser currentUser, string token = "") { int count = 0; if (currentUser.PreviousLogin != null) { string link = "https://graph.facebook.com/v4.0/me?fields=id,name,feed.since("; string time = currentUser.PreviousLogin.ToString("s", DateTimeFormatInfo.InvariantInfo); //bunu userin prevLogin - den gotureceyik ok//api key deyis string linkEnd = "){message,application,full_picture,created_time}&&access_token="; string fullLink = link + time + linkEnd + token; var client = new RestClient(fullLink); var request = new RestRequest(); // do whatever else you want/need to, to the request // ... //// ... and use it like we used to var response = client.Execute(request); var usersFB = JsonConvert.DeserializeObject <FacebookImage>(response.Content); if (usersFB.Feed != null) { foreach (var item in usersFB.Feed.data.ToList()) { if (item.application != null) { if (item.application.name == "PUBG Mobile") { var uploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); await CropImage(item.full_picture); string imageName = item.created_time.ToString("dd-MM-yyyy-HH-mm") + currentUser.PubgUsername + ".jpg"; string res; using (var engine = new TesseractEngine(Path.Combine(".\\", "tessdata"), "eng", EngineMode.Default)) { using (var image = Pix.LoadFromFile(".\\Uploads\\foo.jpg")) { using (var page = engine.Process(image)) { res = page.GetText(); } } } int val = res.IndexOf("#"); if (val > 0) { var rating = res.Substring(val, 3); string value = rating.Substring(1, 2); var trm = value.Trim(); if (trm == "|" || trm == "l" || trm == "1") { trm = "1"; count++; Posted post = new Posted(); post.Season = _context.Seasons.FirstOrDefault(x => x.IsActive == true); post.SeasonId = _context.Seasons.FirstOrDefault(x => x.IsActive == true).Id; post.ImagePath = imageName; post.CreatedAt = item.created_time; post.ApplicationUser = currentUser; post.ApplicationUserId = currentUser.Id; _context.Posteds.Add(post); _context.SaveChanges(); await CropImage2(item.full_picture, imageName, uploadFolder); } } } } } } } //return Content(count.ToString()); return(RedirectToAction("cabinet")); }
static void Main(string[] args) { string text = string.Empty; try { using (var engine = new TesseractEngine(@"C:\Users\birkan\Source\Repos\OCR-Sample\OCR-Sample\tessdata", "tur", EngineMode.Default)) { using (var img = Pix.LoadFromFile(@"C:\Users\birkan\Source\Repos\OCR-Sample\OCR-Sample\test.png")) { using (var page = engine.Process(img)) { using (var iter = page.GetIterator()) { iter.Begin(); do { do { do { do { string buffer = iter.GetText(PageIteratorLevel.Word); if (!(buffer == "" || buffer == null || buffer == string.Empty || buffer == " ")) { text += " " + iter.GetText(PageIteratorLevel.Word); } if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word)) { if (!(text == " " || text == String.Empty)) { text += " "; } } } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word)); if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine)) { if (!(text == "" || text == String.Empty)) { text += " "; } } } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine)); } while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para)); } while (iter.Next(PageIteratorLevel.Block)); } } } } } catch (Exception e) { text = ""; } Console.WriteLine(text); Console.ReadKey(); }
public static decimal Capture(IntPtr handle, Point pos, Size size, bool debug = false) { if (handle == IntPtr.Zero) { return(0); } Bitmap b = CaptureWindowArea(handle, pos, size); if (b == null) { return(0); } if (debug) { Directory.CreateDirectory(Application.StartupPath + @"\debug"); } if (debug) { b.Save(Application.StartupPath + @"\debug\00_input.png"); } using (Pix pixc = PixConverter.ToPix(b)) { b.Dispose(); using (Pix pixg = pixc.ConvertRGBToGray(0, 0, 0)) { if (debug) { pixg.Save(Application.StartupPath + @"\debug\01_grayscale.png"); } using (Pix pixs = pixg.ScaleGrayLI(Config.OCRScaleFactor, Config.OCRScaleFactor)) { if (debug) { pixs.Save(Application.StartupPath + @"\debug\02_scale.png"); } //pix = pix.UnsharpMaskingGray(5, 2.5f); //issues with light text on light bg using (Pix pixb = pixs.BinarizeOtsuAdaptiveThreshold(2000, 2000, 0, 0, 0.0f)) { if (debug) { pixb.Save(Application.StartupPath + @"\debug\03_binarize.png"); } float pixAvg = pixb.AverageOnLine(0, 0, pixb.Width - 1, 0, 1); pixAvg += pixb.AverageOnLine(0, pixb.Height - 1, pixb.Width - 1, pixb.Height - 1, 1); pixAvg += pixb.AverageOnLine(0, 0, 0, pixb.Height - 1, 1); pixAvg += pixb.AverageOnLine(pixb.Width - 1, 0, pixb.Width - 1, pixb.Height - 1, 1); pixAvg /= 4.0f; using (Pix pixi = pixAvg > 0.5f ? pixb.Invert() : pixb) { if (debug) { pixi.Save(Application.StartupPath + $@"\debug\04_invert_{pixAvg > 0.5f}.png"); } using (Pix pixn = pixi.SelectBySize(Config.OCRNoiseSize, Config.OCRNoiseSize, Config.OCRNoiseConnectivity, Config.OCRNoiseType, Config.OCRNoiseRelation)) { if (debug) { pixn.Save(Application.StartupPath + @"\debug\05_removenoise.png"); } //pixn.ClipToForeground(IntPtr.Zero); using (Pix pix = pixn.AddBorder(Config.OCRPadding, 0)) { if (debug) { pix.Save(Application.StartupPath + @"\debug\06_border.png"); } pix.XRes = 300; pix.YRes = 300; string ocrText = ""; using (TesseractEngine engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) { using (Page page = engine.Process(pix, PageSegMode.SingleLine)) { ocrText = page.GetText().Trim(); Confidence = page.GetMeanConfidence(); if (Confidence >= Config.OCRMinimumConfidence) { Text = ocrText; if (decimal.TryParse(ocrText, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out decimal cooldown)) { if (cooldown < Config.CooldownMaxPossible) { return(cooldown); } } } } } } } } } } } } return(0); }
public static void Main(string[] args) { var testImagePath = "./phototest.tif"; if (args.Length > 0) { testImagePath = args[0]; } try { using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) { using (var img = Pix.LoadFromFile(testImagePath)) { using (var page = engine.Process(img)) { var text = page.GetText(); Console.WriteLine("Mean confidence: {0}", page.GetMeanConfidence()); Console.WriteLine("Text (GetText): \r\n{0}", text); Console.WriteLine("Text (iterator):"); using (var iter = page.GetIterator()) { iter.Begin(); do { do { do { do { if (iter.IsAtBeginningOf(PageIteratorLevel.Block)) { Console.WriteLine("<BLOCK>"); } Console.Write(iter.GetText(PageIteratorLevel.Word)); Console.Write(" "); if (iter.IsAtFinalOf(PageIteratorLevel.TextLine, PageIteratorLevel.Word)) { Console.WriteLine(); } } while (iter.Next(PageIteratorLevel.TextLine, PageIteratorLevel.Word)); if (iter.IsAtFinalOf(PageIteratorLevel.Para, PageIteratorLevel.TextLine)) { Console.WriteLine(); } } while (iter.Next(PageIteratorLevel.Para, PageIteratorLevel.TextLine)); } while (iter.Next(PageIteratorLevel.Block, PageIteratorLevel.Para)); } while (iter.Next(PageIteratorLevel.Block)); } } } } } catch (Exception e) { Trace.TraceError(e.ToString()); Console.WriteLine("Unexpected Error: " + e.Message); Console.WriteLine("Details: "); Console.WriteLine(e.ToString()); } Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
private bool CaptureLoadsPS2() { bool isLoad = false; framesSinceLastManualSplit++; //Console.WriteLine("TIME NOW: {0}", DateTime.Now - lastTime);13 //Console.WriteLine("TIME DIFF START: {0}", DateTime.Now - lastTime); lastTime = DateTime.Now; //Capture image using the settings defined for the component Bitmap capture = settings.CaptureImage(); wasBlackScreen = isBlackScreen; isBlackScreen = FeatureDetector.IsBlackScreen(ref capture, settings.blacklevel); //BitmapToPixConverter btp = new BitmapToPixConverter(); //if (!testSaved) //{ // Bitmap jpntestbmp = new Bitmap("cutout.bmp"); // FeatureDetector.GetBlackLevel(ref jpntestbmp); // FeatureDetector.ClearBackground(ref jpntestbmp); // jpntestbmp.Save("jpntest.bmp"); // Pix jpntest = btp.Convert(jpntestbmp); // using (var page = engine.Process(jpntest, PageSegMode.SingleChar)) // { // Console.WriteLine(page.GetText()); // } // testSaved = true; //} if (!isBlackScreen && waitOnLoad) { FeatureDetector.ClearBackground(ref capture, settings.blacklevel); BitmapToPixConverter btp = new BitmapToPixConverter(); Pix img = btp.Convert(capture); string ResultText = ""; using (var page = engine.Process(img, PageSegMode.SingleChar)) { ResultText = page.GetText(); //Console.WriteLine(ResultText); } int counter = 0; if (settings.platform == "ENG/PS2") { foreach (char c in expectedResultEng) { if (ResultText.Contains(c)) { counter++; } } if (counter > 5 && ResultText.Length == 8) { isLoading = true; isLoad = true; } else { waitFrames++; if (waitFrames == WAIT_ON_LOAD_FRAMES) { waitOnLoad = false; waitFrames = 0; } } } else if (settings.platform == "JPN/PS2") { foreach (char c in expectedResultJpn) { if (ResultText.Contains(c)) { counter++; } } if (counter > 3) { isLoading = true; isLoad = true; } else { waitFrames++; if (waitFrames == WAIT_ON_LOAD_FRAMES) { waitOnLoad = false; waitFrames = 0; } } } //TODO: add korean else { foreach (char c in expectedResultKrn) { if (ResultText.Contains(c)) { counter++; } } if (counter > 1) { isLoading = true; isLoad = true; } else { waitFrames++; if (waitFrames == WAIT_ON_LOAD_FRAMES) { waitOnLoad = false; waitFrames = 0; } } } } timer.CurrentState.IsGameTimePaused = isLoading || isBlackScreen; if (waitOnFadeIn && isBlackScreen) { waitOnFadeIn = false; isLoading = false; } if (wasBlackScreen && !isBlackScreen) { //This could be a pre-load transition, start timing it transitionStart = DateTime.Now; waitOnLoad = true; waitFrames = 0; } if (isLoading && waitOnLoad) { // This was a pre-load transition, subtract the gametime TimeSpan delta = (DateTime.Now - transitionStart); timer.CurrentState.SetGameTime(timer.CurrentState.GameTimePauseTime - delta); waitOnLoad = false; waitFrames = 0; waitOnFadeIn = true; } return(isLoad); }
/// <summary> /// Gets all the text as string from image. /// </summary> /// <param name="imagePath">The image path.</param> /// <returns></returns> public static string GetStringFromImage(string imagePath, string lang = "eng", EngineMode mode = EngineMode.Default) { if (!Directory.Exists(DataPath)) { throw new Exception(" Directory NOT exist DataPath"); } var textValue = string.Empty; if (!string.IsNullOrWhiteSpace(imagePath) && System.IO.File.Exists(imagePath)) { try { using (var engine = CreateEngine()) { using (var img = Pix.LoadFromFile(imagePath)) { //FBDH67 \FflbHaT var fileName = Path.GetFileName(imagePath).ToLowerInvariant(); //using (var page = engine.Process(img, fileName, new Rect(0, 0, img.Width, img.Height), PageSegMode.CircleWord)) //{ // textValue = page.GetText(); // var s = String.Format("{0:P}", page.GetMeanConfidence()); // var d = page.GetIterator(); // var f = page.GetThresholdedImage(); // var aSymbol = page.GetSegmentedRegions(PageIteratorLevel.Symbol); // var aBlock = page.GetSegmentedRegions(PageIteratorLevel.Block); // var aPara = page.GetSegmentedRegions(PageIteratorLevel.Para); // var aTextLine = page.GetSegmentedRegions(PageIteratorLevel.TextLine); // var aWord = page.GetSegmentedRegions(PageIteratorLevel.Word); //} using (var page = engine.Process(img, fileName, new Rect(0, 0, img.Width, img.Height), PageSegMode.CircleWord)) { textValue = page.GetText(); } using (var page = engine.Process(img, fileName, new Rect(0, 0, img.Width, img.Height), PageSegMode.SparseTextOsd)) { textValue = page.GetText(); } using (var page = engine.Process(img, fileName, new Rect(0, 0, img.Width, img.Height), PageSegMode.Count)) { textValue = page.GetText(); } using (var page = engine.Process(img, fileName, new Rect(0, 0, img.Width, img.Height), PageSegMode.SingleColumn)) { textValue = page.GetText(); } using (var page = engine.Process(img, fileName, new Rect(0, 0, img.Width, img.Height), PageSegMode.SparseText)) { textValue = page.GetText(); } } } } catch (Exception ex) { Console.WriteLine("Unexpected Error: " + ex.Message); } } return(textValue); }
public Pix DisplayNumaBarGraph(Numa NumaH = null, Pix BgPix = null, bool Swap = false) { var NumaW = this; if (Swap == true) { var TMpH = NumaH; var TmpW = this; NumaH = TmpW; NumaW = TMpH; } int NumaHCnt = 1; if (NumaH != null) { NumaHCnt = NumaH.n; } int NumaWCnt = 1; if (NumaW != null) { NumaWCnt = NumaW.n; } double NumaHMax = 1; if (NumaH != null) { NumaHMax = (int)NumaH.array.Max(); } double NumaWMax = 1; if (NumaW != null) { NumaWMax = (int)NumaW.array.Max(); } int VMaxH = (int)Math.Max(NumaHMax, NumaWCnt); if (VMaxH > 2000) { VMaxH = 2000; } int VMaxW = (int)Math.Max(NumaWMax, NumaHCnt); if (VMaxW > 2000) { VMaxW = 2000; } int xres = 96, yres = 96; if (BgPix != null) { VMaxH = (int)BgPix.h; VMaxW = (int)BgPix.w; xres = BgPix.xres; yres = BgPix.yres; } double DiffH = VMaxH / NumaHMax; double DiffW = VMaxW / NumaWMax; using (var n = new Bitmap(VMaxW, VMaxH, PixelFormat.Format32bppArgb)) { n.SetResolution(xres, yres); using (var Pen1 = new Pen(Color.FromArgb(150, 0, 255, 0), 1)) using (var Pen2 = new Pen(Color.FromArgb(150, 255, 0, 0), 1)) using (var g = Graphics.FromImage(n)) { if (BgPix == null) { g.FillRectangle(Brushes.WhiteSmoke, new Rectangle(0, 0, VMaxW, VMaxH)); } if (BgPix != null) { g.DrawImage(BgPix.ToBitmap(), new Point(0, 0)); } if (NumaW != null) { for (int i = 0; i <= NumaW.n - 1; i++) { g.DrawLine(Pen1, new Point(0, i + 1), new Point((int)NumaW.array[i] * (int)DiffW, i + 1)); } } if (NumaH != null) { for (int i = 0; i <= NumaH.n - 1; i++) { g.DrawLine(Pen2, new Point(i + 1, (int)VMaxH - ((int)NumaH.array[i] * (int)DiffH)), new Point(i + 1, VMaxH)); } } return(new Pix(n)); } } }
protected static Pix LoadTestPix(string filename) { var testFilename = TestFilePath(filename); return(Pix.LoadFromFile(testFilename)); }
public void Display(Pix Pix) { var CRed = BitConverter.ToUInt32(new byte[] { 255, 0, 0, 0 }, 0); var CBlu = BitConverter.ToUInt32(new byte[] { 0, 255, 0, 0 }, 0); var n = new ShowPix(_All.pixDisplayHitMissSel(Pix, this, 1, CRed, CBlu)); }
private void browseBtn_Click(object sender, EventArgs e) { openSppFileDialog.ShowDialog(this); pathTextBox.Text = openSppFileDialog.FileName; scanPictureBox.ImageLocation = openSppFileDialog.FileName; using (var engine = new TesseractEngine("./tessdata", "ind", EngineMode.Default)) { Image original_img = new Bitmap(openSppFileDialog.FileName); Point crop_point = new Point(300, 0); Size crop_size = new Size(original_img.Width - 300, original_img.Height); Rectangle crop_area = new Rectangle(crop_point, crop_size); Image cropped_img = cropImage(original_img, crop_area); cropped_img.Save("./cropped_img.png", System.Drawing.Imaging.ImageFormat.Png); using (var img = Pix.LoadFromFile("./cropped_img.png")) { using (Page page = engine.Process(img)) { string spp_str = page.GetText(); int parse_state = 0; // 0,2,4,6,8,10,12,14,16=none, 1=nama, 3=alamat, 5=nmrpolisi, 7=nmrshipment, 9=tujuan, // 11=pemesanan, 13=denstemp, 15=pembeli, 17=produk var i = 0; SppForm spp = new SppForm(); Console.WriteLine(spp_str); for (i = 0; i < spp_str.Length; i++) { if (spp_str[i] == ':') { parse_state++; } else { if (parse_state == 1) { // name spp.Name = spp.Name + spp_str[i]; } else if (parse_state == 2) { spp.Address = spp.Address + spp_str[i]; } else if (parse_state == 3) {// nmrpolisi spp.Police = spp.Police + spp_str[i]; } else if (parse_state == 4) {// nmrshipment spp.Shipment = spp.Shipment + spp_str[i]; } else if (parse_state == 6) {// pemesanan spp.Volume = spp.Volume + spp_str[i]; } else if (parse_state == 7) {// denstemp spp.Quality = spp.Quality + spp_str[i]; } else if (parse_state == 8) {// pembeli spp.Buyer = spp.Buyer + spp_str[i]; } else if (parse_state == 9) {// produk spp.Product = spp.Product + spp_str[i]; } } } setSppTextBox(false); sppNameTextbox.Text = spp.Name; sppAddressTextbox.Text = spp.Address; sppPoliceTextbox.Text = spp.Police; sppShipmentTextbox.Text = spp.Shipment; sppVolumeTextbox.Text = spp.Volume; sppQualityTextbox.Text = spp.Quality; sppBuyerTextbox.Text = spp.Buyer; sppProductTextbox.Text = spp.Product; } } } }
private static StByte SecretionByteFromPixel(Pix p) { StByte result = new StByte(); result.a1 = (byte)(p.blue - ((byte)(p.blue / 10)) * 10); result.a2 = (byte)(p.green - ((byte)(p.green / 10)) * 10); result.a3 = (byte)(p.red - ((byte)(p.red / 10)) * 10); result.a4 = (byte)(p.alpha - ((byte)(p.alpha / 10)) * 10); return result; }
public static void RecognizeBill(object obj) { object[] objs = (object[])obj; MainForm mform = (MainForm)objs[0]; ori_img = (Bitmap)Bitmap.FromFile((string)objs[1]); RecognizeForm rform = new RecognizeForm(); ShowRecognizeForm(mform, rform); mform.UpdateText("Clear Edges"); EdgeFilter(); rform.UpdateImage(show_img); mform.UpdateLog("Clear Edges"); mform.UpdateText("Execute Sobel Filter"); SobelFilter(); rform.UpdateImage(show_img); mform.UpdateLog("Execute Sobel Filter"); mform.UpdateText("Find target Rectangles"); FindRectangle(10, 20); rform.UpdateImage(show_img); mform.UpdateLog("Find target Rectangles"); mform.UpdateText("Cut image"); CutImage(); mform.UpdateLog("Cut image"); mform.UpdateText("Recognize address"); TesseractEngine ocr = new TesseractEngine(@"C:\Users\Allen Chou\Documents\Visual Studio 2013\Projects\Finder\packages\Tesseract.3.0.2.0\tessdata", "chi_tra+eng", EngineMode.Default); Pix img = PixConverter.ToPix(targets[1]); Page addpage = ocr.Process(img); string address = addpage.GetText().Trim().Replace(" ", String.Empty); ocr.Dispose(); mform.UpdateLog("Recognize address"); mform.UpdateText("Recognize eid, date, price"); Pix idpimg = PixConverter.ToPix(targets[2]); TesseractEngine ocre = new TesseractEngine(@"C:\Users\Allen Chou\Documents\Visual Studio 2013\Projects\Finder\packages\Tesseract.3.0.2.0\tessdata", "eng", EngineMode.Default); Page idppage = ocre.Process(idpimg); string[] idpdata = idppage.GetText().Trim().Split(' '); int tar = 0; string eid = ""; for (int i = 0; i < idpdata.Length; i++) { Regex rex = new Regex("\\d{2}-\\d{2}-\\d{4}-\\d{2}-\\d{1}"); if (rex.IsMatch(idpdata[i])) { tar = i; Match match = rex.Match(idpdata[i]); eid = match.Value; break; } } ocre.Dispose(); string date = idpdata[tar + 1]; string price = idpdata[tar + 2].Replace("*", String.Empty); mform.UpdateLog("Recognize eid, date, price\n"); mform.UpdateText("Recognize kWh"); Pix kwhimg = PixConverter.ToPix(targets[0]); ocre = new TesseractEngine(@"C:\Users\Allen Chou\Documents\Visual Studio 2013\Projects\Finder\packages\Tesseract.3.0.2.0\tessdata", "eng", EngineMode.Default); Page kwhpage = ocre.Process(kwhimg); string[] kwhdata = kwhpage.GetText().Trim().Split(' '); string kwh = ""; for (int i = 0; i < kwhdata.Length; i++) { Regex rex = new Regex("\\*\\d{1,}"); if (rex.IsMatch(kwhdata[i])) { Match match = rex.Match(kwhdata[i]); kwh = match.Value.Replace("*", String.Empty); break; } } ocre.Dispose(); mform.UpdateLog("Recognize kWh"); mform.UpdateLog("Result [ " + eid + " , " + date + " , " + price + " , " + kwh + " , " + address + " ]"); mform.UpdateText("Finished"); mform.UpdateLog("Finished"); }
public Window1() { InitializeComponent(); Mat src = new Mat(@"./desktop.jpg"); Cv2.ImShow("src", src); //src = src.Resize(new Size(src.Width / 2, src.Height / 2)); for (var y = 0; y < src.Height; y++) { for (var x = 0; x < src.Width; x++) { var color = src.Get <Vec3b>(y, x); //if (color.Item2 < 175) if (color.Item2 < 225) { color.Item0 = 255; color.Item1 = 0; color.Item2 = 0; } src.Set(y, x, color); } } Cv2.ImShow("fade", src); Mat gray = new Mat(); Mat binary = new Mat(); Cv2.CvtColor(src, gray, ColorConversionCodes.RGB2GRAY); gray = gray.GaussianBlur(new Size(5, 5), 0); gray = gray.Blur(new Size(5, 5)); gray = gray.BoxFilter(-1, new Size(10, 10), normalize: true); Cv2.ImShow("gray", gray); Cv2.Threshold(gray, binary, 100, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary); var element = Cv2.GetStructuringElement(MorphShapes.Rect, new Size(3, 3)); binary = binary.Erode(element); binary = binary.MorphologyEx(MorphTypes.Close, element); Cv2.ImShow("bin", binary); //var line = binary.Canny(100, 200); //Cv2.ImShow("line", line); //Cv2.WaitKey(); //建立轮廓接受数组 Point[][] contours; HierarchyIndex[] hierarchy; Cv2.FindContours(binary, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxTC89KCOS); //最小外接矩形接收数组 MessageBox.Show(contours.Length.ToString()); RotatedRect[] rotateRect = new RotatedRect[contours.Length]; Point[][] contours_poly = new Point[contours.Length][]; for (int i = 0; i < contours.Length; i++) { contours_poly[i] = Cv2.ApproxPolyDP(contours[i], 30, true); //返回凸包,单线长大于30过滤 rotateRect[i] = Cv2.MinAreaRect(contours_poly[i]); //最小外接矩形集合 Point2f[] pot = new Point2f[4]; //新建点集合接收点集合 //for (int i = 0; i < rotateRect.Length; i++) //{ var angle = rotateRect[i].Angle; //矩形角度 pot = rotateRect[i].Points(); //矩形的4个角 var line1 = Math.Sqrt((pot[0].X - pot[1].X) * (pot[0].X - pot[1].X) + (pot[0].Y - pot[1].Y) * (pot[0].Y - pot[1].Y)); var line2 = Math.Sqrt((pot[0].X - pot[3].X) * (pot[0].X - pot[3].X) + (pot[0].Y - pot[3].Y) * (pot[0].Y - pot[3].Y)); if (line1 * line2 < 1000)//过滤,太小的矩形直接pass { continue; } if (line1 > line2)//依据实际情况进行判断 { angle += 90; } Mat Roi = new Mat(src.Size(), MatType.CV_8UC3); Roi.SetTo(0);//全黑 //Cv2.DrawContours(binary, contours, -1, Scalar.White, -1);//在二值图像中圈出轮廓区域并染白 Cv2.DrawContours(binary, contours, -1, Scalar.White, 1); Cv2.ImShow("bin", binary); src.CopyTo(Roi, binary);//将原图通过mask抠图到Roi Cv2.ImShow("Roi", Roi); Mat afterRotato = new Mat(src.Size(), MatType.CV_8UC3); afterRotato.SetTo(0); Point2f center = rotateRect[i].Center; Mat M = Cv2.GetRotationMatrix2D(center, angle, 1); //计算变换矩阵 Cv2.WarpAffine(Roi, afterRotato, M, Roi.Size(), InterpolationFlags.Linear, BorderTypes.Wrap); //得到变换后的图像,滤除其他信息 Cv2.ImShow("旋转后", afterRotato); Mat bin2 = new Mat(); Cv2.ImShow("after", afterRotato); Cv2.CvtColor(afterRotato, bin2, ColorConversionCodes.RGB2GRAY); Cv2.Threshold(bin2, bin2, 50, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu); Point[][] con; HierarchyIndex[] temp;//接收矫正后的轮廓信息 Cv2.FindContours(bin2, out con, out temp, RetrievalModes.External, ContourApproximationModes.ApproxSimple); for (int j = 0; j < con.Length; j++) { Rect rect = Cv2.BoundingRect(con[j]); //直接使用矫正矩形,因为矫正后不需要再旋转 if (rect.Height * rect.Width < 8000) //过滤干扰信息 { continue; } Mat dstImg = new Mat(afterRotato, rect); dstImg = dstImg.CvtColor(ColorConversionCodes.RGB2GRAY); dstImg = dstImg.Threshold(50, 255, ThresholdTypes.Otsu | ThresholdTypes.Binary); Cv2.BitwiseNot(dstImg, dstImg, new Mat()); dstImg = new Mat(dstImg, new Rect(100, 100, dstImg.Width - 200, dstImg.Height - 200)); Cv2.ImShow("dst", dstImg); dstImg.SaveImage("dst1.jpg"); var engine = new TesseractEngine("./tessdata", "eng", EngineMode.Default); var resProcess = engine.Process(Pix.LoadTiffFromMemory(dstImg.ToBytes(".tiff"))); MessageBox.Show(resProcess.GetText()); ////string name = "dst" + i;//主要看调试的时候有几个结果 //dstImg = dstImg.CvtColor(ColorConversionCodes.RGB2GRAY); //dstImg = dstImg.Threshold(10, 255, ThresholdTypes.Otsu); //Cv2.ImShow("chan", dstImg.Canny(100, 200)); //dstImg.FindContours(out var con1, out var hie1, RetrievalModes.External, // ContourApproximationModes.ApproxNone); //dstImg.DrawContours(con1, -1, Scalar.Green, 3); //Cv2.ImShow("dst2", dstImg); } } Cv2.WaitKey(); Console.ReadLine(); }
public void MemoryLeakDetector(Pix pix, Func<Pix, Pix> op, int iterations = DefaultIterations) { for (int i = 0; i < iterations; i++) { using (Pix result = op(pix)) { //using (Pix binaryPix = grayPix.BinarizeOtsuAdaptiveThreshold(50, 50, 5, 5, 0.1f)) { System.Console.WriteLine("Memory: {0} MB", System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / (1024 * 1024)); } GC.Collect(); } }
private unsafe PixColor GetPixel(Pix pix, int x, int y) { var pixData = pix.GetData(); if (pix.Colormap != null) { var pixLine = (uint*)pixData.Data + pixData.WordsPerLine * y; var pixValue = (int)PixData.GetDataByte(pixLine, x); return pix.Colormap[pixValue]; } else { var pixLine = (uint*)pixData.Data + pixData.WordsPerLine * y; return PixColor.FromRgba(pixLine[x]); } }
private void AssertAreEquivalent(Bitmap bmp, Pix pix, bool checkAlpha) { // verify img metadata Assert.That(pix.Width, Is.EqualTo(bmp.Width)); Assert.That(pix.Height, Is.EqualTo(bmp.Height)); //Assert.That(pix.Resolution.X, Is.EqualTo(bmp.HorizontalResolution)); //Assert.That(pix.Resolution.Y, Is.EqualTo(bmp.VerticalResolution)); // do some random sampling over image var height = pix.Height; var width = pix.Width; for (int y = 0; y < height; y += height / 4) { for (int x = 0; x < width; x += width / 4) { PixColor sourcePixel = (PixColor)bmp.GetPixel(x, y); PixColor destPixel = GetPixel(pix, x, y); if (checkAlpha) { Assert.That(destPixel, Is.EqualTo(sourcePixel), "Expected pixel at <{0},{1}> to be same in both source and dest.", x, y); } else { Assert.That(destPixel, Is.EqualTo(sourcePixel).Using<PixColor>((c1, c2) => (c1.Red == c2.Red && c1.Blue == c2.Blue && c1.Green == c2.Green) ? 0 : 1), "Expected pixel at <{0},{1}> to be same in both source and dest.", x, y); } } } }
private void SaveResult(Pix result, string filename) { if (!Directory.Exists(ResultsDirectory)) Directory.CreateDirectory(ResultsDirectory); result.Save(Path.Combine(ResultsDirectory, filename)); }
private unsafe PixColor GetPixel(Pix pix, int x, int y) { var pixDepth = pix.Depth; var pixData = pix.GetData(); var pixLine = (uint*)pixData.Data + pixData.WordsPerLine * y; uint pixValue; if (pixDepth == 1) { pixValue = PixData.GetDataBit(pixLine, x); } else if (pixDepth == 4) { pixValue = PixData.GetDataQBit(pixLine, x); } else if (pixDepth == 8) { pixValue = PixData.GetDataByte(pixLine, x); } else if (pixDepth == 32) { pixValue = PixData.GetDataFourByte(pixLine, x); } else { throw new ArgumentException(String.Format("Bit depth of {0} is not supported.", pix.Depth), "pix"); } if (pix.Colormap != null) { return pix.Colormap[(int)pixValue]; } else { if (pixDepth == 32) { return PixColor.FromRgba(pixValue); } else { byte grayscale = (byte)(pixValue * 255 / ((1 << 16) - 1)); return new PixColor(grayscale, grayscale, grayscale); } } }
private static Pix MixByteAndPixel(Pix p, StByte b) { p.blue = (byte)(((byte)(p.blue / 10)) * 10); if (p.blue == 250) p.blue = 240; p.blue += b.a1; p.green = (byte)(((byte)(p.green / 10)) * 10); if (p.green == 250) p.green = 240; p.green += b.a2; p.red = (byte)(((byte)(p.red / 10)) * 10); if (p.red == 250) p.red = 240; p.red += b.a3; p.alpha = (byte)(((byte)(p.alpha / 10)) * 10); if (p.alpha == 250) p.alpha = 240; p.alpha += b.a4; return p; }
internal static byte[] Extraction(byte[] img) { byte[] length = new byte[4]; byte[] data = null; using (var bmp = (Bitmap)Image.FromStream(new MemoryStream(img))) { int index = 0; bool end = false; unsafe { BitmapData bmd = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat); int size = bmd.Stride / bmp.Width; bool hasAlpha = size == 4; for (int y = 0; y < bmp.Height; y++) { for (int x = 0; x < bmp.Width; x++) { int offset = (y * bmd.Stride) + (x * size); Pix pix = new Pix { blue = ((byte*)bmd.Scan0)[offset], green = ((byte*)bmd.Scan0)[offset + 1], red = ((byte*)bmd.Scan0)[offset + 2], alpha = ((hasAlpha) ? ((byte*)bmd.Scan0)[offset + 3] : (byte)255) }; StByte b = SecretionByteFromPixel(pix); byte wb = ByteComposition(b, hasAlpha); if (data == null) { length[index] = wb; index++; if (index == 4) { data = new byte[BitConverter.ToInt32(length, 0)]; index = 0; } } else { data[index] = wb; index++; if (index >= data.Length) { end = true; break; } } } if (end) break; } bmp.UnlockBits(bmd); } } return data; }