Beispiel #1
0
 public static void Process(CLIOption.Result opt, string imageFile, Action <dynamic> postProcess, int maxRetries = 10)
 {
     for (var i = 0; i < maxRetries; i++)
     {
         Console.WriteLine($"[OCR] {imageFile}");
         try
         {
             if (ProcessImpl(opt, imageFile, postProcess))
             {
                 Console.WriteLine($"[OK]");
                 return;
             }
         }
         catch (Exception ex)
         {
             Console.Error.WriteLine(ex.ToString());
         }
         finally
         {
             GC.Collect();
         }
         var waitSec = (int)Math.Pow(i + 1, 2);
         Console.WriteLine($"[NG]");
         Console.WriteLine($"Waiting {waitSec} sec...");
         Task.Delay(waitSec * 1000).Wait();
     }
 }
Beispiel #2
0
        private static Option <Task <dynamic> > Image2Html(
            CLIOption.Result opt, string imageFile, string jsonFile, string htmlFile, string cssFile, string jsFile)
        {
            return(GetLossyImageBytes(imageFile).Map(async x =>
            {
                var languageHints = SerializeLanguageHints(opt.LanguageHints);

                using (var client = new HttpClient())
                {
                    var r = await GoogleCloudVision.QueryGoogleCloudVisionAPI(client, opt.EntryPoint, languageHints, opt.GoogleAPIKey, x);
                    Console.WriteLine($"StatusCode: {r.StatusCode}");
                    if ((int)r.StatusCode == 200)
                    {
                        var jsonText = await r.Content.ReadAsStringAsync();
                        dynamic json = JsonConvert.DeserializeObject(jsonText);
                        File.WriteAllText(jsonFile, jsonText);
                        Json2Html(imageFile, jsonFile, htmlFile, cssFile, jsFile, jsonText);
                        return json;
                    }
                    else
                    {
                        var responseText = await r.Content.ReadAsStringAsync();
                        Console.Error.WriteLine($"Response Headers: {r.Headers.ToString()}");
                        Console.Error.WriteLine($"Response Content: {responseText}");
                        return (dynamic) new Object();
                    }
                }
            }));
        }
Beispiel #3
0
        private static bool ProcessImpl(CLIOption.Result opt, string imageFile, Action <dynamic> postProcess)
        {
            if (!IsImageExtension(imageFile))
            {
                return(true);
            }

            var parent   = Directory.GetParent(Path.GetFullPath(imageFile)).FullName;
            var jsonFile = imageFile + ".json";
            var htmlFile = imageFile + ".html";
            var cssFile  = Path.Combine(parent, "cOCR.css");
            var jsFile   = Path.Combine(parent, "cOCR.js");

            if (File.Exists(jsonFile))
            {
                if (File.Exists(htmlFile))
                {
                    return(true);
                }

                var jsonText = File.ReadAllText(jsonFile);
                Json2Html(imageFile, jsonFile, htmlFile, cssFile, jsFile, jsonText);

                if (File.Exists(htmlFile))
                {
                    return(true);
                }

                try
                {
                    File.Delete(jsonFile);
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.ToString());
                }
            }

            return(Image2Html(opt, imageFile, jsonFile, htmlFile, cssFile, jsFile).Match(x =>
            {
                try
                {
                    x.Wait();
                    var json = x.Result;
                    postProcess(json);
                    return true;
                }
                catch (Exception ex)
                {
                    Console.Error.WriteLine(ex.ToString());
                }
                return false;
            },
                                                                                         () => false));
        }
Beispiel #4
0
        public static void BulkProcess(CLIOption.Result opt, Action <dynamic> postProcess)
        {
            var files = Directory.EnumerateFiles(opt.Directory, "*", SearchOption.AllDirectories);
            var index = 0;

            foreach (var file in files)
            {
                if (IsImageExtension(file))
                {
                    var fullPath = Path.GetFullPath(file);
                    Console.WriteLine($"[{index}] {fullPath}");
                    Process(opt, fullPath, postProcess);
                    index += 1;
                }
            }
        }
Beispiel #5
0
 public static void BulkProcess(CLIOption.Result opt)
 => BulkProcess(opt, (d) => { });
Beispiel #6
0
        public Form1(CLIOption.Result opt)
        {
            this.cliOption = opt;

            InitializeComponent();

            if (opt.Bulk)
            {
                throw new InvalidOperationException();
            }

            Console.WriteLine("Mode: FileSystemWatcher");

            fsWatcher.Path                  = opt.Directory;
            fsWatcher.Filter                = "*";
            fsWatcher.NotifyFilter          = NotifyFilters.FileName;
            fsWatcher.IncludeSubdirectories = true;
            fsWatcher.Created              += (Object sender, FileSystemEventArgs args) =>
            {
                if (args.ChangeType == WatcherChangeTypes.Created)
                {
                    if (Processor.IsImageExtension(args.FullPath))
                    {
                        Console.WriteLine($"[Created] {args.FullPath}");
                        Processor.Process(opt, args.FullPath, (json) =>
                        {
                            string text = json.responses[0].fullTextAnnotation.text;
                            if (opt.Clipboard)
                            {
                                InvokeProperly(() =>
                                {
                                    try
                                    {
                                        ClipboardHelper.SetText(text);
                                    }
                                    catch (Exception ex)
                                    {
                                        Console.Error.WriteLine(ex.ToString());
                                    }
                                });
                            }
                            if (opt.ShowResult)
                            {
                                try
                                {
                                    System.Diagnostics.Process.Start(args.FullPath + ".html");
                                }
                                catch (Exception ex)
                                {
                                    Console.Error.WriteLine(ex.ToString());
                                }
                            }
                            if (opt.Notice)
                            {
                                InvokeProperly(() =>
                                {
                                    ShowBalloon(text, ToolTipIcon.Info, 5);
                                });
                            }
                        });
                    }
                }
            };
            fsWatcher.EnableRaisingEvents = true;
        }