Esempio n. 1
0
        public void ProcessTextField(string sourceFilePath, string outputFilePath, TextFieldProcessingSettings settings)
        {
            Console.WriteLine("Uploading..");
            Task task = restClient.ProcessTextField(sourceFilePath, settings);

            waitAndDownload(task, outputFilePath);
        }
Esempio n. 2
0
        private static TextFieldProcessingSettings buildTextFieldSettings(string language, string customOptions)
        {
            TextFieldProcessingSettings settings = new TextFieldProcessingSettings();

            settings.Language      = language;
            settings.CustomOptions = customOptions;
            return(settings);
        }
Esempio n. 3
0
        private static string CallOcrWebService(FileInfo pngFile, int topLeftX, int topLeftY, int bottomRightX, int bottomRightY)
        {
            var restClient = new RestServiceClient
            {
                Proxy         = { Credentials = CredentialCache.DefaultCredentials },
                ApplicationId = ConfigurationManager.AppSettings["ApplicationId"].ToString(),
                Password      = ConfigurationManager.AppSettings["Password"].ToString()
            };

            var textFieldProcessingSettings = new TextFieldProcessingSettings
            {
                CustomOptions = "region=" + topLeftX + "," + topLeftY + "," + bottomRightX + "," + bottomRightY,
                Language      = "english"
            };

            if (pngFile.DirectoryName == null)
            {
                throw new Exception("png file directory name is blank");
            }

            var outputXmlFile = Path.Combine(pngFile.DirectoryName, GetFileNameWithoutExtension(pngFile) + ".xml");

            // call the REST service
            var task = restClient.ProcessTextField(pngFile.ToString(), textFieldProcessingSettings);

            System.Threading.Thread.Sleep(4000);
            task = restClient.GetTaskStatus(task.Id);
            //Console.WriteLine("Task status: {0}", task.Status);
            while (task.IsTaskActive())
            {
                // Note: it's recommended that your application waits
                // at least 2 seconds before making the first getTaskStatus request
                // and also between such requests for the same task.
                // Making requests more often will not improve your application performance.
                // Note: if your application queues several files and waits for them
                // it's recommended that you use listFinishedTasks instead (which is described
                // at http://ocrsdk.com/documentation/apireference/listFinishedTasks/).
                System.Threading.Thread.Sleep(4000);
                task = restClient.GetTaskStatus(task.Id);
                //Console.WriteLine("Task status: {0}", task.Status);
            }
            if (task.Status == TaskStatus.Completed)
            {
                //Console.WriteLine("Processing completed.");
                restClient.DownloadResult(task, outputXmlFile);
                //Console.WriteLine("Download completed.");
            }
            else
            {
                //Console.WriteLine("Error while processing the task");
                return(outputXmlFile);
            }
            return(outputXmlFile);
        }
Esempio n. 4
0
        private void fieldSelected(object sender, RegionSelectedEventArgs e)
        {
            string tempFilePath = System.IO.Path.GetTempFileName();

            e.CroppedImage.Save(tempFilePath, System.Drawing.Imaging.ImageFormat.Tiff);

            string outputDir = getOutputDir();

            UserTask task = new UserTask(tempFilePath);

            task.TaskStatus       = "Uploading";
            task.SourceIsTempFile = true;
            task.IsFieldLevel     = true;

            // TODO: correct output name
            task.OutputFilePath = System.IO.Path.Combine(
                outputDir,
                "field-level" + System.IO.Path.GetRandomFileName() + ".xml");

            task.SourceImage = e.CroppedImage;

            _userTasks.Add(task);
            _fieldLevelTasks.Add(task);



            // Select mode: text, barcode, checkmark
            if (flModeText.IsChecked == true)
            {
                TextFieldProcessingSettings settings = new TextFieldProcessingSettings();
                restClientAsync.ProcessTextFieldAsync(tempFilePath, settings, task);
            }
            else if (flModeBarcode.IsChecked == true)
            {
                BarcodeFieldProcessingSettings settings = new BarcodeFieldProcessingSettings();
                restClientAsync.ProcessBarcodeFieldAsync(tempFilePath, settings, task);
            }
            else
            {
                CheckmarkFieldProcessingSettings settings = new CheckmarkFieldProcessingSettings();
                string userSettings = Properties.Settings.Default.CheckmarkOptions;
                if (!String.IsNullOrEmpty(userSettings))
                {
                    settings.Params = userSettings;
                }

                restClientAsync.ProcessCheckmarkFieldAsync(tempFilePath, settings, task);
            }

            // temp file will be deleted in ProcessingCompleted callback
        }
        /// <summary>
        /// Perform text recognition of a field
        /// Throws an exception if something goes wrong
        /// </summary>
        /// <returns>Id of created task</returns>
        public OcrSdkTask ProcessTextField(string filePath, TextFieldProcessingSettings settings)
        {
            string url = String.Format("{0}/processTextField{1}", ServerUrl, settings.AsUrlParams);

            // Build post request
            WebRequest request = createPostRequest(url);

            writeFileToRequest(filePath, request);

            XDocument  response = performRequest(request);
            OcrSdkTask task     = ServerXml.GetTaskStatus(response);

            return(task);
        }
Esempio n. 6
0
        public void ProcessTextField(string sourceFilePath, string outputFilePath, TextFieldProcessingSettings settings)
        {
            Console.WriteLine("Uploading..");
            Task task = restClient.ProcessTextField(sourceFilePath, settings);

            // For field-level

            /*
             * var flSettings = new TextFieldProcessingSettings();
             * TaskId taskId = restClient.ProcessTextField(sourceFilePath, flSettings);
             */

            TaskId taskId = task.Id;

            while (true)
            {
                task = restClient.GetTaskStatus(taskId);
                if (!Task.IsTaskActive(task.Status))
                {
                    break;
                }

                Console.WriteLine(String.Format("Task status: {0}", task.Status));
                System.Threading.Thread.Sleep(1000);
            }

            if (task.Status == TaskStatus.Completed)
            {
                Console.WriteLine("Processing completed.");
                restClient.DownloadResult(task, outputFilePath);
                Console.WriteLine("Download completed.");
            }
            else
            {
                Console.WriteLine("Error while processing the task");
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Process directory or file with given path
        /// </summary>
        /// <param name="sourcePath">File or directory to be processed</param>
        /// <param name="outputPath">Path to directory to store results
        /// Will be created if it doesn't exist
        /// </param>
        /// <param name="processAsDocument">If true, all images are processed as a single document</param>
        public void ProcessPath(string sourcePath, string outputPath,
                                IProcessingSettings settings,
                                ProcessingModeEnum processingMode)
        {
            List <string> sourceFiles = new List <string>();

            if (Directory.Exists(sourcePath))
            {
                sourceFiles.AddRange(Directory.GetFiles(sourcePath));
                sourceFiles.Sort();
            }
            else if (File.Exists(sourcePath))
            {
                sourceFiles.Add(sourcePath);
            }
            else
            {
                Console.WriteLine("Invalid source path");
                return;
            }

            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }

            if (processingMode == ProcessingModeEnum.SinglePage ||
                (processingMode == ProcessingModeEnum.MultiPage && sourceFiles.Count == 1))
            {
                ProcessingSettings fullTextSettings = settings as ProcessingSettings;
                foreach (string filePath in sourceFiles)
                {
                    string outputFileName = Path.GetFileNameWithoutExtension(filePath);
                    string outputFilePath = Path.Combine(outputPath, outputFileName);

                    Console.WriteLine("Processing " + Path.GetFileName(filePath));

                    ProcessFile(filePath, outputFilePath, fullTextSettings);
                }
            }
            else if (processingMode == ProcessingModeEnum.MultiPage)
            {
                ProcessingSettings fullTextSettings = settings as ProcessingSettings;
                string             outputFileName   = "document";
                string             outputFilePath   = Path.Combine(outputPath, outputFileName);

                ProcessDocument(sourceFiles, outputFilePath, fullTextSettings);
            }
            else if (processingMode == ProcessingModeEnum.ProcessTextField)
            {
                TextFieldProcessingSettings fieldSettings = settings as TextFieldProcessingSettings;
                foreach (string filePath in sourceFiles)
                {
                    string outputFileName = Path.GetFileNameWithoutExtension(filePath);
                    string ext            = ".xml";
                    string outputFilePath = Path.Combine(outputPath, outputFileName + ext);

                    Console.WriteLine("Processing " + Path.GetFileName(filePath));

                    ProcessTextField(filePath, outputFilePath, fieldSettings);
                }
            }
            else if (processingMode == ProcessingModeEnum.ProcessMrz)
            {
                foreach (string filePath in sourceFiles)
                {
                    string outputFileName = Path.GetFileNameWithoutExtension(filePath);
                    string ext            = ".xml";
                    string outputFilePath = Path.Combine(outputPath, outputFileName + ext);

                    Console.WriteLine("Processing " + Path.GetFileName(filePath));

                    ProcessMrz(filePath, outputFilePath);
                }
            }
        }
Esempio n. 8
0
        static void Main(string[] args)
        {
            try
            {
                Test tester = new Test();

                ProcessingModeEnum processingMode = ProcessingModeEnum.SinglePage;

                string outFormat     = null;
                string profile       = null;
                string language      = "english";
                string customOptions = "";

                var p = new OptionSet()
                {
                    { "asDocument", v => processingMode = ProcessingModeEnum.MultiPage },
                    { "asTextField", v => processingMode = ProcessingModeEnum.ProcessTextField },
                    { "asFields", v => processingMode = ProcessingModeEnum.ProcessFields },
                    { "asMRZ", var => processingMode = ProcessingModeEnum.ProcessMrz },
                    { "captureData", v => processingMode = ProcessingModeEnum.CaptureData },
                    { "out=", (string v) => outFormat = v },
                    { "profile=", (string v) => profile = v },
                    { "lang=", (string v) => language = v },
                    { "options=", (string v) => customOptions = v }
                };

                List <string> additionalArgs = null;
                try
                {
                    additionalArgs = p.Parse(args);
                }
                catch (OptionException)
                {
                    Console.WriteLine("Invalid arguments.");
                    displayHelp();
                    return;
                }

                string sourcePath   = null;
                string xmlPath      = null;
                string targetPath   = Directory.GetCurrentDirectory();
                string templateName = null;

                switch (processingMode)
                {
                case ProcessingModeEnum.SinglePage:
                case ProcessingModeEnum.MultiPage:
                case ProcessingModeEnum.ProcessTextField:
                case ProcessingModeEnum.ProcessMrz:
                    if (additionalArgs.Count != 2)
                    {
                        displayHelp();
                        return;
                    }

                    sourcePath = additionalArgs[0];
                    targetPath = additionalArgs[1];
                    break;

                case ProcessingModeEnum.ProcessFields:
                    if (additionalArgs.Count != 3)
                    {
                        displayHelp();
                        return;
                    }

                    sourcePath = additionalArgs[0];
                    xmlPath    = additionalArgs[1];
                    targetPath = additionalArgs[2];
                    break;

                case ProcessingModeEnum.CaptureData:
                    if (additionalArgs.Count != 3)
                    {
                        displayHelp();
                        return;
                    }

                    sourcePath   = additionalArgs[0];
                    templateName = additionalArgs[1];
                    targetPath   = additionalArgs[2];
                    break;
                }

                if (!Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }

                if (String.IsNullOrEmpty(outFormat))
                {
                    if (processingMode == ProcessingModeEnum.ProcessFields ||
                        processingMode == ProcessingModeEnum.ProcessTextField ||
                        processingMode == ProcessingModeEnum.ProcessMrz ||
                        processingMode == ProcessingModeEnum.CaptureData)
                    {
                        outFormat = "xml";
                    }
                    else
                    {
                        outFormat = "txt";
                    }
                }

                if (outFormat != "xml" &&
                    (processingMode == ProcessingModeEnum.ProcessFields ||
                     processingMode == ProcessingModeEnum.ProcessTextField) ||
                    processingMode == ProcessingModeEnum.CaptureData)
                {
                    Console.WriteLine("Only xml is supported as output format for field-level recognition.");
                    outFormat = "xml";
                }

                if (processingMode == ProcessingModeEnum.SinglePage || processingMode == ProcessingModeEnum.MultiPage)
                {
                    ProcessingSettings settings = buildSettings(language, outFormat, profile);
                    settings.CustomOptions = customOptions;
                    tester.ProcessPath(sourcePath, targetPath, settings, processingMode);
                }
                else if (processingMode == ProcessingModeEnum.ProcessTextField)
                {
                    TextFieldProcessingSettings settings = buildTextFieldSettings(language, customOptions);
                    tester.ProcessPath(sourcePath, targetPath, settings, processingMode);
                }
                else if (processingMode == ProcessingModeEnum.ProcessFields)
                {
                    string outputFilePath = Path.Combine(targetPath, Path.GetFileName(sourcePath) + ".xml");
                    tester.ProcessFields(sourcePath, xmlPath, outputFilePath);
                }
                else if (processingMode == ProcessingModeEnum.ProcessMrz)
                {
                    tester.ProcessPath(sourcePath, targetPath, null, processingMode);
                }
                else if (processingMode == ProcessingModeEnum.CaptureData)
                {
                    string outputFilePath = Path.Combine(targetPath, Path.GetFileName(sourcePath) + ".xml");
                    tester.CaptureData(sourcePath, templateName, outputFilePath);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: ");
                Console.WriteLine(e.Message);
            }
        }