Beispiel #1
0
        private void teachButton_Click(object sender, EventArgs e)
        {
            _terminate    = false;
            _error        = 1.0;
            _progress     = 0;
            _terminated   = false;
            _controlError = -1;
            _updateProgress();

            new Thread(() =>
            {
                var samples      = CaptchasLibrary.GetRecognizedCaptchas();
                var digitImages  = new List <Image>();
                var digitResults = new List <int>();

                int controlStart = (int)(samples.Count * 0.8);
                for (var i = 0; i < controlStart; i++)
                {
                    var recognizer = new CaptchaRecognizer(samples[i].Image);
                    var rectangles = recognizer.SplitDigits();

                    for (var j = 0; j < rectangles.Length; j++)
                    {
                        digitImages.Add(ImageHelper.Crop(recognizer.Target, rectangles[j]));
                        digitResults.Add(int.Parse(samples[i].Answer.Substring(j, 1)));
                    }
                }
                var teacher = new NeuralNetworkTrainer(digitImages.ToArray(), digitResults.ToArray());

                teacher.Run((net, data, epochs, reports, er, u, userData) =>
                {
                    _error    = net.MSE;
                    _progress = 1.0 * u / epochs;
                    if (_terminate)
                    {
                        return(-1);
                    }
                    return(0);
                });
                teacher.Network.Save(CaptchaRecognizer.NetworkPath);

                var controlTotal  = 0;
                var controlPasses = 0;
                for (var i = controlStart; i < samples.Count; i++)
                {
                    var rec = new CaptchaRecognizer(samples[i].Image);
                    var res = rec.Recognize();
                    controlTotal++;
                    if (res.Result == samples[i].Answer)
                    {
                        controlPasses++;
                    }
                }
                _controlError = 1.0 * controlPasses / controlTotal;
                _error        = teacher.Network.MSE;
                _terminate    = true;
                _terminated   = true;
                _progress     = 1;
            }).Start();
        }
Beispiel #2
0
        private string _process(HttpListenerContext context)
        {
            var rand     = context.Request.QueryString["rand"];
            var url      = context.Request.QueryString["url"];
            var errorStr = "{\"status\": \"error\"}";

            if (url == null)
            {
                return(errorStr);
            }
            var captcha = new Captcha
            {
                Name   = rand,
                Source = url,
            };

            try
            {
                captcha.Download();
            }
            catch (Exception)
            {
                return(errorStr);
            }

            var tries   = 5;
            var results = new List <CaptchaRecognizer.RecognizeResult>();

            for (var tryNo = 0; tryNo < tries; tryNo++)
            {
                var rec = new CaptchaRecognizer(captcha.Image);
                try
                {
                    var res = rec.Recognize();
                    results.Add(res);
                    if (res.Probability > 0.9)
                    {
                        break;
                    }
                }
                catch (CaptchaRecognizer.CantSplitDigitsException)
                {
                    results.Add(new CaptchaRecognizer.RecognizeResult
                    {
                        Probabilities = new double[] { 0, 0, 0, 0 },
                        Result        = "1111"// just random string
                    });
                }
            }
            var result = results.OrderByDescending(x => x.Probability).First();

            return("{\"status\": \"OK\", \"result\": \"" + result.Result + "\", \"probability\": \"" + result.Probability + "\"}");
        }
Beispiel #3
0
 public GeneticIndividual(CaptchaRecognizer recognizer, bool init = false)
 {
     _recognizer = recognizer;
     if (init)
     {
         Items = Enumerable.Range(0, _recognizer.DigitsCount)
                 .Select(GenerateRandomItem)
                 .ToArray();
     }
     else
     {
         Items = new GeneticIndividualItem[_recognizer.DigitsCount];
     }
 }
Beispiel #4
0
        private void _next()
        {
            try
            {
                _currentCaptcha = GKRegistration.GetNextCaptcha();
            }
            catch (Exception e)
            {
                Thread.Sleep(100);
                MessageBox.Show(e.Message, "Error");
                _next();
                return;
            }
            imagePanel.BackgroundImage       = _currentCaptcha.Image;
            imagePanel.BackgroundImageLayout = ImageLayout.Stretch;
            answerTextBox.Text = "";
            randLabel.Text     = _currentCaptcha.Name;

            var img = _currentCaptcha.Image;

            img = ImageHelper.Crop(img, new Rectangle(1, 1, img.Width - 2, img.Height - 2));
            img = ImageHelper.Binarize(img);
            img = ImageHelper.RemoveLines(img);

            var recognizer = new CaptchaRecognizer(_currentCaptcha.Image);
            var rects      = recognizer.SplitDigits();

            var drawArea = new Bitmap(img);

            hintImagePanel.BackgroundImage       = drawArea;
            hintImagePanel.BackgroundImageLayout = ImageLayout.Stretch;

            Graphics g   = Graphics.FromImage(drawArea);
            var      pen = new Pen(Brushes.Blue);

            foreach (var rect in rects)
            {
                g.DrawRectangle(pen, rect);
            }
        }
Beispiel #5
0
            public GeneticPopolation(CaptchaRecognizer recognizer, int maxSize)
            {
                _recognizer = recognizer;
                MaxSize     = maxSize;
                var consecutiveInvalidCount = 0;

                while (Individuals.Count < MaxSize)
                {
                    var nxt = GeneticIndividual.Mutation(new GeneticIndividual(_recognizer, true));
                    if (!nxt.IsInvalid())
                    {
                        consecutiveInvalidCount = 0;
                        Individuals.Add(nxt);
                    }
                    else
                    {
                        consecutiveInvalidCount++;
                        if (consecutiveInvalidCount > MaxConsecutiveFailures)
                        {
                            throw new CantSplitDigitsException();
                        }
                    }
                }
            }
Beispiel #6
0
 public GeneticIndividualItem(CaptchaRecognizer recognizer, int x, int y)
 {
     _recognizer = recognizer;
     X           = x;
     Y           = y;
 }
Beispiel #7
0
        public Form1()
        {
            InitializeComponent();
            timer1.Start();
            var samples = CaptchasLibrary.GetRecognizedCaptchas();

            for (var i = 0; i < Math.Min(20, samples.Count); i++)
            {
                Controls.Add(new Panel
                {
                    Location              = new Point(0, (CaptchaHeight + 3) * i),
                    Name                  = "imagePanel",
                    Size                  = new Size(CaptchaWidth, CaptchaHeight),
                    TabIndex              = 0,
                    BackgroundImage       = samples[i].Image,
                    BackgroundImageLayout = ImageLayout.Stretch
                });

                var img = samples[i].Image;
                img = ImageHelper.Crop(img, new Rectangle(1, 1, img.Width - 2, img.Height - 2));
                //img = ImageHelper.Binarize(img);
                //img = ImageHelper.Blur(img);
                img = ImageHelper.EdgeDetector(img);

                Controls.Add(new Panel
                {
                    Location              = new Point(CaptchaWidth + 3, (CaptchaHeight + 3) * i),
                    Name                  = "imagePanel",
                    Size                  = new Size(CaptchaWidth, CaptchaHeight),
                    TabIndex              = 0,
                    BackgroundImage       = img,
                    BackgroundImageLayout = ImageLayout.Stretch
                });

                img = ImageHelper.RemoveLines2(img);
                Controls.Add(new Panel
                {
                    Location              = new Point((CaptchaWidth + 3) * 2, (CaptchaHeight + 3) * i),
                    Name                  = "imagePanel",
                    Size                  = new Size(CaptchaWidth, CaptchaHeight),
                    TabIndex              = 0,
                    BackgroundImage       = img,
                    BackgroundImageLayout = ImageLayout.Stretch
                });


                var recognizer = new CaptchaRecognizer(samples[i].Image);
                var rects      = recognizer.SplitDigits();

                var dividedPanel = new Panel
                {
                    Location = new Point((CaptchaWidth + 3) * 3, (CaptchaHeight + 3) * i),
                    Name     = "imagePanel",
                    Size     = new Size(recognizer.Target.Width, recognizer.Target.Height),
                    TabIndex = 0,
                    BackgroundImageLayout = ImageLayout.Stretch
                };

                var drawArea = new Bitmap(recognizer.Target);
                dividedPanel.BackgroundImage = drawArea;

                Graphics g   = Graphics.FromImage(drawArea);
                var      pen = new Pen(Brushes.Blue);

                foreach (var rect in rects)
                {
                    g.DrawRectangle(pen, rect);
                }
                Controls.Add(dividedPanel);
            }
        }