Ejemplo n.º 1
0
        public static OcrResult Load(OcrImage image, string language, OcrEngineType targetType)
        {
            try
            {
                string ip = null;
                foreach (KeyValuePair <string, Dictionary <OcrEngineType, List <string> > > remoteType in RemoteTypes)
                {
                    if (remoteType.Value.ContainsKey(targetType))
                    {
                        ip = remoteType.Key;
                        break;
                    }
                }
                if (string.IsNullOrWhiteSpace(ip))
                {
                    return(OcrResult.Create(OcrResultType.NotInstalled));
                }

                using (WebClient webClient = new WebClient())
                {
                    string url = "http://" + ip + ":" + port + "/" + RunOcrCommand;

                    webClient.Proxy    = null;
                    webClient.Encoding = Encoding.UTF8;
                    string response = webClient.UploadString(url, string.Format("type={0}&language={1}&data={2}",
                                                                                targetType.ToString(), language, HttpUtility.UrlEncode(Convert.ToBase64String(image.ToBinary()))));

                    return(OcrResult.FromBinary(Convert.FromBase64String(HttpUtility.UrlDecode(response))));
                }
            }
            catch (Exception e)
            {
                return(OcrResult.Create(OcrResultType.Exception, e.ToString()));
            }
        }
Ejemplo n.º 2
0
        public OcrResult LoadFile(string file, string language)
        {
            try
            {
                if (!File.Exists(file))
                {
                    return(OcrResult.Create(OcrResultType.InvalidFilePath));
                }

                OcrImage image = new OcrImage();
                try
                {
                    using (Image bitmap = Bitmap.FromFile(file))
                    {
                        image.Width  = bitmap.Width;
                        image.Height = bitmap.Height;
                    }
                    image.Data = File.ReadAllBytes(file);
                    // Path is mostly ignored, remove it for now to stop it being sent over the network
                    //image.Path = file;
                }
                catch (Exception e)
                {
                    return(OcrResult.Create(OcrResultType.InvalidFile, e.ToString()));
                }

                return(Load(image, language));
            }
            catch (Exception e)
            {
                return(OcrResult.Create(OcrResultType.Exception, e.ToString()));
            }
        }
Ejemplo n.º 3
0
        public override OcrResult Load(OcrImage image, string language, string apiLanguage)
        {
            string inputPath  = null;
            string outputPath = null;

            try
            {
                inputPath  = GetTempPath();
                outputPath = GetTempPath(false);
                string spacing  = AllowsSpacing(language) ? string.Empty : "--nospacing";
                string newLines = AllowsNewLines(language) ? string.Empty : "--nolines";

                File.WriteAllBytes(inputPath, image.Data);
                RunCommand("\"{0}\" \"{1}\" \"{2}\" {3} {4}", apiLanguage, inputPath, outputPath, spacing, newLines);

                if (File.Exists(outputPath))
                {
                    string[] lines = File.ReadAllLines(outputPath);

                    OcrResult result = new OcrResult();
                    result.ResultType = OcrResultType.Success;
                    foreach (string line in lines)
                    {
                        result.AddLine(line);
                    }
                    return(result);
                }
                else
                {
                    return(OcrResult.Create(OcrResultType.Failed));
                }
            }
            catch (Exception e)
            {
                return(OcrResult.Create(OcrResultType.Exception, e.ToString()));
            }
            finally
            {
                try
                {
                    if (!string.IsNullOrEmpty(inputPath))
                    {
                        File.Delete(inputPath);
                    }
                }
                catch { }
                try
                {
                    if (!string.IsNullOrEmpty(outputPath))
                    {
                        File.Delete(outputPath);
                    }
                }
                catch { }
            }
        }
Ejemplo n.º 4
0
 public static void LoadFileAsync <T>(string file, string language, Action <OcrResult> callback) where T : OcrEngine, new()
 {
     Task.Factory.StartNew(() =>
     {
         OcrResult ocrResult = LoadFile <T>(file, language);
         if (callback != null)
         {
             callback(ocrResult);
         }
     });
 }
Ejemplo n.º 5
0
 public void LoadFileAsync(string file, string language, Action <OcrResult> callback)
 {
     Task.Factory.StartNew(() =>
     {
         OcrResult ocrResult = LoadFile(file, language);
         if (callback != null)
         {
             callback(ocrResult);
         }
     });
 }
Ejemplo n.º 6
0
        public OcrResult Load(OcrImage image, string language)
        {
            try
            {
                OcrResult result = null;

                if (GetType() != typeof(RemoteOcr) && RemoteOcr.IsRemote(GetType()))
                {
                    OcrEngineType targetType = GetType(GetType());
                    RemoteOcr     remoteOcr  = new RemoteOcr(targetType);
                    result = remoteOcr.Load(image, language, language);
                }
                else if (!IsInstalled)
                {
                    return(OcrResult.Create(OcrResultType.NotInstalled));
                }
                else
                {
                    string apiLanguage = language == null ? null : GetSupportedLanguageName(language);
                    if (string.IsNullOrEmpty(apiLanguage))
                    {
                        return(OcrResult.Create(OcrResultType.LanguageNotSupported));
                    }
                    result = Load(image, language, apiLanguage);
                }

                if (result != null && result.Rect == OcrRect.Empty)
                {
                    result.Rect = new OcrRect(0, 0, image.Width, image.Height);
                }

                return(result);
            }
            catch (Exception e)
            {
                return(OcrResult.Create(OcrResultType.Exception, e.ToString()));
            }
        }
Ejemplo n.º 7
0
 private static void RunGoogleOcrTests()
 {
     while (true)
     {
         OcrEngine abbyy = OcrEngine.Create(OcrEngineType.Google);//OcrEngineType.ABBYY);
         MessageBox.Show("Installed: " + abbyy.IsInstalled);
         Stopwatch sw1 = new Stopwatch();
         sw1.Start();
         if (abbyy.IsInstalled)
         {
             OcrResult result = abbyy.LoadFile("image1.png", "ko");
             if (result != null)
             {
                 sw1.Stop();
                 if (!string.IsNullOrEmpty(result.Text))
                 {
                     Clipboard.SetText(result.Text);
                 }
                 MessageBox.Show(result.ResultType + Environment.NewLine + result.Text + " " + sw1.Elapsed);
             }
         }
     }
 }
Ejemplo n.º 8
0
 private static void RunNicomsoftOcrTests()
 {
     while (true)
     {
         OcrEngine engine = OcrEngine.Create(OcrEngineType.Nicomsoft);
         MessageBox.Show("Installed: " + engine.IsInstalled);
         Stopwatch sw1 = new Stopwatch();
         sw1.Start();
         if (engine.IsInstalled)
         {
             OcrResult result = engine.LoadFile("image1.png", "ko");
             if (result != null)
             {
                 sw1.Stop();
                 if (!string.IsNullOrEmpty(result.Text))
                 {
                     Clipboard.SetText(result.Text);
                 }
                 MessageBox.Show(result.ResultType + Environment.NewLine + result.Text + " " + result.Error + " " + sw1.Elapsed);
             }
         }
     }
 }
Ejemplo n.º 9
0
        public static OcrResult FromBinary(byte[] buffer)
        {
            try
            {
                using (BinaryReader reader = new BinaryReader(new MemoryStream(buffer)))
                {
                    OcrResult result = new OcrResult();

                    result.Rect       = ReadRect(reader);
                    result.ResultType = (OcrResultType)reader.ReadByte();
                    result.Error      = reader.ReadString();
                    if (string.IsNullOrEmpty(result.Error))
                    {
                        result.Error = null;
                    }

                    int areaCount = reader.ReadInt32();
                    for (int i = 0; i < areaCount; i++)
                    {
                        result.AddArea(ReadArea(reader));
                    }

                    int lineCount = reader.ReadInt32();
                    for (int i = 0; i < lineCount; i++)
                    {
                        result.AddLine(ReadLine(reader));
                    }

                    return(result);
                }
            }
            catch (Exception e)
            {
                return(OcrResult.Create(OcrResultType.Exception, e.ToString()));
            }
        }
Ejemplo n.º 10
0
        public override OcrResult Load(OcrImage image, string language, string apiLanguage)
        {
            try
            {
                if (!SaveLoadableImage(image))
                {
                    return(OcrResult.Create(OcrResultType.InvalidFilePath));
                }

                Program.ClipSync.Text = null;

                if (automation.Run())
                {
                    string text = WaitForClipSync();
                    if (!string.IsNullOrEmpty(text))
                    {
                        return(new OcrResult(text));
                    }
                    return(OcrResult.Create(OcrResultType.AutomationFailed));
                }
                else
                {
                    //System.Windows.Forms.MessageBox.Show("Failed at index: " + automation.Index);
                }

                return(OcrResult.Create(OcrResultType.AutomationFailed));
            }
            finally
            {
                for (int i = 0; i < 10; i++)
                {
                    automation.SendKey(Keys.Escape);
                    System.Threading.Thread.Sleep(10);
                }
            }
        }
Ejemplo n.º 11
0
            private void Process(HttpListenerContext context)
            {
                try
                {
                    string url = context.Request.Url.OriginalString;

                    string response = string.Empty;

                    if (url.Contains("favicon.ico"))
                    {
                        context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                        context.Response.OutputStream.Close();
                        return;
                    }

                    if (url.Contains(GetEnginesCommand))
                    {
                        if (Program.Engines != null)
                        {
                            Dictionary <OcrEngineType, OcrEngine> engines = new Dictionary <OcrEngineType, OcrEngine>(Program.Engines);
                            foreach (OcrEngine engine in engines.Values)
                            {
                                if (engine.IsInstalled)
                                {
                                    string languages = string.Empty;
                                    foreach (string language in engine.SupportedLanguages.Keys)
                                    {
                                        languages += language + ",";
                                    }
                                    languages = languages.TrimEnd(',');
                                    response += engine.Name + "," + languages + "|";
                                }
                            }
                            response = response.TrimEnd('|', ',');
                        }
                    }
                    else if (url.Contains(RunOcrCommand))
                    {
                        Dictionary <string, string> postData = GetPostData(context);
                        OcrEngineType type     = (OcrEngineType)Enum.Parse(typeof(OcrEngineType), postData["type"]);
                        string        language = postData["language"];
                        byte[]        data     = Convert.FromBase64String(postData["data"]);
                        OcrImage      image    = OcrImage.FromBinary(data);
                        OcrResult     result   = null;

                        try
                        {
                            result = OcrEngine.Create(type).Load(image, language);
                        }
                        catch (Exception e)
                        {
                            result = OcrResult.Create(OcrResultType.Exception, e.ToString());
                        }

                        try
                        {
                            response = HttpUtility.UrlEncode(Convert.ToBase64String(result.ToBinary()));
                        }
                        catch
                        {
                        }
                    }

                    byte[] resposeBuffer = Encoding.UTF8.GetBytes(response.ToString());

                    context.Response.ContentType     = "text/html";
                    context.Response.ContentEncoding = Encoding.UTF8;
                    context.Response.ContentLength64 = resposeBuffer.Length;
                    context.Response.AddHeader("Date", DateTime.Now.ToString("r"));
                    context.Response.AddHeader("Last-Modified", DateTime.Now.ToString("r"));
                    context.Response.OutputStream.Write(resposeBuffer, 0, resposeBuffer.Length);
                    context.Response.OutputStream.Flush();
                    context.Response.StatusCode = (int)HttpStatusCode.OK;
                }
                catch
                {
                }

                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.Response.OutputStream.Close();
            }
Ejemplo n.º 12
0
        public override OcrResult Load(OcrImage image, string language, string apiLanguage)
        {
            string inputPath   = null;
            string outputPath  = null;
            string outputPath2 = null;
            string outputPath3 = null;

            try
            {
                inputPath   = GetTempPath();
                outputPath  = GetTempPath(false);
                outputPath2 = outputPath + (IncludeTextRegions ? ".hocr" : ".txt");
                outputPath3 = outputPath + (IncludeTextRegions ? ".txt" : ".hocr");

                File.WriteAllBytes(inputPath, image.Data);
                RunCommand("\"{0}\" \"{1}\" -l {2} {3}", inputPath, outputPath, apiLanguage, IncludeTextRegions ? "-c tessedit_create_hocr=1" : string.Empty);

                OcrResult result = null;

                if (IncludeTextRegions)
                {
                    result = OcrXml.FromFile(outputPath2);
                }
                else
                {
                    string[] lines = File.ReadAllLines(outputPath2);

                    result            = new OcrResult();
                    result.ResultType = OcrResultType.Success;
                    foreach (string line in lines)
                    {
                        result.AddLine(line);
                    }
                }

                return(result);
            }
            catch (Exception e)
            {
                return(OcrResult.Create(OcrResultType.Exception, e.ToString()));
            }
            finally
            {
                try
                {
                    if (!string.IsNullOrEmpty(inputPath))
                    {
                        File.Delete(inputPath);
                    }
                }
                catch { }
                try
                {
                    if (!string.IsNullOrEmpty(outputPath))
                    {
                        File.Delete(outputPath);
                    }
                }
                catch { }
                try
                {
                    if (!string.IsNullOrEmpty(outputPath2))
                    {
                        File.Delete(outputPath2);
                    }
                }
                catch { }
                try
                {
                    if (!string.IsNullOrEmpty(outputPath3))
                    {
                        File.Delete(outputPath3);
                    }
                }
                catch { }
            }
        }
Ejemplo n.º 13
0
        static OcrHelper()
        {
            thread = new Thread(delegate()
            {
                byte[] image     = null;
                OcrResult result = null;

                while (Program.Running)
                {
                    image  = null;
                    result = null;

                    lock (locker)
                    {
                        image = currentImage;
                    }

                    if (image != null)
                    {
                        string path = GetTempPath(".png");

                        try
                        {
                            if (Start != null)
                            {
                                Start();
                            }
                        }
                        catch
                        {
                        }

                        try
                        {
                            File.WriteAllBytes(path, currentImage);
                            result = Program.ActiveOcrEngine.LoadFile(path, Program.ActiveLanguageFrom);
                        }
                        finally
                        {
                            try
                            {
                                File.Delete(path);
                            }
                            catch
                            {
                            }
                        }
                    }

                    if (image != null && result != null && Complete != null)
                    {
                        Complete(result);
                    }

                    lock (locker)
                    {
                        if (image != null)
                        {
                            currentImage = nextImage;
                            nextImage    = null;
                        }
                    }

                    Thread.Sleep(idleWait);
                }
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
        }
Ejemplo n.º 14
0
Archivo: OcrXml.cs Proyecto: radtek/Ocr
        public static OcrResult Parse(string xml)
        {
            Dictionary <string, string> meta = new Dictionary <string, string>();
            List <Page> pages = new List <Page>();

            XmlReaderSettings settings = new XmlReaderSettings();

            settings.DtdProcessing = DtdProcessing.Parse;
            settings.XmlResolver   = new XmlPreloadedResolver(XmlKnownDtds.Xhtml10);

            using (StringReader stringReader = new StringReader(xml))
                using (XmlReader reader = XmlReader.Create(stringReader, settings))
                {
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Element:
                            switch (reader.Name)
                            {
                            case "meta":
                            {
                                string metaName    = reader.GetAttribute("name");
                                string metaContent = reader.GetAttribute("content");
                                string metaValue   = reader.GetAttribute("value");
                                if (!string.IsNullOrEmpty(metaName))
                                {
                                    meta[metaName] = metaContent == null ? metaValue : metaContent;
                                }
                            }
                            break;

                            case "div":
                            case "span":
                            case "p":
                            {
                                switch (reader.GetAttribute("class"))
                                {
                                case "ocr_page":
                                    pages.Add(Parse <Page>(reader, null));
                                    break;
                                }
                            }
                            break;
                            }
                            break;

                        case XmlNodeType.EndElement:
                            break;
                        }
                    }
                }

            OcrResult result = new OcrResult();

            foreach (Page page in pages)
            {
                foreach (Area area in page.Areas)
                {
                    OcrArea ocrArea = new OcrArea(area.Rect.ToRect());

                    List <Line> lines = new List <Line>();
                    lines.AddRange(area.Lines);
                    foreach (Paragraph paragraph in area.Paragraphs)
                    {
                        lines.AddRange(paragraph.Lines);
                    }

                    foreach (Line line in lines)
                    {
                        OcrLine ocrLine = new OcrLine();
                        ocrLine.Rect = line.Rect.ToRect();
                        foreach (Word word in line.Words)
                        {
                            OcrWord ocrWord = new OcrWord();
                            ocrWord.Rect       = word.Rect.ToRect();
                            ocrWord.Confidence = word.Confidence;
                            ocrWord.Text       = word.Text;
                            ocrLine.Words.Add(ocrWord);
                        }
                        ocrArea.Lines.Add(ocrLine);
                    }

                    if (!string.IsNullOrEmpty(ocrArea.Text.Trim()))
                    {
                        result.AddArea(ocrArea);
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 15
0
        public override OcrResult Load(OcrImage image, string language, string apiLanguage)
        {
            try
            {
                Automation automation = new Automation();
                automation.UseImageCaching = true;
                automation.Path            = System.IO.Path.Combine("Images", "ABBYY");

                // Task icon
                automation.AddReferenceImage("AbbyyReference.png");

                // Task icon
                automation.AddStartImage("AbbyyStart.png");

                // Click on language text box
                automation.AddRelativeClick(450, 50);
                //automation.AddWait(50);

                // Select language text box text
                automation.AddSelectAll();
                automation.AddWait(50);

                automation.AddText(apiLanguage, true);
                automation.AddWait(50);
                automation.AddKey(Keys.Enter);
                automation.AddWait(50);
                automation.AddKey(Keys.Enter);

                // Click open image button
                automation.AddRelativeClick(140, 30);

                // Wait for the open image dialog
                automation.AddWaitImage("OpenImage.png");

                // Type the file name
                automation.AddOpenImage();

                // Wait for the complete dialog
                automation.AddWaitImage("Complete.png");

                // Press escape to close complete dialog
                automation.AddKey(Keys.Escape);

                // Seems to freeze if we don't do this
                for (int i = 0; i < 15; i++)
                {
                    // Click the ocr text box
                    automation.AddMoveMouseImageOffset("OutputOffset.png", 50 + ((i + 1) * 1), 0 + ((i + 1) * 1));
                    automation.AddWait(100);
                }

                automation.AddClickableImageOffset("OutputOffset.png", 50, 0);
                //automation.AddWait(50);

                automation.AddSelectAll();
                //automation.AddWait(50);

                automation.AddCopy();

                if (!SaveLoadableImage(image))
                {
                    return(OcrResult.Create(OcrResultType.InvalidFilePath));
                }

                // Clear the clipboard
                Clipboard.Clear();

                if (automation.Run())
                {
                    string text = WaitForClipboard();
                    if (!string.IsNullOrEmpty(text))
                    {
                        return(new OcrResult(text));
                    }
                    return(OcrResult.Create(OcrResultType.AutomationFailed));
                }
                else
                {
                    //System.Windows.Forms.MessageBox.Show("Failed at index: " + automation.Index);
                }
            }
            catch (Exception e)
            {
                //MessageBox.Show(e.ToString());
                return(OcrResult.Create(OcrResultType.Exception, e.ToString()));
            }

            return(OcrResult.Create(OcrResultType.AutomationFailed));
        }