Пример #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();
        }
Пример #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 + "\"}");
        }