Esempio n. 1
0
        public static SpecificResult_Text DetectText(MyPic mPic, bool debug = false, int minSize = 0)
        {
            SpecificResult_Text r  = new SpecificResult_Text();
            StringBuilder       sb = new StringBuilder();

            try
            {
                DebLog(debug, "New");
                MyPic mClone = (MyPic)mPic.getClone();
                mClone.Threshold_AlmostBlack();
                bool first = true;
                mClone.ToFileIfConfigured("TextRecognitionLibrary.DetectText.SaveInput");

                ObjectDetection od = new ObjectDetection(mClone, true, true);
                int             id = 0;
                od.DoDetection(new dOnDetectObject(delegate(ObjectPoints points, IPicAccess originalPic)
                {
                    id++;
                    string cnfDumpAll = ConfigurationManager.AppSettings["TextRecognitionLibrary.DetectText.DumpAll"];
                    if ((cnfDumpAll != null) && (Boolean.Parse(cnfDumpAll)))
                    {
                        MyPic picDumpAll = (MyPic)ObjectDetection.ObjectToPic_RegionOfInterest(points, originalPic);
                        picDumpAll.ToFile(id.ToString());
                    }

                    DebLog(debug, $"id{id} {points.Rect.Height}x{points.Rect.Width} soFar{sb}");

                    if (points.Rect.Height == 10)
                    {
                        MyPic pic10 = (MyPic)ObjectDetection.ObjectToPic_RegionOfInterest(points, originalPic);
                        char c      = TextCourierNew10.RecogniseChar(pic10);
                        sb.Append(c);
                        if (first)
                        {
                            first     = false;
                            r.TextPos = points.Points[0];
                        }
                    }
                    else if (points.Rect.Height == 11)
                    {
                        MyPic pic11 = (MyPic)ObjectDetection.ObjectToPic_RegionOfInterest(points, originalPic);
                        char c      = TextCourierNew11.RecogniseChar(pic11);
                        sb.Append(c);
                        if (first)
                        {
                            first     = false;
                            r.TextPos = points.Points[0];
                        }
                    }
                    else if (points.Rect.Height == 12)
                    {
                        ObjectDetection.ObjectToPic_RegionOfInterest(points, originalPic).ToFile("a12");
                        if ((points.Rect.Width == 4) || (points.Rect.Width == 5) || (points.Rect.Width == 7))
                        {
                            IPicAccess pchar = ObjectDetection.ObjectToPic_RegionOfInterest(points, originalPic);
                            IPicAccess pbu   = pchar.getClone();
                            char c           = TextRecognition.RecogniseChar(pchar);
                            sb.Append(c);
                            if (first)
                            {
                                first     = false;
                                r.TextPos = points.Points[0];
                            }
                        }
                        else if (points.Rect.Width == 8)
                        {
                            IPicAccess pchar =
                                ObjectDetection.ObjectToPic_RegionOfInterest(points, originalPic)
                                .getSubPic(1, 0, 7, 12);
                            IPicAccess pbu = pchar.getClone();
                            char c         = TextRecognition.RecogniseChar(pchar);
                            sb.Append(c);
                            if (first)
                            {
                                first     = false;
                                r.TextPos = points.Points[0];
                            }
                        }
                        else
                        {
                            // could this be 2 or more chars together?
                            ObjectDetection.ObjectToPic_RegionOfInterest(points, originalPic).ToFile("subAll");
                            int x = 0;
                            while (x < points.Rect.Width)
                            {
                                IPicAccess sub = mPic.getSubPic(points.Rect.X + x, points.Rect.Y, 7, 12);
                                sub.ToFile("subtogether");
                                char c = TextRecognition.RecogniseChar(sub.getClone());
                                if (c == '?')
                                {
                                    // not recognised
                                    x = points.Rect.Width;
                                }
                                else
                                {
                                    x += 9;
                                    sb.Append(c);
                                    if (first)
                                    {
                                        first     = false;
                                        r.TextPos = points.Points[0];
                                    }
                                }
                            }
                        }
                    }
                    return(true);
                }), debug, minSize);
                r.Text = sb.ToString();
                return(r);
            }
            catch (Exception ex)
            {
                DebLog(debug, "Exception");
                DebLog(debug, ex.Message);
                if (sb.ToString().Length > 0)
                {
                    r.Text = sb.ToString();
                    return(r);
                }
                var err = new SpecificResult_Text();
                err.Text    = "Exception: " + ex.Message;
                err.TextPos = new Point(0, 0);
                return(err);
            }
        }