Ejemplo n.º 1
0
        public static string ImgToFile(string pathToFile, int Language)
        {
            if (FormatFile(pathToFile))
            {
                string result = "";
                using (Bitmap load = new Bitmap(pathToFile))
                {
                    using (PumaPage image = new PumaPage(load))
                    {
                        image.FileFormat                = PumaFileFormat.TxtAscii;
                        image.AutoRotateImage           = true;
                        image.EnableSpeller             = false;
                        image.RecognizeTables           = true;
                        image.FontSettings.DetectItalic = true;
                        image.Language = SetLanguage(Language);

                        try
                        {
                            result = image.RecognizeToString();
                        }
                        catch (Exception e)
                        {
                            image.Dispose();
                        }
                    }
                }
                return(result);
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 2
0
        public static string Recognize(Bitmap bitmap)
        {
            string result = "";
            using (PumaPage image = new PumaPage(bitmap))
            {
                image.FileFormat = PumaFileFormat.TxtAnsi;
                image.AutoRotateImage = false;
                image.EnableSpeller = false;
                image.RecognizeTables = false;
                image.FontSettings.DetectItalic = true;
                image.FontSettings.DetectBold = true;
                image.FontSettings.SerifName = "Arial";
                image.Language = PumaLanguage.RussianEnglish;

                try
                {
                    result = image.RecognizeToString();
                }
                catch (RecognitionEngineException exception)
                {
                    //сложно разобрать?
                    if (exception.ErrorCode == 0)
                    {

                    }
                    else
                    {
                        throw;
                    }

                }
            }
            return result;
        }
Ejemplo n.º 3
0
        public void ImageToText()
        {
            string result = "";
            //picture.Save("picture.png");
            PumaPage image = new PumaPage(picture);

            using (image)
            {
                image.FileFormat         = PumaFileFormat.TxtAscii;
                image.AutoRotateImage    = false;
                image.EnableSpeller      = false;
                image.Language           = PumaLanguage.RussianEnglish;
                image.PreserveLineBreaks = false;

                try
                {
                    result = image.RecognizeToString();
                }
                catch (Exception)
                {
                    Console.WriteLine("Recognition failed");
                }
                finally
                {
                    image.Dispose();
                }
            }

            picture.Dispose();
            result    = result.Replace(" ", "");
            imageText = String.Copy(result);
        }
Ejemplo n.º 4
0
        public static string Recognize(string path, bool languagePolish = false)
        {
            try
            {
                if (System.IO.File.Exists(path) == false)
                {
                    throw new FileNotFoundException();
                }

                using (var ocr = new PumaPage(path))
                {
                    ocr.EnableSpeller = false;
                    if (languagePolish)
                    {
                        ocr.Language = PumaLanguage.Polish;
                    }
                    else
                    {
                        ocr.Language = PumaLanguage.English;
                    }
                    return(ocr.RecognizeToString());
                }
            }
            catch (FileNotFoundException)
            {
                return("Błędna ścieżka pliku/brak pliku");
            }
            catch
            {
                return("Can't recognize text - PumaNET framework error");
            }
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            int i;
            Console.WriteLine(string.Join(" ", args));
            string sErrString = "Should use cmd line: folder showerrors(True/False) correcterrors(True/False) percents(True/False) countname(>0) arrnames[countname]";
            if (args.Length <4)
            {
                Console.WriteLine(sErrString);
                return;
            }
            string sDir = args[0];
            if(!Directory.Exists(sDir))
            {
                Console.WriteLine("No such directory.");
                return;
            }
            bool bShowErrors = false;
            bool bCorrectErros = false;
            bool bPercents = false;
            int iCountNames = 0;
            if (!bool.TryParse(args[1], out bShowErrors) ||
                !bool.TryParse(args[2], out bCorrectErros) ||
                !bool.TryParse(args[3], out bPercents) ||
                !int.TryParse(args[4], out iCountNames) || iCountNames<1 || (iCountNames+5)!=args.Length)
            {
                Console.WriteLine(sErrString);
                return;
            }

            for (i = 0; i < iCountNames; i++)
            {
                sNamesList.Add(args[i + 5]);
            }

            foreach (string el in Directory.GetFiles(sDir, "*.gif"))
            {
                FileInfo fi = new FileInfo(el);
                string fExt = fi.Extension;
                string fName = fi.Name.Substring(0, fi.Name.Length - fExt.Length);
                int iPos = fName.IndexOf("-");
                string sTypeName = fName.Substring(iPos + 1);
                fName = fName.Substring(0,iPos);

                var pumaPage = new PumaPage(el);
                Console.WriteLine(el);
                double dVal = -1;
                try
                {
                    using (pumaPage)
                    {
                        pumaPage.FileFormat = PumaFileFormat.RtfAnsi;
                        pumaPage.EnableSpeller = false;
                        pumaPage.Language = PumaLanguage.Lithuanian;
                        string sVal = pumaPage.RecognizeToString();

                        i = 0;
                        sVal = sVal.Trim();
                        sVal = sVal.Replace(" ", "");
                        iPos = sVal.IndexOf("%");
                        if (iPos!=-1)
                            sVal = sVal.Substring(0, iPos);
                        sVal = sVal.Replace(".", ",");
                        if (!double.TryParse(sVal, out dVal))
                            dVal = -1;
                        else
                        {
                            while (bPercents&&dVal > 100)
                                dVal /= 100.0;
                        }

                    }
                }
                catch (System.Exception e)
                {
                    Console.WriteLine(e.Message);
                }
                Console.WriteLine(dVal.ToString());
                Dictionary<string, double> bufDict = null;

                if (!vals.TryGetValue(fName, out bufDict))
                {
                    bufDict = new Dictionary<string, double>();
                    vals.Add(fName, bufDict);
                }
                bufDict.Add(sTypeName, dVal);

            }
            if (vals.Count == 0)
                return;
            i = 1;
            string[] lines = new string[vals.Count + 1];

            sNames = sNamesList.ToArray();
            Array.Sort(sNames);
            sTimes = vals.Keys.ToArray();
            Array.Sort(sTimes);
            if (bShowErrors&&bCorrectErros)
            {
                CorrectErrors(bPercents);
            }

            lines[0] = "Секунда;" + string.Join(";", sNamesList.ToArray()) + ";";
            foreach (var t in sTimes)
            {
                bool bHasError = false;
                foreach (var n in sNames)
                {
                    if (vals[t][n] < 0)
                    {
                        bHasError = true;
                        break;
                    }
                }
                if (bHasError && !bShowErrors)
                    continue;
                string sBufLine = t + ";";
                foreach (var n in sNames)
                {
                    sBufLine += vals[t][n].ToString() + ";";
                }
                lines[i++] = sBufLine;
            }
            System.IO.File.WriteAllLines(sDir + ".csv", lines, Encoding.GetEncoding(1251));
        }