Ejemplo n.º 1
0
        public async Task Scan(int split, PageNumber guess)
        {
            var _open = false;

            if (bitmap == null)
            {
                await Open();

                _open = true;
            }
            var list = new List <PageNumber>();

            int height = bitmap.PixelHeight / split;
            int top    = 0;

            if ((guess.Pos & Position.Bottom) != 0)
            {
                top = bitmap.PixelHeight - height;
            }
            var side = (guess.Pos & (Position.Top | Position.Bottom));
            var min  = 999999;

            var ocr = new OCR();

            using (var newbmp = Bitmap.Crop(bitmap, 0, top, bitmap.PixelWidth, height))
            {
                var r = await ocr.Run(newbmp);

                min = ocrResultCheck(r, min, side, guess);
                if (min != 0 && min != 999999)
                {
                    using (var b = Bitmap.Binarize(newbmp))
                    {
                        r = await ocr.Run(b);

                        min = ocrResultCheck(r, min, side, guess);
                    }
                }
            }
            if (_open)
            {
                Close();
            }
            switch (min)
            {
            case 0:
                Score = 0;
                break;

            case 999999:
                Score = 1;
                break;

            default:
                Score = 2;
                break;
            }

            return;
        }
Ejemplo n.º 2
0
        static void scanFolder(Options op)
        {
#if DEBUG
            var sw = new System.Diagnostics.Stopwatch();
            sw.Start();
#endif
            if (!OCR.Lang("en-US"))
            {
                Console.WriteLine("OCRにen-USを指定できません。\nWin10設定からen-USをインストールしてください。");
                Environment.Exit(1);
            }
            if (op.guess < 1)
            {
                op.guess = 10;
            }

            if (System.IO.Directory.Exists(op.Input))
            {
                var pages = new Pages(op.Input);
                pages.Split = op.split;
                var guess = pages.Guess(op.guess, op.thread);
                while (guess.Pos == Position.None && --op.split > 3)
                {
                    pages.Split = op.split;
                    guess       = pages.Guess(op.guess, op.thread);
                }
                if (guess.Pos == Position.None)
                {
                    Console.WriteLine("ページ番号を検出できませんでした。");
                    Environment.Exit(1);
                }
                Console.Write("1枚目画像はページ番号「{0}」で", guess.No);
                if ((guess.Pos & Position.Top) == Position.Top)
                {
                    Console.Write("上部・");
                }
                if ((guess.Pos & Position.Bottom) == Position.Bottom)
                {
                    Console.Write("下部・");
                }
                if ((guess.Pos & Position.Left) == Position.Left)
                {
                    Console.Write("左側");
                }
                if ((guess.Pos & Position.Right) == Position.Right)
                {
                    Console.Write("右側");
                }
                if ((guess.Pos & Position.Center) == Position.Center)
                {
                    Console.Write("中央");
                }
                Console.WriteLine("に印字されていると想定。");

                pages.Check(guess, op.thread);
            }
            else
            {
                Console.WriteLine("フォルダを指定してください");
                Environment.Exit(1);
            }
#if DEBUG
            sw.Stop();
            Console.WriteLine("{0}ミリ秒", sw.ElapsedMilliseconds);
            Console.WriteLine("何かキーを押してください");
            Console.ReadKey();
#endif
        }
Ejemplo n.º 3
0
        public async Task <List <PageNumber> > Guess(int split)
        {
            var _open = false;

            if (bitmap == null)
            {
                await Open();

                if (bitmap == null)
                {
                    Console.WriteLine("画像を開けませんでした。");
                    Environment.Exit(1);
                }
                _open = true;
            }
            var list = new List <PageNumber>();

            int height = bitmap.PixelHeight / split;

            using (var newbmp = Bitmap.Crop(bitmap, 0, 0, bitmap.PixelWidth, height))
            {
                //await Bitmap.Save(newbmp,"test.png");
                var ocr = new OCR();
                var r   = await ocr.Run(newbmp);

                int no    = 0;
                int left  = bitmap.PixelWidth / 3;
                int right = left * 2;
                foreach (var line in r.Lines)
                {
                    foreach (var word in line.Words)
                    {
                        if (int.TryParse(word.Text, out no))
                        {
                            Position pos = Position.Top;
                            if (word.BoundingRect.Left < left)
                            {
                                pos |= Position.Left;
                            }
                            else if (word.BoundingRect.Left > right)
                            {
                                pos |= Position.Right;
                            }
                            else
                            {
                                pos |= Position.Center;
                            }
                            list.Add(new PageNumber(no, pos));
                        }
                    }
                }
            }

            using (var newbmp = Bitmap.Crop(bitmap, 0, bitmap.PixelHeight - height, bitmap.PixelWidth, height))
            {
                if (newbmp == null)
                {
                    Console.WriteLine("null");
                }
                else
                {
                    var ocr = new OCR();
                    var r   = await ocr.Run(newbmp);

                    int no    = 0;
                    int left  = bitmap.PixelWidth / 3;
                    int right = left * 2;
                    foreach (var line in r.Lines)
                    {
                        foreach (var word in line.Words)
                        {
                            if (int.TryParse(word.Text, out no))
                            {
                                Position pos = Position.Bottom;
                                if (word.BoundingRect.Left < left)
                                {
                                    pos |= Position.Left;
                                }
                                else if (word.BoundingRect.Left > right)
                                {
                                    pos |= Position.Right;
                                }
                                else
                                {
                                    pos |= Position.Center;
                                }
                                list.Add(new PageNumber(no, pos));
                            }
                        }
                    }
                }
            }

            if (_open)
            {
                Close();
            }
            return(list);
        }
Ejemplo n.º 4
0
        int ocrResultCheck(OcrResult r, int min, Position side, PageNumber guess)
        {
            int no    = 0;
            int left  = bitmap.PixelWidth / 3;
            int right = left * 2;

            foreach (var line in r.Lines)
            {
                foreach (var word in line.Words)
                {
                    if (int.TryParse(word.Text, out no))
                    {
                        Position pos = side;
                        if (word.BoundingRect.X < left)
                        {
                            pos |= Position.Left;
                        }
                        else if (word.BoundingRect.X > right)
                        {
                            pos |= Position.Right;
                        }
                        else
                        {
                            pos |= Position.Center;
                        }
                        var i = Math.Abs(guess.No - no);
                        if (min > i)
                        {
                            min        = i;
                            PageNo.No  = no;
                            PageNo.Pos = pos;
                            if (min == 0)
                            {
                                break;
                            }
                        }
                    }
                }
                if (min == 0)
                {
                    break;
                }
            }
            var yoko = OCR.GetYoko(r);

            foreach (var line in yoko)
            {
                if (int.TryParse(line.Text, out no))
                {
                    Position pos = side;
                    if (line.Rect.X < left)
                    {
                        pos |= Position.Left;
                    }
                    else if (line.Rect.X > right)
                    {
                        pos |= Position.Right;
                    }
                    else
                    {
                        pos |= Position.Center;
                    }
                    var i = Math.Abs(guess.No - no);
                    if (min > i)
                    {
                        min        = i;
                        PageNo.No  = no;
                        PageNo.Pos = pos;
                        if (min == 0)
                        {
                            break;
                        }
                    }
                }
            }
            return(min);
        }