Пример #1
0
        public IExportedImage Export(IScanImage scanImage, ExporterOptions options)
        {
            var export = new StringBuilder();

            using (var writer = new StringWriter(export))
            {
                // Group by RowIndex and select the group Left, Right, Top, Bottom
                foreach (var row in scanImage.Blobs.GroupBy(bp => bp.RowIndex))
                {
                    foreach (var blob in row)
                    {
                        var key = row.Key + 1;
                        // Get the blob image.
                        var newImage = blob.CropBlob(scanImage.Image, options.ExtractedBackColor, options.ExportSize);
                        Write(writer, key, newImage);

                        // Write a rotated version.
                        newImage = blob.CropBlob(scanImage.Image, options.ExtractedBackColor, options.ExportSize, 10);
                        Write(writer, key, newImage);

                        // Write another rotated version (to the other dir).
                        newImage = blob.CropBlob(scanImage.Image, options.ExtractedBackColor, options.ExportSize, -10);
                        Write(writer, key, newImage);
                    }
                }
                writer.Flush();
            }

            return(new ExportedImage(scanImage, export.ToString()));
        }
Пример #2
0
 public ExportedImage(IScanImage scanImage, string exportData)
 {
     ExportData = exportData;
     Image      = scanImage.Image;
     Blobs      = new IBlob[scanImage.Blobs.Length];
     scanImage.Blobs.CopyTo(Blobs, 0);
 }
Пример #3
0
        public IScanImage Scan(IScanner scanner, Action <IScannerConfiguration> configuration)
        {
            ScannerConfiguration scannerConfiguration;

            configuration(scannerConfiguration = new ScannerConfiguration(scanner));

            _scanImage = scannerConfiguration.Scan(scannerConfiguration.Source, scannerConfiguration.Options);
            return(_scanImage);
        }
Пример #4
0
        public IPredictImage PredictModel(IScanImage scanImage, IAlphabet alphabet, PredictOptions options)
        {
            _options = options;
            var blobs = new List <IBlob>();

            // Get the model params.
            var thetas = GetModelParamsFromFile();

            // Loop thru all blobs and predict.
            foreach (var blob in scanImage.Blobs)
            {
                // Get the blob pixels.
                Vector xs = blob.GetPixels(scanImage.Image, options.ExtractedBackColor, options.ExportSize);

                // Get the model value (this is what to be used in the Sigmoid function).
                var v = (thetas * xs);

                // This is for finding the maximum value of all letters predictions (1-vs-all), so
                // we know what letter to choose.
                var   max      = new double[3];
                int[] maxIndex = { -1, -1, -1 };

                // Loop thru the values
                for (int i = 0; i < v.Count; i++)
                {
                    // Get the final model prediction (Sigmoid).
                    v[i] = SpecialFunctions.Logistic(v[i].Real);

                    // Check if this prediction is in the "top 3".
                    for (int j = 0; j < max.Length; j++)
                    {
                        if (v[i].Real > max[j])
                        {
                            max[j]      = v[i].Real;
                            maxIndex[j] = i;

                            // We want to kepp max array sorted, so once we found a value
                            // it is bigger than we stop.
                            break;
                        }
                    }
                }

                var b = (IBlob)blob.Clone();
                b.Title = alphabet.Data[maxIndex[0]];

                blobs.Add(b);
            }

            var collection = blobs.OrderBy(b => b.RowIndex)
                             .ThenBy(b => b.Left)
                             .GroupBy(g => g.RowIndex)
                             .ToDictionary(g => g.Key, s => s.Select(g => g).ToList());

            return(new PredictImage(scanImage.Image, collection));
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="scanImage">Scan Image Service</param>
 /// <param name="logger">Logger</param>
 public BlobFunction(IScanImage scanImage, ILogger <BlobFunction> logger)
 {
     _scanImageService = scanImage ?? throw new ArgumentNullException(nameof(scanImage));
     _logger           = logger ?? throw new ArgumentNullException(nameof(logger));
 }